Browse Source

Clean up pack.py.

-Removed trailing whitespace.
-Fixed long lines.
-Various docstring cleanup.

Change-Id: I2ccde03bc56d0f69ba2e3be7e11b3effce2d30ed
Dave Borowitz 15 years ago
parent
commit
93fda2d108
1 changed files with 101 additions and 102 deletions
  1. 101 102
      dulwich/pack.py

+ 101 - 102
dulwich/pack.py

@@ -1,17 +1,17 @@
 # pack.py -- For dealing wih packed git objects.
 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
 # Copryight (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
-# 
+#
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
 # as published by the Free Software Foundation; version 2
 # of the License or (at your option) a later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
@@ -75,7 +75,7 @@ supports_mmap_offset = (sys.version_info[0] >= 3 or
 
 def take_msb_bytes(read):
     """Read bytes marked with most significant bit.
-    
+
     :param read: Read function
     """
     ret = []
@@ -117,10 +117,9 @@ def read_zlib_chunks(read_some, dec_size, buffer_size=4096):
     comp_len = fed - len(obj.unused_data)
     return ret, comp_len, obj.unused_data
 
-
 def iter_sha1(iter):
     """Return the hexdigest of the SHA1 over a set of names.
-    
+
     :param iter: Iterator over string objects
     :return: 40-byte hex sha1 digest
     """
@@ -134,6 +133,7 @@ def load_pack_index(path):
     """Load an index file by path.
 
     :param filename: Path to the index file
+    :return: A PackIndex loaded from the given path
     """
     f = GitFile(path, 'rb')
     return load_pack_index_file(path, f)
@@ -163,6 +163,7 @@ def load_pack_index_file(path, f):
 
     :param path: Path for the index file
     :param f: File-like object
+    :return: A PackIndex loaded from the given file
     """
     contents, size = _load_file_contents(f)
     if contents[:4] == '\377tOc':
@@ -178,7 +179,7 @@ def load_pack_index_file(path, f):
 
 def bisect_find_sha(start, end, sha, unpack_name):
     """Find a SHA in a data blob with sorted SHAs.
-    
+
     :param start: Start index of range to search
     :param end: End index of range to search
     :param sha: Sha to find
@@ -201,10 +202,10 @@ def bisect_find_sha(start, end, sha, unpack_name):
 
 class PackIndex(object):
     """An index in to a packfile.
-  
+
     Given a sha id of an object a pack index can tell you the location in the
     packfile of that object if it has it.
-  
+
     To do the loop it opens the file, and indexes first 256 4 byte groups
     with the first byte of the sha id. The value in the four byte group indexed
     is the end of the group that shares the same starting byte. Subtract one
@@ -212,10 +213,10 @@ class PackIndex(object):
     The values are sorted by sha id within the group, so do the math to find
     the start and end offset and then bisect in to find if the value is present.
     """
-  
+
     def __init__(self, filename, file=None, contents=None, size=None):
         """Create a pack index object.
-    
+
         Provide it with the name of the index file to consider, and it will map
         it whenever required.
         """
@@ -230,14 +231,14 @@ class PackIndex(object):
             self._contents, self._size = _load_file_contents(file, size)
         else:
             self._contents, self._size = (contents, size)
-  
+
     def __eq__(self, other):
         if not isinstance(other, PackIndex):
             return False
-    
+
         if self._fan_out_table != other._fan_out_table:
             return False
-    
+
         for (name1, _, _), (name2, _, _) in izip(self.iterentries(),
                                                  other.iterentries()):
             if name1 != name2:
@@ -246,25 +247,25 @@ class PackIndex(object):
 
     def __ne__(self, other):
         return not self.__eq__(other)
-  
+
     def close(self):
         self._file.close()
-  
+
     def __len__(self):
         """Return the number of entries in this pack index."""
         return self._fan_out_table[-1]
-  
+
     def _unpack_entry(self, i):
         """Unpack the i-th entry in the index file.
-    
-        :return: Tuple with object name (SHA), offset in pack file and 
-              CRC32 checksum (if known)."""
+
+        :return: Tuple with object name (SHA), offset in pack file and CRC32
+            checksum (if known)."""
         raise NotImplementedError(self._unpack_entry)
-  
+
     def _unpack_name(self, i):
         """Unpack the i-th name from the index file."""
         raise NotImplementedError(self._unpack_name)
-  
+
     def _unpack_offset(self, i):
         """Unpack the i-th object offset from the index file."""
         raise NotImplementedError(self._unpack_offset)
@@ -272,43 +273,43 @@ class PackIndex(object):
     def _unpack_crc32_checksum(self, i):
         """Unpack the crc32 checksum for the i-th object from the index file."""
         raise NotImplementedError(self._unpack_crc32_checksum)
-  
+
     def __iter__(self):
         """Iterate over the SHAs in this pack."""
         return imap(sha_to_hex, self._itersha())
-  
+
     def _itersha(self):
         for i in range(len(self)):
             yield self._unpack_name(i)
-  
+
     def objects_sha1(self):
         """Return the hex SHA1 over all the shas of all objects in this pack.
-        
+
         :note: This is used for the filename of the pack.
         """
         return iter_sha1(self._itersha())
-  
+
     def iterentries(self):
         """Iterate over the entries in this pack index.
-       
+
         Will yield tuples with object name, offset in packfile and crc32
         checksum.
         """
         for i in range(len(self)):
             yield self._unpack_entry(i)
-  
+
     def _read_fan_out_table(self, start_offset):
         ret = []
         for i in range(0x100):
-            ret.append(struct.unpack(">L",
-                self._contents[start_offset+i*4:start_offset+(i+1)*4])[0])
+            fanout_entry = self._contents[start_offset+i*4:start_offset+(i+1)*4]
+            ret.append(struct.unpack(">L", fanout_entry)[0])
         return ret
-  
+
     def check(self):
         """Check that the stored checksum matches the actual checksum."""
         # TODO: Check pack contents, too
         return self.calculate_checksum() == self.get_stored_checksum()
-  
+
     def calculate_checksum(self):
         """Calculate the SHA1 checksum over this pack index.
 
@@ -318,21 +319,21 @@ class PackIndex(object):
 
     def get_pack_checksum(self):
         """Return the SHA1 checksum stored for the corresponding packfile.
-        
+
         :return: 20-byte binary digest
         """
         return str(self._contents[-40:-20])
-  
+
     def get_stored_checksum(self):
         """Return the SHA1 checksum stored for this index.
-        
+
         :return: 20-byte binary digest
         """
         return str(self._contents[-20:])
-  
+
     def object_index(self, sha):
         """Return the index in to the corresponding packfile for the object.
-    
+
         Given the name of an object it will return the offset that object
         lives at within the corresponding pack file. If the pack file doesn't
         have the object then None will be returned.
@@ -340,10 +341,10 @@ class PackIndex(object):
         if len(sha) == 40:
             sha = hex_to_sha(sha)
         return self._object_index(sha)
-  
+
     def _object_index(self, sha):
         """See object_index.
-        
+
         :param sha: A *binary* SHA string. (20 characters long)_
         """
         assert len(sha) == 20
@@ -357,7 +358,6 @@ class PackIndex(object):
         if i is None:
             raise KeyError(sha)
         return self._unpack_offset(i)
-            
 
 
 class PackIndex1(PackIndex):
@@ -369,22 +369,22 @@ class PackIndex1(PackIndex):
         self._fan_out_table = self._read_fan_out_table(0)
 
     def _unpack_entry(self, i):
-        (offset, name) = unpack_from(">L20s", self._contents, 
-            (0x100 * 4) + (i * 24))
+        (offset, name) = unpack_from(">L20s", self._contents,
+                                     (0x100 * 4) + (i * 24))
         return (name, offset, None)
- 
+
     def _unpack_name(self, i):
         offset = (0x100 * 4) + (i * 24) + 4
         return self._contents[offset:offset+20]
-  
+
     def _unpack_offset(self, i):
         offset = (0x100 * 4) + (i * 24)
         return unpack_from(">L", self._contents, offset)[0]
-  
+
     def _unpack_crc32_checksum(self, i):
         # Not stored in v1 index files
-        return None 
-  
+        return None
+
 
 class PackIndex2(PackIndex):
     """Version 2 Pack Index."""
@@ -397,24 +397,24 @@ class PackIndex2(PackIndex):
         self._fan_out_table = self._read_fan_out_table(8)
         self._name_table_offset = 8 + 0x100 * 4
         self._crc32_table_offset = self._name_table_offset + 20 * len(self)
-        self._pack_offset_table_offset = self._crc32_table_offset + 4 * len(self)
+        self._pack_offset_table_offset = (self._crc32_table_offset +
+                                          4 * len(self))
 
     def _unpack_entry(self, i):
-        return (self._unpack_name(i), self._unpack_offset(i), 
+        return (self._unpack_name(i), self._unpack_offset(i),
                 self._unpack_crc32_checksum(i))
- 
+
     def _unpack_name(self, i):
         offset = self._name_table_offset + i * 20
         return self._contents[offset:offset+20]
-  
+
     def _unpack_offset(self, i):
         offset = self._pack_offset_table_offset + i * 4
         return unpack_from(">L", self._contents, offset)[0]
-  
+
     def _unpack_crc32_checksum(self, i):
-        return unpack_from(">L", self._contents, 
+        return unpack_from(">L", self._contents,
                           self._crc32_table_offset + i * 4)[0]
-  
 
 
 def read_pack_header(read):
@@ -476,8 +476,7 @@ def unpack_object(read_all, read_some=None):
 
 
 def _compute_object_size((num, obj)):
-    """Compute the size of a unresolved object for use with LRUSizeCache.
-    """
+    """Compute the size of a unresolved object for use with LRUSizeCache."""
     if num in (6, 7):
         return chunks_length(obj[1])
     return chunks_length(obj)
@@ -485,38 +484,37 @@ def _compute_object_size((num, obj)):
 
 class PackData(object):
     """The data contained in a packfile.
-  
+
     Pack files can be accessed both sequentially for exploding a pack, and
     directly with the help of an index to retrieve a specific object.
-  
+
     The objects within are either complete or a delta aginst another.
-  
+
     The header is variable length. If the MSB of each byte is set then it
     indicates that the subsequent byte is still part of the header.
     For the first byte the next MS bits are the type, which tells you the type
     of object, and whether it is a delta. The LS byte is the lowest bits of the
     size. For each subsequent byte the LS 7 bits are the next MS bits of the
     size, i.e. the last byte of the header contains the MS bits of the size.
-  
+
     For the complete objects the data is stored as zlib deflated data.
     The size in the header is the uncompressed object size, so to uncompress
     you need to just keep feeding data to zlib until you get an object back,
     or it errors on bad data. This is done here by just giving the complete
     buffer from the start of the deflated object on. This is bad, but until I
     get mmap sorted out it will have to do.
-  
+
     Currently there are no integrity checks done. Also no attempt is made to
     try and detect the delta case, or a request for an object at the wrong
     position.  It will all just throw a zlib or KeyError.
     """
-  
+
     def __init__(self, filename, file=None, size=None):
-        """Create a PackData object that represents the pack in the given
-        filename.
-    
+        """Create a PackData object representing the pack in the given filename.
+
         The file must exist and stay readable until the object is disposed of.
         It must also stay the same size. It will be mapped whenever needed.
-    
+
         Currently there is a restriction on the size of the pack as the python
         mmap implementation is flawed.
         """
@@ -528,7 +526,7 @@ class PackData(object):
         else:
             self._file = file
         (version, self._num_objects) = read_pack_header(self._file.read)
-        self._offset_cache = LRUSizeCache(1024*1024*20, 
+        self._offset_cache = LRUSizeCache(1024*1024*20,
             compute_size=_compute_object_size)
 
     @classmethod
@@ -546,13 +544,15 @@ class PackData(object):
         if self._size is not None:
             return self._size
         self._size = os.path.getsize(self._filename)
-        assert self._size >= self._header_size, "%s is too small for a packfile (%d < %d)" % (self._filename, self._size, self._header_size)
+        errmsg = ("%s is too small for a packfile (%d < %d)" %
+                  (self._filename, self._size, self._header_size))
+        assert self._size >= self._header_size, errmsg
         return self._size
-  
+
     def __len__(self):
         """Returns the number of objects in this pack."""
         return self._num_objects
-  
+
     def calculate_checksum(self):
         """Calculate the checksum for this pack.
 
@@ -569,7 +569,7 @@ class PackData(object):
 
     def resolve_object(self, offset, type, obj, get_ref, get_offset=None):
         """Resolve an object, possibly resolving deltas when necessary.
-        
+
         :return: Tuple with object type and contents.
         """
         if type not in (6, 7): # Not a delta
@@ -577,7 +577,7 @@ class PackData(object):
 
         if get_offset is None:
             get_offset = self.get_object_at
-      
+
         if type == 6: # offset delta
             (delta_offset, delta) = obj
             assert isinstance(delta_offset, int)
@@ -593,15 +593,15 @@ class PackData(object):
             assert type != 6
             base_offset = None
         type, base_chunks = self.resolve_object(base_offset, type, base_obj,
-            get_ref)
+                                                get_ref)
         if base_offset is not None:
             self._offset_cache[base_offset] = type, base_chunks
         return (type, apply_delta(base_chunks, delta))
-  
+
     def iterobjects(self, progress=None):
 
         class ObjectIterator(object):
-            
+
             def __init__(self, pack):
                 self.i = 0
                 self.offset = pack._header_size
@@ -613,7 +613,7 @@ class PackData(object):
 
             def __len__(self):
                 return self.num
-            
+
             def next(self):
                 if self.i == self.num:
                     raise StopIteration
@@ -628,7 +628,7 @@ class PackData(object):
                 self.i+=1
                 return ret
         return ObjectIterator(self)
-  
+
     def iterentries(self, ext_resolve_ref=None, progress=None):
         """Yield entries summarizing the contents of this pack.
 
@@ -643,7 +643,7 @@ class PackData(object):
         postponed = defaultdict(list)
         class Postpone(Exception):
             """Raised to postpone delta resolving."""
-          
+
         def get_ref_text(sha):
             assert len(sha) == 20
             if sha in found:
@@ -672,20 +672,20 @@ class PackData(object):
                 extra.extend(postponed.get(sha, []))
         if postponed:
             raise KeyError([sha_to_hex(h) for h in postponed.keys()])
-  
+
     def sorted_entries(self, resolve_ext_ref=None, progress=None):
         """Return entries in this pack, sorted by SHA.
 
-        :param ext_resolve_ref: Optional function to resolve base
+        :param resolve_ext_ref: Optional function to resolve base
             objects (in case this is a thin pack)
         :param progress: Progress function, called with current and
-            total object count.
+            total object count
         :return: List of tuples with (sha, offset, crc32)
         """
         ret = list(self.iterentries(resolve_ext_ref, progress=progress))
         ret.sort()
         return ret
-  
+
     def create_index_v1(self, filename, resolve_ext_ref=None, progress=None):
         """Create a version 1 file for this data file.
 
@@ -696,7 +696,7 @@ class PackData(object):
         """
         entries = self.sorted_entries(resolve_ext_ref, progress=progress)
         write_pack_index_v1(filename, entries, self.calculate_checksum())
-  
+
     def create_index_v2(self, filename, resolve_ext_ref=None, progress=None):
         """Create a version 2 index file for this data file.
 
@@ -723,19 +723,19 @@ class PackData(object):
             self.create_index_v2(filename, resolve_ext_ref, progress)
         else:
             raise ValueError("unknown index format %d" % version)
-  
+
     def get_stored_checksum(self):
         """Return the expected checksum stored in this pack."""
         self._file.seek(self._get_size()-20)
         return self._file.read(20)
-  
+
     def check(self):
         """Check the consistency of this pack."""
         return (self.calculate_checksum() == self.get_stored_checksum())
-  
+
     def get_object_at(self, offset):
         """Given an offset in to the packfile return the object that is there.
-    
+
         Using the associated index the location of an object can be looked up,
         and then the packfile can be asked directly for that object using this
         function.
@@ -750,8 +750,7 @@ class PackData(object):
 
 
 class SHA1Reader(object):
-    """Wrapper around a file-like object that remembers the SHA1 of 
-    the data read from it."""
+    """Wrapper around a file-like object that remembers the SHA1 of its data."""
 
     def __init__(self, f):
         self.f = f
@@ -775,9 +774,8 @@ class SHA1Reader(object):
 
 
 class SHA1Writer(object):
-    """Wrapper around a file-like object that remembers the SHA1 of 
-    the data written to it."""
-    
+    """Wrapper around a file-like object that remembers the SHA1 of its data."""
+
     def __init__(self, f):
         self.f = f
         self.sha1 = make_sha("")
@@ -805,7 +803,8 @@ def write_pack_object(f, type, object):
     """Write pack object to a file.
 
     :param f: File to write to
-    :param o: Object to write
+    :param type: Numeric type of the object
+    :param object: Object to write
     :return: Tuple with offset at which the object was written, and crc32
     """
     offset = f.tell()
@@ -870,8 +869,8 @@ def write_pack_data(f, objects, num_objects, window=10):
     for obj, path in recency:
         magic.append( (obj.type_num, path, 1, -obj.raw_length(), obj) )
     magic.sort()
-    # Build a map of objects and their index in magic - so we can find preceeding objects
-    # to diff against
+    # Build a map of objects and their index in magic - so we can find
+    # preceeding objects to diff against
     offs = {}
     for i in range(len(magic)):
         offs[magic[i][4]] = i
@@ -987,7 +986,7 @@ def create_delta(base_buf, target_buf):
 
 def apply_delta(src_buf, delta):
     """Based on the similar function in git's patch-delta.c.
-    
+
     :param src_buf: Source buffer
     :param delta: Delta instructions
     """
@@ -1018,17 +1017,17 @@ def apply_delta(src_buf, delta):
         if cmd & 0x80:
             cp_off = 0
             for i in range(4):
-                if cmd & (1 << i): 
+                if cmd & (1 << i):
                     x = ord(delta[index])
                     index += 1
                     cp_off |= x << (i * 8)
             cp_size = 0
             for i in range(3):
-                if cmd & (1 << (4+i)): 
+                if cmd & (1 << (4+i)):
                     x = ord(delta[index])
                     index += 1
                     cp_size |= x << (i * 8)
-            if cp_size == 0: 
+            if cp_size == 0:
                 cp_size = 0x10000
             if (cp_off + cp_size < cp_size or
                 cp_off + cp_size > src_size or
@@ -1040,7 +1039,7 @@ def apply_delta(src_buf, delta):
             index += cmd
         else:
             raise ApplyDeltaError("Invalid opcode 0")
-    
+
     if index != delta_length:
         raise ApplyDeltaError("delta not empty: %r" % delta[index:])
 
@@ -1113,7 +1112,7 @@ class Pack(object):
             idx_stored_checksum = self.index.get_pack_checksum()
             data_stored_checksum = self._data.get_stored_checksum()
             if idx_stored_checksum != data_stored_checksum:
-                raise ChecksumMismatch(sha_to_hex(idx_stored_checksum), 
+                raise ChecksumMismatch(sha_to_hex(idx_stored_checksum),
                                        sha_to_hex(data_stored_checksum))
         return self._data