17
17
"""Tree classes, representing directory at point in time.
21
import os.path, os, fnmatch
23
from osutils import pumpfile, compare_files, 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
22
from bzrlib.trace import mutter, note
23
from bzrlib.errors import BzrError
36
30
"""Abstract file tree.
38
32
There are several subclasses:
86
76
if ie.text_size != None:
87
77
if ie.text_size != fp['size']:
88
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
78
raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
89
79
["inventory expects %d bytes" % ie.text_size,
90
80
"file is actually %d bytes" % fp['size'],
91
81
"store is probably damaged/corrupt"])
93
83
if ie.text_sha1 != fp['sha1']:
94
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
84
raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
95
85
["inventory expects %s" % ie.text_sha1,
96
86
"file is actually %s" % fp['sha1'],
97
87
"store is probably damaged/corrupt"])
103
93
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))
96
def export(self, dest, format='dir'):
97
"""Export this tree."""
99
exporter = exporters[format]
101
raise BzrCommandError("export format %r not supported" % format)
245
218
if old_name != new_name:
246
219
yield (old_name, new_name)
223
######################################################################
226
def dir_exporter(tree, dest):
227
"""Export this tree to a new directory.
229
`dest` should not exist, and will be created holding the
230
contents of this tree.
232
TODO: To handle subdirectories we need to create the
235
:note: If the export fails, the destination directory will be
236
left in a half-assed state.
240
mutter('export version %r' % tree)
242
for dp, ie in inv.iter_entries():
244
fullpath = appendpath(dest, dp)
245
if kind == 'directory':
248
pumpfile(tree.get_file(ie.file_id), file(fullpath, 'wb'))
250
raise BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
251
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
252
exporters['dir'] = dir_exporter
259
def tar_exporter(tree, dest, compression=None):
260
"""Export this tree to a new tar file.
262
`dest` will be created holding the contents of this tree; if it
263
already exists, it will be clobbered, like with "tar -c".
265
from time import time
267
compression = str(compression or '')
269
ball = tarfile.open(dest, 'w:' + compression)
270
except tarfile.CompressionError, e:
271
raise BzrError(str(e))
272
mutter('export version %r' % tree)
274
for dp, ie in inv.iter_entries():
275
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
276
item = tarfile.TarInfo(dp)
277
# TODO: would be cool to actually set it to the timestamp of the
278
# revision it was last changed
280
if ie.kind == 'directory':
281
item.type = tarfile.DIRTYPE
286
elif ie.kind == 'file':
287
item.type = tarfile.REGTYPE
288
fileobj = tree.get_file(ie.file_id)
289
item.size = _find_file_size(fileobj)
292
raise BzrError("don't know how to export {%s} of kind %r" %
293
(ie.file_id, ie.kind))
295
ball.addfile(item, fileobj)
297
exporters['tar'] = tar_exporter
299
def tgz_exporter(tree, dest):
300
tar_exporter(tree, dest, compression='gz')
301
exporters['tgz'] = tgz_exporter
303
def tbz_exporter(tree, dest):
304
tar_exporter(tree, dest, compression='bz2')
305
exporters['tbz2'] = tbz_exporter
308
def _find_file_size(fileobj):
309
offset = fileobj.tell()
312
size = fileobj.tell()
314
# gzip doesn't accept second argument to seek()
318
nread = len(fileobj.read())