~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: aaron.bentley at utoronto
  • Date: 2005-09-04 02:59:56 UTC
  • mfrom: (1172)
  • mto: (1185.3.4)
  • mto: This revision was merged to the branch mainline in revision 1178.
  • Revision ID: aaron.bentley@utoronto.ca-20050904025956-776ba4f07de97700
Merged mpool's latest changes (~0.0.7)

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 bzrlib.inventory import Inventory
 
20
import os
 
21
 
 
22
import bzrlib
29
23
from bzrlib.trace import mutter, note
30
24
from bzrlib.errors import BzrError
31
 
import branch
 
25
from bzrlib.inventory import Inventory
 
26
from bzrlib.osutils import pumpfile, appendpath, fingerprint_file
32
27
 
33
 
import bzrlib
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")
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
 
137
135
 
138
136
    def get_file_sha1(self, file_id):
139
137
        ie = self._inventory[file_id]
140
 
        return ie.text_sha1
 
138
        if ie.kind == "file":
 
139
            return ie.text_sha1
141
140
 
142
141
    def has_filename(self, filename):
143
142
        return bool(self.inventory.path2id(filename))
159
158
        if False:  # just to make it a generator
160
159
            yield None
161
160
    
 
161
    def __contains__(self, file_id):
 
162
        return file_id in self._inventory
 
163
 
 
164
    def get_file_sha1(self, file_id):
 
165
        assert self._inventory[file_id].kind == "root_directory"
 
166
        return None
 
167
 
 
168
 
162
169
 
163
170
 
164
171
######################################################################
230
237
######################################################################
231
238
# export
232
239
 
233
 
def dir_exporter(tree, dest):
 
240
def dir_exporter(tree, dest, root):
234
241
    """Export this tree to a new directory.
235
242
 
236
243
    `dest` should not exist, and will be created holding the
242
249
    :note: If the export fails, the destination directory will be
243
250
           left in a half-assed state.
244
251
    """
 
252
    import os
245
253
    os.mkdir(dest)
246
254
    mutter('export version %r' % tree)
247
255
    inv = tree.inventory
262
270
except ImportError:
263
271
    pass
264
272
else:
265
 
    def tar_exporter(tree, dest, compression=None):
 
273
    def get_root_name(dest):
 
274
        """Get just the root name for a tarball.
 
275
 
 
276
        >>> get_root_name('mytar.tar')
 
277
        'mytar'
 
278
        >>> get_root_name('mytar.tar.bz2')
 
279
        'mytar'
 
280
        >>> get_root_name('tar.tar.tar.tgz')
 
281
        'tar.tar.tar'
 
282
        >>> get_root_name('bzr-0.0.5.tar.gz')
 
283
        'bzr-0.0.5'
 
284
        >>> get_root_name('a/long/path/mytar.tgz')
 
285
        'mytar'
 
286
        >>> get_root_name('../parent/../dir/other.tbz2')
 
287
        'other'
 
288
        """
 
289
        endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
 
290
        dest = os.path.basename(dest)
 
291
        for end in endings:
 
292
            if dest.endswith(end):
 
293
                return dest[:-len(end)]
 
294
 
 
295
    def tar_exporter(tree, dest, root, compression=None):
266
296
        """Export this tree to a new tar file.
267
297
 
268
298
        `dest` will be created holding the contents of this tree; if it
269
299
        already exists, it will be clobbered, like with "tar -c".
270
300
        """
271
 
        now = time.time()
 
301
        from time import time
 
302
        now = time()
272
303
        compression = str(compression or '')
 
304
        if root is None:
 
305
            root = get_root_name(dest)
273
306
        try:
274
307
            ball = tarfile.open(dest, 'w:' + compression)
275
308
        except tarfile.CompressionError, e:
278
311
        inv = tree.inventory
279
312
        for dp, ie in inv.iter_entries():
280
313
            mutter("  export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
281
 
            item = tarfile.TarInfo(dp)
 
314
            item = tarfile.TarInfo(os.path.join(root, dp))
282
315
            # TODO: would be cool to actually set it to the timestamp of the
283
316
            # revision it was last changed
284
317
            item.mtime = now
301
334
        ball.close()
302
335
    exporters['tar'] = tar_exporter
303
336
 
304
 
    def tgz_exporter(tree, dest):
305
 
        tar_exporter(tree, dest, compression='gz')
 
337
    def tgz_exporter(tree, dest, root):
 
338
        tar_exporter(tree, dest, root, compression='gz')
306
339
    exporters['tgz'] = tgz_exporter
307
340
 
308
 
    def tbz_exporter(tree, dest):
309
 
        tar_exporter(tree, dest, compression='bz2')
 
341
    def tbz_exporter(tree, dest, root):
 
342
        tar_exporter(tree, dest, root, compression='bz2')
310
343
    exporters['tbz2'] = tbz_exporter
311
344
 
312
345