]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/cmd/get.py
pylint: enable inconsistent-return-statements
[bup.git] / lib / bup / cmd / get.py
index f9f6573cf6c306437c9760ce79268e09376f8c03..599dcc6b489c1aee2f5a128e087c2ca1a2abe197 100755 (executable)
@@ -1,36 +1,21 @@
-#!/bin/sh
-"""": # -*-python-*-
-# https://sourceware.org/bugzilla/show_bug.cgi?id=26034
-export "BUP_ARGV_0"="$0"
-arg_i=1
-for arg in "$@"; do
-    export "BUP_ARGV_${arg_i}"="$arg"
-    shift
-    arg_i=$((arg_i + 1))
-done
-# Here to end of preamble replaced during install
-bup_python="$(dirname "$0")/../../../config/bin/python" || exit $?
-exec "$bup_python" "$0"
-"""
-# end of bup preamble
 
 from __future__ import absolute_import, print_function
-
-# Intentionally replace the dirname "$0" that python prepends
-import os, sys
-sys.path[0] = os.path.dirname(os.path.realpath(__file__)) + '/..'
-
 from binascii import hexlify, unhexlify
 from collections import namedtuple
-from functools import partial
 from stat import S_ISDIR
-import textwrap, time
-
-from bup import compat, git, client, helpers, vfs
-from bup.compat import argv_bytes, environ, hexstr, items, wrap_main
+import os, sys, textwrap, time
+
+from bup import compat, git, client, vfs
+from bup.compat import (
+    argv_bytes,
+    bytes_from_byte,
+    environ,
+    hexstr,
+    items,
+)
 from bup.git import get_cat_data, parse_commit, walk_object
-from bup.helpers import add_error, debug1, handle_ctrl_c, log, saved_errors
-from bup.helpers import hostname, shstr, tty_width
+from bup.helpers import add_error, debug1, log, saved_errors
+from bup.helpers import hostname, tty_width, parse_num
 from bup.io import path_msg
 from bup.pwdgrp import userfullname, username
 from bup.repo import LocalRepo, RemoteRepo
@@ -144,46 +129,47 @@ def parse_args(args):
     remaining = args[1:]  # Skip argv[0]
     while remaining:
         arg = remaining[0]
-        if arg in ('-h', '--help'):
+        if arg in (b'-h', b'--help'):
             sys.stdout.write(usage(argspec))
             sys.exit(0)
-        elif arg in ('-v', '--verbose'):
+        elif arg in (b'-v', b'--verbose'):
             opt.verbose += 1
             remaining = remaining[1:]
-        elif arg in ('--ff', '--append', '--pick', '--force-pick',
-                     '--new-tag', '--replace', '--unnamed'):
+        elif arg in (b'--ff', b'--append', b'--pick', b'--force-pick',
+                     b'--new-tag', b'--replace', b'--unnamed'):
             (ref,), remaining = require_n_args_or_die(1, remaining)
-            ref = argv_bytes(ref)
-            opt.target_specs.append(Spec(method=arg[2:], src=ref, dest=None))
-        elif arg in ('--ff:', '--append:', '--pick:', '--force-pick:',
-                     '--new-tag:', '--replace:'):
+            opt.target_specs.append(Spec(method=arg[2:].decode('ascii'),
+                                         src=ref, dest=None))
+        elif arg in (b'--ff:', b'--append:', b'--pick:', b'--force-pick:',
+                     b'--new-tag:', b'--replace:'):
             (ref, dest), remaining = require_n_args_or_die(2, remaining)
-            ref, dest = argv_bytes(ref), argv_bytes(dest)
-            opt.target_specs.append(Spec(method=arg[2:-1], src=ref, dest=dest))
-        elif arg in ('-s', '--source'):
+            opt.target_specs.append(Spec(method=arg[2:-1].decode('ascii'),
+                                         src=ref, dest=dest))
+        elif arg in (b'-s', b'--source'):
             (opt.source,), remaining = require_n_args_or_die(1, remaining)
-        elif arg in ('-r', '--remote'):
+        elif arg in (b'-r', b'--remote'):
             (opt.remote,), remaining = require_n_args_or_die(1, remaining)
-        elif arg in ('-c', '--print-commits'):
+        elif arg in (b'-c', b'--print-commits'):
             opt.print_commits, remaining = True, remaining[1:]
-        elif arg in ('-t', '--print-trees'):
+        elif arg in (b'-t', b'--print-trees'):
             opt.print_trees, remaining = True, remaining[1:]
-        elif arg == '--print-tags':
+        elif arg == b'--print-tags':
             opt.print_tags, remaining = True, remaining[1:]
-        elif arg in ('-0', '-1', '-2', '-3', '-4', '-5', '-6', '-7', '-8', '-9'):
+        elif arg in (b'-0', b'-1', b'-2', b'-3', b'-4', b'-5', b'-6', b'-7',
+                     b'-8', b'-9'):
             opt.compress = int(arg[1:])
             remaining = remaining[1:]
-        elif arg == '--compress':
+        elif arg == b'--compress':
             (opt.compress,), remaining = require_n_args_or_die(1, remaining)
             opt.compress = int(opt.compress)
