]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/index.py
get: adjust for python 3 and test there
[bup.git] / lib / bup / index.py
index 5f6c366957cb93d0b98e90dd411c4b09bd890f08..ae6201853f45b9f4e7dc7816d03cdc7fda8895ec 100644 (file)
@@ -1,14 +1,17 @@
-import errno, metadata, os, stat, struct, tempfile
 
-from bup import xstat
-from bup._helpers import UINT_MAX
+from __future__ import absolute_import, print_function
+import errno, os, stat, struct, tempfile
+
+from bup import compat, metadata, xstat
+from bup._helpers import UINT_MAX, bytescmp
+from bup.compat import range
 from bup.helpers import (add_error, log, merge_iter, mmap_readwrite,
                          progress, qprogress, resolve_parent, slashappend)
 
-EMPTY_SHA = '\0'*20
-FAKE_SHA = '\x01'*20
+EMPTY_SHA = b'\0' * 20
+FAKE_SHA = b'\x01' * 20
 
-INDEX_HDR = 'BUPI\0\0\0\7'
+INDEX_HDR = b'BUPI\0\0\0\7'
 
 # Time values are handled as integer nanoseconds since the epoch in
 # memory, but are written as xstat/metadata timespecs.  This behavior
@@ -162,15 +165,17 @@ def _golevel(level, f, ename, newentry, metastore, tmax):
 
 class Entry:
     def __init__(self, basename, name, meta_ofs, tmax):
-        self.basename = str(basename)
-        self.name = str(name)
+        assert basename is None or type(basename) == bytes
+        assert name is None or type(name) == bytes
+        self.basename = basename
+        self.name = name
         self.meta_ofs = meta_ofs
         self.tmax = tmax
         self.children_ofs = 0
         self.children_n = 0
 
     def __repr__(self):
-        return ("(%s,0x%04x,%d,%d,%d,%d,%d,%d,%s/%s,0x%04x,%d,0x%08x/%d)"
+        return ("(%r,0x%04x,%d,%d,%d,%d,%d,%d,%s/%s,0x%04x,%d,0x%08x/%d)"
                 % (self.name, self.dev, self.ino, self.nlink,
                    self.ctime, self.mtime, self.atime,
                    self.size, self.mode, self.gitmode,
@@ -279,13 +284,39 @@ class Entry:
     def is_fake(self):
         return not self.ctime
 
-    def __cmp__(a, b):
-        return (cmp(b.name, a.name)
-                or cmp(a.is_valid(), b.is_valid())
-                or cmp(a.is_fake(), b.is_fake()))
+    def _cmp(self, other):
+        # Note reversed name ordering
+        bc = bytescmp(other.name, self.name)
+        if bc != 0:
+            return bc
+        vc = self.is_valid() - other.is_valid()
+        if vc != 0:
+            return vc
+        fc = self.is_fake() - other.is_fake()
+        if fc != 0:
+            return fc
+        return 0
+
+    def __eq__(self, other):
+        return self._cmp(other) == 0
+
+    def __ne__(self, other):
+        return self._cmp(other) != 0
+
+    def __lt__(self, other):
+        return self._cmp(other) < 0
+
+    def __gt__(self, other):
+        return self._cmp(other) > 0
+
+    def __le__(self, other):
+        return self._cmp(other) <= 0
+
+    def __ge__(self, other):
+        return self._cmp(other) >= 0
 
     def write(self, f):
-        f.write(self.basename + '\0' + self.packed())
+        f.write(self.basename + b'\0' + self.packed())
 
 
 class NewEntry(Entry):
@@ -319,7 +350,7 @@ class ExistingEntry(Entry):
          self.ctime, ctime_ns, self.mtime, mtime_ns, self.atime, atime_ns,
          self.size, self.mode, self.gitmode, self.sha,
          self.flags, self.children_ofs, self.children_n, self.meta_ofs
-         ) = struct.unpack(INDEX_SIG, str(buffer(m, ofs, ENTLEN)))
+         ) = struct.unpack(INDEX_SIG, m[ofs : ofs + ENTLEN])
         self.atime = xstat.timespec_to_nsecs((self.atime, atime_ns))
         self.mtime = xstat.timespec_to_nsecs((self.mtime, mtime_ns))
         self.ctime = xstat.timespec_to_nsecs((self.ctime, ctime_ns))
@@ -350,22 +381,22 @@ class ExistingEntry(Entry):
 
     def iter(self, name=None, wantrecurse=None):
         dname = name
-        if dname and not dname.endswith('/'):
-            dname += '/'
+        if dname and not dname.endswith(b'/'):
+            dname += b'/'
         ofs = self.children_ofs
         assert(ofs <= len(self._m))
         assert(self.children_n <= UINT_MAX)  # i.e. python struct 'I'
-        for i in xrange(self.children_n):
-            eon = self._m.find('\0', ofs)
+        for i in range(self.children_n):
+            eon = self._m.find(b'\0', ofs)
             assert(eon >= 0)
             assert(eon >= ofs)
             assert(eon > ofs)
-            basename = str(buffer(self._m, ofs, eon-ofs))
+            basename = self._m[ofs : ofs + (eon - ofs)]
             child = ExistingEntry(self, basename, self.name + basename,
                                   self._m, eon+1)
             if (not dname
                  or child.name.startswith(dname)
-                 or child.name.endswith('/') and dname.startswith(child.name)):
+                 or child.name.endswith(b'/') and dname.startswith(child.name)):
                 if not wantrecurse or wantrecurse(child):
                     for e in child.iter(name=name, wantrecurse=wantrecurse):
                         yield e
