~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: Martin Pool
  • Date: 2006-01-23 02:09:39 UTC
  • mfrom: (1534.1.11 integration)
  • Revision ID: mbp@sourcefrog.net-20060123020939-567fb193328ed7a6
[merge] robert's integration

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from bzrlib.inventory import Inventory
27
27
from bzrlib.osutils import appendpath, fingerprint_file
28
28
 
29
 
 
30
 
exporters = {}
31
 
 
32
29
class Tree(object):
33
30
    """Abstract file tree.
34
31
 
107
104
        """Print file with id `file_id` to stdout."""
108
105
        import sys
109
106
        sys.stdout.write(self.get_file_text(file_id))
110
 
        
111
 
        
112
 
    def export(self, dest, format='dir', root=None):
113
 
        """Export this tree."""
114
 
        try:
115
 
            exporter = exporters[format]
116
 
        except KeyError:
117
 
            from bzrlib.errors import BzrCommandError
118
 
            raise BzrCommandError("export format %r not supported" % format)
119
 
        exporter(self, dest, root)
120
 
 
121
 
 
122
 
 
 
107
 
 
108
    def lock_read(self):
 
109
        pass
 
110
 
 
111
    def unlock(self):
 
112
        pass
 
113
        
 
114
        
123
115
class RevisionTree(Tree):
124
116
    """Tree viewing a previous revision.
125
117
 
130
122
           or at least passing a description to the constructor.
131
123
    """
132
124
    
133
 
    def __init__(self, weave_store, inv, revision_id):
134
 
        self._weave_store = weave_store
 
125
    def __init__(self, branch, inv, revision_id):
 
126
        self._branch = branch
 
127
        self._weave_store = branch.weave_store
135
128
        self._inventory = inv
136
129
        self._revision_id = revision_id
137
130
 
138
131
    def get_weave(self, file_id):
139
 
        # FIXME: RevisionTree should be given a branch
140
 
        # not a store, or the store should know the branch.
141
132
        import bzrlib.transactions as transactions
142
133
        return self._weave_store.get_weave(file_id,
143
 
            transactions.PassThroughTransaction())
 
134
                self._branch.get_transaction())
144
135
 
 
136
    def get_weave_prelude(self, file_id):
 
137
        import bzrlib.transactions as transactions
 
138
        return self._weave_store.get_weave_prelude(file_id,
 
139
                self._branch.get_transaction())
145
140
 
146
141
    def get_file_lines(self, file_id):
147
142
        ie = self._inventory[file_id]
148
143
        weave = self.get_weave(file_id)
149
144
        return weave.get(ie.revision)
150
 
        
151
145
 
152
146
    def get_file_text(self, file_id):
153
147
        return ''.join(self.get_file_lines(file_id))
154
148
 
155
 
 
156
149
    def get_file(self, file_id):
157
150
        return StringIO(self.get_file_text(file_id))
158
151
 
185
178
    def kind(self, file_id):
186
179
        return self._inventory[file_id].kind
187
180
 
 
181
    def lock_read(self):
 
182
        self._branch.lock_read()
 
183
 
 
184
    def unlock(self):
 
185
        self._branch.unlock()
 
186
 
188
187
 
189
188
class EmptyTree(Tree):
190
189
    def __init__(self):
277
276
            
278
277
 
279
278
 
280
 
######################################################################
281
 
# export
282
 
 
283
 
def dir_exporter(tree, dest, root):
284
 
    """Export this tree to a new directory.
285
 
 
286
 
    `dest` should not exist, and will be created holding the
287
 
    contents of this tree.
288
 
 
289
 
    TODO: To handle subdirectories we need to create the
290
 
           directories first.
291
 
 
292
 
    :note: If the export fails, the destination directory will be
293
 
           left in a half-assed state.
294
 
    """
295
 
    import os
296
 
    os.mkdir(dest)
297
 
    mutter('export version %r', tree)
298
 
    inv = tree.inventory
299
 
    for dp, ie in inv.iter_entries():
300
 
        if dp != ".bzrignore":
301
 
            ie.put_on_disk(dest, dp, tree)
302
 
 
303
 
exporters['dir'] = dir_exporter
304
 
 
305
 
try:
306
 
    import tarfile
307
 
except ImportError:
308
 
    pass
309
 
else:
310
 
    def get_root_name(dest):
311
 
        """Get just the root name for a tarball.
312
 
 
313
 
        >>> get_root_name('mytar.tar')
314
 
        'mytar'
315
 
        >>> get_root_name('mytar.tar.bz2')
316
 
        'mytar'
317
 
        >>> get_root_name('tar.tar.tar.tgz')
318
 
        'tar.tar.tar'
319
 
        >>> get_root_name('bzr-0.0.5.tar.gz')
320
 
        'bzr-0.0.5'
321
 
        >>> get_root_name('a/long/path/mytar.tgz')
322
 
        'mytar'
323
 
        >>> get_root_name('../parent/../dir/other.tbz2')
324
 
        'other'
325
 
        """
326
 
        endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
327
 
        dest = os.path.basename(dest)
328
 
        for end in endings:
329
 
            if dest.endswith(end):
330
 
                return dest[:-len(end)]
331
 
 
332
 
    def tar_exporter(tree, dest, root, compression=None):
333
 
        """Export this tree to a new tar file.
334
 
 
335
 
        `dest` will be created holding the contents of this tree; if it
336
 
        already exists, it will be clobbered, like with "tar -c".
337
 
        """
338
 
        from time import time
339
 
        now = time()
340
 
        compression = str(compression or '')
341
 
        if root is None:
342
 
            root = get_root_name(dest)
343
 
        try:
344
 
            ball = tarfile.open(dest, 'w:' + compression)
345
 
        except tarfile.CompressionError, e:
346
 
            raise BzrError(str(e))
347
 
        mutter('export version %r', tree)
348
 
        inv = tree.inventory
349
 
        for dp, ie in inv.iter_entries():
350
 
            if dp != ".bzrignore":
351
 
                mutter("  export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
352
 
                item, fileobj = ie.get_tar_item(root, dp, now, tree)
353
 
                ball.addfile(item, fileobj)
354
 
        ball.close()
355
 
 
356
 
    exporters['tar'] = tar_exporter
357
 
 
358
 
    def tgz_exporter(tree, dest, root):
359
 
        tar_exporter(tree, dest, root, compression='gz')
360
 
    exporters['tgz'] = tgz_exporter
361
 
 
362
 
    def tbz_exporter(tree, dest, root):
363
 
        tar_exporter(tree, dest, root, compression='bz2')
364
 
    exporters['tbz2'] = tbz_exporter