17
17
"""Tree classes, representing directory at point in time.
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
20
from osutils import pumpfile, appendpath, fingerprint_file
23
from bzrlib.trace import mutter, note
24
from bzrlib.errors import BzrError
35
30
class Tree(object):
36
31
"""Abstract file tree.
82
77
if ie.text_size != None:
83
78
if ie.text_size != fp['size']:
84
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
79
raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
85
80
["inventory expects %d bytes" % ie.text_size,
86
81
"file is actually %d bytes" % fp['size'],
87
82
"store is probably damaged/corrupt"])
89
84
if ie.text_sha1 != fp['sha1']:
90
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
85
raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
91
86
["inventory expects %s" % ie.text_sha1,
92
87
"file is actually %s" % fp['sha1'],
93
88
"store is probably damaged/corrupt"])
99
94
pumpfile(self.get_file(fileid), sys.stdout)
102
def export(self, dest):
103
"""Export this tree to a new directory.
105
`dest` should not exist, and will be created holding the
106
contents of this tree.
108
TODO: To handle subdirectories we need to create the
111
:note: If the export fails, the destination directory will be
112
left in a half-assed state.
115
mutter('export version %r' % self)
117
for dp, ie in inv.iter_entries():
119
fullpath = appendpath(dest, dp)
120
if kind == 'directory':
123
pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb'))
125
bailout("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
126
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
97
def export(self, dest, format='dir', root=None):
98
"""Export this tree."""
100
exporter = exporters[format]
102
from bzrlib.errors import BzrCommandError
103
raise BzrCommandError("export format %r not supported" % format)
104
exporter(self, dest, root)
241
229
if old_name != new_name:
242
230
yield (old_name, new_name)
234
######################################################################
237
def dir_exporter(tree, dest, root):
238
"""Export this tree to a new directory.
240
`dest` should not exist, and will be created holding the
241
contents of this tree.
243
TODO: To handle subdirectories we need to create the
246
:note: If the export fails, the destination directory will be
247
left in a half-assed state.
251
mutter('export version %r' % tree)
253
for dp, ie in inv.iter_entries():
255
fullpath = appendpath(dest, dp)
256
if kind == 'directory':
259
pumpfile(tree.get_file(ie.file_id), file(fullpath, 'wb'))
261
raise BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
262
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
263
exporters['dir'] = dir_exporter
270
def get_root_name(dest):
271
"""Get just the root name for a tarball.
273
>>> get_root_name('mytar.tar')
275
>>> get_root_name('mytar.tar.bz2')
277
>>> get_root_name('tar.tar.tar.tgz')
279
>>> get_root_name('bzr-0.0.5.tar.gz')
281
>>> get_root_name('a/long/path/mytar.tgz')
283
>>> get_root_name('../parent/../dir/other.tbz2')
286
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
287
dest = os.path.basename(dest)
289
if dest.endswith(end):
290
return dest[:-len(end)]
292
def tar_exporter(tree, dest, root, compression=None):
293
"""Export this tree to a new tar file.
295
`dest` will be created holding the contents of this tree; if it
296
already exists, it will be clobbered, like with "tar -c".
298
from time import time
300
compression = str(compression or '')
302
root = get_root_name(dest)
304
ball = tarfile.open(dest, 'w:' + compression)
305
except tarfile.CompressionError, e:
306
raise BzrError(str(e))
307
mutter('export version %r' % tree)
309
for dp, ie in inv.iter_entries():
310
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
311
item = tarfile.TarInfo(os.path.join(root, dp))
312
# TODO: would be cool to actually set it to the timestamp of the
313
# revision it was last changed
315
if ie.kind == 'directory':
316
item.type = tarfile.DIRTYPE
321
elif ie.kind == 'file':
322
item.type = tarfile.REGTYPE
323
fileobj = tree.get_file(ie.file_id)
324
item.size = _find_file_size(fileobj)
327
raise BzrError("don't know how to export {%s} of kind %r" %
328
(ie.file_id, ie.kind))
330
ball.addfile(item, fileobj)
332
exporters['tar'] = tar_exporter
334
def tgz_exporter(tree, dest, root):
335
tar_exporter(tree, dest, root, compression='gz')
336
exporters['tgz'] = tgz_exporter
338
def tbz_exporter(tree, dest, root):
339
tar_exporter(tree, dest, root, compression='bz2')
340
exporters['tbz2'] = tbz_exporter
343
def _find_file_size(fileobj):
344
offset = fileobj.tell()
347
size = fileobj.tell()
349
# gzip doesn't accept second argument to seek()
353
nread = len(fileobj.read())