All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yi Cong <cong.yi@linux.dev>
To: hauke@hauke-m.de, backports@vger.kernel.org
Cc: yicong@kylinos.cn
Subject: [PATCH] gentree: keep directory intact and ignore if missing
Date: Fri,  8 May 2026 15:02:59 +0800	[thread overview]
Message-ID: <20260508070259.193255-1-cong.yi@linux.dev> (raw)

From: Yi Cong <yicong@kylinos.cn>

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 <yicong@kylinos.cn>
---
 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


                 reply	other threads:[~2026-05-08  7:03 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260508070259.193255-1-cong.yi@linux.dev \
    --to=cong.yi@linux.dev \
    --cc=backports@vger.kernel.org \
    --cc=hauke@hauke-m.de \
    --cc=yicong@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.