]>
crepu.dev Git - config.git/blob - djavu-asus/emacs/elpy/rpc-venv/lib/python3.11/site-packages/setuptools/glob.py
2 Filename globbing utility. Mostly a copy of `glob` from Python 3.5.
5 * `yield from` and PEP3102 `*` removed.
6 * Hidden files are not ignored.
13 __all__
= ["glob", "iglob", "escape"]
16 def glob(pathname
, recursive
=False):
17 """Return a list of paths matching a pathname pattern.
19 The pattern may contain simple shell-style wildcards a la
20 fnmatch. However, unlike fnmatch, filenames starting with a
21 dot are special cases that are not matched by '*' and '?'
24 If recursive is true, the pattern '**' will match any files and
25 zero or more directories and subdirectories.
27 return list(iglob(pathname
, recursive
=recursive
))
30 def iglob(pathname
, recursive
=False):
31 """Return an iterator which yields the paths matching a pathname pattern.
33 The pattern may contain simple shell-style wildcards a la
34 fnmatch. However, unlike fnmatch, filenames starting with a
35 dot are special cases that are not matched by '*' and '?'
38 If recursive is true, the pattern '**' will match any files and
39 zero or more directories and subdirectories.
41 it
= _iglob(pathname
, recursive
)
42 if recursive
and _isrecursive(pathname
):
43 s
= next(it
) # skip empty string
48 def _iglob(pathname
, recursive
):
49 dirname
, basename
= os
.path
.split(pathname
)
50 glob_in_dir
= glob2
if recursive
and _isrecursive(basename
) else glob1
52 if not has_magic(pathname
):
54 if os
.path
.lexists(pathname
):
57 # Patterns ending with a slash should match only directories
58 if os
.path
.isdir(dirname
):
63 yield from glob_in_dir(dirname
, basename
)
65 # `os.path.split()` returns the argument itself as a dirname if it is a
66 # drive or UNC path. Prevent an infinite recursion if a drive or UNC path
67 # contains magic characters (i.e. r'\\?\C:').
68 if dirname
!= pathname
and has_magic(dirname
):
69 dirs
= _iglob(dirname
, recursive
)
72 if not has_magic(basename
):
75 for name
in glob_in_dir(dirname
, basename
):
76 yield os
.path
.join(dirname
, name
)
79 # These 2 helper functions non-recursively glob inside a literal directory.
80 # They return a list of basenames. `glob1` accepts a pattern while `glob0`
81 # takes a literal basename (so it only has to check for its existence).
84 def glob1(dirname
, pattern
):
86 if isinstance(pattern
, bytes
):
87 dirname
= os
.curdir
.encode('ASCII')
91 names
= os
.listdir(dirname
)
94 return fnmatch
.filter(names
, pattern
)
97 def glob0(dirname
, basename
):
99 # `os.path.split()` returns an empty basename for paths ending with a
100 # directory separator. 'q*x/' should match only directories.
101 if os
.path
.isdir(dirname
):
104 if os
.path
.lexists(os
.path
.join(dirname
, basename
)):
109 # This helper function recursively yields relative pathnames inside a literal
113 def glob2(dirname
, pattern
):
114 assert _isrecursive(pattern
)
116 for x
in _rlistdir(dirname
):
120 # Recursively yields relative pathnames inside a literal directory.
121 def _rlistdir(dirname
):
123 if isinstance(dirname
, bytes
):
124 dirname
= os
.curdir
.encode('ASCII')
128 names
= os
.listdir(dirname
)
133 path
= os
.path
.join(dirname
, x
) if dirname
else x
134 for y
in _rlistdir(path
):
135 yield os
.path
.join(x
, y
)
138 magic_check
= re
.compile('([*?[])')
139 magic_check_bytes
= re
.compile(b
'([*?[])')
143 if isinstance(s
, bytes
):
144 match
= magic_check_bytes
.search(s
)
146 match
= magic_check
.search(s
)
147 return match
is not None
150 def _isrecursive(pattern
):
151 if isinstance(pattern
, bytes
):
152 return pattern
== b
'**'
154 return pattern
== '**'
157 def escape(pathname
):
158 """Escape all special characters.
160 # Escaping is done by wrapping any of "*?[" between square brackets.
161 # Metacharacters do not work in the drive part and shouldn't be escaped.
162 drive
, pathname
= os
.path
.splitdrive(pathname
)
163 if isinstance(pathname
, bytes
):
164 pathname
= magic_check_bytes
.sub(br
'[\1]', pathname
)
166 pathname
= magic_check
.sub(r
'[\1]', pathname
)
167 return drive
+ pathname