Dulwich.io dulwich / e8acf0e
Add more docstrings. Jelmer Vernooij 10 years ago
2 changed file(s) with 35 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
4040 Pack,
4141 PackData,
4242 iter_sha1,
43 load_packs,
4443 load_pack_index,
4544 write_pack,
4645 write_pack_data,
168167 def packs(self):
169168 """List with pack objects."""
170169 if self._pack_cache is None:
171 self._pack_cache = list(load_packs(self.pack_dir))
170 self._pack_cache = list(self._load_packs())
172171 return self._pack_cache
172
173 def _load_packs(self):
174 if not os.path.exists(self.pack_dir):
175 return
176 for name in os.listdir(self.pack_dir):
177 if name.startswith("pack-") and name.endswith(".pack"):
178 yield Pack(os.path.join(self.pack_dir, name[:-len(".pack")]))
173179
174180 def _add_known_pack(self, path):
175181 """Add a newly appeared pack to the cache by path.
6767
6868
6969 def take_msb_bytes(map, offset):
70 """Read bytes marked with most significant bit.
71
72 :param map: The buffer.
73 :param offset: Offset in the buffer at which to start reading.
74 """
7075 ret = []
7176 while len(ret) == 0 or ret[-1] & 0x80:
7277 ret.append(ord(map[offset]))
7479 return ret
7580
7681
77 def read_zlib_chunks(data, offset, dec_size):
82 def read_zlib_chunks(data, offset):
83 """Read chunks of zlib data from a buffer.
84
85 :param data: Buffer to read from
86 :param offset: Offset at which to start reading
87 :return: Tuple with list of chunks and length of
88 compressed data length
89 """
7890 obj = zlib.decompressobj()
7991 ret = []
8092 fed = 0
90102
91103
92104 def read_zlib(data, offset, dec_size):
93 ret, comp_len = read_zlib_chunks(data, offset, dec_size)
105 """Read zlib-compressed data from a buffer.
106
107 :param data: Buffer
108 :param offset: Offset in the buffer at which to read
109 :param dec_size: Size of the decompressed buffer
110 :return: Uncompressed buffer and compressed buffer length.
111 """
112 ret, comp_len = read_zlib_chunks(data, offset)
94113 x = "".join(ret)
95114 assert len(x) == dec_size
96115 return x, comp_len
122141
123142
124143 def load_pack_index(filename):
144 """Load an index file by path.
145
146 :param filename: Path to the index file
147 """
125148 f = open(filename, 'rb')
126149 if f.read(4) == '\377tOc':
127150 version = struct.unpack(">L", f.read(4))[0]
370393 assert version in (2, 3), "Version was %d" % version
371394 (num_objects,) = unpack_from(">L", header, 8)
372395 return (version, num_objects)
373
374
375 def read_pack_tail(f):
376 return (f.read(20),)
377396
378397
379398 def unpack_object(map, offset=0):
471490 def _read_header(self):
472491 (version, self._num_objects) = read_pack_header(self._file)
473492 self._file.seek(self._size-20)
474 (self._stored_checksum,) = read_pack_tail(self._file)
493 self._stored_checksum = self._file.read(20)
475494
476495 def __len__(self):
477496 """Returns the number of objects in this pack."""
635654 return self._stored_checksum
636655
637656 def check(self):
657 """Check the consistency of this pack."""
638658 return (self.calculate_checksum() == self.get_stored_checksum())
639659
640660 def get_object_at(self, offset):
10961116 *self.data.resolve_object(offset, type, obj, get_raw))
10971117
10981118
1099 def load_packs(path):
1100 if not os.path.exists(path):
1101 return
1102 for name in os.listdir(path):
1103 if name.startswith("pack-") and name.endswith(".pack"):
1104 yield Pack(os.path.join(path, name[:-len(".pack")]))
1105
1106
11071119 try:
11081120 from dulwich._pack import apply_delta, bisect_find_sha
11091121 except ImportError: