def __init__(self, path): path = Path(path) size = None if path.exists(): if path.is_link(): self.comment = "Link to %s" % path.read_link(absolute=True) elif path.is_dir(): self.comment = "Directory" else: size = path.size() self.comment = hsize(size) File.__init__(self, path, size)
def expand_patterns(patterns): files = set() dirs = set() # Finds all matching paths for pattern in patterns: if logging.root.isEnabledFor(logging.DEBUG): logging.debug("Expanding pattern %r into %d paths", pattern, len(list(Path('/').recursedir(pattern)))) for path in Path('/').recursedir(pattern): if path.is_dir(): dirs.add(path) else: files.add(path) # Don't include directories whose files are included non_empty_dirs = set([Path('/')]) for p in files | dirs: path = Path('/') for c in p.components[1:]: path = path / c non_empty_dirs.add(path) # Builds the final list return [File(p) for p in itertools.chain(dirs - non_empty_dirs, files)]