Bläddra i källkod

Port index to python3

Gary van der Merwe 10 år sedan
förälder
incheckning
7da496f1a9
3 ändrade filer med 39 tillägg och 41 borttagningar
  1. 23 17
      dulwich/index.py
  2. 1 1
      dulwich/pack.py
  3. 15 23
      dulwich/tests/test_index.py

+ 23 - 17
dulwich/index.py

@@ -23,6 +23,7 @@ import errno
 import os
 import stat
 import struct
+import sys
 
 from dulwich.file import GitFile
 from dulwich.objects import (
@@ -38,6 +39,11 @@ from dulwich.pack import (
     SHA1Writer,
     )
 
+if sys.version_info[0] == 2:
+    iteritems = lambda d: d.iteritems()
+else:
+    iteritems = lambda d: d.items()
+
 
 IndexEntry = collections.namedtuple(
     'IndexEntry', [
@@ -52,9 +58,9 @@ def pathsplit(path):
     :return: Tuple with directory name and basename
     """
     try:
-        (dirname, basename) = path.rsplit("/", 1)
+        (dirname, basename) = path.rsplit(b"/", 1)
     except ValueError:
-        return ("", path)
+        return (b"", path)
     else:
         return (dirname, basename)
 
@@ -63,7 +69,7 @@ def pathjoin(*args):
     """Join a /-delimited path.
 
     """
-    return "/".join([p for p in args if p])
+    return b"/".join([p for p in args if p])
 
 
 def read_cache_time(f):
@@ -122,18 +128,18 @@ def write_cache_entry(f, entry):
     write_cache_time(f, ctime)
     write_cache_time(f, mtime)
     flags = len(name) | (flags &~ 0x0fff)
-    f.write(struct.pack(">LLLLLL20sH", dev & 0xFFFFFFFF, ino & 0xFFFFFFFF, mode, uid, gid, size, hex_to_sha(sha), flags))
+    f.write(struct.pack(b'>LLLLLL20sH', dev & 0xFFFFFFFF, ino & 0xFFFFFFFF, mode, uid, gid, size, hex_to_sha(sha), flags))
     f.write(name)
     real_size = ((f.tell() - beginoffset + 8) & ~7)
-    f.write("\0" * ((beginoffset + real_size) - f.tell()))
+    f.write(b'\0' * ((beginoffset + real_size) - f.tell()))
 
 
 def read_index(f):
     """Read an index file, yielding the individual entries."""
     header = f.read(4)
-    if header != "DIRC":
+    if header != b'DIRC':
         raise AssertionError("Invalid index file header: %r" % header)
-    (version, num_entries) = struct.unpack(">LL", f.read(4 * 2))
+    (version, num_entries) = struct.unpack(b'>LL', f.read(4 * 2))
     assert version in (1, 2)
     for i in range(num_entries):
         yield read_cache_entry(f)
@@ -156,8 +162,8 @@ def write_index(f, entries):
     :param f: File-like object to write to
     :param entries: Iterable over the entries to write
     """
-    f.write("DIRC")
-    f.write(struct.pack(">LL", 2, len(entries)))
+    f.write(b'DIRC')
+    f.write(struct.pack(b'>LL', 2, len(entries)))
     for x in entries:
         write_cache_entry(f, x)
 
@@ -263,20 +269,20 @@ class Index(object):
         self._byname = {}
 
     def __setitem__(self, name, x):
-        assert isinstance(name, str)
+        assert isinstance(name, bytes)
         assert len(x) == 10
         # Remove the old entry if any
         self._byname[name] = x
 
     def __delitem__(self, name):
-        assert isinstance(name, str)
+        assert isinstance(name, bytes)
         del self._byname[name]
 
     def iteritems(self):
-        return self._byname.iteritems()
+        return iteritems(self._byname)
 
     def update(self, entries):
-        for name, value in entries.iteritems():
+        for name, value in iteritems(entries):
             self[name] = value
 
     def changes_from_tree(self, object_store, tree, want_unchanged=False):
@@ -312,14 +318,14 @@ def commit_tree(object_store, blobs):
     :return: SHA1 of the created tree.
     """
 
-    trees = {"": {}}
+    trees = {b'': {}}
 
     def add_tree(path):
         if path in trees:
             return trees[path]
         dirname, basename = pathsplit(path)
         t = add_tree(dirname)
-        assert isinstance(basename, str)
+        assert isinstance(basename, bytes)
         newtree = {}
         t[basename] = newtree
         trees[path] = newtree
@@ -332,7 +338,7 @@ def commit_tree(object_store, blobs):
 
     def build_tree(path):
         tree = Tree()
-        for basename, entry in trees[path].iteritems():
+        for basename, entry in iteritems(trees[path]):
             if isinstance(entry, dict):
                 mode = stat.S_IFDIR
                 sha = build_tree(pathjoin(path, basename))
@@ -341,7 +347,7 @@ def commit_tree(object_store, blobs):
             tree.add(basename, mode, sha)
         object_store.add_object(tree)
         return tree.id
-    return build_tree("")
+    return build_tree(b'')
 
 
 def commit_index(object_store, index):

+ 1 - 1
dulwich/pack.py

@@ -1381,7 +1381,7 @@ class SHA1Reader(object):
 
     def __init__(self, f):
         self.f = f
-        self.sha1 = sha1('')
+        self.sha1 = sha1(b'')
 
     def read(self, num=None):
         data = self.f.read(num)

+ 15 - 23
dulwich/tests/test_index.py

@@ -52,7 +52,6 @@ from dulwich.repo import Repo
 from dulwich.tests import TestCase
 from dulwich.tests.utils import skipIfPY3
 
-@skipIfPY3
 class IndexTestCase(TestCase):
 
     datadir = os.path.join(os.path.dirname(__file__), 'data/indexes')
@@ -61,20 +60,19 @@ class IndexTestCase(TestCase):
         return Index(os.path.join(self.datadir, name))
 
 
-@skipIfPY3
 class SimpleIndexTestCase(IndexTestCase):
 
     def test_len(self):
         self.assertEqual(1, len(self.get_simple_index("index")))
 
     def test_iter(self):
-        self.assertEqual(['bla'], list(self.get_simple_index("index")))
+        self.assertEqual([b'bla'], list(self.get_simple_index("index")))
 
     def test_getitem(self):
         self.assertEqual(((1230680220, 0), (1230680220, 0), 2050, 3761020,
                            33188, 1000, 1000, 0,
-                           'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0),
-                          self.get_simple_index("index")["bla"])
+                           b'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0),
+                          self.get_simple_index("index")[b"bla"])
 
     def test_empty(self):
         i = self.get_simple_index("notanindex")
@@ -86,11 +84,9 @@ class SimpleIndexTestCase(IndexTestCase):
         changes = list(i.changes_from_tree(MemoryObjectStore(), None))
         self.assertEqual(1, len(changes))
         (oldname, newname), (oldmode, newmode), (oldsha, newsha) = changes[0]
-        self.assertEqual('bla', newname)
-        self.assertEqual('e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', newsha)
-
+        self.assertEqual(b'bla', newname)
+        self.assertEqual(b'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', newsha)
 
-@skipIfPY3
 class SimpleIndexWriterTestCase(IndexTestCase):
 
     def setUp(self):
@@ -102,18 +98,17 @@ class SimpleIndexWriterTestCase(IndexTestCase):
         shutil.rmtree(self.tempdir)
 
     def test_simple_write(self):
-        entries = [('barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
+        entries = [(b'barbla', (1230680220, 0), (1230680220, 0), 2050, 3761020,
                     33188, 1000, 1000, 0,
-                    'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
+                    b'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)]
         filename = os.path.join(self.tempdir, 'test-simple-write-index')
-        with open(filename, 'w+') as x:
+        with open(filename, 'wb+') as x:
             write_index(x, entries)
 
-        with open(filename, 'r') as x:
+        with open(filename, 'rb') as x:
             self.assertEqual(entries, list(read_index(x)))
 
 
-@skipIfPY3
 class ReadIndexDictTests(IndexTestCase):
 
     def setUp(self):
@@ -125,14 +120,14 @@ class ReadIndexDictTests(IndexTestCase):
         shutil.rmtree(self.tempdir)
 
     def test_simple_write(self):
-        entries = {'barbla': ((1230680220, 0), (1230680220, 0), 2050, 3761020,
+        entries = {b'barbla': ((1230680220, 0), (1230680220, 0), 2050, 3761020,
                     33188, 1000, 1000, 0,
-                    'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)}
+                    b'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391', 0)}
         filename = os.path.join(self.tempdir, 'test-simple-write-index')
-        with open(filename, 'w+') as x:
+        with open(filename, 'wb+') as x:
             write_index_dict(x, entries)
 
-        with open(filename, 'r') as x:
+        with open(filename, 'rb') as x:
             self.assertEqual(entries, read_index_dict(x))
 
 
@@ -168,7 +163,6 @@ class CommitTreeTests(TestCase):
                           set(self.store._data.keys()))
 
 
-@skipIfPY3
 class CleanupModeTests(TestCase):
 
     def test_file(self):
@@ -187,7 +181,6 @@ class CleanupModeTests(TestCase):
         self.assertEqual(0o160000, cleanup_mode(0o160744))
 
 
-@skipIfPY3
 class WriteCacheTimeTests(TestCase):
 
     def test_write_string(self):
@@ -210,7 +203,6 @@ class WriteCacheTimeTests(TestCase):
         self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
 
 
-@skipIfPY3
 class IndexEntryFromStatTests(TestCase):
 
     def test_simple(self):
@@ -249,7 +241,7 @@ class IndexEntryFromStatTests(TestCase):
             0))
 
 
-@skipIfPY3
+@skipIfPY3  # These tests depend on repo.
 class BuildIndexTests(TestCase):
 
     def assertReasonableIndexEntry(self, index_entry, mode, filesize, sha):
@@ -384,7 +376,7 @@ class BuildIndexTests(TestCase):
             sorted(os.listdir(os.path.join(repo.path, 'c'))))
 
 
-@skipIfPY3
+@skipIfPY3  # These tests depend on repo.
 class GetUnstagedChangesTests(TestCase):
 
     def test_get_unstaged_changes(self):