]> arthur.barton.de Git - bup.git/blob - lib/cmd/list-idx-cmd.py
features: show version number of the Python interpreter
[bup.git] / lib / cmd / list-idx-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, print_function
18 from binascii import hexlify, unhexlify
19 import os, sys
20
21 sys.path[:0] = [os.path.dirname(os.path.realpath(__file__)) + '/..']
22
23 from bup import compat, git, options
24 from bup.compat import argv_bytes
25 from bup.helpers import add_error, handle_ctrl_c, log, qprogress, saved_errors
26 from bup.io import byte_stream
27
28 optspec = """
29 bup list-idx [--find=<prefix>] <idxfilenames...>
30 --
31 find=   display only objects that start with <prefix>
32 """
33 o = options.Options(optspec)
34 opt, flags, extra = o.parse(compat.argv[1:])
35
36 handle_ctrl_c()
37
38 opt.find = argv_bytes(opt.find) if opt.find else b''
39
40 if not extra:
41     o.fatal('you must provide at least one filename')
42
43 if len(opt.find) > 40:
44     o.fatal('--find parameter must be <= 40 chars long')
45 else:
46     if len(opt.find) % 2:
47         s = opt.find + b'0'
48     else:
49         s = opt.find
50     try:
51         bin = unhexlify(s)
52     except TypeError:
53         o.fatal('--find parameter is not a valid hex string')
54
55 sys.stdout.flush()
56 out = byte_stream(sys.stdout)
57 find = opt.find.lower()
58 count = 0
59 idxfiles = [argv_bytes(x) for x in extra]
60 for name in idxfiles:
61     try:
62         ix = git.open_idx(name)
63     except git.GitError as e:
64         add_error('%r: %s' % (name, e))
65         continue
66     if len(opt.find) == 40:
67         if ix.exists(bin):
68             out.write(b'%s %s\n' % (name, find))
69     else:
70         # slow, exhaustive search
71         for _i in ix:
72             i = hexlify(_i)
73             if i.startswith(find):
74                 out.write(b'%s %s\n' % (name, i))
75             qprogress('Searching: %d\r' % count)
76             count += 1
77
78 if saved_errors:
79     log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))
80     sys.exit(1)