Skip to content

Commit a35af82

Browse files
committed
Fix ab bug where glob's exclude=[] wasn't working as expected.
1 parent a5539f3 commit a35af82

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

build/utils.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,34 @@ def collectattrs(*, targets, name, initial=[]):
4040

4141
@functools.cache
4242
def _glob_to_re(glob_str):
43-
opts = re.compile('([.]|[*][*]/|[*]|[?])|(.)')
44-
out = ''
45-
for (pattern_match, literal_text) in opts.findall(glob_str):
46-
if pattern_match == '.':
47-
out += '[.]'
48-
elif pattern_match == '**/':
49-
out += '(?:.*/)?'
50-
elif pattern_match == '*':
51-
out += '[^/]*'
52-
elif pattern_match == '?':
53-
out += '.'
43+
opts = re.compile("([.]|[*][*]/|[*]|[?])|(.)")
44+
out = ""
45+
for pattern_match, literal_text in opts.findall(glob_str):
46+
if pattern_match == ".":
47+
out += "[.]"
48+
elif pattern_match == "**/":
49+
out += "(?:.*/)?"
50+
elif pattern_match == "*":
51+
out += "[^/]*"
52+
elif pattern_match == "?":
53+
out += "."
5454
elif literal_text:
5555
out += literal_text
5656
return re.compile(out)
5757

58+
5859
def _glob_filter(paths, pattern):
5960
r = _glob_to_re(pattern)
6061
for f in paths:
6162
if r.match(f):
6263
yield f
6364

65+
6466
def _glob_matches(path, pattern):
6567
r = _glob_to_re(pattern)
6668
return r.match(path)
6769

70+
6871
def glob(include=["*"], exclude=[], dir=None, relative_to="."):
6972
if not dir:
7073
dir = getcwd()
@@ -81,11 +84,16 @@ def iterate():
8184
filenames = [normpath(join(dirpath, f)) for f in filenames]
8285
matching = set()
8386
for p in include:
84-
matching.update(_glob_filter(filenames, p))
87+
matching.update(
88+
[
89+
normpath(relpath(join(dir, f), relative_to))
90+
for f in _glob_filter(filenames, p)
91+
]
92+
)
8593
for p in exclude:
86-
matching = [n for n in matching if not _glob_matches(n, p)]
94+
matching = [n for n in matching if not _glob_matches(n, p)]
8795
for f in matching:
88-
yield normpath(relpath(join(dir, f), relative_to))
96+
yield f
8997

9098
return list(iterate())
9199

0 commit comments

Comments
 (0)