1
# Copyright (C) 2005, 2006, 2008-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
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Export a tree to a tarball."""
19
from __future__ import absolute_import
29
from ..archive import _export_iter_entries
30
from ..sixish import (
35
def prepare_tarball_item(tree, root, final_path, tree_path, entry, force_mtime=None):
36
"""Prepare a tarball item for exporting
38
:param tree: Tree to export
39
:param final_path: Final path to place item
40
:param tree_path: Path for the entry in the tree
41
:param entry: Entry to export
42
:param force_mtime: Option mtime to force, instead of using tree
45
Returns a (tarinfo, fileobj) tuple
47
filename = osutils.pathjoin(root, final_path).encode('utf8')
48
item = tarfile.TarInfo(filename)
49
if force_mtime is not None:
50
item.mtime = force_mtime
52
item.mtime = tree.get_file_mtime(tree_path, entry.file_id)
53
if entry.kind == "file":
54
item.type = tarfile.REGTYPE
55
if tree.is_executable(tree_path, entry.file_id):
59
# This brings the whole file into memory, but that's almost needed for
60
# the tarfile contract, which wants the size of the file up front. We
61
# want to make sure it doesn't change, and we need to read it in one
62
# go for content filtering.
63
content = tree.get_file_text(tree_path, entry.file_id)
64
item.size = len(content)
65
fileobj = BytesIO(content)
66
elif entry.kind in ("directory", "tree-reference"):
67
item.type = tarfile.DIRTYPE
72
elif entry.kind == "symlink":
73
item.type = tarfile.SYMTYPE
76
item.linkname = tree.get_symlink_target(tree_path, entry.file_id)
79
raise errors.BzrError("don't know how to export {%s} of kind %r"
80
% (entry.file_id, entry.kind))
81
return (item, fileobj)
84
def export_tarball_generator(tree, ball, root, subdir=None, force_mtime=None):
85
"""Export tree contents to a tarball.
87
:returns: A generator that will repeatedly produce None as each file is
88
emitted. The entire generator must be consumed to complete writing
91
:param tree: Tree to export
93
:param ball: Tarball to export to; it will be closed when writing is
96
:param subdir: Sub directory to export
98
:param force_mtime: Option mtime to force, instead of using tree
102
for final_path, tree_path, entry in _export_iter_entries(tree, subdir):
103
(item, fileobj) = prepare_tarball_item(
104
tree, root, final_path, tree_path, entry, force_mtime)
105
ball.addfile(item, fileobj)
111
def tgz_archive_generator(tree, dest, root, subdir, force_mtime=None,
113
"""Export this tree to a new tar file.
115
`dest` will be created holding the contents of this tree; if it
116
already exists, it will be clobbered, like with "tar -c".
119
if force_mtime is not None:
120
root_mtime = force_mtime
121
elif (getattr(tree, "repository", None) and
122
getattr(tree, "get_revision_id", None)):
123
# If this is a revision tree, use the revisions' timestamp
124
rev = tree.repository.get_revision(tree.get_revision_id())
125
root_mtime = rev.timestamp
126
elif tree.get_root_id() is not None:
127
root_mtime = tree.get_file_mtime('', tree.get_root_id())
133
if fileobj is not None:
139
stream = open(dest, 'wb')
140
# gzip file is used with an explicit fileobj so that
141
# the basename can be stored in the gzip file rather than
143
basename = os.path.basename(dest)
144
zipstream = gzip.GzipFile(basename, 'w', fileobj=stream,
146
ball = tarfile.open(None, 'w|', fileobj=zipstream)
147
for _ in export_tarball_generator(
148
tree, ball, root, subdir, force_mtime):
150
# Closing zipstream may trigger writes to stream
153
# Now we can safely close the stream
157
def tbz_archive_generator(tree, dest, root, subdir,
158
force_mtime=None, fileobj=None):
159
"""Export this tree to a new tar file.
161
`dest` will be created holding the contents of this tree; if it
162
already exists, it will be clobbered, like with "tar -c".
164
if fileobj is not None:
165
ball = tarfile.open(None, 'w|bz2', fileobj)
167
ball = tarfile.open(None, 'w|bz2', sys.stdout)
169
ball = tarfile.open(dest, 'w:bz2')
170
return export_tarball_generator(
171
tree, ball, root, subdir, force_mtime)
174
def plain_tar_archive_generator(tree, dest, root, subdir, compression=None,
175
force_mtime=None, fileobj=None):
176
"""Export this tree to a new tar file.
178
`dest` will be created holding the contents of this tree; if it
179
already exists, it will be clobbered, like with "tar -c".
181
if fileobj is not None:
186
stream = open(dest, 'wb')
187
ball = tarfile.open(None, 'w|', stream)
188
return export_tarball_generator(
189
tree, ball, root, subdir, force_mtime)
192
def tar_xz_archive_generator(tree, dest, root, subdir,
193
force_mtime=None, fileobj=None):
194
return tar_lzma_archive_generator(tree, dest, root, subdir,
195
force_mtime, fileobj, "xz")
198
def tar_lzma_archive_generator(tree, dest, root, subdir,
199
force_mtime=None, fileobj=None,
200
compression_format="alone"):
201
"""Export this tree to a new .tar.lzma file.
203
`dest` will be created holding the contents of this tree; if it
204
already exists, it will be clobbered, like with "tar -c".
207
raise errors.BzrError("Writing to stdout not supported for .tar.lzma")
209
if fileobj is not None:
210
raise errors.BzrError(
211
"Writing to fileobject not supported for .tar.lzma")
214
except ImportError as e:
215
raise errors.DependencyNotPresent('lzma', e)
217
stream = lzma.LZMAFile(dest.encode(osutils._fs_enc), 'w',
218
options={"format": compression_format})
219
ball = tarfile.open(None, 'w:', fileobj=stream)
220
return export_tarball_generator(
221
tree, ball, root, subdir, force_mtime=force_mtime)