Patchwork [01,of,11] util: add tryunlink function

login
register
mail settings
Submitter Ryan McElroy
Date March 21, 2017, 1:54 p.m.
Message ID <5c9cdd8046845f76e169.1490104463@devbig314.prn1.facebook.com>
Download mbox | patch
Permalink /patch/19517/
State Accepted
Headers show

Comments

Ryan McElroy - March 21, 2017, 1:54 p.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1490104228 25200
#      Tue Mar 21 06:50:28 2017 -0700
# Node ID 5c9cdd8046845f76e169885ed490a603b911d0d4
# Parent  49de4dfb282e2ad4dc91328ffd7fc396ee92b4a0
util: add tryunlink function

Throughout mercurial cdoe, there is a common pattern of attempting to remove
a file and ignoring ENOENT errors. Let's move this into a common function to
allow for cleaner code.
Ryan McElroy - March 21, 2017, 1:56 p.m.
Argh, I missed --flag v2 on this series. Apologies.

I also split this from the previous three patches because they are 
actually unrelated cleanups.

On 3/21/17 1:54 PM, Ryan McElroy wrote:
> # HG changeset patch
> # User Ryan McElroy <rmcelroy@fb.com>
> # Date 1490104228 25200
> #      Tue Mar 21 06:50:28 2017 -0700
> # Node ID 5c9cdd8046845f76e169885ed490a603b911d0d4
> # Parent  49de4dfb282e2ad4dc91328ffd7fc396ee92b4a0
> util: add tryunlink function
>
> Throughout mercurial cdoe, there is a common pattern of attempting to remove
> a file and ignoring ENOENT errors. Let's move this into a common function to
> allow for cleaner code.
>
> diff --git a/mercurial/util.py b/mercurial/util.py
> --- a/mercurial/util.py
> +++ b/mercurial/util.py
> @@ -1617,6 +1617,14 @@ def unlinkpath(f, ignoremissing=False):
>       except OSError:
>           pass
>   
> +def tryunlink(f):
> +    """Attempt to remove a file, ignoring ENOENT errors."""
> +    try:
> +        unlink(f)
> +    except OSError as e:
> +        if e.errno != errno.ENOENT:
> +            raise
> +
>   def makedirs(name, mode=None, notindexed=False):
>       """recursive directory creation with parent mode inheritance
>
Yuya Nishihara - March 21, 2017, 2:22 p.m.
On Tue, 21 Mar 2017 06:54:23 -0700, Ryan McElroy wrote:
> # HG changeset patch
> # User Ryan McElroy <rmcelroy@fb.com>
> # Date 1490104228 25200
> #      Tue Mar 21 06:50:28 2017 -0700
> # Node ID 5c9cdd8046845f76e169885ed490a603b911d0d4
> # Parent  49de4dfb282e2ad4dc91328ffd7fc396ee92b4a0
> util: add tryunlink function

Also looks good. Queued, thanks.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1617,6 +1617,14 @@  def unlinkpath(f, ignoremissing=False):
     except OSError:
         pass
 
+def tryunlink(f):
+    """Attempt to remove a file, ignoring ENOENT errors."""
+    try:
+        unlink(f)
+    except OSError as e:
+        if e.errno != errno.ENOENT:
+            raise
+
 def makedirs(name, mode=None, notindexed=False):
     """recursive directory creation with parent mode inheritance