]> arthur.barton.de Git - bup.git/blobdiff - main.py
Clean subprocess output without newliner
[bup.git] / main.py
diff --git a/main.py b/main.py
index d22150be86e0191f38db78784c9878cd17fc373f..119826564d7266815639d978c9d11400c5364ddc 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -1,53 +1,59 @@
-#!/usr/bin/env python
+#!/bin/sh
+"""": # -*-python-*- # -*-python-*-
+bup_python="$(dirname "$0")/cmd/bup-python" || exit $?
+exec "$bup_python" "$0" ${1+"$@"}
+"""
+# end of bup preamble
 
-import sys, os, subprocess, signal, getopt
+import errno, re, sys, os, subprocess, signal, getopt
+
+from fcntl import F_GETFL, F_SETFL
+from subprocess import PIPE
+from sys import stderr, stdout
+import fcntl, select
 
 argv = sys.argv
-exe = argv[0]
+exe = os.path.realpath(argv[0])
 exepath = os.path.split(exe)[0] or '.'
+exeprefix = os.path.split(os.path.abspath(exepath))[0]
 
 # fix the PYTHONPATH to include our lib dir
-libpath = os.path.join(exepath, 'lib')
-cmdpath = os.path.join(exepath, 'cmd')
+if os.path.exists("%s/lib/bup/cmd/." % exeprefix):
+    # installed binary in /.../bin.
+    # eg. /usr/bin/bup means /usr/lib/bup/... is where our libraries are.
+    cmdpath = "%s/lib/bup/cmd" % exeprefix
+    libpath = "%s/lib/bup" % exeprefix
+    resourcepath = libpath
+else:
+    # running from the src directory without being installed first
+    cmdpath = os.path.join(exepath, 'cmd')
+    libpath = os.path.join(exepath, 'lib')
+    resourcepath = libpath
 sys.path[:0] = [libpath]
 os.environ['PYTHONPATH'] = libpath + ':' + os.environ.get('PYTHONPATH', '')
 os.environ['BUP_MAIN_EXE'] = os.path.abspath(exe)
+os.environ['BUP_RESOURCE_PATH'] = resourcepath
+
+
+from bup import helpers
+from bup.compat import add_ex_tb, chain_ex, wrap_main
+from bup.helpers import atoi, columnate, debug1, log, tty_width
+
 
-from bup.helpers import *
-
-
-def columnate(l, prefix):
-    l = l[:]
-    clen = max(len(s) for s in l)
-    ncols = (78 - len(prefix)) / (clen + 2)
-    if ncols <= 1:
-        ncols = 1
-        clen = 0
-    cols = []
-    while len(l) % ncols:
-        l.append('')
-    rows = len(l)/ncols
-    for s in range(0, len(l), rows):
-        cols.append(l[s:s+rows])
-    out = ''
-    for row in zip(*cols):
-        out += prefix + ''.join(('%-*s' % (clen+2, s)) for s in row) + '\n'
-    return out
-
-
-def usage():
-    log('Usage: bup [-?|--help] COMMAND [ARGS]\n\n')
+def usage(msg=""):
+    log('Usage: bup [-?|--help] [-d BUP_DIR] [--debug] [--profile] '
+        '<command> [options...]\n\n')
     common = dict(
         ftp = 'Browse backup sets using an ftp-like client',
         fsck = 'Check backup sets for damage and add redundancy information',
         fuse = 'Mount your backup sets as a filesystem',
         help = 'Print detailed help for the given command',
         index = 'Create or display the index of files to back up',
-        join = 'Retrieve a file backed up using "bup split"',
-        ls = 'Browse the files in your backup sets',
-        midx = 'Index objects to speed up future backups',
+        on = 'Backup a remote machine to the local one',
+        restore = 'Extract files from a backup set',
         save = 'Save files into a backup set (note: run "bup index" first)',
-        split = 'Split a single file into its own backup set',
+        tag = 'Tag commits for easier access',
+        web = 'Launch a web server to examine backup sets',
     )
 
     log('Common commands:\n')
@@ -67,6 +73,8 @@ def usage():
     
     log("See 'bup help COMMAND' for more information on " +
         "a specific command.\n")
+    if msg:
+        log("\n%s\n" % msg)
     sys.exit(99)
 
 
@@ -75,19 +83,32 @@ if len(argv) < 2:
 
 # Handle global options.
 try:
