def test_nested_magic(self): with file_utils.Tempdir() as d: d.create_file("foo/bar.py") d.create_file("foo/baz/qux.py") with file_utils.cd(d.path): self.assertPathsEqual(compat.recursive_glob("foo/**/*.py"), ["foo/bar.py", "foo/baz/qux.py"])
def test_redundant_magic(self): with file_utils.Tempdir() as d: d.create_file("foo.py") d.create_file("bar/baz.py") with file_utils.cd(d.path): self.assertPathsEqual(compat.recursive_glob("**/**/*.py"), ["foo.py", "bar/baz.py"])
def test_multiple_magic(self): with file_utils.Tempdir() as d: d.create_file("d1/d2/d3/f1.py") d.create_file("d1/d2/f2.py") d.create_file("d2/f3.py") d.create_file("d2/d3/f4.py") with file_utils.cd(d.path): self.assertPathsEqual(compat.recursive_glob("**/d2/**/*.py"), [ "d1/d2/d3/f1.py", "d1/d2/f2.py", "d2/f3.py", "d2/d3/f4.py" ])
def expand_source_files(filenames, cwd=None): """Expand a space-separated string of filenames passed in as sources. This is a helper function for handling command line arguments that specify a list of source files and directories. Any directories in filenames will be scanned recursively for .py files. Any files that do not end with ".py" will be dropped. Args: filenames: A space-separated string of filenames to process. cwd: An optional working directory to expand relative paths Returns: A set of full paths to .py files """ out = [] for f in expand_globpaths(filenames.split(), cwd): if os.path.isdir(f): # If we have a directory, collect all the .py files within it. out += compat.recursive_glob(os.path.join(f, "**", "*.py")) elif f.endswith(".py"): out.append(f) return set(out)
def test_simple_magic(self): with file_utils.Tempdir() as d: d.create_file("foo.py") with file_utils.cd(d.path): self.assertPathsEqual(compat.recursive_glob("*.py"), ["foo.py"])
def expand_globpaths(globpaths, cwd=None): """Expand a list of glob expressions into a list of full paths.""" with cd(cwd): paths = sum((compat.recursive_glob(p) for p in globpaths), []) return expand_paths(paths, cwd)