14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Export a Tree to a non-versioned directory."""
17
"""Export a tree to a tarball."""
28
28
from bzrlib.export import _export_iter_entries
29
from bzrlib.filters import (
31
filtered_output_bytes,
35
def prepare_tarball_item(tree, root, final_path, entry, filtered=False,
31
def prepare_tarball_item(tree, root, final_path, entry, force_mtime=None):
37
32
"""Prepare a tarball item for exporting
39
34
:param tree: Tree to export
65
chunks = tree.get_file_lines(entry.file_id)
66
filters = tree._content_filter_stack(final_path)
67
context = ContentFilterContext(final_path, tree, entry)
68
contents = filtered_output_bytes(chunks, filters, context)
69
content = ''.join(contents)
70
item.size = len(content)
71
fileobj = StringIO.StringIO(content)
73
item.size = tree.get_file_size(entry.file_id)
74
fileobj = tree.get_file(entry.file_id)
57
# This brings the whole file into memory, but that's almost needed for
58
# the tarfile contract, which wants the size of the file up front. We
59
# want to make sure it doesn't change, and we need to read it in one
60
# go for content filtering.
61
content = tree.get_file_text(entry.file_id)
62
item.size = len(content)
63
fileobj = StringIO.StringIO(content)
75
64
elif entry.kind == "directory":
76
65
item.type = tarfile.DIRTYPE
90
79
return (item, fileobj)
93
def export_tarball_generator(tree, ball, root, subdir=None, filtered=False,
82
def export_tarball_generator(tree, ball, root, subdir=None, force_mtime=None):
95
83
"""Export tree contents to a tarball.
97
85
:returns: A generator that will repeatedly produce None as each file is
103
91
:param ball: Tarball to export to; it will be closed when writing is
106
:param filtered: Whether to apply filters
108
94
:param subdir: Sub directory to export
110
96
:param force_mtime: Option mtime to force, instead of using tree
114
100
for final_path, entry in _export_iter_entries(tree, subdir):
115
101
(item, fileobj) = prepare_tarball_item(
116
tree, root, final_path, entry, filtered, force_mtime)
102
tree, root, final_path, entry, force_mtime)
117
103
ball.addfile(item, fileobj)
123
def tgz_exporter_generator(tree, dest, root, subdir, filtered=False,
124
force_mtime=None, fileobj=None):
109
def tgz_exporter_generator(tree, dest, root, subdir, force_mtime=None,
125
111
"""Export this tree to a new tar file.
127
113
`dest` will be created holding the contents of this tree; if it
161
147
zipstream = gzip.GzipFile(basename, 'w', fileobj=stream)
162
148
ball = tarfile.open(None, 'w|', fileobj=zipstream)
163
149
for _ in export_tarball_generator(
164
tree, ball, root, subdir, filtered, force_mtime):
150
tree, ball, root, subdir, force_mtime):
166
152
# Closing zipstream may trigger writes to stream
167
153
zipstream.close()
173
def tbz_exporter_generator(tree, dest, root, subdir, filtered=False,
159
def tbz_exporter_generator(tree, dest, root, subdir,
174
160
force_mtime=None, fileobj=None):
175
161
"""Export this tree to a new tar file.
189
175
# Python 2.6.5 and 2.7b1)
190
176
ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:bz2')
191
177
return export_tarball_generator(
192
tree, ball, root, subdir, filtered, force_mtime)
178
tree, ball, root, subdir, force_mtime)
195
181
def plain_tar_exporter_generator(tree, dest, root, subdir, compression=None,
196
filtered=False, force_mtime=None,
182
force_mtime=None, fileobj=None):
198
183
"""Export this tree to a new tar file.
200
185
`dest` will be created holding the contents of this tree; if it
208
193
stream = open(dest, 'wb')
209
194
ball = tarfile.open(None, 'w|', stream)
210
195
return export_tarball_generator(
211
tree, ball, root, subdir, filtered, force_mtime)
214
def tar_xz_exporter_generator(tree, dest, root, subdir, filtered=False,
196
tree, ball, root, subdir, force_mtime)
199
def tar_xz_exporter_generator(tree, dest, root, subdir,
215
200
force_mtime=None, fileobj=None):
216
return tar_lzma_exporter_generator(tree, dest, root, subdir, filtered,
201
return tar_lzma_exporter_generator(tree, dest, root, subdir,
217
202
force_mtime, fileobj, "xz")
220
def tar_lzma_exporter_generator(tree, dest, root, subdir, filtered=False,
205
def tar_lzma_exporter_generator(tree, dest, root, subdir,
221
206
force_mtime=None, fileobj=None,
222
207
compression_format="alone"):
223
208
"""Export this tree to a new .tar.lzma file.
240
225
options={"format": compression_format})
241
226
ball = tarfile.open(None, 'w:', fileobj=stream)
242
227
return export_tarball_generator(
243
tree, ball, root, subdir, filtered=filtered, force_mtime=force_mtime)
228
tree, ball, root, subdir, force_mtime=force_mtime)