-    global_args, subcmd = getopt.getopt(argv[1:], '?', ['help'])
-except getopt.GetoptError, ex:
-    log('error: ' + ex.msg + '\n')
-    usage()
+    optspec = ['help', 'version', 'debug', 'profile', 'bup-dir=']
+    global_args, subcmd = getopt.getopt(argv[1:], '?VDd:', optspec)
+except getopt.GetoptError as ex:
+    usage('error: %s' % ex.msg)
 
 help_requested = None
+do_profile = False
 
 for opt in global_args:
-    if opt[0] == '-?' or opt[0] == '--help':
+    if opt[0] in ['-?', '--help']:
         help_requested = True
+    elif opt[0] in ['-V', '--version']:
+        subcmd = ['version']
+    elif opt[0] in ['-D', '--debug']:
+        helpers.buglvl += 1
+        os.environ['BUP_DEBUG'] = str(helpers.buglvl)
+    elif opt[0] in ['--profile']:
+        do_profile = True
+    elif opt[0] in ['-d', '--bup-dir']:
+        os.environ['BUP_DIR'] = opt[1]
     else:
-        log('error: unexpected option "%s"\n' % opt[0])
-        usage()
+        usage('error: unexpected option "%s"' % opt[0])
+
+# Make BUP_DIR absolute, so we aren't affected by chdir (i.e. save -C, etc.).
+if 'BUP_DIR' in os.environ:
+    os.environ['BUP_DIR'] = os.path.abspath(os.environ['BUP_DIR'])
 
 if len(subcmd) == 0:
     if help_requested:
@@ -111,12 +132,12 @@ def subpath(s):
         sp = os.path.join(cmdpath, 'bup-%s' % s)
     return sp
 
-if not os.path.exists(subpath(subcmd_name)):
-    log('error: unknown command "%s"\n' % subcmd_name)
-    usage()
+subcmd[0] = subpath(subcmd_name)
+if not os.path.exists(subcmd[0]):
+    usage('error: unknown command "%s"' % subcmd_name)
 
 already_fixed = atoi(os.environ.get('BUP_FORCE_TTY'))
-if subcmd_name in ['ftp', 'help']:
+if subcmd_name in ['mux', 'ftp', 'help']:
     already_fixed = True
 fix_stdout = not already_fixed and os.isatty(1)
 fix_stderr = not already_fixed and os.isatty(2)
@@ -125,60 +146,118 @@ def force_tty():
     if fix_stdout or fix_stderr:
         amt = (fix_stdout and 1 or 0) + (fix_stderr and 2 or 0)
         os.environ['BUP_FORCE_TTY'] = str(amt)
-    os.setsid()  # make sure ctrl-c is sent just to us, not to child too
-
-if fix_stdout or fix_stderr:
-    realf = fix_stderr and 2 or 1
-    drealf = os.dup(realf)  # Popen goes crazy with stdout=2
-    n = subprocess.Popen([subpath('newliner')],
-                         stdin=subprocess.PIPE, stdout=drealf,
-                         close_fds=True, preexec_fn=force_tty)
-    os.close(drealf)
-    outf = fix_stdout and n.stdin.fileno() or None
-    errf = fix_stderr and n.stdin.fileno() or None
-else:
-    n = None
-    outf = None
-    errf = None
 
 
-class SigException(Exception):
-    def __init__(self, signum):
-        self.signum = signum
-        Exception.__init__(self, 'signal %d received' % signum)
-def handler(signum, frame):
-    raise SigException(signum)
+sep_rx = re.compile(r'([\r\n])')
 
-signal.signal(signal.SIGTERM, handler)
-signal.signal(signal.SIGINT, handler)
+def print_clean_line(dest, content, width, sep=None):
+    """Write some or all of content, followed by sep, to the dest fd after
+    padding the content with enough spaces to fill the current
+    terminal width or truncating it to the terminal width if sep is a
+    carriage return."""
+    global sep_rx
+    assert sep in ('\r', '\n', None)
+    if not content:
+        if sep:
+            os.write(dest, sep)
+        return
+    for x in content:
+        assert not sep_rx.match(x)
+    content = ''.join(content)
+    if sep == '\r' and len(content) > width:
+        content = content[width:]
+    os.write(dest, content)
+    if len(content) < width:
+        os.write(dest, ' ' * (width - len(content)))
+    os.write(dest, sep)
 
