[PATCH 3 of 3] util.makedirs: cleanup of termination of recursion

Adrian Buehlmann adrian at cadifra.com
Mon Aug 22 03:25:30 CDT 2011


On 2011-08-22 10:07, Adrian Buehlmann wrote:
> If we want to postpone (and reorder) the parent calculation, we could do:
> 
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -780,12 +780,12 @@
>  
>  def makedirs(name, mode=None):
>      """recursive directory creation with parent mode inheritance"""
> -    parent = os.path.abspath(os.path.dirname(name))
>      try:
>          os.mkdir(name)
>      except OSError, err:
>          if err.errno == errno.EEXIST:
>              return
> +        parent = os.path.dirname(os.path.abspath(name))
>          if not name or parent == name or err.errno != errno.ENOENT:
>              raise
>          makedirs(parent, mode)
> 

or perhaps:

# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1314001316 -7200
# Node ID 906f611f9c32ec362d7c4e9d67397ad05f232445
# Parent  5a50f36ec65f3eb2246407f46fdf9bf53e38aca7
util: postpone and reorder parent calculation in makedirs

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -780,13 +780,15 @@

 def makedirs(name, mode=None):
     """recursive directory creation with parent mode inheritance"""
-    parent = os.path.abspath(os.path.dirname(name))
     try:
         os.mkdir(name)
     except OSError, err:
         if err.errno == errno.EEXIST:
             return
-        if not name or parent == name or err.errno != errno.ENOENT:
+        if err.errno != errno.ENOENT or not name:
+            raise
+        parent = os.path.dirname(os.path.abspath(name))
+        if parent == name:
             raise
         makedirs(parent, mode)
         os.mkdir(name)


More information about the Mercurial-devel mailing list