util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
authorMads Kiilerich <madski@unity3d.com>
Fri, 28 Dec 2012 11:55:57 +0100
changeset 18143 242d2f4ec01c
parent 18142 11d1a9143adb
child 18144 e16982a74bf7
util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag Refactor a common pattern.
hgext/largefiles/lfutil.py
hgext/mq.py
mercurial/commands.py
mercurial/merge.py
mercurial/patch.py
mercurial/posix.py
mercurial/windows.py
--- a/hgext/largefiles/lfutil.py	Fri Dec 28 11:55:45 2012 +0100
+++ b/hgext/largefiles/lfutil.py	Fri Dec 28 11:55:57 2012 +0100
@@ -36,11 +36,7 @@
         try:
             if unlink:
                 for f in list:
-                    try:
-                        util.unlinkpath(repo.wjoin(f))
-                    except OSError, inst:
-                        if inst.errno != errno.ENOENT:
-                            raise
+                    util.unlinkpath(repo.wjoin(f), ignoremissing=True)
             repo[None].forget(list)
         finally:
             wlock.release()
--- a/hgext/mq.py	Fri Dec 28 11:55:45 2012 +0100
+++ b/hgext/mq.py	Fri Dec 28 11:55:57 2012 +0100
@@ -1329,11 +1329,7 @@
                 # created while patching
                 for f in all_files:
                     if f not in repo.dirstate:
-                        try:
-                            util.unlinkpath(repo.wjoin(f))
-                        except OSError, inst:
-                            if inst.errno != errno.ENOENT:
-                                raise
+                        util.unlinkpath(repo.wjoin(f), ignoremissing=True)
                 self.ui.warn(_('done\n'))
                 raise
 
@@ -1442,11 +1438,7 @@
                 self.backup(repo, tobackup)
 
                 for f in a:
-                    try:
-                        util.unlinkpath(repo.wjoin(f))
-                    except OSError, e:
-                        if e.errno != errno.ENOENT:
-                            raise
+                    util.unlinkpath(repo.wjoin(f), ignoremissing=True)
                     repo.dirstate.drop(f)
                 for f in m + r:
                     fctx = ctx[f]
--- a/mercurial/commands.py	Fri Dec 28 11:55:45 2012 +0100
+++ b/mercurial/commands.py	Fri Dec 28 11:55:57 2012 +0100
@@ -4957,11 +4957,7 @@
             for f in list:
                 if f in added:
                     continue # we never unlink added files on remove
-                try:
-                    util.unlinkpath(repo.wjoin(f))
-                except OSError, inst:
-                    if inst.errno != errno.ENOENT:
-                        raise
+                util.unlinkpath(repo.wjoin(f), ignoremissing=True)
         repo[None].forget(list)
     finally:
         wlock.release()
--- a/mercurial/merge.py	Fri Dec 28 11:55:45 2012 +0100
+++ b/mercurial/merge.py	Fri Dec 28 11:55:57 2012 +0100
@@ -382,11 +382,10 @@
             if f == '.hgsubstate': # subrepo states need updating
                 subrepo.submerge(repo, wctx, mctx, wctx, overwrite)
             try:
-                util.unlinkpath(repo.wjoin(f))
+                util.unlinkpath(repo.wjoin(f), ignoremissing=True)
             except OSError, inst:
-                if inst.errno != errno.ENOENT:
-                    repo.ui.warn(_("update failed to remove %s: %s!\n") %
-                                 (f, inst.strerror))
+                repo.ui.warn(_("update failed to remove %s: %s!\n") %
+                             (f, inst.strerror))
             removed += 1
         elif m == "m": # merge
             if f == '.hgsubstate': # subrepo states need updating
--- a/mercurial/patch.py	Fri Dec 28 11:55:45 2012 +0100
+++ b/mercurial/patch.py	Fri Dec 28 11:55:57 2012 +0100
@@ -439,11 +439,7 @@
                 util.setflags(self._join(fname), False, True)
 
     def unlink(self, fname):
-        try:
-            util.unlinkpath(self._join(fname))
-        except OSError, inst:
-            if inst.errno != errno.ENOENT:
-                raise
+        util.unlinkpath(self._join(fname), ignoremissing=True)
 
     def writerej(self, fname, failed, total, lines):
         fname = fname + ".rej"
--- a/mercurial/posix.py	Fri Dec 28 11:55:45 2012 +0100
+++ b/mercurial/posix.py	Fri Dec 28 11:55:57 2012 +0100
@@ -443,9 +443,13 @@
 def makedir(path, notindexed):
     os.mkdir(path)
 
-def unlinkpath(f):
+def unlinkpath(f, ignoremissing=False):
     """unlink and remove the directory if it is empty"""
-    os.unlink(f)
+    try:
+        os.unlink(f)
+    except OSError, e:
+        if not (ignoremissing and e.errno == errno.ENOENT):
+            raise
     # try removing directories that might now be empty
     try:
         os.removedirs(os.path.dirname(f))
--- a/mercurial/windows.py	Fri Dec 28 11:55:45 2012 +0100
+++ b/mercurial/windows.py	Fri Dec 28 11:55:57 2012 +0100
@@ -275,9 +275,13 @@
             break
         head, tail = os.path.split(head)
 
-def unlinkpath(f):
+def unlinkpath(f, ignoremissing=False):
     """unlink and remove the directory if it is empty"""
-    unlink(f)
+    try:
+        unlink(f)
+    except OSError, e:
+        if not (ignoremissing and e.errno == errno.ENOENT):
+            raise
     # try removing directories that might now be empty
     try:
         _removedirs(os.path.dirname(f))