106
106
sys.stdout.write(self.get_file_text(file_id))
109
def export(self, dest, format='dir', root=None):
110
"""Export this tree."""
112
exporter = exporters[format]
114
from bzrlib.errors import BzrCommandError
115
raise BzrCommandError("export format %r not supported" % format)
116
exporter(self, dest, root)
109
120
class RevisionTree(Tree):
110
121
"""Tree viewing a previous revision.
272
######################################################################
275
def dir_exporter(tree, dest, root):
276
"""Export this tree to a new directory.
278
`dest` should not exist, and will be created holding the
279
contents of this tree.
281
TODO: To handle subdirectories we need to create the
284
:note: If the export fails, the destination directory will be
285
left in a half-assed state.
289
mutter('export version %r' % tree)
291
for dp, ie in inv.iter_entries():
292
ie.put_on_disk(dest, dp, tree)
294
exporters['dir'] = dir_exporter
301
def get_root_name(dest):
302
"""Get just the root name for a tarball.
304
>>> get_root_name('mytar.tar')
306
>>> get_root_name('mytar.tar.bz2')
308
>>> get_root_name('tar.tar.tar.tgz')
310
>>> get_root_name('bzr-0.0.5.tar.gz')
312
>>> get_root_name('a/long/path/mytar.tgz')
314
>>> get_root_name('../parent/../dir/other.tbz2')
317
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
318
dest = os.path.basename(dest)
320
if dest.endswith(end):
321
return dest[:-len(end)]
323
def tar_exporter(tree, dest, root, compression=None):
324
"""Export this tree to a new tar file.
326
`dest` will be created holding the contents of this tree; if it
327
already exists, it will be clobbered, like with "tar -c".
329
from time import time
331
compression = str(compression or '')
333
root = get_root_name(dest)
335
ball = tarfile.open(dest, 'w:' + compression)
336
except tarfile.CompressionError, e:
337
raise BzrError(str(e))
338
mutter('export version %r' % tree)
340
for dp, ie in inv.iter_entries():
341
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
342
item, fileobj = ie.get_tar_item(root, dp, now, tree)
343
ball.addfile(item, fileobj)
346
exporters['tar'] = tar_exporter
348
def tgz_exporter(tree, dest, root):
349
tar_exporter(tree, dest, root, compression='gz')
350
exporters['tgz'] = tgz_exporter
352
def tbz_exporter(tree, dest, root):
353
tar_exporter(tree, dest, root, compression='bz2')
354
exporters['tbz2'] = tbz_exporter