]> arthur.barton.de Git - bup.git/commitdiff
compat.hexstr: add and use
authorRob Browning <rlb@defaultvalue.org>
Mon, 23 Dec 2019 18:59:15 +0000 (12:59 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sat, 11 Jan 2020 20:39:27 +0000 (14:39 -0600)
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
cmd/bloom-cmd.py
cmd/get-cmd.py
cmd/midx-cmd.py
cmd/server-cmd.py
lib/bup/compat.py
lib/bup/gc.py
lib/bup/rm.py
lib/bup/vfs.py

index 0e7fbf4986facb51491c9539e4c6e5ed28b401fe..3d195ca01bee44d80aef7ed9992d6d7bc733f5ae 100755 (executable)
@@ -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
index f5fe01a5bd73b672590910d89147e1454d5ddce9..809464d617111164cabb3d7527f30af90a8b6c2d 100755 (executable)
@@ -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'):
index c7062db20ebfae51ada9ae27814ea7e6054d16eb..ef0a312e0318df72fda9ec05de120f7f381977ec 100755 (executable)
@@ -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
 
 
index abb02203c78fb77ab10bc32226c59231b34b012c..b1b77297e4977ca21b7c1e2e36adf7c31af621be 100755 (executable)
@@ -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
index f45d9ab53259abd95b331e5e413c49095997ac84..a8aead44a0bc49168945a0301a937272f9940150 100644 (file)
@@ -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.
 
index a70339eb1ebb6af50caf0f469c60ecd79b4a7de1..abf157ed521192b496f7877af3d74f641b09f5d0 100644 (file)
@@ -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
index d0e0551402ad6860e856b15dbff54c4552551e7f..e43abc5b7e1de1ac3b08e892dea71e35e6961447 100644 (file)
@@ -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))
index 1fd1b462348ccfe6962e5440aeccb21f4cfe5479..3c97d21c7943166f4094e7a2eb285b42f93e488b 100644 (file)
@@ -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