@@ -380,12 +411,12 @@ class ExistingEntry(Entry):
 class Reader:
     def __init__(self, filename):
         self.filename = filename
-        self.m = ''
+        self.m = b''
         self.writable = False
         self.count = 0
         f = None
         try:
-            f = open(filename, 'r+')
+            f = open(filename, 'rb+')
         except IOError as e:
             if e.errno == errno.ENOENT:
                 pass
@@ -402,7 +433,8 @@ class Reader:
                     self.m = mmap_readwrite(f)
                     self.writable = True
                     self.count = struct.unpack(FOOTER_SIG,
-                          str(buffer(self.m, st.st_size-FOOTLEN, FOOTLEN)))[0]
+                                               self.m[st.st_size - FOOTLEN
+                                                      : st.st_size])[0]
 
     def __del__(self):
         self.close()
@@ -413,20 +445,20 @@ class Reader:
     def forward_iter(self):
         ofs = len(INDEX_HDR)
         while ofs+ENTLEN <= len(self.m)-FOOTLEN:
-            eon = self.m.find('\0', ofs)
+            eon = self.m.find(b'\0', ofs)
             assert(eon >= 0)
             assert(eon >= ofs)
             assert(eon > ofs)
-            basename = str(buffer(self.m, ofs, eon-ofs))
+            basename = self.m[ofs : ofs + (eon - ofs)]
             yield ExistingEntry(None, basename, basename, self.m, eon+1)
             ofs = eon + 1 + ENTLEN
 
     def iter(self, name=None, wantrecurse=None):
         if len(self.m) > len(INDEX_HDR)+ENTLEN:
             dname = name
-            if dname and not dname.endswith('/'):
-                dname += '/'
-            root = ExistingEntry(None, '/', '/',
+            if dname and not dname.endswith(b'/'):
+                dname += b'/'
+            root = ExistingEntry(None, b'/', b'/',
                                  self.m, len(self.m)-FOOTLEN-ENTLEN)
             for sub in root.iter(name=name, wantrecurse=wantrecurse):
                 yield sub
@@ -476,9 +508,9 @@ class Reader:
 # in an odd way and depends on a terminating '/' to indicate directories.
 def pathsplit(p):
     """Split a path into a list of elements of the file system hierarchy."""
-    l = p.split('/')
-    l = [i+'/' for i in l[:-1]] + l[-1:]
-    if l[-1] == '':
+    l = p.split(b'/')
+    l = [i + b'/' for i in l[:-1]] + l[-1:]
+    if l[-1] == b'':
         l.pop()  # extra blank caused by terminating '/'
     return l
 
@@ -494,7 +526,7 @@ class Writer:
         self.metastore = metastore
         self.tmax = tmax
         (dir,name) = os.path.split(filename)
-        (ffd,self.tmpname) = tempfile.mkstemp('.tmp', filename, dir)
+        ffd, self.tmpname = tempfile.mkstemp(b'.tmp', filename, dir)
         self.f = os.fdopen(ffd, 'wb', 65536)
         self.f.write(INDEX_HDR)
 
@@ -536,7 +568,7 @@ class Writer:
                               self.metastore, self.tmax)
 
     def add(self, name, st, meta_ofs, hashgen = None):
-        endswith = name.endswith('/')
+        endswith = name.endswith(b'/')
         ename = pathsplit(name)
         basename = ename[-1]
         #log('add: %r %r\n' % (basename, name))
@@ -600,14 +632,14 @@ def reduce_paths(paths):
     for p in paths:
         rp = _slashappend_or_add_error(resolve_parent(p), 'reduce_paths')
         if rp:
-            xpaths.append((rp, slashappend(p) if rp.endswith('/') else p))
+            xpaths.append((rp, slashappend(p) if rp.endswith(b'/') else p))
     xpaths.sort()
 
     paths = []
     prev = None
     for (rp, p) in xpaths:
         if prev and (prev == rp 
-                     or (prev.endswith('/') and rp.startswith(prev))):
+                     or (prev.endswith(b'/') and rp.startswith(prev))):
             continue # already superceded by previous path
         paths.append((rp, p))
         prev = rp