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