]> arthur.barton.de Git - bup.git/commitdiff
index-cmd: accommodate python 3
authorRob Browning <rlb@defaultvalue.org>
Sat, 28 Dec 2019 01:46:02 +0000 (19:46 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sun, 19 Jan 2020 17:46:10 +0000 (11:46 -0600)
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
cmd/index-cmd.py

index 61a679a5cbd1ca3a02d4bcd4ecdb0871a2c24d81..c2a22f913aa64bd2fe83f4a6b68812ed5837613b 100755 (executable)
@@ -6,13 +6,16 @@ exec "$bup_python" "$0" ${1+"$@"}
 # end of bup preamble
 
 from __future__ import absolute_import, print_function
 # end of bup preamble
 
 from __future__ import absolute_import, print_function
+from binascii import hexlify
 import sys, stat, time, os, errno, re
 
 from bup import metadata, options, git, index, drecurse, hlinkdb
 import sys, stat, time, os, errno, re
 
 from bup import metadata, options, git, index, drecurse, hlinkdb
+from bup.compat import argv_bytes
 from bup.drecurse import recursive_dirlist
 from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE
 from bup.helpers import (add_error, handle_ctrl_c, log, parse_excludes, parse_rx_excludes,
                          progress, qprogress, saved_errors)
 from bup.drecurse import recursive_dirlist
 from bup.hashsplit import GIT_MODE_TREE, GIT_MODE_FILE
 from bup.helpers import (add_error, handle_ctrl_c, log, parse_excludes, parse_rx_excludes,
                          progress, qprogress, saved_errors)
+from bup.io import byte_stream, path_msg
 
 
 class IterHelper:
 
 
 class IterHelper:
@@ -21,10 +24,11 @@ class IterHelper:
         self.cur = None
         self.next()
 
         self.cur = None
         self.next()
 
-    def next(self):
+    def __next__(self):
         self.cur = next(self.i, None)
         return self.cur
 
         self.cur = next(self.i, None)
         return self.cur
 
+    next = __next__
 
 def check_index(reader):
     try:
 
 def check_index(reader):
     try:
@@ -35,9 +39,9 @@ def check_index(reader):
             if e.children_n:
                 if opt.verbose:
                     log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n,
             if e.children_n:
                 if opt.verbose:
                     log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n,
-                                            e.name))
+                                            path_msg(e.name)))
                 assert(e.children_ofs)
                 assert(e.children_ofs)
-                assert e.name[-1] == b'/'
+                assert e.name.endswith(b'/')
                 assert(not d.get(e.children_ofs))
                 d[e.children_ofs] = 1
             if e.flags & index.IX_HASHVALID:
                 assert(not d.get(e.children_ofs))
                 d[e.children_ofs] = 1
             if e.flags & index.IX_HASHVALID:
@@ -57,28 +61,28 @@ def check_index(reader):
 
 
 def clear_index(indexfile):
 
 
 def clear_index(indexfile):
-    indexfiles = [indexfile, indexfile + '.meta', indexfile + '.hlink']
+    indexfiles = [indexfile, indexfile + b'.meta', indexfile + b'.hlink']
     for indexfile in indexfiles:
         path = git.repo(indexfile)
         try:
             os.remove(path)
             if opt.verbose:
     for indexfile in indexfiles:
         path = git.repo(indexfile)
         try:
             os.remove(path)
             if opt.verbose:
-                log('clear: removed %s\n' % path)
+                log('clear: removed %s\n' % path_msg(path))
         except OSError as e:
             if e.errno != errno.ENOENT:
                 raise
 
 
         except OSError as e:
             if e.errno != errno.ENOENT:
                 raise
 
 
-def update_index(top, excluded_paths, exclude_rxs, xdev_exceptions):
+def update_index(top, excluded_paths, exclude_rxs, xdev_exceptions, out=None):
     # tmax and start must be epoch nanoseconds.
     tmax = (time.time() - 1) * 10**9
     ri = index.Reader(indexfile)
     # tmax and start must be epoch nanoseconds.
     tmax = (time.time() - 1) * 10**9
     ri = index.Reader(indexfile)
-    msw = index.MetaStoreWriter(indexfile + '.meta')
+    msw = index.MetaStoreWriter(indexfile + b'.meta')
     wi = index.Writer(indexfile, msw, tmax)
     rig = IterHelper(ri.iter(name=top))
     tstart = int(time.time()) * 10**9
 
     wi = index.Writer(indexfile, msw, tmax)
     rig = IterHelper(ri.iter(name=top))
     tstart = int(time.time()) * 10**9
 