-ret = 95
-p = None
-try:
+def filter_output(src_out, src_err, dest_out, dest_err):
+    """Transfer data from src_out to dest_out and src_err to dest_err via
+    print_clean_line until src_out and src_err close."""
+    global sep_rx
+    assert not isinstance(src_out, bool)
+    assert not isinstance(src_err, bool)
+    assert not isinstance(dest_out, bool)
+    assert not isinstance(dest_err, bool)
+    assert src_out is not None or src_err is not None
+    assert (src_out is None) == (dest_out is None)
+    assert (src_err is None) == (dest_err is None)
+    pending = {}
+    pending_ex = None
     try:
-        p = subprocess.Popen([subpath(subcmd_name)] + subcmd[1:],
-                             stdout=outf, stderr=errf, preexec_fn=force_tty)
-        while 1:
-            # if we get a signal while waiting, we have to keep waiting, just
-            # in case our child doesn't die.
+        fds = tuple([x for x in (src_out, src_err) if x is not None])
+        for fd in fds:
+            flags = fcntl.fcntl(fd, F_GETFL)
+            assert fcntl.fcntl(fd, F_SETFL, flags | os.O_NONBLOCK) == 0
+        while fds:
+            ready_fds, _, _ = select.select(fds, [], [])
+            width = tty_width()
+            for fd in ready_fds:
+                buf = os.read(fd, 4096)
+                dest = dest_out if fd == src_out else dest_err
+                if not buf:
+                    fds = tuple([x for x in fds if x is not fd])
+                    print_clean_line(dest, pending.pop(fd, []), width)
+                else:
+                    split = sep_rx.split(buf)
+                    if len(split) > 2:
+                        while len(split) > 1:
+                            content, sep = split[:2]
+                            split = split[2:]
+                            print_clean_line(dest,
+                                             pending.pop(fd, []) + [content],
+                                             width,
+                                             sep)
+                    else:
+                        assert(len(split) == 1)
+                        pending.setdefault(fd, []).extend(split)
+    except BaseException as ex:
+        pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
+    try:
+        # Try to finish each of the streams
+        for fd, pending_items in pending.iteritems():
+            dest = dest_out if fd == src_out else dest_err
             try:
-                ret = p.wait()
-                break
-            except SigException, e:
-                log('\nbup: %s\n' % e)
-                os.kill(p.pid, e.signum)
-                ret = 94
-    except OSError, e:
-        log('%s: %s\n' % (subpath(subcmd_name), e))
-        ret = 98
-finally:
-    if p and p.poll() == None:
-        os.kill(p.pid, signal.SIGTERM)
-        p.wait()
-    if n:
-        n.stdin.close()
+                print_clean_line(dest, pending_items, width)
+            except (EnvironmentError, EOFError) as ex:
+                pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
+    except BaseException as ex:
+        pending_ex = chain_ex(add_ex_tb(ex), pending_ex)
+    if pending_ex:
+        raise pending_ex
+
+def run_subcmd(subcmd):
+
+    c = (do_profile and [sys.executable, '-m', 'cProfile'] or []) + subcmd
+    if not (fix_stdout or fix_stderr):
+        os.execvp(c[0], c)
+
+    p = None
+    try:
+        p = subprocess.Popen(c,
+                             stdout=PIPE if fix_stdout else sys.stdout,
+                             stderr=PIPE if fix_stderr else sys.stderr,
+                             preexec_fn=force_tty,
+                             bufsize=4096,
+                             close_fds=True)
+        # Assume p will receive these signals and quit, which will
+        # then cause us to quit.
+        for sig in (signal.SIGINT, signal.SIGTERM, signal.SIGQUIT):
+            signal.signal(sig, signal.SIG_IGN)
+
+        filter_output(fix_stdout and p.stdout.fileno() or None,
+                      fix_stderr and p.stderr.fileno() or None,
+                      fix_stdout and sys.stdout.fileno() or None,
+                      fix_stderr and sys.stderr.fileno() or None)
+        return p.wait()
+    except BaseException as ex:
+        add_ex_tb(ex)
         try:
-            n.wait()
-        except:
-            pass
-sys.exit(ret)
+            if p and p.poll() == None:
+                os.kill(p.pid, signal.SIGTERM)
+                p.wait()
+        except BaseException as kill_ex:
+            raise chain_ex(add_ex_tb(kill_ex), ex)
+        raise ex
+        
+wrap_main(lambda : run_subcmd(subcmd))