]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/helpers.py
Clean subprocess output without newliner
[bup.git] / lib / bup / helpers.py
index f7cd26883ccfede83243719c02cb8a36fc37b7a4..d05226ebc28207dd94435a6c920ef99c4933b721 100644 (file)
@@ -1,13 +1,19 @@
 """Helper functions and classes for bup."""
 
 from collections import namedtuple
+from contextlib import contextmanager
 from ctypes import sizeof, c_void_p
 from os import environ
-from contextlib import contextmanager
+from pipes import quote
+from subprocess import PIPE, Popen
 import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re, struct
 import hashlib, heapq, math, operator, time, grp, tempfile
 
 from bup import _helpers
+from bup import compat
+# This function should really be in helpers, not in bup.options.  But we
+# want options.py to be standalone so people can include it in other projects.
+from bup.options import _tty_width as tty_width
 
 
 class Nonlocal:
@@ -22,10 +28,12 @@ sc_arg_max = os.sysconf('SC_ARG_MAX')
 if sc_arg_max == -1:  # "no definite limit" - let's choose 2M
     sc_arg_max = 2 * 1024 * 1024
 
-# This function should really be in helpers, not in bup.options.  But we
-# want options.py to be standalone so people can include it in other projects.
-from bup.options import _tty_width
-tty_width = _tty_width
+
+def last(iterable):
+    result = None
+    for result in iterable:
+        pass
+    return result
 
 
 def atoi(s):
@@ -91,6 +99,26 @@ def partition(predicate, stream):
     return (leading_matches(), rest())
 
 
+def lines_until_sentinel(f, sentinel, ex_type):
+    # sentinel must end with \n and must contain only one \n
+    while True:
+        line = f.readline()
+        if not (line and line.endswith('\n')):
+            raise ex_type('Hit EOF while reading line')
+        if line == sentinel:
+            return
+        yield line
+
+
+def stat_if_exists(path):
+    try:
+        return os.stat(path)
+    except OSError as e:
+        if e.errno != errno.ENOENT:
+            raise
+    return None
+
+
 # Write (blockingly) to sockets that may or may not be in blocking mode.
 # We need this because our stderr is sometimes eaten by subprocesses
 # (probably ssh) that sometimes make it nonblocking, if only temporarily,
@@ -179,25 +207,6 @@ def mkdirp(d, mode=None):
             raise
 
 
-_unspecified_next_default = object()
-
-def _fallback_next(it, default=_unspecified_next_default):
-    """Retrieve the next item from the iterator by calling its
-    next() method. If default is given, it is returned if the
-    iterator is exhausted, otherwise StopIteration is raised."""
-
-    if default is _unspecified_next_default:
-        return it.next()
-    else:
-        try:
-            return it.next()
-        except StopIteration:
-            return default
-
-if sys.version_info < (2, 6):
-    next =  _fallback_next
-
-
 def merge_iter(iters, pfreq, pfunc, pfinal, key=None):
     if key:
         samekey = lambda e, pe: getattr(e, key) == getattr(pe, key, None)
@@ -220,7 +229,7 @@ def merge_iter(iters, pfreq, pfunc, pfinal, key=None):
             yield e
         count += 1
         try:
-            e = it.next() # Don't use next() function, it's too expensive
+            e = next(it)
         except StopIteration:
             heapq.heappop(heap) # remove current
         else:
@@ -241,6 +250,34 @@ def unlink(f):
             raise
 
 
+def shstr(cmd):
+    if isinstance(cmd, compat.str_type):
+        return cmd
+    else:
+        return ' '.join(map(quote, cmd))
+
+exc = subprocess.check_call
+
+def exo(cmd,
+        input=None,
+        stdin=None,
+        stderr=None,
+        shell=False,
+        check=True,
+        preexec_fn=None):
+    if input:
+        assert stdin in (None, PIPE)
+        stdin = PIPE
+    p = Popen(cmd,
+              stdin=stdin, stdout=PIPE, stderr=stderr,
+              shell=shell,
+              preexec_fn=preexec_fn)
+    out, err = p.communicate(input)
+    if check and p.returncode != 0:
+        raise Exception('subprocess %r failed with status %d, stderr: %r'
+                        % (' '.join(map(quote, cmd)), p.returncode, err))
+    return out, err, p
+
 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,
@@ -614,7 +651,7 @@ class DemuxConn(BaseConn):
                 if not self._next_packet(timeout):
                     return False
             try:
-                self.buf = self.reader.next()
+                self.buf = next(self.reader)
                 return True
             except StopIteration:
                 self.reader = None