1
# Copyright (C) 2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
"""Exports an archive from a bazaar branch"""
15
from bzrlib.export import get_export_generator
18
class ExporterFileObject(object):
19
"""Shim that accumulates temporarily written out data.
21
There are python tarfile classes that want to write to a file like object.
22
We want to stream data. But wsgi assumes it can pull data from the
23
handler, rather than having bytes pushed.
25
So this class holds the data temporarily, until it is pulled. It
26
should never buffer everything because as soon as a chunk is produced,
27
wsgi will be given the chance to take it.
34
self._buffer.append(s)
38
return ''.join(self._buffer)
46
def export_archive(history, root, revid, archive_format):
47
"""Export tree contents to an archive
49
:param history: Instance of history to export
50
:param root: Root location inside the archive.
51
:param revid: Revision to export
52
:param archive_format: Format of the archive, eg 'tar.gz'.
54
fileobj = ExporterFileObject()
55
tree = history._branch.repository.revision_tree(revid)
56
for _ in get_export_generator(tree=tree, root=root, fileobj=fileobj,
57
format=archive_format):
58
yield fileobj.get_buffer()
59
# Might have additonal contents written
60
yield fileobj.get_buffer()