123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- """Generates tarballs for Git trees.
- """
- import posixpath
- import stat
- import tarfile
- import struct
- from os import SEEK_END
- from io import BytesIO
- from contextlib import closing
- class ChunkedBytesIO(object):
- """Turn a list of bytestrings into a file-like object.
- This is similar to creating a `BytesIO` from a concatenation of the
- bytestring list, but saves memory by NOT creating one giant bytestring
- first::
- BytesIO(b''.join(list_of_bytestrings)) =~= ChunkedBytesIO(
- list_of_bytestrings)
- """
- def __init__(self, contents):
- self.contents = contents
- self.pos = (0, 0)
- def read(self, maxbytes=None):
- if maxbytes < 0:
- maxbytes = float('inf')
- buf = []
- chunk, cursor = self.pos
- while chunk < len(self.contents):
- if maxbytes < len(self.contents[chunk]) - cursor:
- buf.append(self.contents[chunk][cursor:cursor+maxbytes])
- cursor += maxbytes
- self.pos = (chunk, cursor)
- break
- else:
- buf.append(self.contents[chunk][cursor:])
- maxbytes -= len(self.contents[chunk]) - cursor
- chunk += 1
- cursor = 0
- self.pos = (chunk, cursor)
- return b''.join(buf)
- def tar_stream(store, tree, mtime, format=''):
- """Generate a tar stream for the contents of a Git tree.
- Returns a generator that lazily assembles a .tar.gz archive, yielding it in
- pieces (bytestrings). To obtain the complete .tar.gz binary file, simply
- concatenate these chunks.
- :param store: Object store to retrieve objects from
- :param tree: Tree object for the tree root
- :param mtime: UNIX timestamp that is assigned as the modification time for
- all files, and the gzip header modification time if format='gz'
- :param format: Optional compression format for tarball
- :return: Bytestrings
- """
- buf = BytesIO()
- with closing(tarfile.open(None, "w:%s" % format, buf)) as tar:
- if format == 'gz':
-
-
-
-
- buf.seek(0)
- assert buf.read(2) == b'\x1f\x8b', 'Invalid gzip header'
- buf.seek(4)
- buf.write(struct.pack('<L', mtime))
- buf.seek(0, SEEK_END)
- for entry_abspath, entry in _walk_tree(store, tree):
- try:
- blob = store[entry.sha]
- except KeyError:
-
-
- continue
- data = ChunkedBytesIO(blob.chunked)
- info = tarfile.TarInfo()
-
- info.name = entry_abspath.decode('ascii')
- info.size = blob.raw_length()
- info.mode = entry.mode
- info.mtime = mtime
- tar.addfile(info, data)
- yield buf.getvalue()
- buf.truncate(0)
- buf.seek(0)
- yield buf.getvalue()
- def _walk_tree(store, tree, root=b''):
- """Recursively walk a dulwich Tree, yielding tuples of
- (absolute path, TreeEntry) along the way.
- """
- for entry in tree.iteritems():
- entry_abspath = posixpath.join(root, entry.path)
- if stat.S_ISDIR(entry.mode):
- for _ in _walk_tree(store, store[entry.sha], entry_abspath):
- yield _
- else:
- yield (entry_abspath, entry)
|