~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: Martin Pool
  • Date: 2005-08-04 22:04:40 UTC
  • Revision ID: mbp@sourcefrog.net-20050804220440-99562df8151d1ac5
- add pending merge from aaron

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Tree classes, representing directory at point in time.
18
18
"""
19
19
 
20
 
from sets import Set
21
 
import os.path, os, fnmatch, time
22
 
 
23
 
from osutils import pumpfile, filesize, quotefn, sha_file, \
24
 
     joinpath, splitpath, appendpath, isdir, isfile, file_kind, fingerprint_file
25
 
import errno
26
 
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
27
 
 
28
 
from inventory import Inventory
29
 
from trace import mutter, note
30
 
from errors import bailout
31
 
import branch
 
20
import os
32
21
 
33
22
import bzrlib
 
23
from bzrlib.trace import mutter, note
 
24
from bzrlib.errors import BzrError
 
25
from bzrlib.inventory import Inventory
 
26
from bzrlib.osutils import pumpfile, appendpath, fingerprint_file
 
27
 
34
28
 
35
29
exporters = {}
36
30
 
73
67
 
74
68
    def _get_inventory(self):
75
69
        return self._inventory
 
70
    
 
71
    def get_file_by_path(self, path):
 
72
        return self.get_file(self._inventory.path2id(path))
76
73
 
77
74
    inventory = property(_get_inventory,
78
75
                         doc="Inventory of this Tree")
83
80
        
84
81
        if ie.text_size != None:
85
82
            if ie.text_size != fp['size']:
86
 
                bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
 
83
                raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
87
84
                        ["inventory expects %d bytes" % ie.text_size,
88
85
                         "file is actually %d bytes" % fp['size'],
89
86
                         "store is probably damaged/corrupt"])
90
87
 
91
88
        if ie.text_sha1 != fp['sha1']:
92
 
            bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
 
89
            raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
93
90
                    ["inventory expects %s" % ie.text_sha1,
94
91
                     "file is actually %s" % fp['sha1'],
95
92
                     "store is probably damaged/corrupt"])
101
98
        pumpfile(self.get_file(fileid), sys.stdout)
102
99
        
103
100
        
104
 
    def export(self, dest, format='dir'):
 
101
    def export(self, dest, format='dir', root=None):
105
102
        """Export this tree."""
106
103
        try:
107
104
            exporter = exporters[format]
108
105
        except KeyError:
 
106
            from bzrlib.errors import BzrCommandError
109
107
            raise BzrCommandError("export format %r not supported" % format)
110
 
        exporter(self, dest)
 
108
        exporter(self, dest, root)
111
109
 
112
110
 
113
111
 
230
228
######################################################################
231
229
# export
232
230
 
233
 
def dir_exporter(tree, dest):
 
231
def dir_exporter(tree, dest, root):
234
232
    """Export this tree to a new directory.
235
233
 
236
234
    `dest` should not exist, and will be created holding the
242
240
    :note: If the export fails, the destination directory will be
243
241
           left in a half-assed state.
244
242
    """
 
243
    import os
245
244
    os.mkdir(dest)
246
245
    mutter('export version %r' % tree)
247
246
    inv = tree.inventory
253
252
        elif kind == 'file':
254
253
            pumpfile(tree.get_file(ie.file_id), file(fullpath, 'wb'))
255
254
        else:
256
 
            bailout("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
 
255
            raise BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
257
256
        mutter("  export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
258
257
exporters['dir'] = dir_exporter
259
258
 
262
261
except ImportError:
263
262
    pass
264
263
else:
265
 
    def tar_exporter(tree, dest, compression=None):
 
264
    def get_root_name(dest):
 
265
        """Get just the root name for a tarball.
 
266
 
 
267
        >>> get_root_name('mytar.tar')
 
268
        'mytar'
 
269
        >>> get_root_name('mytar.tar.bz2')
 
270
        'mytar'
 
271
        >>> get_root_name('tar.tar.tar.tgz')
 
272
        'tar.tar.tar'
 
273
        >>> get_root_name('bzr-0.0.5.tar.gz')
 
274
        'bzr-0.0.5'
 
275
        >>> get_root_name('a/long/path/mytar.tgz')
 
276
        'mytar'
 
277
        >>> get_root_name('../parent/../dir/other.tbz2')
 
278
        'other'
 
279
        """
 
280
        endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
 
281
        dest = os.path.basename(dest)
 
282
        for end in endings:
 
283
            if dest.endswith(end):
 
284
                return dest[:-len(end)]
 
285
 
 
286
    def tar_exporter(tree, dest, root, compression=None):
266
287
        """Export this tree to a new tar file.
267
288
 
268
289
        `dest` will be created holding the contents of this tree; if it
269
290
        already exists, it will be clobbered, like with "tar -c".
270
291
        """
271
 
        now = time.time()
 
292
        from time import time
 
293
        now = time()
272
294
        compression = str(compression or '')
 
295
        if root is None:
 
296
            root = get_root_name(dest)
273
297
        try:
274
298
            ball = tarfile.open(dest, 'w:' + compression)
275
299
        except tarfile.CompressionError, e:
276
 
            bailout(str(e))
 
300
            raise BzrError(str(e))
277
301
        mutter('export version %r' % tree)
278
302
        inv = tree.inventory
279
303
        for dp, ie in inv.iter_entries():
280
304
            mutter("  export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
281
 
            item = tarfile.TarInfo(dp)
 
305
            item = tarfile.TarInfo(os.path.join(root, dp))
282
306
            # TODO: would be cool to actually set it to the timestamp of the
283
307
            # revision it was last changed
284
308
            item.mtime = now
294
318
                item.size = _find_file_size(fileobj)
295
319
                item.mode = 0644
296
320
            else:
297
 
                bailout("don't know how to export {%s} of kind %r" %
 
321
                raise BzrError("don't know how to export {%s} of kind %r" %
298
322
                        (ie.file_id, ie.kind))
299
323
 
300
324
            ball.addfile(item, fileobj)
301
325
        ball.close()
302
326
    exporters['tar'] = tar_exporter
303
327
 
304
 
    def tgz_exporter(tree, dest):
305
 
        tar_exporter(tree, dest, compression='gz')
 
328
    def tgz_exporter(tree, dest, root):
 
329
        tar_exporter(tree, dest, root, compression='gz')
306
330
    exporters['tgz'] = tgz_exporter
307
331
 
308
 
    def tbz_exporter(tree, dest):
309
 
        tar_exporter(tree, dest, compression='bz2')
 
332
    def tbz_exporter(tree, dest, root):
 
333
        tar_exporter(tree, dest, root, compression='bz2')
310
334
    exporters['tbz2'] = tbz_exporter
311
335
 
312
336