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."""
25
from bzrlib import export, osutils
26
28
from bzrlib.export import _export_iter_entries
27
from bzrlib.filters import (
29
filtered_output_bytes,
31
from bzrlib.trace import mutter
34
def tar_exporter(tree, dest, root, subdir, compression=None, filtered=False,
35
per_file_timestamps=False):
36
"""Export this tree to a new tar file.
38
`dest` will be created holding the contents of this tree; if it
39
already exists, it will be clobbered, like with "tar -c".
41
mutter('export version %r', tree)
43
compression = str(compression or '')
31
def prepare_tarball_item(tree, root, final_path, tree_path, entry, force_mtime=None):
32
"""Prepare a tarball item for exporting
34
:param tree: Tree to export
35
:param final_path: Final path to place item
36
:param tree_path: Path for the entry in the tree
37
:param entry: Entry to export
38
:param force_mtime: Option mtime to force, instead of using tree
41
Returns a (tarinfo, fileobj) tuple
43
filename = osutils.pathjoin(root, final_path).encode('utf8')
44
item = tarfile.TarInfo(filename)
45
if force_mtime is not None:
46
item.mtime = force_mtime
48
item.mtime = tree.get_file_mtime(entry.file_id, tree_path)
49
if entry.kind == "file":
50
item.type = tarfile.REGTYPE
51
if tree.is_executable(entry.file_id, tree_path):
55
# This brings the whole file into memory, but that's almost needed for
56
# the tarfile contract, which wants the size of the file up front. We
57
# want to make sure it doesn't change, and we need to read it in one
58
# go for content filtering.
59
content = tree.get_file_text(entry.file_id, tree_path)
60
item.size = len(content)
61
fileobj = StringIO.StringIO(content)
62
elif entry.kind == "directory":
63
item.type = tarfile.DIRTYPE
68
elif entry.kind == "symlink":
69
item.type = tarfile.SYMTYPE
72
item.linkname = tree.get_symlink_target(entry.file_id, tree_path)
75
raise errors.BzrError("don't know how to export {%s} of kind %r"
76
% (entry.file_id, entry.kind))
77
return (item, fileobj)
80
def export_tarball_generator(tree, ball, root, subdir=None, force_mtime=None):
81
"""Export tree contents to a tarball.
83
:returns: A generator that will repeatedly produce None as each file is
84
emitted. The entire generator must be consumed to complete writing
87
:param tree: Tree to export
89
:param ball: Tarball to export to; it will be closed when writing is
92
:param subdir: Sub directory to export
94
:param force_mtime: Option mtime to force, instead of using tree
98
for final_path, tree_path, entry in _export_iter_entries(tree, subdir):
99
(item, fileobj) = prepare_tarball_item(
100
tree, root, final_path, tree_path, entry, force_mtime)
101
ball.addfile(item, fileobj)
107
def tgz_exporter_generator(tree, dest, root, subdir, force_mtime=None,
109
"""Export this tree to a new tar file.
111
`dest` will be created holding the contents of this tree; if it
112
already exists, it will be clobbered, like with "tar -c".
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.get_root_id() is not None:
123
root_mtime = tree.get_file_mtime(tree.get_root_id())
129
if fileobj is not None:
135
stream = open(dest, 'wb')
136
# gzip file is used with an explicit fileobj so that
137
# the basename can be stored in the gzip file rather than
139
basename = os.path.basename(dest)
141
zipstream = gzip.GzipFile(basename, 'w', fileobj=stream,
144
# Python < 2.7 doesn't support the mtime argument
145
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_exporter_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
# tarfile.open goes on to do 'os.getcwd() + dest' for opening the
170
# tar file. With dest being unicode, this throws UnicodeDecodeError
171
# unless we encode dest before passing it on. This works around
172
# upstream python bug http://bugs.python.org/issue8396 (fixed in
173
# Python 2.6.5 and 2.7b1)
174
ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:bz2')
175
return export_tarball_generator(
176
tree, ball, root, subdir, force_mtime)
179
def plain_tar_exporter_generator(tree, dest, root, subdir, compression=None,
180
force_mtime=None, fileobj=None):
181
"""Export this tree to a new tar file.
183
`dest` will be created holding the contents of this tree; if it
184
already exists, it will be clobbered, like with "tar -c".
186
if fileobj is not None:
191
stream = open(dest, 'wb')
192
ball = tarfile.open(None, 'w|', stream)
193
return export_tarball_generator(
194
tree, ball, root, subdir, force_mtime)
197
def tar_xz_exporter_generator(tree, dest, root, subdir,
198
force_mtime=None, fileobj=None):
199
return tar_lzma_exporter_generator(tree, dest, root, subdir,
200
force_mtime, fileobj, "xz")
203
def tar_lzma_exporter_generator(tree, dest, root, subdir,
204
force_mtime=None, fileobj=None,
205
compression_format="alone"):
206
"""Export this tree to a new .tar.lzma file.
208
`dest` will be created holding the contents of this tree; if it
209
already exists, it will be clobbered, like with "tar -c".
45
# XXX: If no root is given, the output tarball will contain files
46
# named '-/foo'; perhaps this is the most reasonable thing.
47
ball = tarfile.open(None, 'w|' + compression, sys.stdout)
50
root = export.get_root_name(dest)
52
# tarfile.open goes on to do 'os.getcwd() + dest' for opening
53
# the tar file. With dest being unicode, this throws UnicodeDecodeError
54
# unless we encode dest before passing it on. This works around
55
# upstream python bug http://bugs.python.org/issue8396
56
# (fixed in Python 2.6.5 and 2.7b1)
57
ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:' + compression)
59
for dp, ie in _export_iter_entries(tree, subdir):
60
filename = osutils.pathjoin(root, dp).encode('utf8')
61
item = tarfile.TarInfo(filename)
62
if per_file_timestamps:
63
item.mtime = tree.get_file_mtime(ie.file_id, dp)
67
item.type = tarfile.REGTYPE
68
if tree.is_executable(ie.file_id):
73
chunks = tree.get_file_lines(ie.file_id)
74
filters = tree._content_filter_stack(dp)
75
context = ContentFilterContext(dp, tree, ie)
76
contents = filtered_output_bytes(chunks, filters, context)
77
content = ''.join(contents)
78
item.size = len(content)
79
fileobj = StringIO.StringIO(content)
81
item.size = ie.text_size
82
fileobj = tree.get_file(ie.file_id)
83
elif ie.kind == "directory":
84
item.type = tarfile.DIRTYPE
89
elif ie.kind == "symlink":
90
item.type = tarfile.SYMTYPE
93
item.linkname = ie.symlink_target
96
raise BzrError("don't know how to export {%s} of kind %r" %
97
(ie.file_id, ie.kind))
98
ball.addfile(item, fileobj)
102
def tgz_exporter(tree, dest, root, subdir, filtered=False,
103
per_file_timestamps=False):
104
tar_exporter(tree, dest, root, subdir, compression='gz',
105
filtered=filtered, per_file_timestamps=per_file_timestamps)
108
def tbz_exporter(tree, dest, root, subdir, filtered=False,
109
per_file_timestamps=False):
110
tar_exporter(tree, dest, root, subdir, compression='bz2',
111
filtered=filtered, per_file_timestamps=per_file_timestamps)
212
raise errors.BzrError("Writing to stdout not supported for .tar.lzma")
214
if fileobj is not None:
215
raise errors.BzrError(
216
"Writing to fileobject not supported for .tar.lzma")
219
except ImportError, e:
220
raise errors.DependencyNotPresent('lzma', e)
222
stream = lzma.LZMAFile(dest.encode(osutils._fs_enc), 'w',
223
options={"format": compression_format})
224
ball = tarfile.open(None, 'w:', fileobj=stream)
225
return export_tarball_generator(
226
tree, ball, root, subdir, force_mtime=force_mtime)