/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: Martin Pool
  • Date: 2005-06-15 04:14:26 UTC
  • Revision ID: mbp@sourcefrog.net-20050615041426-6a94f75bc556be2f
- glob expand add arguments on win32
  patch from Roncaglia Julien

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