]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/main.py
main: fix output string truncation
[bup.git] / lib / bup / main.py
index 928f5a6b2962306eabb93be593a76d14c52cd928..dcab41b59eab18cf17b42df4d14a950c74f6e498 100755 (executable)
@@ -14,26 +14,23 @@ from importlib import import_module
 from pkgutil import iter_modules
 from subprocess import PIPE
 from threading import Thread
-import errno, re, select, signal, subprocess
+import re, select, signal, subprocess
 
 from bup import compat, path, helpers
 from bup.compat import (
     ModuleNotFoundError,
     add_ex_ctx,
     add_ex_tb,
-    argv_bytes,
     environ,
     fsdecode,
     int_types,
     wrap_main
 )
-from bup.compat import add_ex_tb, add_ex_ctx, argv_bytes, wrap_main
+from bup.compat import add_ex_tb, add_ex_ctx, wrap_main
 from bup.helpers import (
     columnate,
-    debug1,
     handle_ctrl_c,
     log,
-    merge_dict,
     tty_width
 )
 from bup.git import close_catpipes
@@ -84,7 +81,7 @@ def usage(msg=""):
     for cmd,synopsis in sorted(common.items()):
         log('    %-10s %s\n' % (cmd, synopsis))
     log('\n')
-    
+
     log('Other available commands:\n')
     cmds = set()
     for c in sorted(os.listdir(cmdpath)):
@@ -100,13 +97,31 @@ def usage(msg=""):
 
     log(columnate(sorted(cmds), '    '))
     log('\n')
-    
+
     log("See 'bup help COMMAND' for more information on " +
         "a specific command.\n")
     if msg:
         log("\n%s\n" % msg)
     sys.exit(99)
 
+def extract_argval(args):
+    """Assume args (all elements bytes) starts with a -x, --x, or --x=,
+argument that requires a value and return that value and the remaining
+args.  Exit with an errror if the value is missing.
+
+    """
+    # Assumes that first arg is a valid arg
+    arg = args[0]
+    if b'=' in arg:
+        val = arg.split(b'=')[1]
+        if not val:
+            usage('error: no value provided for %s option' % arg)
+        return val, args[1:]
+    if len(args) < 2:
+        usage('error: no value provided for %s option' % arg)
+    return args[1], args[2:]
+
+
 args = compat.get_argvb()
 if len(args) < 2:
     usage()
@@ -131,13 +146,11 @@ while args:
     elif arg == b'--profile':
         do_profile = True
         args = args[1:]
-    elif arg in (b'-d', b'--bup-dir'):
-        if len(args) < 2:
-            usage('error: no path provided for %s option' % arg)
-        bup_dir = args[1]
-        args = args[2:]
-    elif arg == b'--import-py-module':
-        args = args[2:]
+    elif arg in (b'-d', b'--bup-dir') or arg.startswith(b'--bup-dir='):
+        bup_dir, args = extract_argval(args)
+    elif arg == b'--import-py-module' or arg.startswith(b'--import-py-module='):
+        # Just need to skip it here
+        _, args = extract_argval(args)
     elif arg.startswith(b'-'):
         usage('error: unexpected option "%s"'
               % arg.decode('ascii', 'backslashescape'))
@@ -184,13 +197,9 @@ fix_stdout = not already_fixed and os.isatty(1)
 fix_stderr = not already_fixed and os.isatty(2)
 
 if fix_stdout or fix_stderr:
-    tty_env = merge_dict(environ,
-                         {b'BUP_FORCE_TTY': (b'%d'
-                                             % ((fix_stdout and 1 or 0)
-                                                + (fix_stderr and 2 or 0))),
-                          b'BUP_TTY_WIDTH': b'%d' % _tty_width(), })
-else:
-    tty_env = environ
+    _ttymask = (fix_stdout and 1 or 0) + (fix_stderr and 2 or 0)
+    environ[b'BUP_FORCE_TTY'] = b'%d' % _ttymask
+    environ[b'BUP_TTY_WIDTH'] = b'%d' % _tty_width()
 
 
 sep_rx = re.compile(br'([\r\n])')
@@ -210,7 +219,7 @@ def print_clean_line(dest, content, width, sep=None):
         assert not sep_rx.match(x)
     content = b''.join(content)
     if sep == b'\r' and len(content) > width:
-        content = content[width:]
+        content = content[:width]
     os.write(dest, content)
     if len(content) < width:
         os.write(dest, b' ' * (width - len(content)))
@@ -224,8 +233,7 @@ def filter_output(srcs, dests):
 
     """
     global sep_rx
-    assert all(type(x) in int_types for x in srcs)
-    assert all(type(x) in int_types for x in srcs)
+    assert all(isinstance(x, int_types) for x in srcs)
     assert len(srcs) == len(dests)
     srcs = tuple(srcs)
     dest_for = dict(zip(srcs, dests))
@@ -369,7 +377,7 @@ def run_subproc_cmd(args):
         p = subprocess.Popen(c,
                              stdout=PIPE if fix_stdout else out,
                              stderr=PIPE if fix_stderr else err,
-                             env=tty_env, bufsize=4096, close_fds=True)
+                             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):