]> arthur.barton.de Git - bup.git/blob - lib/cmd/join-cmd.py
Prefer python 3, and mention intent to drop python 2 support
[bup.git] / lib / cmd / join-cmd.py
1 #!/bin/sh
2 """": # -*-python-*-
3 # https://sourceware.org/bugzilla/show_bug.cgi?id=26034
4 export "BUP_ARGV_0"="$0"
5 arg_i=1
6 for arg in "$@"; do
7     export "BUP_ARGV_${arg_i}"="$arg"
8     shift
9     arg_i=$((arg_i + 1))
10 done
11 # Here to end of preamble replaced during install
12 bup_python="$(dirname "$0")/../../config/bin/python" || exit $?
13 exec "$bup_python" "$0"
14 """
15 # end of bup preamble
16
17 from __future__ import absolute_import
18 import os.path, sys
19
20 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/..']
21
22 from bup import compat, git, options
23 from bup.compat import argv_bytes
24 from bup.helpers import linereader, log
25 from bup.io import byte_stream
26 from bup.repo import LocalRepo, RemoteRepo
27
28
29 optspec = """
30 bup join [-r host:path] [refs or hashes...]
31 --
32 r,remote=  remote repository path
33 o=         output filename
34 """
35 o = options.Options(optspec)
36 opt, flags, extra = o.parse(compat.argv[1:])
37 if opt.remote:
38     opt.remote = argv_bytes(opt.remote)
39
40 git.check_repo_or_die()
41
42 stdin = byte_stream(sys.stdin)
43
44 if not extra:
45     extra = linereader(stdin)
46
47 ret = 0
48 repo = RemoteRepo(opt.remote) if opt.remote else LocalRepo()
49
50 if opt.o:
51     outfile = open(opt.o, 'wb')
52 else:
53     sys.stdout.flush()
54     outfile = byte_stream(sys.stdout)
55
56 for ref in [argv_bytes(x) for x in extra]:
57     try:
58         for blob in repo.join(ref):
59             outfile.write(blob)
60     except KeyError as e:
61         outfile.flush()
62         log('error: %s\n' % e)
63         ret = 1
64
65 sys.exit(ret)