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 contextlib import closing
20
from io import BytesIO
29
from ..export import _export_iter_entries
32
def prepare_tarball_item(tree, root, final_path, tree_path, entry, force_mtime=None):
33
"""Prepare a tarball item for exporting
35
:param tree: Tree to export
36
:param final_path: Final path to place item
37
:param tree_path: Path for the entry in the tree
38
:param entry: Entry to export
39
:param force_mtime: Option mtime to force, instead of using tree
42
Returns a (tarinfo, fileobj) tuple
44
file_id = getattr(entry, 'file_id', None)
45
filename = osutils.pathjoin(root, final_path)
46
item = tarfile.TarInfo(filename)
47
if force_mtime is not None:
48
item.mtime = force_mtime
50
item.mtime = tree.get_file_mtime(tree_path)
51
if entry.kind == "file":
52
item.type = tarfile.REGTYPE
53
if tree.is_executable(tree_path):
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(tree_path)
62
item.size = len(content)
63
fileobj = BytesIO(content)
64
elif entry.kind in ("directory", "tree-reference"):
65
item.type = tarfile.DIRTYPE
70
elif entry.kind == "symlink":
71
item.type = tarfile.SYMTYPE
74
item.linkname = tree.get_symlink_target(tree_path)
77
raise errors.BzrError("don't know how to export {%s} of kind %r"
78
% (file_id, entry.kind))
79
return (item, fileobj)
82
def tarball_generator(tree, root, subdir=None, force_mtime=None, format=''):
83
"""Export tree contents to a tarball.
85
:returns: A generator that will produce file content chunks.
87
:param tree: Tree to export
89
:param subdir: Sub directory to export
91
:param force_mtime: Option mtime to force, instead of using tree
95
with closing(tarfile.open(None, "w:%s" % format, buf)) as ball, tree.lock_read():
96
for final_path, tree_path, entry in _export_iter_entries(tree, subdir):
97
(item, fileobj) = prepare_tarball_item(
98
tree, root, final_path, tree_path, entry, force_mtime)
99
ball.addfile(item, fileobj)
100
# Yield the data that was written so far, rinse, repeat.
107
def tgz_generator(tree, dest, root, subdir, force_mtime=None):
108
"""Export this tree to a new tar file.
110
`dest` will be created holding the contents of this tree; if it
111
already exists, it will be clobbered, like with "tar -c".
113
with tree.lock_read():
115
if force_mtime is not None:
116
root_mtime = force_mtime
117
elif (getattr(tree, "repository", None) and
118
getattr(tree, "get_revision_id", None)):
119
# If this is a revision tree, use the revisions' timestamp
120
rev = tree.repository.get_revision(tree.get_revision_id())
121
root_mtime = rev.timestamp
122
elif tree.is_versioned(u''):
123
root_mtime = tree.get_file_mtime('')
129
# gzip file is used with an explicit fileobj so that
130
# the basename can be stored in the gzip file rather than
132
basename = os.path.basename(dest)
134
zipstream = gzip.GzipFile(basename, 'w', fileobj=buf,
136
for chunk in tarball_generator(tree, root, subdir, force_mtime):
137
zipstream.write(chunk)
138
# Yield the data that was written so far, rinse, repeat.
142
# Closing zipstream may trigger writes to stream
147
def tbz_generator(tree, dest, root, subdir, force_mtime=None):
148
"""Export this tree to a new tar file.
150
`dest` will be created holding the contents of this tree; if it
151
already exists, it will be clobbered, like with "tar -c".
153
return tarball_generator(
154
tree, root, subdir, force_mtime, format='bz2')
157
def plain_tar_generator(tree, dest, root, subdir,
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
return tarball_generator(
165
tree, root, subdir, force_mtime, format='')
168
def tar_xz_generator(tree, dest, root, subdir, force_mtime=None):
169
return tar_lzma_generator(tree, dest, root, subdir, force_mtime, "xz")
172
def tar_lzma_generator(tree, dest, root, subdir, force_mtime=None,
173
compression_format="alone"):
174
"""Export this tree to a new .tar.lzma file.
176
`dest` will be created holding the contents of this tree; if it
177
already exists, it will be clobbered, like with "tar -c".
181
except ImportError as e:
182
raise errors.DependencyNotPresent('lzma', e)
184
compressor = lzma.LZMACompressor(
186
'xz': lzma.FORMAT_XZ,
187
'raw': lzma.FORMAT_RAW,
188
'alone': lzma.FORMAT_ALONE,
189
}[compression_format])
191
for chunk in tarball_generator(
192
tree, root, subdir, force_mtime=force_mtime):
193
yield compressor.compress(chunk)
195
yield compressor.flush()