diff mercurial/util.py @ 31545:52361c4f4dac

util: unify unlinkpath Previously, there were two slightly different versions of unlinkpath between windows and posix, but these differences were eliminated in previous patches. Now we can unify these two code paths inside of the util module.
author Ryan McElroy <rmcelroy@fb.com>
date Tue, 21 Mar 2017 06:50:28 -0700
parents 28f00d07e2ee
children 6d5b77abf306
line wrap: on
line diff
--- a/mercurial/util.py	Tue Mar 21 06:50:28 2017 -0700
+++ b/mercurial/util.py	Tue Mar 21 06:50:28 2017 -0700
@@ -136,7 +136,6 @@
 testpid = platform.testpid
 umask = platform.umask
 unlink = platform.unlink
-unlinkpath = platform.unlinkpath
 username = platform.username
 
 # Python compatibility
@@ -1605,6 +1604,19 @@
         else:
             self.close()
 
+def unlinkpath(f, ignoremissing=False):
+    """unlink and remove the directory if it is empty"""
+    try:
+        unlink(f)
+    except OSError as e:
+        if not (ignoremissing and e.errno == errno.ENOENT):
+            raise
+    # try removing directories that might now be empty
+    try:
+        removedirs(os.path.dirname(f))
+    except OSError:
+        pass
+
 def makedirs(name, mode=None, notindexed=False):
     """recursive directory creation with parent mode inheritance