]> arthur.barton.de Git - bup.git/blob - lib/bup/cmd/list_idx.py
15e8b4c836ca3970dc1f9449eacb063607b82ba7
[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             continue
51         if len(opt.find) == 40:
52             if ix.exists(bin):
53                 out.write(b'%s %s\n' % (name, find))
54         else:
55             # slow, exhaustive search
56             for _i in ix:
57                 i = hexlify(_i)
58                 if i.startswith(find):
59                     out.write(b'%s %s\n' % (name, i))
60                 qprogress('Searching: %d\r' % count)
61                 count += 1
62
63     if saved_errors:
64         log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))
65         sys.exit(1)