gentree: keep directory intact and ignore if missing
PATCH 1/1 gentree: keep directory intact and ignore if missing
From: Yi Cong <[email protected]>
Previously, shutil.rmtree removed the target directory itself, breaking active terminal sessions in target output and then need to cd target directory again which add some additional operations during the manual debug test.
Optimize it so that only the output directory is cleared, rather than deleting the entire thing.
Signed-off-by Yi Cong <[email protected]>
--- gentree.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
gentree.py +15 -6
@@ -119,13 +119,22 @@ def check_output_dir(d, clean): sanity check the output when generating a tree, so usually running with --clean isn't suggested. """ + if not os.path.exists(d): + return + if clean: - shutil.rmtree(d, ignore_errors=True) - try: - os.rmdir(d) - except OSError as e: - if e.errno != errno.ENOENT: - raise + for item in os.listdir(d): + item_path = os.path.join(d, item) + if os.path.isdir(item_path) and not os.path.islink(item_path): + shutil.rmtree(item_path) + else: + os.unlink(item_path) + else: + if os.listdir(d): + raise OSError( + "Output directory '{}' exists and is not empty. " + "Use --clean to overwrite.".format(d) + ) def copytree(src, dst, symlinks=False, ignore=None):
Signature · 1 line
2.25.1