-        elif arg == '--bwlimit':
+        elif arg == b'--bwlimit':
             (opt.bwlimit,), remaining = require_n_args_or_die(1, remaining)
-            opt.bwlimit = long(opt.bwlimit)
-        elif arg.startswith('-') and len(arg) > 2 and arg[1] != '-':
+            opt.bwlimit = int(opt.bwlimit)
+        elif arg.startswith(b'-') and len(arg) > 2 and arg[1] != b'-':
             # Try to interpret this as -xyz, i.e. "-xyz -> -x -y -z".
             # We do this last so that --foo -bar is valid if --foo
             # requires a value.
-            remaining[0:1] = ('-' + c for c in arg[1:])
+            remaining[0:1] = (b'-' + bytes_from_byte(c) for c in arg[1:])
             # FIXME
             continue
         else:
@@ -243,7 +229,7 @@ def find_vfs_item(name, repo):
     elif kind == vfs.RevList:
         kind = 'branch'
     elif kind == vfs.Commit:
-        if len(res) > 1 and type(res[-2][1]) == vfs.RevList:
+        if len(res) > 1 and isinstance(res[-2][1], vfs.RevList):
             kind = 'save'
         else:
             kind = 'commit'
@@ -262,7 +248,7 @@ def find_vfs_item(name, repo):
                            follow=False, want_meta=False)
         leaf_name, leaf_item = res[-1]
         assert leaf_item
-        assert type(leaf_item) == vfs.Commit
+        assert isinstance(leaf_item, vfs.Commit)
         name = b'/'.join(x[0] for x in res)
         kind = 'save'
     else:
@@ -295,11 +281,11 @@ def cleanup_vfs_path(p):
     return b'/' + result
 
 
-def validate_vfs_path(p):
+def validate_vfs_path(p, spec):
     if p.startswith(b'/.') \
        and not p.startswith(b'/.tag/'):
         misuse('unsupported destination path %s in %s'
-               % (path_msg(dest.path), spec_msg(spec)))
+               % (path_msg(p), spec_msg(spec)))
     return p
 
 
@@ -384,6 +370,8 @@ def handle_ff(item, src_repo, writer, opt):
         return item.src.hash, unhexlify(commit_items.tree)
     misuse('destination is not an ancestor of source for %s'
            % spec_msg(item.spec))
+    # misuse() doesn't return
+    return None
 
 
 def resolve_append(spec, src_repo, dest_repo):
@@ -403,7 +391,7 @@ def handle_append(item, src_repo, writer, opt):
     if item.src.type == 'tree':
         get_random_item(item.spec.src, src_oidx, src_repo, writer, opt)
         parent = item.dest.hash
-        msg = b'bup save\n\nGenerated by command:\n%r\n' % compat.argvb
+        msg = b'bup save\n\nGenerated by command:\n%r\n' % compat.get_argvb()
         userline = b'%s <%s@%s>' % (userfullname(), username(), hostname())
         now = time.time()
         commit = writer.new_commit(item.src.hash, parent,
@@ -433,7 +421,7 @@ def resolve_pick(spec, src_repo, dest_repo):
         misuse('no destination provided for %s', spec_args)
     dest = find_vfs_item(spec.dest, dest_repo)
     if not dest:
-        cp = validate_vfs_path(cleanup_vfs_path(spec.dest))
+        cp = validate_vfs_path(cleanup_vfs_path(spec.dest), spec)
         dest = default_loc._replace(path=cp)
     else:
         if not dest.type == 'branch' and not dest.path.startswith(b'/.tag/'):
@@ -495,7 +483,7 @@ def resolve_replace(spec, src_repo, dest_repo):
             misuse('%s impossible; can only overwrite branch or tag'
                   % spec_args)
     else:
-        cp = validate_vfs_path(cleanup_vfs_path(spec.dest))
+        cp = validate_vfs_path(cleanup_vfs_path(spec.dest), spec)
         dest = default_loc._replace(path=cp)
     if not dest.path.startswith(b'/.tag/') \
        and not src.type in ('branch', 'save', 'commit'):
@@ -583,10 +571,9 @@ def log_item(name, type, opt, tree=None, commit=None, tag=None):
                 last = '/'
         log('%s%s\n' % (path_msg(name), last))
 
-def main():
-    handle_ctrl_c()
+def main(argv):
     is_reverse = environ.get(b'BUP_SERVER_REVERSE')
-    opt = parse_args(compat.argv)
+    opt = parse_args(argv)
     git.check_repo_or_die()
     if opt.source:
         opt.source = argv_bytes(opt.source)
@@ -594,8 +581,6 @@ def main():
         client.bwlimit = parse_num(opt.bwlimit)
     if is_reverse and opt.remote:
         misuse("don't use -r in reverse mode; it's automatic")
-    if opt.remote:
-        opt.remote = argv_bytes(opt.remote)
     if opt.remote or is_reverse:
         dest_repo = RemoteRepo(opt.remote)
     else:
@@ -677,5 +662,3 @@ def main():
     if saved_errors:
         log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))
         sys.exit(1)
-
-wrap_main(main)