24
24
from bzrlib.trace import mutter, note
25
25
from bzrlib.errors import BzrError, BzrCheckError
26
26
from bzrlib.inventory import Inventory
27
from bzrlib.osutils import pumpfile, appendpath, fingerprint_file
27
from bzrlib.osutils import appendpath, fingerprint_file
58
58
def has_id(self, file_id):
59
59
return self.inventory.has_id(file_id)
61
def has_or_had_id(self, file_id):
62
if file_id == self.inventory.root.file_id:
64
return self.inventory.has_id(file_id)
61
66
__contains__ = has_id
63
68
def __iter__(self):
66
71
def id2path(self, file_id):
67
72
return self.inventory.id2path(file_id)
74
def kind(self, file_id):
75
raise NotImplementedError("subclasses must implement kind")
69
77
def _get_inventory(self):
70
78
return self._inventory
126
136
self._revision_id = revision_id
128
138
def get_weave(self, file_id):
129
return self._weave_store.get_weave(file_id)
139
# FIXME: RevisionTree should be given a branch
140
# not a store, or the store should know the branch.
141
import bzrlib.transactions as transactions
142
return self._weave_store.get_weave(file_id,
143
transactions.PassThroughTransaction())
146
def get_file_lines(self, file_id):
147
ie = self._inventory[file_id]
148
weave = self.get_weave(file_id)
149
return weave.get(ie.revision)
132
152
def get_file_text(self, file_id):
133
ie = self._inventory[file_id]
134
weave = self.get_weave(file_id)
135
idx = weave.lookup(ie.text_version)
136
content = weave.get_text(idx)
137
if len(content) != ie.text_size:
138
raise BzrCheckError('mismatched size on revision %s of file %s: '
140
% (self._revision_id, file_id, len(content),
153
return ''.join(self.get_file_lines(file_id))
144
156
def get_file(self, file_id):
145
157
return StringIO(self.get_file_text(file_id))
152
164
if ie.kind == "file":
153
165
return ie.text_sha1
167
def is_executable(self, file_id):
168
ie = self._inventory[file_id]
169
if ie.kind != "file":
171
return self._inventory[file_id].executable
155
173
def has_filename(self, filename):
156
174
return bool(self.inventory.path2id(filename))
158
176
def list_files(self):
159
177
# The only files returned by this are those from the version
160
178
for path, entry in self.inventory.iter_entries():
161
yield path, 'V', entry.kind, entry.file_id
179
yield path, 'V', entry.kind, entry.file_id, entry
181
def get_symlink_target(self, file_id):
182
ie = self._inventory[file_id]
183
return ie.symlink_target;
185
def kind(self, file_id):
186
return self._inventory[file_id].kind
164
189
class EmptyTree(Tree):
165
190
def __init__(self):
166
191
self._inventory = Inventory()
193
def get_symlink_target(self, file_id):
168
196
def has_filename(self, filename):
199
def kind(self, file_id):
200
assert self._inventory[file_id].kind == "root_directory"
201
return "root_directory"
171
203
def list_files(self):
172
if False: # just to make it a generator
175
206
def __contains__(self, file_id):
176
207
return file_id in self._inventory
268
mutter('export version %r' % tree)
297
mutter('export version %r', tree)
269
298
inv = tree.inventory
270
299
for dp, ie in inv.iter_entries():
272
fullpath = appendpath(dest, dp)
273
if kind == 'directory':
276
pumpfile(tree.get_file(ie.file_id), file(fullpath, 'wb'))
278
raise BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
279
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
300
if dp != ".bzrignore":
301
ie.put_on_disk(dest, dp, tree)
280
303
exporters['dir'] = dir_exporter
321
344
ball = tarfile.open(dest, 'w:' + compression)
322
345
except tarfile.CompressionError, e:
323
346
raise BzrError(str(e))
324
mutter('export version %r' % tree)
347
mutter('export version %r', tree)
325
348
inv = tree.inventory
326
349
for dp, ie in inv.iter_entries():
327
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
328
item = tarfile.TarInfo(os.path.join(root, dp))
329
# TODO: would be cool to actually set it to the timestamp of the
330
# revision it was last changed
332
if ie.kind == 'directory':
333
item.type = tarfile.DIRTYPE
338
elif ie.kind == 'file':
339
item.type = tarfile.REGTYPE
340
fileobj = tree.get_file(ie.file_id)
341
item.size = _find_file_size(fileobj)
344
raise BzrError("don't know how to export {%s} of kind %r" %
345
(ie.file_id, ie.kind))
347
ball.addfile(item, fileobj)
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)
349
356
exporters['tar'] = tar_exporter
351
358
def tgz_exporter(tree, dest, root):
355
362
def tbz_exporter(tree, dest, root):
356
363
tar_exporter(tree, dest, root, compression='bz2')
357
364
exporters['tbz2'] = tbz_exporter
360
def _find_file_size(fileobj):
361
offset = fileobj.tell()
364
size = fileobj.tell()
366
# gzip doesn't accept second argument to seek()
370
nread = len(fileobj.read())