From a9a970ea6b95affa931d3543224b08257d3ade93 Mon Sep 17 00:00:00 2001 From: Rob Browning Date: Wed, 1 Jan 2020 13:16:43 -0600 Subject: [PATCH] list-idx-cmd: adjust for python 3 and enable test-list-idx Signed-off-by: Rob Browning Tested-by: Rob Browning --- Makefile | 2 +- cmd/list-idx-cmd.py | 25 +++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 385fddd..46a4d33 100644 --- a/Makefile +++ b/Makefile @@ -175,6 +175,7 @@ cmdline_tests := \ t/test-drecurse.sh \ t/test-fsck.sh \ t/test-index-clear.sh \ + t/test-list-idx.sh \ t/test-ls \ t/test-ls-remote \ t/test-tz.sh @@ -189,7 +190,6 @@ ifeq "2" "$(bup_python_majver)" t/test-rm.sh \ t/test-gc.sh \ t/test-main.sh \ - t/test-list-idx.sh \ t/test-index.sh \ t/test-split-join.sh \ t/test-fuse.sh \ diff --git a/cmd/list-idx-cmd.py b/cmd/list-idx-cmd.py index 90753de..78bb0a0 100755 --- a/cmd/list-idx-cmd.py +++ b/cmd/list-idx-cmd.py @@ -6,11 +6,13 @@ exec "$bup_python" "$0" ${1+"$@"} # end of bup preamble from __future__ import absolute_import, print_function +from binascii import hexlify, unhexlify import sys, os from bup import git, options +from bup.compat import argv_bytes from bup.helpers import add_error, handle_ctrl_c, log, qprogress, saved_errors - +from bup.io import byte_stream optspec = """ bup list-idx [--find=] @@ -21,7 +23,8 @@ o = options.Options(optspec) (opt, flags, extra) = o.parse(sys.argv[1:]) handle_ctrl_c() -opt.find = opt.find or '' + +opt.find = argv_bytes(opt.find) if opt.find else b'' if not extra: o.fatal('you must provide at least one filename') @@ -30,32 +33,34 @@ if len(opt.find) > 40: o.fatal('--find parameter must be <= 40 chars long') else: if len(opt.find) % 2: - s = opt.find + '0' + s = opt.find + b'0' else: s = opt.find try: - bin = s.decode('hex') + bin = unhexlify(s) except TypeError: o.fatal('--find parameter is not a valid hex string') +sys.stdout.flush() +out = byte_stream(sys.stdout) find = opt.find.lower() - count = 0 -for name in extra: +idxfiles = [argv_bytes(x) for x in extra] +for name in idxfiles: try: ix = git.open_idx(name) except git.GitError as e: - add_error('%s: %s' % (name, e)) + add_error('%r: %s' % (name, e)) continue if len(opt.find) == 40: if ix.exists(bin): - print(name, find) + out.write(b'%s %s\n' % (name, find)) else: # slow, exhaustive search for _i in ix: - i = str(_i).encode('hex') + i = hexlify(_i) if i.startswith(find): - print(name, i) + out.write(b'%s %s\n' % (name, i)) qprogress('Searching: %d\r' % count) count += 1 -- 2.39.2