]> arthur.barton.de Git - bup.git/blob - lib/bup/cmd/list_idx.py
chmod -x lib/bup/cmd/*.py
[bup.git] / lib / bup / cmd / list_idx.py
1
2 from __future__ import absolute_import, print_function
3 from binascii import hexlify, unhexlify
4 import sys
5
6 from bup import git, options
7 from bup.compat import argv_bytes
8 from bup.helpers import add_error, handle_ctrl_c, log, qprogress, saved_errors
9 from bup.io import byte_stream
10
11 optspec = """
12 bup list-idx [--find=<prefix>] <idxfilenames...>
13 --
14 find=   display only objects that start with <prefix>
15 """
16
17 def main(argv):
18     o = options.Options(optspec)
19     opt, flags, extra = o.parse_bytes(argv[1:])
20
21     handle_ctrl_c()
22
23     opt.find = argv_bytes(opt.find) if opt.find else b''
24
25     if not extra:
26         o.fatal('you must provide at least one filename')
27
28     if len(opt.find) > 40:
29         o.fatal('--find parameter must be <= 40 chars long')
30     else:
31         if len(opt.find) % 2:
32             s = opt.find + b'0'
33         else:
34             s = opt.find
35         try:
36             bin = unhexlify(s)
37         except TypeError:
38             o.fatal('--find parameter is not a valid hex string')
39
40     sys.stdout.flush()
41     out = byte_stream(sys.stdout)
42     find = opt.find.lower()
43     count = 0
44     idxfiles = [argv_bytes(x) for x in extra]
45     for name in idxfiles:
46         try:
47             ix = git.open_idx(name)
48         except git.GitError as e:
49             add_error('%r: %s' % (name, e))
50             ix.close()
51             continue
52         with ix:
53             if len(opt.find) == 40:
54                 if ix.exists(bin):
55                     out.write(b'%s %s\n' % (name, find))
56             else:
57                 # slow, exhaustive search
58                 for _i in ix:
59                     i = hexlify(_i)
60                     if i.startswith(find):
61                         out.write(b'%s %s\n' % (name, i))
62                     qprogress('Searching: %d\r' % count)
63                     count += 1
64
65     if saved_errors:
66         log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))
67         sys.exit(1)