Use list instead of generator to avoid changes while generating.

This commit is contained in:
voussoir 2021-12-21 16:59:11 -08:00
parent 644b58adb1
commit 663e658286
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -11,24 +11,30 @@ import os
import sys import sys
from voussoirkit import betterhelp from voussoirkit import betterhelp
from voussoirkit import spinal
from voussoirkit import pathclass 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): def prune_dirs(starting):
starting = pathclass.Path(starting) starting = pathclass.Path(starting)
walker = spinal.walk(starting, yield_directories=True, yield_files=False) walker = spinal.walk(starting, yield_directories=True, yield_files=False)
directories = list(walker)
double_check = set() double_check = set()
def pruneme(directory): def pruneme(directory):
log.debug('Checking %s.', directory.absolute_path)
if directory == starting or directory not in starting: if directory == starting or directory not in starting:
return return
if len(directory.listdir()) == 0: if len(directory.listdir()) == 0:
print(directory.absolute_path) pipeable.stdout(directory.absolute_path)
os.rmdir(directory.absolute_path) os.rmdir(directory)
double_check.add(directory.parent) double_check.add(directory.parent)
for directory in walker: for directory in directories:
pruneme(directory) pruneme(directory)
while double_check: while double_check:
@ -38,6 +44,7 @@ def prune_dirs(starting):
def prune_dirs_argparse(args): def prune_dirs_argparse(args):
return prune_dirs(args.starting) return prune_dirs(args.starting)
@vlogging.main_decorator
def main(argv): def main(argv):
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)