-    hlinks = hlinkdb.HLinkDB(indexfile + '.hlink')
+    hlinks = hlinkdb.HLinkDB(indexfile + b'.hlink')
 
     fake_hash = None
     if opt.fake_valid:
 
     fake_hash = None
     if opt.fake_valid:
@@ -95,8 +99,8 @@ def update_index(top, excluded_paths, exclude_rxs, xdev_exceptions):
                                        exclude_rxs=exclude_rxs,
                                        xdev_exceptions=xdev_exceptions):
         if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
                                        exclude_rxs=exclude_rxs,
                                        xdev_exceptions=xdev_exceptions):
         if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
-            sys.stdout.write('%s\n' % path)
-            sys.stdout.flush()
+            out.write(b'%s\n' % path)
+            out.flush()
             elapsed = time.time() - index_start
             paths_per_sec = total / elapsed if elapsed else 0
             qprogress('Indexing: %d (%d paths/s)\r' % (total, paths_per_sec))
             elapsed = time.time() - index_start
             paths_per_sec = total / elapsed if elapsed else 0
             qprogress('Indexing: %d (%d paths/s)\r' % (total, paths_per_sec))
@@ -250,10 +254,17 @@ tick_start = time.time()
 time.sleep(1 - (tick_start - int(tick_start)))
 
 git.check_repo_or_die()
 time.sleep(1 - (tick_start - int(tick_start)))
 
 git.check_repo_or_die()
-indexfile = opt.indexfile or git.repo('bupindex')
 
 handle_ctrl_c()
 
 
 handle_ctrl_c()
 
+if opt.verbose is None:
+    opt.verbose = 0
+
+if opt.indexfile:
+    indexfile = argv_bytes(opt.indexfile)
+else:
+    indexfile = git.repo(b'bupindex')
+
 if opt.check:
     log('check: starting initial check.\n')
     check_index(index.Reader(indexfile))
 if opt.check:
     log('check: starting initial check.\n')
     check_index(index.Reader(indexfile))
@@ -262,36 +273,42 @@ if opt.clear:
     log('clear: clearing index.\n')
     clear_index(indexfile)
 
     log('clear: clearing index.\n')
     clear_index(indexfile)
 
+sys.stdout.flush()
+out = byte_stream(sys.stdout)
+
 if opt.update:
     if not extra:
         o.fatal('update mode (-u) requested but no paths given')
 if opt.update:
     if not extra:
         o.fatal('update mode (-u) requested but no paths given')
+    extra = [argv_bytes(x) for x in extra]
     excluded_paths = parse_excludes(flags, o.fatal)
     exclude_rxs = parse_rx_excludes(flags, o.fatal)
     xexcept = index.unique_resolved_paths(extra)
     for rp, path in index.reduce_paths(extra):
     excluded_paths = parse_excludes(flags, o.fatal)
     exclude_rxs = parse_rx_excludes(flags, o.fatal)
     xexcept = index.unique_resolved_paths(extra)
     for rp, path in index.reduce_paths(extra):
-        update_index(rp, excluded_paths, exclude_rxs, xdev_exceptions=xexcept)
+        update_index(rp, excluded_paths, exclude_rxs, xdev_exceptions=xexcept,
+                     out=out)
 
 if opt['print'] or opt.status or opt.modified:
 
 if opt['print'] or opt.status or opt.modified:
-    for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
+    extra = [argv_bytes(x) for x in extra]
+    for name, ent in index.Reader(indexfile).filter(extra or [b'']):
         if (opt.modified 
             and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
             continue
         if (opt.modified 
             and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
             continue
-        line = ''
+        line = b''
         if opt.status:
             if ent.is_deleted():
         if opt.status:
             if ent.is_deleted():
-                line += 'D '
+                line += b'D '
             elif not ent.is_valid():
                 if ent.sha == index.EMPTY_SHA:
             elif not ent.is_valid():
                 if ent.sha == index.EMPTY_SHA:
-                    line += 'A '
+                    line += b'A '
                 else:
                 else:
-                    line += 'M '
+                    line += b'M '
             else:
             else:
-                line += '  '
+                line += b'  '
         if opt.hash:
         if opt.hash:
-            line += ent.sha.encode('hex') + ' '
+            line += hexlify(ent) + b' '
         if opt.long:
         if opt.long:
-            line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
-        print(line + (name or './'))
+            line += b'%7s %7s ' % (oct(ent.mode), oct(ent.gitmode))
+        out.write(line + (name or b'./') + b'\n')
 
 if opt.check and (opt['print'] or opt.status or opt.modified or opt.update):
     log('check: starting final check.\n')
 
 if opt.check and (opt['print'] or opt.status or opt.modified or opt.update):
     log('check: starting final check.\n')