17
17
"""Tree classes, representing directory at point in time.
20
from osutils import pumpfile, appendpath, fingerprint_file
23
from bzrlib.trace import mutter, note
24
from bzrlib.errors import BzrError
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
30
35
class Tree(object):
31
36
"""Abstract file tree.
77
82
if ie.text_size != None:
78
83
if ie.text_size != fp['size']:
79
raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
84
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
80
85
["inventory expects %d bytes" % ie.text_size,
81
86
"file is actually %d bytes" % fp['size'],
82
87
"store is probably damaged/corrupt"])
84
89
if ie.text_sha1 != fp['sha1']:
85
raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
90
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
86
91
["inventory expects %s" % ie.text_sha1,
87
92
"file is actually %s" % fp['sha1'],
88
93
"store is probably damaged/corrupt"])
94
99
pumpfile(self.get_file(fileid), sys.stdout)
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)
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))
220
241
if old_name != new_name:
221
242
yield (old_name, new_name)
225
######################################################################
228
def dir_exporter(tree, dest, root):
229
"""Export this tree to a new directory.
231
`dest` should not exist, and will be created holding the
232
contents of this tree.
234
TODO: To handle subdirectories we need to create the
237
:note: If the export fails, the destination directory will be
238
left in a half-assed state.
242
mutter('export version %r' % tree)
244
for dp, ie in inv.iter_entries():
246
fullpath = appendpath(dest, dp)
247
if kind == 'directory':
250
pumpfile(tree.get_file(ie.file_id), file(fullpath, 'wb'))
252
raise BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
253
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
254
exporters['dir'] = dir_exporter
261
def get_root_name(dest):
262
"""Get just the root name for a tarball.
264
>>> get_root_name('mytar.tar')
266
>>> get_root_name('mytar.tar.bz2')
268
>>> get_root_name('tar.tar.tar.tgz')
270
>>> get_root_name('bzr-0.0.5.tar.gz')
272
>>> get_root_name('a/long/path/mytar.tgz')
274
>>> get_root_name('../parent/../dir/other.tbz2')
277
endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
278
dest = os.path.basename(dest)
280
if dest.endswith(end):
281
return dest[:-len(end)]
283
def tar_exporter(tree, dest, root, compression=None):
284
"""Export this tree to a new tar file.
286
`dest` will be created holding the contents of this tree; if it
287
already exists, it will be clobbered, like with "tar -c".
289
from time import time
291
compression = str(compression or '')
293
root = get_root_name(dest)
295
ball = tarfile.open(dest, 'w:' + compression)
296
except tarfile.CompressionError, e:
297
raise BzrError(str(e))
298
mutter('export version %r' % tree)
300
for dp, ie in inv.iter_entries():
301
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
302
item = tarfile.TarInfo(os.path.join(root, dp))
303
# TODO: would be cool to actually set it to the timestamp of the
304
# revision it was last changed
306
if ie.kind == 'directory':
307
item.type = tarfile.DIRTYPE
312
elif ie.kind == 'file':
313
item.type = tarfile.REGTYPE
314
fileobj = tree.get_file(ie.file_id)
315
item.size = _find_file_size(fileobj)
318
raise BzrError("don't know how to export {%s} of kind %r" %
319
(ie.file_id, ie.kind))
321
ball.addfile(item, fileobj)
323
exporters['tar'] = tar_exporter
325
def tgz_exporter(tree, dest, root):
326
tar_exporter(tree, dest, root, compression='gz')
327
exporters['tgz'] = tgz_exporter
329
def tbz_exporter(tree, dest, root):
330
tar_exporter(tree, dest, root, compression='bz2')
331
exporters['tbz2'] = tbz_exporter
334
def _find_file_size(fileobj):
335
offset = fileobj.tell()
338
size = fileobj.tell()
340
# gzip doesn't accept second argument to seek()
344
nread = len(fileobj.read())