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, 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 inventory import Inventory
29
from trace import mutter, note
30
from errors import bailout
77
84
if ie.text_size != None:
78
85
if ie.text_size != fp['size']:
79
raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
86
bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
80
87
["inventory expects %d bytes" % ie.text_size,
81
88
"file is actually %d bytes" % fp['size'],
82
89
"store is probably damaged/corrupt"])
84
91
if ie.text_sha1 != fp['sha1']:
85
raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
92
bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
86
93
["inventory expects %s" % ie.text_sha1,
87
94
"file is actually %s" % fp['sha1'],
88
95
"store is probably damaged/corrupt"])
94
101
pumpfile(self.get_file(fileid), sys.stdout)
97
def export(self, dest, format='dir', root=None):
104
def export(self, dest, format='dir'):
98
105
"""Export this tree."""
100
107
exporter = exporters[format]
102
from bzrlib.errors import BzrCommandError
103
109
raise BzrCommandError("export format %r not supported" % format)
104
exporter(self, dest, root)
145
151
class EmptyTree(Tree):
146
def __init__(self, root_id):
147
from bzrlib.inventory import Inventory
148
self._inventory = Inventory(root_id)
153
self._inventory = Inventory()
150
155
def has_filename(self, filename):
225
230
######################################################################
228
def dir_exporter(tree, dest, root):
233
def dir_exporter(tree, dest):
229
234
"""Export this tree to a new directory.
231
236
`dest` should not exist, and will be created holding the
249
253
elif kind == 'file':
250
254
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))
256
bailout("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
253
257
mutter(" export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
254
258
exporters['dir'] = dir_exporter
258
262
except ImportError:
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):
265
def tar_exporter(tree, dest, compression=None):
284
266
"""Export this tree to a new tar file.
286
268
`dest` will be created holding the contents of this tree; if it
287
269
already exists, it will be clobbered, like with "tar -c".
289
from time import time
291
272
compression = str(compression or '')
293
root = get_root_name(dest)
295
274
ball = tarfile.open(dest, 'w:' + compression)
296
275
except tarfile.CompressionError, e:
297
raise BzrError(str(e))
298
277
mutter('export version %r' % tree)
299
278
inv = tree.inventory
300
279
for dp, ie in inv.iter_entries():
301
280
mutter(" export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
302
item = tarfile.TarInfo(os.path.join(root, dp))
281
item = tarfile.TarInfo(dp)
303
282
# TODO: would be cool to actually set it to the timestamp of the
304
283
# revision it was last changed
315
294
item.size = _find_file_size(fileobj)
318
raise BzrError("don't know how to export {%s} of kind %r" %
297
bailout("don't know how to export {%s} of kind %r" %
319
298
(ie.file_id, ie.kind))
321
300
ball.addfile(item, fileobj)
323
302
exporters['tar'] = tar_exporter
325
def tgz_exporter(tree, dest, root):
326
tar_exporter(tree, dest, root, compression='gz')
304
def tgz_exporter(tree, dest):
305
tar_exporter(tree, dest, compression='gz')
327
306
exporters['tgz'] = tgz_exporter
329
def tbz_exporter(tree, dest, root):
330
tar_exporter(tree, dest, root, compression='bz2')
308
def tbz_exporter(tree, dest):
309
tar_exporter(tree, dest, compression='bz2')
331
310
exporters['tbz2'] = tbz_exporter