git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 04/10] Drop utils.remove_dirs() in favor of os.removedirs().

From
Yann Dirson <ydirson@altern.org>
Date
Mar 15, 2007, 00:26 UTC
Message-ID
<20070315002649.9094.6314.stgit@gandelf.nowhere.earth>
In-Reply-To
<20070315001808.9094.68529.stgit@gandelf.nowhere.earth>
Signed-off-by: Yann Dirson <ydirson@altern.org>
---
 stgit/utils.py |   24 ++++++++++--------------
 1 files changed, 10 insertions(+), 14 deletions(-)
diff --git a/stgit/utils.py b/stgit/utils.py
index bfe7797..d04d077 100644
--- a/stgit/utils.py
+++ b/stgit/utils.py
@@ -116,24 +116,16 @@ def strip_suffix(suffix, string):
     assert string.endswith(suffix)
     return string[:-len(suffix)]
 
-def remove_dirs(basedir, dirs):
-    """Starting at join(basedir, dirs), remove the directory if empty,
-    and try the same with its parent, until we find a nonempty
-    directory or reach basedir."""
-    path = dirs
-    while path:
-        try:
-            os.rmdir(os.path.join(basedir, path))
-        except OSError:
-            return # can't remove nonempty directory
-        path = os.path.dirname(path)
-
 def remove_file_and_dirs(basedir, file):
     """Remove join(basedir, file), and then remove the directory it
     was in if empty, and try the same with its parent, until we find a
     nonempty directory or reach basedir."""
     os.remove(os.path.join(basedir, file))
-    remove_dirs(basedir, os.path.dirname(file))
+    try:
+        os.removedirs(os.path.join(basedir, os.path.dirname(file)))
+    except OSError:
+        # file's parent dir may not be empty after removal
+        pass
 
 def create_dirs(directory):
     """Create the given directory, if the path doesn't already exist."""
@@ -152,7 +144,11 @@ def rename(basedir, file1, file2):
     full_file2 = os.path.join(basedir, file2)
     create_dirs(os.path.dirname(full_file2))
     os.rename(os.path.join(basedir, file1), full_file2)
-    remove_dirs(basedir, os.path.dirname(file1))
+    try:
+        os.removedirs(os.path.join(basedir, os.path.dirname(file1)))
+    except OSError:
+        # file1's parent dir may not be empty after move
+        pass
 
 class EditorException(Exception):
     pass
Previous: Yann DirsonNext: Yann Dirson
Message 5 of 11 in “Various fixes and improvements”
  1. 00/10 Various fixes and improvementsYann Dirson, Mar 15, 2007
  2. 01/10 Fix stack deletion when orig-base is present.Yann Dirson, Mar 15, 2007
  3. 02/10 Fix diagnostic messages on patch deletion and simplify others.Yann Dirson, Mar 15, 2007
  4. 03/10 Make use exception raised by removedirs.Yann Dirson, Mar 15, 2007
  5. 04/10 Drop utils.remove_dirs() in favor of os.removedirs().Yann Dirson, Mar 15, 2007
  6. 05/10 Copy parentbranch setting on 'stg branch --clone'.Yann Dirson, Mar 15, 2007
  7. 06/10 Remove debugging output from contrib/stg-sinkYann Dirson, Mar 15, 2007
  8. 07/10 Stop recording branch.*.remote to '.' for local parents.Yann Dirson, Mar 15, 2007
  9. 08/10 Add a script to quickly run an interactive test session.Yann Dirson, Mar 15, 2007
  10. 09/10 Add support for unsetting config vars.Yann Dirson, Mar 15, 2007
  11. 10/10 Cleanup parent information on stgit branch deletion.Yann Dirson, Mar 15, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.