# HG changeset patch # User Mads Kiilerich # Date 2012-12-28 10:55:57 # Node ID 242d2f4ec01cf028f7b122bd45919b28a35dc5f9 # Parent 11d1a9143adbbd9eae1d1da28d1ce2e4496d32f5 util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag Refactor a common pattern. diff --git a/hgext/largefiles/lfutil.py b/hgext/largefiles/lfutil.py --- a/hgext/largefiles/lfutil.py +++ b/hgext/largefiles/lfutil.py @@ -36,11 +36,7 @@ def reporemove(repo, list, unlink=False) 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() diff --git a/hgext/mq.py b/hgext/mq.py --- a/hgext/mq.py +++ b/hgext/mq.py @@ -1329,11 +1329,7 @@ class queue(object): # 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 @@ class queue(object): 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] diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -4957,11 +4957,7 @@ def remove(ui, repo, *pats, **opts): 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() diff --git a/mercurial/merge.py b/mercurial/merge.py --- a/mercurial/merge.py +++ b/mercurial/merge.py @@ -382,11 +382,10 @@ def applyupdates(repo, action, wctx, mct 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 diff --git a/mercurial/patch.py b/mercurial/patch.py --- a/mercurial/patch.py +++ b/mercurial/patch.py @@ -439,11 +439,7 @@ class fsbackend(abstractbackend): 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" diff --git a/mercurial/posix.py b/mercurial/posix.py --- a/mercurial/posix.py +++ b/mercurial/posix.py @@ -443,9 +443,13 @@ def termwidth(): 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)) diff --git a/mercurial/windows.py b/mercurial/windows.py --- a/mercurial/windows.py +++ b/mercurial/windows.py @@ -275,9 +275,13 @@ def _removedirs(name): 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))