From 77e9b9a5e52ade4357a58707635a85dd4dd2ed3c Mon Sep 17 00:00:00 2001 From: Rob Browning Date: Mon, 23 Dec 2019 12:59:15 -0600 Subject: [PATCH 1/1] compat.hexstr: add and use Signed-off-by: Rob Browning --- cmd/bloom-cmd.py | 4 ++-- cmd/get-cmd.py | 8 ++++---- cmd/midx-cmd.py | 11 ++++------- cmd/server-cmd.py | 3 ++- lib/bup/compat.py | 7 +++++++ lib/bup/gc.py | 6 +++--- lib/bup/rm.py | 21 +++++++++++---------- lib/bup/vfs.py | 4 ++-- 8 files changed, 35 insertions(+), 29 deletions(-) diff --git a/cmd/bloom-cmd.py b/cmd/bloom-cmd.py index 0e7fbf4..3d195ca 100755 --- a/cmd/bloom-cmd.py +++ b/cmd/bloom-cmd.py @@ -9,6 +9,7 @@ from __future__ import absolute_import import glob, os, sys, tempfile from bup import options, git, bloom +from bup.compat import hexstr from bup.helpers import (add_error, debug1, handle_ctrl_c, log, progress, qprogress, saved_errors) @@ -54,8 +55,7 @@ def check_bloom(path, bloomfilename, idx): log("bloom: checking %s\n" % ridx) for objsha in git.open_idx(idx): if not b.exists(objsha): - add_error("bloom: ERROR: object %s missing" - % str(objsha).encode('hex')) + add_error('bloom: ERROR: object %s missing' % hexstr(objsha)) _first = None diff --git a/cmd/get-cmd.py b/cmd/get-cmd.py index f5fe01a..809464d 100755 --- a/cmd/get-cmd.py +++ b/cmd/get-cmd.py @@ -12,7 +12,7 @@ from functools import partial from stat import S_ISDIR from bup import git, client, helpers, vfs -from bup.compat import wrap_main +from bup.compat import hexstr, wrap_main from bup.git import get_cat_data, parse_commit, walk_object from bup.helpers import add_error, debug1, handle_ctrl_c, log, saved_errors from bup.helpers import hostname, shstr, tty_width @@ -556,11 +556,11 @@ def resolve_targets(specs, src_repo, dest_repo): def log_item(name, type, opt, tree=None, commit=None, tag=None): if tag and opt.print_tags: - print(tag.encode('hex')) + print(hexstr(tag)) if tree and opt.print_trees: - print(tree.encode('hex')) + print(hexstr(tree)) if commit and opt.print_commits: - print(commit.encode('hex')) + print(hexstr(commit)) if opt.verbose: last = '' if type in ('root', 'branch', 'save', 'commit', 'tree'): diff --git a/cmd/midx-cmd.py b/cmd/midx-cmd.py index c7062db..ef0a312 100755 --- a/cmd/midx-cmd.py +++ b/cmd/midx-cmd.py @@ -9,7 +9,7 @@ from __future__ import absolute_import import glob, math, os, resource, struct, sys, tempfile from bup import options, git, midx, _helpers, xstat -from bup.compat import range +from bup.compat import hexstr, range from bup.helpers import (Sha1, add_error, atomically_replaced_file, debug1, fdatasync, handle_ctrl_c, log, mmap_readwrite, qprogress, saved_errors, unlink) @@ -64,20 +64,17 @@ def check_midx(name): git.shorten_hash(subname), ecount, len(sub))) if not sub.exists(e): add_error("%s: %s: %s missing from idx" - % (nicename, git.shorten_hash(subname), - str(e).encode('hex'))) + % (nicename, git.shorten_hash(subname), hexstr(e))) if not ix.exists(e): add_error("%s: %s: %s missing from midx" - % (nicename, git.shorten_hash(subname), - str(e).encode('hex'))) + % (nicename, git.shorten_hash(subname), hexstr(e))) prev = None for ecount,e in enumerate(ix): if not (ecount % 1234): qprogress(' Ordering: %d/%d\r' % (ecount, len(ix))) if not e >= prev: add_error('%s: ordering error: %s < %s' - % (nicename, - str(e).encode('hex'), str(prev).encode('hex'))) + % (nicename, hexstr(e), hexstr(prev))) prev = e diff --git a/cmd/server-cmd.py b/cmd/server-cmd.py index abb0220..b1b7729 100755 --- a/cmd/server-cmd.py +++ b/cmd/server-cmd.py @@ -9,6 +9,7 @@ from __future__ import absolute_import import os, sys, struct, subprocess from bup import options, git, vfs, vint +from bup.compat import hexstr from bup.git import MissingObject from bup.helpers import (Conn, debug1, debug2, linereader, lines_until_sentinel, log) @@ -131,7 +132,7 @@ def receive_objects_v2(conn, junk): debug1("bup server: suggesting index %s\n" % git.shorten_hash(name)) debug1("bup server: because of object %s\n" - % shar.encode('hex')) + % hexstr(shar)) conn.write('index %s\n' % name) suggested.add(name) continue diff --git a/lib/bup/compat.py b/lib/bup/compat.py index f45d9ab..a8aead4 100644 --- a/lib/bup/compat.py +++ b/lib/bup/compat.py @@ -1,6 +1,7 @@ from __future__ import absolute_import, print_function from array import array +from binascii import hexlify from traceback import print_exception import sys @@ -30,6 +31,10 @@ if py3: str_type = str int_types = (int,) + def hexstr(b): + """Return hex string (not bytes as with hexlify) representation of b.""" + return b.hex() + def add_ex_tb(ex): """Do nothing (already handled by Python 3 infrastructure).""" return ex @@ -73,6 +78,8 @@ else: # Python 2 str_type = basestring int_types = (int, long) + hexstr = hexlify + def add_ex_tb(ex): """Add a traceback to ex if it doesn't already have one. Return ex. diff --git a/lib/bup/gc.py b/lib/bup/gc.py index a70339e..abf157e 100644 --- a/lib/bup/gc.py +++ b/lib/bup/gc.py @@ -2,7 +2,7 @@ from __future__ import absolute_import import glob, os, subprocess, sys, tempfile from bup import bloom, git, midx -from bup.compat import range +from bup.compat import hexstr, range from bup.git import MissingObject, walk_object from bup.helpers import Nonlocal, log, progress, qprogress from os.path import basename @@ -60,7 +60,7 @@ def count_objects(dir, verbosity): def report_live_item(n, total, ref_name, ref_id, item, verbosity): status = 'scanned %02.2f%%' % (n * 100.0 / total) - hex_id = ref_id.encode('hex') + hex_id = hexstr(ref_id) dirslash = '/' if item.type == 'tree' else '' chunk_path = item.chunk_path @@ -227,7 +227,7 @@ def bup_gc(threshold=10, compression=1, verbosity=0): live_objects = find_live_objects(existing_count, cat_pipe, verbosity=verbosity) except MissingObject as ex: - log('bup: missing object %r \n' % ex.oid.encode('hex')) + log('bup: missing object %s \n' % hexstr(ex.oid)) sys.exit(1) try: # FIXME: just rename midxes and bloom, and restore them at the end if diff --git a/lib/bup/rm.py b/lib/bup/rm.py index d0e0551..e43abc5 100644 --- a/lib/bup/rm.py +++ b/lib/bup/rm.py @@ -4,6 +4,7 @@ import sys from bup import compat, git, vfs from bup.client import ClientError +from bup.compat import hexstr from bup.git import get_commit_items from bup.helpers import add_error, die_if_errors, log, saved_errors @@ -135,17 +136,17 @@ def bup_rm(repo, paths, compression=6, verbosity=None): else: git.update_ref(ref_name, new_ref, orig_ref) if verbosity: - new_hex = new_ref.encode('hex') - if orig_ref: - orig_hex = orig_ref.encode('hex') - log('updated %r (%s -> %s)\n' - % (ref_name, orig_hex, new_hex)) - else: - log('updated %r (%s)\n' % (ref_name, new_hex)) + log('updated %r (%s%s)\n' + % (ref_name, + hexstr(orig_ref) + ' -> ' if orig_ref else '', + hexstr(new_ref))) except (git.GitError, ClientError) as ex: if new_ref: - add_error('while trying to update %r (%s -> %s): %s' - % (ref_name, orig_ref, new_ref, ex)) + add_error('while trying to update %r (%s%s): %s' + % (ref_name, + hexstr(orig_ref) + ' -> ' if orig_ref else '', + hexstr(new_ref), + ex)) else: add_error('while trying to delete %r (%s): %s' - % (ref_name, orig_ref, ex)) + % (ref_name, hexstr(orig_ref), ex)) diff --git a/lib/bup/vfs.py b/lib/bup/vfs.py index 1fd1b46..3c97d21 100644 --- a/lib/bup/vfs.py +++ b/lib/bup/vfs.py @@ -56,7 +56,7 @@ from time import localtime, strftime import re, sys from bup import git, metadata, vint -from bup.compat import range +from bup.compat import hexstr, range from bup.git import BUP_CHUNKED, cp, get_commit_items, parse_commit, tree_decode from bup.helpers import debug2, last from bup.metadata import Metadata @@ -485,7 +485,7 @@ def tree_data_and_bupm(repo, oid): data = ''.join(it) assert item_t == 'tree' elif item_t != 'tree': - raise Exception('%r is not a tree or commit' % oid.encode('hex')) + raise Exception('%s is not a tree or commit' % hexstr(oid)) for _, mangled_name, sub_oid in tree_decode(data): if mangled_name == '.bupm': return data, sub_oid -- 2.39.2