]> arthur.barton.de Git - bup.git/commitdiff
helpers: add close_fds to exo
authorRob Browning <rlb@defaultvalue.org>
Sun, 12 Jan 2020 17:54:26 +0000 (11:54 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sun, 2 Feb 2020 19:30:12 +0000 (13:30 -0600)
Add close_fds to exo() for import-duplicity, and while we're
there, define readpipe in terms of exo(), since it's just a
simplification.

Default close_fds to True, since it doesn't look like we need to
preserve the open fds in our other calls.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/helpers.py
lib/bup/t/thelpers.py

index a5c5165bc5d92bd3274e755886a1f055c46d87a2..cfbdff49692bda777f82547f7dbcffa8030642fe 100644 (file)
@@ -317,14 +317,16 @@ def exo(cmd,
         stderr=None,
         shell=False,
         check=True,
-        preexec_fn=None):
+        preexec_fn=None,
+        close_fds=True):
     if input:
         assert stdin in (None, PIPE)
         stdin = PIPE
     p = Popen(cmd,
               stdin=stdin, stdout=PIPE, stderr=stderr,
               shell=shell,
-              preexec_fn=preexec_fn)
+              preexec_fn=preexec_fn,
+              close_fds=close_fds)
     out, err = p.communicate(input)
     if check and p.returncode != 0:
         raise Exception('subprocess %r failed with status %d%s'
@@ -334,13 +336,7 @@ def exo(cmd,
 
 def readpipe(argv, preexec_fn=None, shell=False):
     """Run a subprocess and return its output."""
-    p = subprocess.Popen(argv, stdout=subprocess.PIPE, preexec_fn=preexec_fn,
-                         shell=shell)
-    out, err = p.communicate()
-    if p.returncode != 0:
-        raise Exception('subprocess %r failed with status %d'
-                        % (b' '.join(argv), p.returncode))
-    return out
+    return exo(argv, preexec_fn=preexec_fn, shell=shell)[0]
 
 
 def _argmax_base(command):
index c71cbb71a448b71257aec81649c583bda34d5ab6..17ee6357debac8960c9130a2ce6a5f1856a4ee17 100644 (file)
@@ -133,11 +133,9 @@ def test_readpipe():
         try:
             readpipe([b'bash', b'-c', b'exit 42'])
         except Exception as ex:
-            if not re.match("^subprocess b?'bash -c exit 42' failed with status 42$",
-                            str(ex)):
-                WVPASSEQ(str(ex),
-                         "^subprocess b?'bash -c exit 42' failed with status 42$")
-
+            rx = '^subprocess b?"bash -c \'exit 42\'" failed with status 42$'
+            if not re.match(rx, str(ex)):
+                WVPASSEQ(str(ex), rx)
 
 
 @wvtest