From 663e658286977fa6f95974febe31bd3f17eea627 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 21 Dec 2021 16:59:11 -0800 Subject: [PATCH] Use list instead of generator to avoid changes while generating. --- prune_dirs.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/prune_dirs.py b/prune_dirs.py index a01f7a7..b9e2f17 100644 --- a/prune_dirs.py +++ b/prune_dirs.py @@ -11,24 +11,30 @@ import os import sys from voussoirkit import betterhelp -from voussoirkit import spinal from voussoirkit import pathclass +from voussoirkit import pipeable +from voussoirkit import spinal +from voussoirkit import vlogging + +log = vlogging.get_logger(__name__, 'prune_dirs') def prune_dirs(starting): starting = pathclass.Path(starting) walker = spinal.walk(starting, yield_directories=True, yield_files=False) + directories = list(walker) double_check = set() def pruneme(directory): + log.debug('Checking %s.', directory.absolute_path) if directory == starting or directory not in starting: return if len(directory.listdir()) == 0: - print(directory.absolute_path) - os.rmdir(directory.absolute_path) + pipeable.stdout(directory.absolute_path) + os.rmdir(directory) double_check.add(directory.parent) - for directory in walker: + for directory in directories: pruneme(directory) while double_check: @@ -38,6 +44,7 @@ def prune_dirs(starting): def prune_dirs_argparse(args): return prune_dirs(args.starting) +@vlogging.main_decorator def main(argv): parser = argparse.ArgumentParser(description=__doc__)