Patchwork hghave: use subprocess instead of os.popen

login
register
mail settings
Submitter Augie Fackler
Date Sept. 1, 2015, 4:06 a.m.
Message ID <44178fe4cc007fd3188e.1441080418@imladris.local>
Download mbox | patch
Permalink /patch/10334/
State Accepted
Headers show

Comments

Augie Fackler - Sept. 1, 2015, 4:06 a.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1441075460 14400
#      Mon Aug 31 22:44:20 2015 -0400
# Node ID 44178fe4cc007fd3188ecec7e96e025dd634fb6a
# Parent  049005de325ea400893f45bd6221215cc9b26db0
hghave: use subprocess instead of os.popen

os.popen was deprecated in Python 2.6 in favor of subprocess, so let's
move into the future.
Gregory Szorc - Sept. 1, 2015, 4:26 a.m.
> On Aug 31, 2015, at 21:06, Augie Fackler <raf@durin42.com> wrote:
> 
> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # Date 1441075460 14400
> #      Mon Aug 31 22:44:20 2015 -0400
> # Node ID 44178fe4cc007fd3188ecec7e96e025dd634fb6a
> # Parent  049005de325ea400893f45bd6221215cc9b26db0
> hghave: use subprocess instead of os.popen
> 
> os.popen was deprecated in Python 2.6 in favor of subprocess, so let's
> move into the future.

I'm pretty sure I have this patch in one of my unfinished run-tests refactoring a series!

LGTM.

> 
> diff --git a/tests/hghave.py b/tests/hghave.py
> --- a/tests/hghave.py
> +++ b/tests/hghave.py
> @@ -1,6 +1,9 @@
> -import os, stat
> +import errno
> +import os
> import re
> import socket
> +import stat
> +import subprocess
> import sys
> import tempfile
> 
> @@ -69,14 +72,16 @@ def matchoutput(cmd, regexp, ignorestatu
>     is matched by the supplied regular expression.
>     """
>     r = re.compile(regexp)
> -    fh = os.popen(cmd)
> -    s = fh.read()
>     try:
> -        ret = fh.close()
> -    except IOError:
> -        # Happen in Windows test environment
> -        ret = 1
> -    return (ignorestatus or ret is None) and r.search(s)
> +        p = subprocess.Popen(
> +            cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
> +    except OSError as e:
> +        if e.errno != errno.ENOENT:
> +            raise
> +        ret = -1
> +    ret = p.wait()
> +    s = p.stdout.read()
> +    return (ignorestatus or not ret) and r.search(s)
> 
> @check("baz", "GNU Arch baz client")
> def has_baz():
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel
Matt Mackall - Sept. 1, 2015, 8:43 p.m.
On Tue, 2015-09-01 at 00:06 -0400, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <augie@google.com>
> # Date 1441075460 14400
> #      Mon Aug 31 22:44:20 2015 -0400
> # Node ID 44178fe4cc007fd3188ecec7e96e025dd634fb6a
> # Parent  049005de325ea400893f45bd6221215cc9b26db0
> hghave: use subprocess instead of os.popen

Queued for default, thanks.

Patch

diff --git a/tests/hghave.py b/tests/hghave.py
--- a/tests/hghave.py
+++ b/tests/hghave.py
@@ -1,6 +1,9 @@ 
-import os, stat
+import errno
+import os
 import re
 import socket
+import stat
+import subprocess
 import sys
 import tempfile
 
@@ -69,14 +72,16 @@  def matchoutput(cmd, regexp, ignorestatu
     is matched by the supplied regular expression.
     """
     r = re.compile(regexp)
-    fh = os.popen(cmd)
-    s = fh.read()
     try:
-        ret = fh.close()
-    except IOError:
-        # Happen in Windows test environment
-        ret = 1
-    return (ignorestatus or ret is None) and r.search(s)
+        p = subprocess.Popen(
+            cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    except OSError as e:
+        if e.errno != errno.ENOENT:
+            raise
+        ret = -1
+    ret = p.wait()
+    s = p.stdout.read()
+    return (ignorestatus or not ret) and r.search(s)
 
 @check("baz", "GNU Arch baz client")
 def has_baz():