~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: Martin Pool
  • Date: 2005-06-27 01:26:11 UTC
  • Revision ID: mbp@sourcefrog.net-20050627012611-4effb7007553fde1
- tweak rsync upload script

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 osutils import pumpfile, appendpath, fingerprint_file
21
 
import os
22
 
 
 
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
23
29
from bzrlib.trace import mutter, note
24
30
from bzrlib.errors import BzrError
 
31
import branch
25
32
 
26
33
import bzrlib
27
34
 
94
101
        pumpfile(self.get_file(fileid), sys.stdout)
95
102
        
96
103
        
97
 
    def export(self, dest, format='dir', root=None):
 
104
    def export(self, dest, format='dir'):
98
105
        """Export this tree."""
99
106
        try:
100
107
            exporter = exporters[format]
101
108
        except KeyError:
102
 
            from bzrlib.errors import BzrCommandError
103
109
            raise BzrCommandError("export format %r not supported" % format)
104
 
        exporter(self, dest, root)
 
110
        exporter(self, dest)
105
111
 
106
112
 
107
113
 
131
137
 
132
138
    def get_file_sha1(self, file_id):
133
139
        ie = self._inventory[file_id]
134
 
        if ie.kind == "file":
135
 
            return ie.text_sha1
 
140
        return ie.text_sha1
136
141
 
137
142
    def has_filename(self, filename):
138
143
        return bool(self.inventory.path2id(filename))
144
149
 
145
150
 
146
151
class EmptyTree(Tree):
147
 
    def __init__(self, root_id):
148
 
        from bzrlib.inventory import Inventory
149
 
        self._inventory = Inventory(root_id)
 
152
    def __init__(self):
 
153
        self._inventory = Inventory()
150
154
 
151
155
    def has_filename(self, filename):
152
156
        return False
155
159
        if False:  # just to make it a generator
156
160
            yield None
157
161
    
158
 
    def __contains__(self, file_id):
159
 
        return file_id in self._inventory
160
 
 
161
 
    def get_file_sha1(self, file_id):
162
 
        assert self._inventory[file_id].kind == "root_directory"
163
 
        return None
164
 
 
165
 
 
166
162
 
167
163
 
168
164
######################################################################
234
230
######################################################################
235
231
# export
236
232
 
237
 
def dir_exporter(tree, dest, root):
 
233
def dir_exporter(tree, dest):
238
234
    """Export this tree to a new directory.
239
235
 
240
236
    `dest` should not exist, and will be created holding the
246
242
    :note: If the export fails, the destination directory will be
247
243
           left in a half-assed state.
248
244
    """
249
 
    import os
250
245
    os.mkdir(dest)
251
246
    mutter('export version %r' % tree)
252
247
    inv = tree.inventory
267
262
except ImportError:
268
263
    pass
269
264
else:
270
 
    def get_root_name(dest):
271
 
        """Get just the root name for a tarball.
272
 
 
273
 
        >>> get_root_name('mytar.tar')
274
 
        'mytar'
275
 
        >>> get_root_name('mytar.tar.bz2')
276
 
        'mytar'
277
 
        >>> get_root_name('tar.tar.tar.tgz')
278
 
        'tar.tar.tar'
279
 
        >>> get_root_name('bzr-0.0.5.tar.gz')
280
 
        'bzr-0.0.5'
281
 
        >>> get_root_name('a/long/path/mytar.tgz')
282
 
        'mytar'
283
 
        >>> get_root_name('../parent/../dir/other.tbz2')
284
 
        'other'
285
 
        """
286
 
        endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
287
 
        dest = os.path.basename(dest)
288
 
        for end in endings:
289
 
            if dest.endswith(end):
290
 
                return dest[:-len(end)]
291
 
 
292
 
    def tar_exporter(tree, dest, root, compression=None):
 
265
    def tar_exporter(tree, dest, compression=None):
293
266
        """Export this tree to a new tar file.
294
267
 
295
268
        `dest` will be created holding the contents of this tree; if it
296
269
        already exists, it will be clobbered, like with "tar -c".
297
270
        """
298
 
        from time import time
299
 
        now = time()
 
271
        now = time.time()
300
272
        compression = str(compression or '')
301
 
        if root is None:
302
 
            root = get_root_name(dest)
303
273
        try:
304
274
            ball = tarfile.open(dest, 'w:' + compression)
305
275
        except tarfile.CompressionError, e:
308
278
        inv = tree.inventory
309
279
        for dp, ie in inv.iter_entries():
310
280
            mutter("  export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
311
 
            item = tarfile.TarInfo(os.path.join(root, dp))
 
281
            item = tarfile.TarInfo(dp)
312
282
            # TODO: would be cool to actually set it to the timestamp of the
313
283
            # revision it was last changed
314
284
            item.mtime = now
331
301
        ball.close()
332
302
    exporters['tar'] = tar_exporter
333
303
 
334
 
    def tgz_exporter(tree, dest, root):
335
 
        tar_exporter(tree, dest, root, compression='gz')
 
304
    def tgz_exporter(tree, dest):
 
305
        tar_exporter(tree, dest, compression='gz')
336
306
    exporters['tgz'] = tgz_exporter
337
307
 
338
 
    def tbz_exporter(tree, dest, root):
339
 
        tar_exporter(tree, dest, root, compression='bz2')
 
308
    def tbz_exporter(tree, dest):
 
309
        tar_exporter(tree, dest, compression='bz2')
340
310
    exporters['tbz2'] = tbz_exporter
341
311
 
342
312