def testGlob(self): print(libc.glob('*.py')) # This will not match anything! print(libc.glob('\\')) # This one will match a file named \ print(libc.glob('\\\\'))
def Expand(self, arg): """Given a string that could be a glob, return a list of strings.""" # e.g. don't glob 'echo' because it doesn't look like a glob if not LooksLikeGlob(arg): u = _GlobUnescape(arg) return [u] if self.exec_opts.noglob: return [arg] try: #g = glob.glob(arg) # Bad Python glob # PROBLEM: / is significant and can't be escaped! Have to avoid # globbing it. g = libc.glob(arg) except Exception as e: # - [C\-D] is invalid in Python? Regex compilation error. # - [:punct:] not supported print("Error expanding glob %r: %s" % (arg, e)) raise #log('glob %r -> %r', arg, g) if g: return g else: # Nothing matched if self.exec_opts.failglob: # TODO: Make the command return status 1. raise NotImplementedError if self.exec_opts.nullglob: return [] else: # Return the original string u = _GlobUnescape(arg) return [u]
def Expand(self, arg): # TODO: Only try to glob if there are any glob metacharacters. # Or maybe it is a conservative "avoid glob" heuristic? # # Non-glob but with glob characters: # echo ][ # echo [] # empty # echo []LICENSE # empty # echo [L]ICENSE # this one is good # So yeah you need to test the validity somehow. try: #g = glob.glob(arg) # Bad Python glob # PROBLEM: / is significant and can't be escaped! Hav eto avoid globbing it. g = libc.glob(arg) except Exception as e: # - [C\-D] is invalid in Python? Regex compilation error. # - [:punct:] not supported print("Error expanding glob %r: %s" % (arg, e)) raise #print('G', arg, g) #log('Globbing %s', arg) if g: return g else: u = _GlobUnescape(arg) return [u]
def Expand(self, arg, out): # type: (str, List[str]) -> int """Given a string that could be a glob, append a list of strings to 'out'. Returns: Number of items appended. """ if not LooksLikeGlob(arg): # e.g. don't glob 'echo' because it doesn't look like a glob out.append(GlobUnescape(arg)) return 1 if self.exec_opts.noglob(): # we didn't glob escape it in osh/word_eval.py out.append(arg) return 1 try: results = libc.glob(arg) except RuntimeError as e: # These errors should be rare: I/O error, out of memory, or unknown # There are no syntax errors. (But see comment about globerr() in # native/libc.c.) # note: MyPy doesn't know RuntimeError has e.message (and e.args) msg = e.message # type: str stderr_line("Error expanding glob %r: %s", arg, msg) raise #log('glob %r -> %r', arg, g) n = len(results) if n: # Something matched for name in results: # Omit files starting with - to solve the --. # dash_glob turned OFF with shopt -s oil:basic. if name.startswith('-') and not self.exec_opts.dashglob(): n -= 1 continue out.append(name) return n # Nothing matched #if self.exec_opts.failglob(): # note: to match bash, the whole command has to return 1. But this also # happens in for loop and array literal contexts. It might not be worth # it? # raise NotImplementedError() if self.exec_opts.nullglob(): return 0 # Return the original string out.append(GlobUnescape(arg)) return 1
def testGlob(self): print('GLOB') print(libc.glob('*.py'))