109
106
sys.stdout.write(self.get_file_text(file_id))
112
def export(self, dest, format='dir', root=None):
113
"""Export this tree."""
115
exporter = exporters[format]
117
from bzrlib.errors import BzrCommandError
118
raise BzrCommandError("export format %r not supported" % format)
119
exporter(self, dest, root)
123
109
class RevisionTree(Tree):
124
110
"""Tree viewing a previous revision.
280
######################################################################
283
def dir_exporter(tree, dest, root):
284
"""Export this tree to a new directory.
286
`dest` should not exist, and will be created holding the
287
contents of this tree.
289
TODO: To handle subdirectories we need to create the
292
:note: If the export fails, the destination directory will be
293
left in a half-assed state.
297
mutter('export version %r', tree)
299
for dp, ie in inv.iter_entries():
300
if dp != ".bzrignore":
301
ie.put_on_disk(dest, dp, tree)
303
exporters['dir'] = dir_exporter
310
def get_root_name(dest):
311
"""Get just the root name for a tarball.
313
>>> get_root_name('mytar.tar')
315
>>> get_root_name('mytar.tar.bz2')
317
>>> get_root_name('tar.tar.tar.tgz')
319
>>> get_root_name('bzr-0.0.5.tar.gz')
321
>>> get_root_name('a/long/path/mytar.tgz')
323
>>> get_root_name('../parent/../dir/other.tbz2')
326
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
327
dest = os.path.basename(dest)
329
if dest.endswith(end):
330
return dest[:-len(end)]
332
def tar_exporter(tree, dest, root, compression=None):
333
"""Export this tree to a new tar file.
335
`dest` will be created holding the contents of this tree; if it
336
already exists, it will be clobbered, like with "tar -c".
338
from time import time
340
compression = str(compression or '')
342
root = get_root_name(dest)
344
ball = tarfile.open(dest, 'w:' + compression)
345
except tarfile.CompressionError, e:
346
raise BzrError(str(e))
347
mutter('export version %r', tree)
349
for dp, ie in inv.iter_entries():
350
if dp != ".bzrignore":
351
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
352
item, fileobj = ie.get_tar_item(root, dp, now, tree)
353
ball.addfile(item, fileobj)
356
exporters['tar'] = tar_exporter
358
def tgz_exporter(tree, dest, root):
359
tar_exporter(tree, dest, root, compression='gz')
360
exporters['tgz'] = tgz_exporter
362
def tbz_exporter(tree, dest, root):
363
tar_exporter(tree, dest, root, compression='bz2')
364
exporters['tbz2'] = tbz_exporter