def iter_paths(root, exclude=[], skip_hidden=True): """yields paths contained in root (symlinks dereferenced) Any path starting with any of the path parts included in exclude are ignored (before and after dereferencing symlinks) Directory symlinks are not followed (except root itself) Args: root (fsnative) exclude (List[fsnative]) skip_hidden (bool): Ignore files which are hidden or where any of the parent directories are hidden. Yields: fsnative: absolute dereferenced paths """ assert isinstance(root, fsnative) assert all((isinstance(p, fsnative) for p in exclude)) assert os.path.abspath(root) def skip(path): if skip_hidden and ishidden(path): return True # FIXME: normalize paths.. return any((path.startswith(p) for p in exclude)) if skip_hidden and ishidden(root): return for path, dnames, fnames in os.walk(root): if skip_hidden: dnames[:] = list(filter( lambda d: not ishidden(os.path.join(path, d)), dnames)) for filename in fnames: fullfilename = os.path.join(path, filename) if skip(fullfilename): continue fullfilename = os.path.realpath(fullfilename) if skip(fullfilename): continue yield fullfilename
def iter_paths(root, exclude=[], skip_hidden=True): """yields paths contained in root (symlinks dereferenced) Any path starting with any of the path parts included in exclude are ignored (before and after dereferencing symlinks) Directory symlinks are not followed (except root itself) Args: root (fsnative) exclude (List[fsnative]) skip_hidden (bool): Ignore files which are hidden or where any of the parent directories are hidden. Yields: fsnative: absolute dereferenced paths """ assert isinstance(root, fsnative) assert all((isinstance(p, fsnative) for p in exclude)) assert os.path.abspath(root) def skip(path): if skip_hidden and ishidden(path): return True # FIXME: normalize paths.. return any((path.startswith(p) for p in exclude)) if skip_hidden and ishidden(root): return for path, dnames, fnames in os.walk(root): if skip_hidden: dnames[:] = list( filter(lambda d: not ishidden(os.path.join(path, d)), dnames)) for filename in fnames: fullfilename = os.path.join(path, filename) if skip(fullfilename): continue fullfilename = os.path.realpath(fullfilename) if skip(fullfilename): continue yield fullfilename
def test_main(self): assert ishidden(fsnative(u".")) assert ishidden(fsnative(u"foo/.bar")) assert not ishidden(fsnative(u".foo/bar")) assert not ishidden(fsnative(u"foo"))
def skip(path): if skip_hidden and ishidden(path): return True # FIXME: normalize paths.. return any((path.startswith(p) for p in exclude))