17
17
"""Tree classes, representing directory at point in time.
21
from cStringIO import StringIO
21
import os.path, os, fnmatch, time
23
from osutils import pumpfile, filesize, quotefn, sha_file, \
24
joinpath, splitpath, appendpath, isdir, isfile, file_kind, fingerprint_file
26
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
28
from bzrlib.inventory import Inventory
29
from bzrlib.trace import mutter, note
30
from bzrlib.errors import BzrError
24
from bzrlib.trace import mutter, note
25
from bzrlib.errors import BzrError, BzrCheckError
26
from bzrlib.inventory import Inventory
27
from bzrlib.osutils import appendpath, fingerprint_file
29
37
class Tree(object):
30
38
"""Abstract file tree.
68
71
def id2path(self, file_id):
69
72
return self.inventory.id2path(file_id)
71
def kind(self, file_id):
72
raise NotImplementedError("subclasses must implement kind")
74
74
def _get_inventory(self):
75
75
return self._inventory
77
def get_file_by_path(self, path):
78
return self.get_file(self._inventory.path2id(path))
80
77
inventory = property(_get_inventory,
81
78
doc="Inventory of this Tree")
83
80
def _check_retrieved(self, ie, f):
86
81
fp = fingerprint_file(f)
100
95
"store is probably damaged/corrupt"])
103
def print_file(self, file_id):
104
"""Print file with id `file_id` to stdout."""
98
def print_file(self, fileid):
99
"""Print file with id `fileid` to stdout."""
106
sys.stdout.write(self.get_file_text(file_id))
101
pumpfile(self.get_file(fileid), sys.stdout)
104
def export(self, dest, format='dir'):
105
"""Export this tree."""
107
exporter = exporters[format]
109
raise BzrCommandError("export format %r not supported" % format)
109
114
class RevisionTree(Tree):
110
115
"""Tree viewing a previous revision.
116
121
or at least passing a description to the constructor.
119
def __init__(self, branch, inv, revision_id):
120
self._branch = branch
121
self._weave_store = branch.weave_store
124
def __init__(self, store, inv):
122
126
self._inventory = inv
123
self._revision_id = revision_id
125
def get_weave(self, file_id):
126
import bzrlib.transactions as transactions
127
return self._weave_store.get_weave(file_id,
128
self._branch.get_transaction())
130
def get_weave_prelude(self, file_id):
131
import bzrlib.transactions as transactions
132
return self._weave_store.get_weave_prelude(file_id,
133
self._branch.get_transaction())
135
def get_file_lines(self, file_id):
136
ie = self._inventory[file_id]
137
weave = self.get_weave(file_id)
138
return weave.get(ie.revision)
140
def get_file_text(self, file_id):
141
return ''.join(self.get_file_lines(file_id))
143
128
def get_file(self, file_id):
144
return StringIO(self.get_file_text(file_id))
129
ie = self._inventory[file_id]
130
f = self._store[ie.text_id]
131
mutter(" get fileid{%s} from %r" % (file_id, self))
132
self._check_retrieved(ie, f)
146
135
def get_file_size(self, file_id):
147
136
return self._inventory[file_id].text_size
149
138
def get_file_sha1(self, file_id):
150
139
ie = self._inventory[file_id]
151
if ie.kind == "file":
154
def is_executable(self, file_id):
155
ie = self._inventory[file_id]
156
if ie.kind != "file":
158
return self._inventory[file_id].executable
160
142
def has_filename(self, filename):
161
143
return bool(self.inventory.path2id(filename))
163
145
def list_files(self):
164
146
# The only files returned by this are those from the version
165
147
for path, entry in self.inventory.iter_entries():
166
yield path, 'V', entry.kind, entry.file_id, entry
168
def get_symlink_target(self, file_id):
169
ie = self._inventory[file_id]
170
return ie.symlink_target;
172
def kind(self, file_id):
173
return self._inventory[file_id].kind
148
yield path, 'V', entry.kind, entry.file_id
176
151
class EmptyTree(Tree):
177
152
def __init__(self):
178
153
self._inventory = Inventory()
180
def get_symlink_target(self, file_id):
183
155
def has_filename(self, filename):
186
def kind(self, file_id):
187
assert self._inventory[file_id].kind == "root_directory"
188
return "root_directory"
190
158
def list_files(self):
159
if False: # just to make it a generator
193
def __contains__(self, file_id):
194
return file_id in self._inventory
196
def get_file_sha1(self, file_id):
197
assert self._inventory[file_id].kind == "root_directory"
201
164
######################################################################
230
######################################################################
233
def dir_exporter(tree, dest):
234
"""Export this tree to a new directory.
236
`dest` should not exist, and will be created holding the
237
contents of this tree.
239
TODO: To handle subdirectories we need to create the
242
:note: If the export fails, the destination directory will be
243
left in a half-assed state.
246
mutter('export version %r' % tree)
248
for dp, ie in inv.iter_entries():
250
fullpath = appendpath(dest, dp)
251
if kind == 'directory':
254
pumpfile(tree.get_file(ie.file_id), file(fullpath, 'wb'))
256
raise BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
257
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
258
exporters['dir'] = dir_exporter
265
def tar_exporter(tree, dest, compression=None):
266
"""Export this tree to a new tar file.
268
`dest` will be created holding the contents of this tree; if it
269
already exists, it will be clobbered, like with "tar -c".
272
compression = str(compression or '')
274
ball = tarfile.open(dest, 'w:' + compression)
275
except tarfile.CompressionError, e:
276
raise BzrError(str(e))
277
mutter('export version %r' % tree)
279
for dp, ie in inv.iter_entries():
280
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
281
item = tarfile.TarInfo(dp)
282
# TODO: would be cool to actually set it to the timestamp of the
283
# revision it was last changed
285
if ie.kind == 'directory':
286
item.type = tarfile.DIRTYPE
291
elif ie.kind == 'file':
292
item.type = tarfile.REGTYPE
293
fileobj = tree.get_file(ie.file_id)
294
item.size = _find_file_size(fileobj)
297
raise BzrError("don't know how to export {%s} of kind %r" %
298
(ie.file_id, ie.kind))
300
ball.addfile(item, fileobj)
302
exporters['tar'] = tar_exporter
304
def tgz_exporter(tree, dest):
305
tar_exporter(tree, dest, compression='gz')
306
exporters['tgz'] = tgz_exporter
308
def tbz_exporter(tree, dest):
309
tar_exporter(tree, dest, compression='bz2')
310
exporters['tbz2'] = tbz_exporter
313
def _find_file_size(fileobj):
314
offset = fileobj.tell()
317
size = fileobj.tell()
319
# gzip doesn't accept second argument to seek()
323
nread = len(fileobj.read())