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 appendpath, fingerprint_file
27
from bzrlib.osutils import pumpfile, 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)
66
61
__contains__ = has_id
68
63
def __iter__(self):
71
66
def id2path(self, file_id):
72
67
return self.inventory.id2path(file_id)
74
def kind(self, file_id):
75
raise NotImplementedError("subclasses must implement kind")
77
69
def _get_inventory(self):
78
70
return self._inventory
136
126
self._revision_id = revision_id
138
128
def get_weave(self, 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):
129
return self._weave_store.get_weave(file_id)
132
def get_file_text(self, file_id):
147
133
ie = self._inventory[file_id]
148
134
weave = self.get_weave(file_id)
149
return weave.get(ie.revision)
152
def get_file_text(self, file_id):
153
return ''.join(self.get_file_lines(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),
156
144
def get_file(self, file_id):
157
145
return StringIO(self.get_file_text(file_id))
164
152
if ie.kind == "file":
165
153
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
173
155
def has_filename(self, filename):
174
156
return bool(self.inventory.path2id(filename))
176
158
def list_files(self):
177
159
# The only files returned by this are those from the version
178
160
for path, entry in self.inventory.iter_entries():
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
161
yield path, 'V', entry.kind, entry.file_id
189
164
class EmptyTree(Tree):
190
165
def __init__(self):
191
166
self._inventory = Inventory()
193
def get_symlink_target(self, file_id):
196
168
def has_filename(self, filename):
199
def kind(self, file_id):
200
assert self._inventory[file_id].kind == "root_directory"
201
return "root_directory"
203
171
def list_files(self):
172
if False: # just to make it a generator
206
175
def __contains__(self, file_id):
207
176
return file_id in self._inventory
297
268
mutter('export version %r' % tree)
298
269
inv = tree.inventory
299
270
for dp, ie in inv.iter_entries():
300
ie.put_on_disk(dest, dp, tree)
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))
302
280
exporters['dir'] = dir_exporter
347
325
inv = tree.inventory
348
326
for dp, ie in inv.iter_entries():
349
327
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
350
item, fileobj = ie.get_tar_item(root, dp, now, tree)
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))
351
347
ball.addfile(item, fileobj)
354
349
exporters['tar'] = tar_exporter
356
351
def tgz_exporter(tree, dest, root):
360
355
def tbz_exporter(tree, dest, root):
361
356
tar_exporter(tree, dest, root, compression='bz2')
362
357
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())