@@ -40,31 +40,37 @@ def collectattrs(*, targets, name, initial=[]):
4040
4141@functools .cache
4242def _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+ if glob_str .startswith ("./" ):
44+ glob_str = normpath (join (getcwd (), glob_str ))
45+
46+ opts = re .compile ("([.]|[*][*]/|[*]|[?])|(.)" )
47+ out = ""
48+ for pattern_match , literal_text in opts .findall (glob_str ):
49+ if pattern_match == "." :
50+ out += "[.]"
51+ elif pattern_match == "**/" :
52+ out += "(?:.*/)?"
53+ elif pattern_match == "*" :
54+ out += "[^/]*"
55+ elif pattern_match == "?" :
56+ out += "."
5457 elif literal_text :
5558 out += literal_text
5659 return re .compile (out )
5760
61+
5862def _glob_filter (paths , pattern ):
5963 r = _glob_to_re (pattern )
6064 for f in paths :
6165 if r .match (f ):
6266 yield f
6367
68+
6469def _glob_matches (path , pattern ):
6570 r = _glob_to_re (pattern )
6671 return r .match (path )
6772
73+
6874def glob (include = ["*" ], exclude = [], dir = None , relative_to = "." ):
6975 if not dir :
7076 dir = getcwd ()
@@ -77,15 +83,15 @@ def iterate():
7783 for dirpath , dirnames , filenames in walk (
7884 dir , topdown = True , followlinks = True
7985 ):
80- dirpath = relpath (dirpath , dir )
86+ dirpath = relpath (dirpath , relative_to )
8187 filenames = [normpath (join (dirpath , f )) for f in filenames ]
8288 matching = set ()
8389 for p in include :
84- matching .update (_glob_filter (filenames , p ))
90+ matching .update ([ f for f in _glob_filter (filenames , p )] )
8591 for p in exclude :
86- matching = [n for n in matching if not _glob_matches (n , p )]
92+ matching = [n for n in matching if not _glob_matches (n , p )]
8793 for f in matching :
88- yield normpath ( relpath ( join ( dir , f ), relative_to ))
94+ yield f
8995
9096 return list (iterate ())
9197
0 commit comments