[PATCH] gentree: keep directory intact and ignore if missing
Yi Cong <[email protected]> Fri, 8 May 2026 15:02:59 +0800
| Newsgroups | org.kernel.vger.backports |
|---|---|
| Message-ID | <[email protected]> |
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(-) diff --git a/gentree.py b/gentree.py index d709b907..7940c145 100755 --- a/gentree.py +++ b/gentree.py @@ -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): -- 2.25.1