104
99
"""Print file with id `file_id` to stdout."""
106
101
sys.stdout.write(self.get_file_text(file_id))
104
def export(self, dest, format='dir', root=None):
105
"""Export this tree."""
107
exporter = exporters[format]
109
from bzrlib.errors import BzrCommandError
110
raise BzrCommandError("export format %r not supported" % format)
111
exporter(self, dest, root)
115
115
class RevisionTree(Tree):
116
116
"""Tree viewing a previous revision.
122
122
or at least passing a description to the constructor.
125
def __init__(self, branch, inv, revision_id):
126
self._branch = branch
127
self._weave_store = branch.weave_store
125
def __init__(self, weave_store, inv, revision_id):
126
self._weave_store = weave_store
128
127
self._inventory = inv
129
128
self._revision_id = revision_id
131
130
def get_weave(self, file_id):
131
# FIXME: RevisionTree should be given a branch
132
# not a store, or the store should know the branch.
132
133
import bzrlib.transactions as transactions
133
134
return self._weave_store.get_weave(file_id,
134
self._branch.get_transaction())
135
transactions.PassThroughTransaction())
136
def get_weave_prelude(self, file_id):
137
import bzrlib.transactions as transactions
138
return self._weave_store.get_weave_prelude(file_id,
139
self._branch.get_transaction())
141
138
def get_file_lines(self, file_id):
142
139
ie = self._inventory[file_id]
143
140
weave = self.get_weave(file_id)
144
141
return weave.get(ie.revision)
146
144
def get_file_text(self, file_id):
147
145
return ''.join(self.get_file_lines(file_id))
149
148
def get_file(self, file_id):
150
149
return StringIO(self.get_file_text(file_id))
262
######################################################################
265
def dir_exporter(tree, dest, root):
266
"""Export this tree to a new directory.
268
`dest` should not exist, and will be created holding the
269
contents of this tree.
271
TODO: To handle subdirectories we need to create the
274
:note: If the export fails, the destination directory will be
275
left in a half-assed state.
279
mutter('export version %r' % tree)
281
for dp, ie in inv.iter_entries():
282
ie.put_on_disk(dest, dp, tree)
284
exporters['dir'] = dir_exporter
291
def get_root_name(dest):
292
"""Get just the root name for a tarball.
294
>>> get_root_name('mytar.tar')
296
>>> get_root_name('mytar.tar.bz2')
298
>>> get_root_name('tar.tar.tar.tgz')
300
>>> get_root_name('bzr-0.0.5.tar.gz')
302
>>> get_root_name('a/long/path/mytar.tgz')
304
>>> get_root_name('../parent/../dir/other.tbz2')
307
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
308
dest = os.path.basename(dest)
310
if dest.endswith(end):
311
return dest[:-len(end)]
313
def tar_exporter(tree, dest, root, compression=None):
314
"""Export this tree to a new tar file.
316
`dest` will be created holding the contents of this tree; if it
317
already exists, it will be clobbered, like with "tar -c".
319
from time import time
321
compression = str(compression or '')
323
root = get_root_name(dest)
325
ball = tarfile.open(dest, 'w:' + compression)
326
except tarfile.CompressionError, e:
327
raise BzrError(str(e))
328
mutter('export version %r' % tree)
330
for dp, ie in inv.iter_entries():
331
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
332
item, fileobj = ie.get_tar_item(root, dp, now, tree)
333
ball.addfile(item, fileobj)
336
exporters['tar'] = tar_exporter
338
def tgz_exporter(tree, dest, root):
339
tar_exporter(tree, dest, root, compression='gz')
340
exporters['tgz'] = tgz_exporter
342
def tbz_exporter(tree, dest, root):
343
tar_exporter(tree, dest, root, compression='bz2')
344
exporters['tbz2'] = tbz_exporter