17
17
"""Tree classes, representing directory at point in time.
21
from cStringIO import StringIO
21
import os.path, os, fnmatch
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 inventory import Inventory
29
from trace import mutter, note
30
from errors import bailout
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
33
36
"""Abstract file tree.
35
38
There are several subclasses:
58
61
def has_id(self, file_id):
59
62
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
64
__contains__ = has_id
67
"""Return set of all ids in this tree."""
68
return self.inventory.id_set()
68
70
def __iter__(self):
69
71
return iter(self.inventory)
71
73
def id2path(self, file_id):
72
74
return self.inventory.id2path(file_id)
74
def kind(self, file_id):
75
raise NotImplementedError("subclasses must implement kind")
77
76
def _get_inventory(self):
78
77
return self._inventory
80
def get_file_by_path(self, path):
81
return self.get_file(self._inventory.path2id(path))
83
79
inventory = property(_get_inventory,
84
80
doc="Inventory of this Tree")
86
82
def _check_retrieved(self, ie, f):
89
83
fp = fingerprint_file(f)
92
86
if ie.text_size != None:
93
87
if ie.text_size != fp['size']:
94
raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
88
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
95
89
["inventory expects %d bytes" % ie.text_size,
96
90
"file is actually %d bytes" % fp['size'],
97
91
"store is probably damaged/corrupt"])
99
93
if ie.text_sha1 != fp['sha1']:
100
raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
94
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
101
95
["inventory expects %s" % ie.text_sha1,
102
96
"file is actually %s" % fp['sha1'],
103
97
"store is probably damaged/corrupt"])
106
def print_file(self, file_id):
107
"""Print file with id `file_id` to stdout."""
100
def print_file(self, fileid):
101
"""Print file with id `fileid` to stdout."""
109
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)
103
pumpfile(self.get_file(fileid), sys.stdout)
106
def export(self, dest):
107
"""Export this tree to a new directory.
109
`dest` should not exist, and will be created holding the
110
contents of this tree.
112
TODO: To handle subdirectories we need to create the
115
:note: If the export fails, the destination directory will be
116
left in a half-assed state.
119
mutter('export version %r' % self)
121
for dp, ie in inv.iter_entries():
123
fullpath = appendpath(dest, dp)
124
if kind == 'directory':
127
pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb'))
129
bailout("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
130
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
130
141
or at least passing a description to the constructor.
133
def __init__(self, weave_store, inv, revision_id):
134
self._weave_store = weave_store
144
def __init__(self, store, inv):
135
146
self._inventory = inv
136
self._revision_id = revision_id
138
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):
147
ie = self._inventory[file_id]
148
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))
156
148
def get_file(self, file_id):
157
return StringIO(self.get_file_text(file_id))
149
ie = self._inventory[file_id]
150
f = self._store[ie.text_id]
151
mutter(" get fileid{%s} from %r" % (file_id, self))
152
self._check_retrieved(ie, f)
159
155
def get_file_size(self, file_id):
160
156
return self._inventory[file_id].text_size
162
158
def get_file_sha1(self, file_id):
163
159
ie = self._inventory[file_id]
164
if ie.kind == "file":
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
162
def has_filename(self, filename):
174
163
return bool(self.inventory.path2id(filename))
176
165
def list_files(self):
177
166
# The only files returned by this are those from the version
178
167
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
168
yield path, 'V', entry.kind, entry.file_id
189
171
class EmptyTree(Tree):
190
172
def __init__(self):
191
173
self._inventory = Inventory()
193
def get_symlink_target(self, file_id):
196
175
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
178
def list_files(self):
179
if False: # just to make it a generator
206
def __contains__(self, file_id):
207
return file_id in self._inventory
209
def get_file_sha1(self, file_id):
210
assert self._inventory[file_id].kind == "root_directory"
214
184
######################################################################
275
245
if old_name != new_name:
276
246
yield (old_name, new_name)
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
ie.put_on_disk(dest, dp, tree)
302
exporters['dir'] = dir_exporter
309
def get_root_name(dest):
310
"""Get just the root name for a tarball.
312
>>> get_root_name('mytar.tar')
314
>>> get_root_name('mytar.tar.bz2')
316
>>> get_root_name('tar.tar.tar.tgz')
318
>>> get_root_name('bzr-0.0.5.tar.gz')
320
>>> get_root_name('a/long/path/mytar.tgz')
322
>>> get_root_name('../parent/../dir/other.tbz2')
325
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
326
dest = os.path.basename(dest)
328
if dest.endswith(end):
329
return dest[:-len(end)]
331
def tar_exporter(tree, dest, root, compression=None):
332
"""Export this tree to a new tar file.
334
`dest` will be created holding the contents of this tree; if it
335
already exists, it will be clobbered, like with "tar -c".
337
from time import time
339
compression = str(compression or '')
341
root = get_root_name(dest)
343
ball = tarfile.open(dest, 'w:' + compression)
344
except tarfile.CompressionError, e:
345
raise BzrError(str(e))
346
mutter('export version %r' % tree)
348
for dp, ie in inv.iter_entries():
349
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
350
item, fileobj = ie.get_tar_item(root, dp, now, tree)
351
ball.addfile(item, fileobj)
354
exporters['tar'] = tar_exporter
356
def tgz_exporter(tree, dest, root):
357
tar_exporter(tree, dest, root, compression='gz')
358
exporters['tgz'] = tgz_exporter
360
def tbz_exporter(tree, dest, root):
361
tar_exporter(tree, dest, root, compression='bz2')
362
exporters['tbz2'] = tbz_exporter