43
42
Returns a (tarinfo, fileobj) tuple
45
filename = osutils.pathjoin(root, final_path).encode('utf8')
44
file_id = getattr(entry, 'file_id', None)
45
filename = osutils.pathjoin(root, final_path)
46
46
item = tarfile.TarInfo(filename)
47
47
if force_mtime is not None:
48
48
item.mtime = force_mtime
50
item.mtime = tree.get_file_mtime(entry.file_id, tree_path)
50
item.mtime = tree.get_file_mtime(tree_path)
51
51
if entry.kind == "file":
52
52
item.type = tarfile.REGTYPE
53
if tree.is_executable(entry.file_id, tree_path):
53
if tree.is_executable(tree_path):
57
57
# This brings the whole file into memory, but that's almost needed for
58
58
# the tarfile contract, which wants the size of the file up front. We
59
59
# want to make sure it doesn't change, and we need to read it in one
60
60
# go for content filtering.
61
content = tree.get_file_text(entry.file_id, tree_path)
61
content = tree.get_file_text(tree_path)
62
62
item.size = len(content)
63
fileobj = StringIO.StringIO(content)
64
elif entry.kind == "directory":
63
fileobj = BytesIO(content)
64
elif entry.kind in ("directory", "tree-reference"):
65
65
item.type = tarfile.DIRTYPE
70
70
elif entry.kind == "symlink":
71
71
item.type = tarfile.SYMTYPE
74
item.linkname = tree.get_symlink_target(entry.file_id, tree_path)
74
item.linkname = tree.get_symlink_target(tree_path)
77
77
raise errors.BzrError("don't know how to export {%s} of kind %r"
78
% (entry.file_id, entry.kind))
78
% (file_id, entry.kind))
79
79
return (item, fileobj)
82
def export_tarball_generator(tree, ball, root, subdir=None, force_mtime=None):
82
def tarball_generator(tree, root, subdir=None, force_mtime=None, format=''):
83
83
"""Export tree contents to a tarball.
85
:returns: A generator that will repeatedly produce None as each file is
86
emitted. The entire generator must be consumed to complete writing
85
:returns: A generator that will produce file content chunks.
89
87
:param tree: Tree to export
91
:param ball: Tarball to export to; it will be closed when writing is
94
89
:param subdir: Sub directory to export
96
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():
100
96
for final_path, tree_path, entry in _export_iter_entries(tree, subdir):
101
97
(item, fileobj) = prepare_tarball_item(
102
98
tree, root, final_path, tree_path, entry, force_mtime)
103
99
ball.addfile(item, fileobj)
109
def tgz_exporter_generator(tree, dest, root, subdir, force_mtime=None,
100
# Yield the data that was written so far, rinse, repeat.
107
def tgz_generator(tree, dest, root, subdir, force_mtime=None):
111
108
"""Export this tree to a new tar file.
113
110
`dest` will be created holding the contents of this tree; if it
114
111
already exists, it will be clobbered, like with "tar -c".
117
if force_mtime is not None:
118
root_mtime = force_mtime
119
elif (getattr(tree, "repository", None) and
120
getattr(tree, "get_revision_id", None)):
121
# If this is a revision tree, use the revisions' timestamp
122
rev = tree.repository.get_revision(tree.get_revision_id())
123
root_mtime = rev.timestamp
124
elif tree.get_root_id() is not None:
125
root_mtime = tree.get_file_mtime(tree.get_root_id())
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('')
131
if fileobj is not None:
137
stream = open(dest, 'wb')
138
129
# gzip file is used with an explicit fileobj so that
139
130
# the basename can be stored in the gzip file rather than
140
131
# dest. (bug 102234)
141
132
basename = os.path.basename(dest)
143
zipstream = gzip.GzipFile(basename, 'w', fileobj=stream,
134
zipstream = gzip.GzipFile(basename, 'w', fileobj=buf,
144
135
mtime=root_mtime)
146
# Python < 2.7 doesn't support the mtime argument
147
zipstream = gzip.GzipFile(basename, 'w', fileobj=stream)
148
ball = tarfile.open(None, 'w|', fileobj=zipstream)
149
for _ in export_tarball_generator(
150
tree, ball, root, subdir, force_mtime):
152
# Closing zipstream may trigger writes to stream
155
# Now we can safely close the stream
159
def tbz_exporter_generator(tree, dest, root, subdir,
160
force_mtime=None, fileobj=None):
161
"""Export this tree to a new tar file.
163
`dest` will be created holding the contents of this tree; if it
164
already exists, it will be clobbered, like with "tar -c".
166
if fileobj is not None:
167
ball = tarfile.open(None, 'w|bz2', fileobj)
169
ball = tarfile.open(None, 'w|bz2', sys.stdout)
171
# tarfile.open goes on to do 'os.getcwd() + dest' for opening the
172
# tar file. With dest being unicode, this throws UnicodeDecodeError
173
# unless we encode dest before passing it on. This works around
174
# upstream python bug http://bugs.python.org/issue8396 (fixed in
175
# Python 2.6.5 and 2.7b1)
176
ball = tarfile.open(dest.encode(osutils._fs_enc), 'w:bz2')
177
return export_tarball_generator(
178
tree, ball, root, subdir, force_mtime)
181
def plain_tar_exporter_generator(tree, dest, root, subdir, compression=None,
182
force_mtime=None, fileobj=None):
183
"""Export this tree to a new tar file.
185
`dest` will be created holding the contents of this tree; if it
186
already exists, it will be clobbered, like with "tar -c".
188
if fileobj is not None:
193
stream = open(dest, 'wb')
194
ball = tarfile.open(None, 'w|', stream)
195
return export_tarball_generator(
196
tree, ball, root, subdir, force_mtime)
199
def tar_xz_exporter_generator(tree, dest, root, subdir,
200
force_mtime=None, fileobj=None):
201
return tar_lzma_exporter_generator(tree, dest, root, subdir,
202
force_mtime, fileobj, "xz")
205
def tar_lzma_exporter_generator(tree, dest, root, subdir,
206
force_mtime=None, fileobj=None,
207
compression_format="alone"):
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"):
208
174
"""Export this tree to a new .tar.lzma file.
210
176
`dest` will be created holding the contents of this tree; if it
211
177
already exists, it will be clobbered, like with "tar -c".
214
raise errors.BzrError("Writing to stdout not supported for .tar.lzma")
216
if fileobj is not None:
217
raise errors.BzrError(
218
"Writing to fileobject not supported for .tar.lzma")
221
except ImportError, e:
181
except ImportError as e:
222
182
raise errors.DependencyNotPresent('lzma', e)
224
stream = lzma.LZMAFile(dest.encode(osutils._fs_enc), 'w',
225
options={"format": compression_format})
226
ball = tarfile.open(None, 'w:', fileobj=stream)
227
return export_tarball_generator(
228
tree, ball, root, subdir, force_mtime=force_mtime)
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()