~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: Martin Pool
  • Date: 2005-09-12 08:29:42 UTC
  • Revision ID: mbp@sourcefrog.net-20050912082942-c0bb5909f6425b4a
- remove duplicate definition of get_merge_type

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
22
 
 
23
 
from inventory import Inventory
24
 
from trace import mutter, note
25
 
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
26
 
     joinpath, splitpath, appendpath, isdir, isfile, file_kind, fingerprint_file
27
 
from errors import bailout
28
 
import branch
29
 
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
 
20
import os
 
21
from cStringIO import StringIO
30
22
 
31
23
import bzrlib
32
 
 
33
 
class Tree:
 
24
from bzrlib.trace import mutter, note
 
25
from bzrlib.errors import BzrError, BzrCheckError
 
26
from bzrlib.inventory import Inventory
 
27
from bzrlib.osutils import pumpfile, appendpath, fingerprint_file
 
28
 
 
29
 
 
30
exporters = {}
 
31
 
 
32
class Tree(object):
34
33
    """Abstract file tree.
35
34
 
36
35
    There are several subclasses:
59
58
    def has_id(self, file_id):
60
59
        return self.inventory.has_id(file_id)
61
60
 
62
 
    def id_set(self):
63
 
        """Return set of all ids in this tree."""
64
 
        return self.inventory.id_set()
 
61
    __contains__ = has_id
 
62
 
 
63
    def __iter__(self):
 
64
        return iter(self.inventory)
65
65
 
66
66
    def id2path(self, file_id):
67
67
        return self.inventory.id2path(file_id)
68
68
 
69
69
    def _get_inventory(self):
70
70
        return self._inventory
 
71
    
 
72
    def get_file_by_path(self, path):
 
73
        return self.get_file(self._inventory.path2id(path))
71
74
 
72
75
    inventory = property(_get_inventory,
73
76
                         doc="Inventory of this Tree")
76
79
        fp = fingerprint_file(f)
77
80
        f.seek(0)
78
81
        
79
 
        if ie.text_size is not None:
80
 
            if fs != fp['size']:
81
 
                bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
 
82
        if ie.text_size != None:
 
83
            if ie.text_size != fp['size']:
 
84
                raise BzrError("mismatched size for file %r in %r" % (ie.file_id, self._store),
82
85
                        ["inventory expects %d bytes" % ie.text_size,
83
86
                         "file is actually %d bytes" % fp['size'],
84
87
                         "store is probably damaged/corrupt"])
85
88
 
86
89
        if ie.text_sha1 != fp['sha1']:
87
 
            bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
 
90
            raise BzrError("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
88
91
                    ["inventory expects %s" % ie.text_sha1,
89
92
                     "file is actually %s" % fp['sha1'],
90
93
                     "store is probably damaged/corrupt"])
91
94
 
92
95
 
93
 
    def export(self, dest):
94
 
        """Export this tree to a new directory.
95
 
 
96
 
        `dest` should not exist, and will be created holding the
97
 
        contents of this tree.
98
 
 
99
 
        :todo: To handle subdirectories we need to create the
100
 
               directories first.
101
 
 
102
 
        :note: If the export fails, the destination directory will be
103
 
               left in a half-assed state.
104
 
        """
105
 
        os.mkdir(dest)
106
 
        mutter('export version %r' % self)
107
 
        inv = self.inventory
108
 
        for dp, ie in inv.iter_entries():
109
 
            kind = ie.kind
110
 
            fullpath = appendpath(dest, dp)
111
 
            if kind == 'directory':
112
 
                os.mkdir(fullpath)
113
 
            elif kind == 'file':
114
 
                pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb'))
115
 
            else:
116
 
                bailout("don't know how to export {%s} of kind %r", fid, kind)
117
 
            mutter("  export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
118
 
 
119
 
 
120
 
 
121
 
class WorkingTree(Tree):
122
 
    """Working copy tree.
123
 
 
124
 
    The inventory is held in the `Branch` working-inventory, and the
125
 
    files are in a directory on disk.
126
 
 
127
 
    It is possible for a `WorkingTree` to have a filename which is
128
 
    not listed in the Inventory and vice versa.
129
 
    """
130
 
    def __init__(self, basedir, inv):
131
 
        self._inventory = inv
132
 
        self.basedir = basedir
133
 
        self.path2id = inv.path2id
134
 
 
135
 
    def __repr__(self):
136
 
        return "<%s of %s>" % (self.__class__.__name__,
137
 
                               self.basedir)
138
 
 
139
 
    def abspath(self, filename):
140
 
        return os.path.join(self.basedir, filename)
141
 
 
142
 
    def has_filename(self, filename):
143
 
        return os.path.exists(self.abspath(filename))
144
 
 
145
 
    def get_file(self, file_id):
146
 
        return self.get_file_byname(self.id2path(file_id))
147
 
 
148
 
    def get_file_byname(self, filename):
149
 
        return file(self.abspath(filename), 'rb')
150
 
 
151
 
    def _get_store_filename(self, file_id):
152
 
        return self.abspath(self.id2path(file_id))
153
 
 
154
 
    def has_id(self, file_id):
155
 
        # files that have been deleted are excluded
156
 
        if not self.inventory.has_id(file_id):
157
 
            return False
158
 
        return os.access(self.abspath(self.inventory.id2path(file_id)), os.F_OK)
159
 
 
160
 
    def get_file_size(self, file_id):
161
 
        return os.stat(self._get_store_filename(file_id))[ST_SIZE]
162
 
 
163
 
    def get_file_sha1(self, file_id):
164
 
        f = self.get_file(file_id)
165
 
        return sha_file(f)
166
 
 
167
 
 
168
 
    def file_class(self, filename):
169
 
        if self.path2id(filename):
170
 
            return 'V'
171
 
        elif self.is_ignored(filename):
172
 
            return 'I'
173
 
        else:
174
 
            return '?'
175
 
 
176
 
 
177
 
    def file_kind(self, filename):
178
 
        if isfile(self.abspath(filename)):
179
 
            return 'file'
180
 
        elif isdir(self.abspath(filename)):
181
 
            return 'directory'
182
 
        else:
183
 
            return 'unknown'
184
 
 
185
 
 
186
 
    def list_files(self):
187
 
        """Recursively list all files as (path, class, kind, id).
188
 
 
189
 
        Lists, but does not descend into unversioned directories.
190
 
 
191
 
        This does not include files that have been deleted in this
192
 
        tree.
193
 
 
194
 
        Skips the control directory.
195
 
        """
196
 
        inv = self.inventory
197
 
 
198
 
        def descend(from_dir, from_dir_id, dp):
199
 
            ls = os.listdir(dp)
200
 
            ls.sort()
201
 
            for f in ls:
202
 
                if bzrlib.BZRDIR == f:
203
 
                    continue
204
 
 
205
 
                # path within tree
206
 
                fp = appendpath(from_dir, f)
207
 
 
208
 
                # absolute path
209
 
                fap = appendpath(dp, f)
210
 
                
211
 
                f_ie = inv.get_child(from_dir_id, f)
212
 
                if f_ie:
213
 
                    c = 'V'
214
 
                elif self.is_ignored(fp):
215
 
                    c = 'I'
216
 
                else:
217
 
                    c = '?'
218
 
 
219
 
                fk = file_kind(fap)
220
 
 
221
 
                if f_ie:
222
 
                    if f_ie.kind != fk:
223
 
                        bailout("file %r entered as kind %r id %r, now of kind %r"
224
 
                                % (fap, f_ie.kind, f_ie.file_id, fk))
225
 
 
226
 
                yield fp, c, fk, (f_ie and f_ie.file_id)
227
 
 
228
 
                if fk != 'directory':
229
 
                    continue
230
 
 
231
 
                if c != 'V':
232
 
                    # don't descend unversioned directories
233
 
                    continue
234
 
                
235
 
                for ff in descend(fp, f_ie.file_id, fap):
236
 
                    yield ff
237
 
 
238
 
        for f in descend('', None, self.basedir):
239
 
            yield f
240
 
            
241
 
 
242
 
 
243
 
    def unknowns(self, path='', dir_id=None):
244
 
        """Yield names of unknown files in this WorkingTree.
245
 
 
246
 
        If there are any unknown directories then only the directory is
247
 
        returned, not all its children.  But if there are unknown files
248
 
        under a versioned subdirectory, they are returned.
249
 
 
250
 
        Currently returned depth-first, sorted by name within directories.
251
 
        """
252
 
        for fpath, fclass, fkind, fid in self.list_files():
253
 
            if fclass == '?':
254
 
                yield fpath
255
 
                
256
 
 
257
 
    def ignored_files(self):
258
 
        for fpath, fclass, fkind, fid in self.list_files():
259
 
            if fclass == 'I':
260
 
                yield fpath
261
 
 
262
 
 
263
 
    def get_ignore_list(self):
264
 
        """Return list of ignore patterns.
265
 
 
266
 
        Cached in the Tree object after the first call.
267
 
        """
268
 
        if hasattr(self, '_ignorelist'):
269
 
            return self._ignorelist
270
 
 
271
 
        l = bzrlib.DEFAULT_IGNORE[:]
272
 
        if self.has_filename(bzrlib.IGNORE_FILENAME):
273
 
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
274
 
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
275
 
        self._ignorelist = l
276
 
        return l
277
 
 
278
 
 
279
 
    def is_ignored(self, filename):
280
 
        """Check whether the filename matches an ignore pattern.
281
 
 
282
 
        Patterns containing '/' need to match the whole path; others
283
 
        match against only the last component."""
284
 
        ## TODO: Use extended zsh-style globs maybe?
285
 
        ## TODO: Use '**' to match directories?
286
 
        for pat in self.get_ignore_list():
287
 
            if '/' in pat:
288
 
                if fnmatch.fnmatchcase(filename, pat):
289
 
                    return True
290
 
            else:
291
 
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
292
 
                    return True
293
 
        return False
294
 
        
295
 
 
296
 
        
297
 
        
 
96
    def print_file(self, file_id):
 
97
        """Print file with id `file_id` to stdout."""
 
98
        import sys
 
99
        sys.stdout.write(self.get_file_text(file_id))
 
100
        
 
101
        
 
102
    def export(self, dest, format='dir', root=None):
 
103
        """Export this tree."""
 
104
        try:
 
105
            exporter = exporters[format]
 
106
        except KeyError:
 
107
            from bzrlib.errors import BzrCommandError
 
108
            raise BzrCommandError("export format %r not supported" % format)
 
109
        exporter(self, dest, root)
 
110
 
 
111
 
298
112
 
299
113
class RevisionTree(Tree):
300
114
    """Tree viewing a previous revision.
301
115
 
302
116
    File text can be retrieved from the text store.
303
117
 
304
 
    :todo: Some kind of `__repr__` method, but a good one
 
118
    TODO: Some kind of `__repr__` method, but a good one
305
119
           probably means knowing the branch and revision number,
306
120
           or at least passing a description to the constructor.
307
121
    """
308
122
    
309
 
    def __init__(self, store, inv):
310
 
        self._store = store
 
123
    def __init__(self, weave_store, inv, revision_id):
 
124
        self._weave_store = weave_store
311
125
        self._inventory = inv
 
126
        self._revision_id = revision_id
 
127
 
 
128
    def get_file_text(self, file_id):
 
129
        ie = self._inventory[file_id]
 
130
        weave = self._weave_store.get_weave(file_id)
 
131
        idx = weave.lookup(self._revision_id)
 
132
        content = weave.get_text(idx)
 
133
        if len(content) != ie.text_size:
 
134
            raise BzrCheckError('mismatched size on revision %s of file %s: '
 
135
                                '%d vs %d bytes'
 
136
                                % (self._revision_id, file_id, len(content),
 
137
                                   ie.text_size))
 
138
        return content
312
139
 
313
140
    def get_file(self, file_id):
314
 
        ie = self._inventory[file_id]
315
 
        f = self._store[ie.text_id]
316
 
        mutter("  get fileid{%s} from %r" % (file_id, self))
317
 
        ## self._check_retrieved(ie, f)
318
 
        return f
 
141
        return StringIO(self.get_file_text(file_id))
319
142
 
320
143
    def get_file_size(self, file_id):
321
144
        return self._inventory[file_id].text_size
322
145
 
323
146
    def get_file_sha1(self, file_id):
324
147
        ie = self._inventory[file_id]
325
 
        return ie.text_sha1
 
148
        if ie.kind == "file":
 
149
            return ie.text_sha1
326
150
 
327
151
    def has_filename(self, filename):
328
152
        return bool(self.inventory.path2id(filename))
344
168
        if False:  # just to make it a generator
345
169
            yield None
346
170
    
 
171
    def __contains__(self, file_id):
 
172
        return file_id in self._inventory
 
173
 
 
174
    def get_file_sha1(self, file_id):
 
175
        assert self._inventory[file_id].kind == "root_directory"
 
176
        return None
 
177
 
 
178
 
347
179
 
348
180
 
349
181
######################################################################
401
233
 
402
234
    
403
235
 
 
236
def find_renames(old_inv, new_inv):
 
237
    for file_id in old_inv:
 
238
        if file_id not in new_inv:
 
239
            continue
 
240
        old_name = old_inv.id2path(file_id)
 
241
        new_name = new_inv.id2path(file_id)
 
242
        if old_name != new_name:
 
243
            yield (old_name, new_name)
 
244
            
 
245
 
 
246
 
 
247
######################################################################
 
248
# export
 
249
 
 
250
def dir_exporter(tree, dest, root):
 
251
    """Export this tree to a new directory.
 
252
 
 
253
    `dest` should not exist, and will be created holding the
 
254
    contents of this tree.
 
255
 
 
256
    TODO: To handle subdirectories we need to create the
 
257
           directories first.
 
258
 
 
259
    :note: If the export fails, the destination directory will be
 
260
           left in a half-assed state.
 
261
    """
 
262
    import os
 
263
    os.mkdir(dest)
 
264
    mutter('export version %r' % tree)
 
265
    inv = tree.inventory
 
266
    for dp, ie in inv.iter_entries():
 
267
        kind = ie.kind
 
268
        fullpath = appendpath(dest, dp)
 
269
        if kind == 'directory':
 
270
            os.mkdir(fullpath)
 
271
        elif kind == 'file':
 
272
            pumpfile(tree.get_file(ie.file_id), file(fullpath, 'wb'))
 
273
        else:
 
274
            raise BzrError("don't know how to export {%s} of kind %r" % (ie.file_id, kind))
 
275
        mutter("  export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
 
276
exporters['dir'] = dir_exporter
 
277
 
 
278
try:
 
279
    import tarfile
 
280
except ImportError:
 
281
    pass
 
282
else:
 
283
    def get_root_name(dest):
 
284
        """Get just the root name for a tarball.
 
285
 
 
286
        >>> get_root_name('mytar.tar')
 
287
        'mytar'
 
288
        >>> get_root_name('mytar.tar.bz2')
 
289
        'mytar'
 
290
        >>> get_root_name('tar.tar.tar.tgz')
 
291
        'tar.tar.tar'
 
292
        >>> get_root_name('bzr-0.0.5.tar.gz')
 
293
        'bzr-0.0.5'
 
294
        >>> get_root_name('a/long/path/mytar.tgz')
 
295
        'mytar'
 
296
        >>> get_root_name('../parent/../dir/other.tbz2')
 
297
        'other'
 
298
        """
 
299
        endings = ['.tar', '.tar.gz', '.tgz', '.tar.bz2', '.tbz2']
 
300
        dest = os.path.basename(dest)
 
301
        for end in endings:
 
302
            if dest.endswith(end):
 
303
                return dest[:-len(end)]
 
304
 
 
305
    def tar_exporter(tree, dest, root, compression=None):
 
306
        """Export this tree to a new tar file.
 
307
 
 
308
        `dest` will be created holding the contents of this tree; if it
 
309
        already exists, it will be clobbered, like with "tar -c".
 
310
        """
 
311
        from time import time
 
312
        now = time()
 
313
        compression = str(compression or '')
 
314
        if root is None:
 
315
            root = get_root_name(dest)
 
316
        try:
 
317
            ball = tarfile.open(dest, 'w:' + compression)
 
318
        except tarfile.CompressionError, e:
 
319
            raise BzrError(str(e))
 
320
        mutter('export version %r' % tree)
 
321
        inv = tree.inventory
 
322
        for dp, ie in inv.iter_entries():
 
323
            mutter("  export {%s} kind %s to %s" % (ie.file_id, ie.kind, dest))
 
324
            item = tarfile.TarInfo(os.path.join(root, dp))
 
325
            # TODO: would be cool to actually set it to the timestamp of the
 
326
            # revision it was last changed
 
327
            item.mtime = now
 
328
            if ie.kind == 'directory':
 
329
                item.type = tarfile.DIRTYPE
 
330
                fileobj = None
 
331
                item.name += '/'
 
332
                item.size = 0
 
333
                item.mode = 0755
 
334
            elif ie.kind == 'file':
 
335
                item.type = tarfile.REGTYPE
 
336
                fileobj = tree.get_file(ie.file_id)
 
337
                item.size = _find_file_size(fileobj)
 
338
                item.mode = 0644
 
339
            else:
 
340
                raise BzrError("don't know how to export {%s} of kind %r" %
 
341
                        (ie.file_id, ie.kind))
 
342
 
 
343
            ball.addfile(item, fileobj)
 
344
        ball.close()
 
345
    exporters['tar'] = tar_exporter
 
346
 
 
347
    def tgz_exporter(tree, dest, root):
 
348
        tar_exporter(tree, dest, root, compression='gz')
 
349
    exporters['tgz'] = tgz_exporter
 
350
 
 
351
    def tbz_exporter(tree, dest, root):
 
352
        tar_exporter(tree, dest, root, compression='bz2')
 
353
    exporters['tbz2'] = tbz_exporter
 
354
 
 
355
 
 
356
def _find_file_size(fileobj):
 
357
    offset = fileobj.tell()
 
358
    try:
 
359
        fileobj.seek(0, 2)
 
360
        size = fileobj.tell()
 
361
    except TypeError:
 
362
        # gzip doesn't accept second argument to seek()
 
363
        fileobj.seek(0)
 
364
        size = 0
 
365
        while True:
 
366
            nread = len(fileobj.read())
 
367
            if nread == 0:
 
368
                break
 
369
            size += nread
 
370
    fileobj.seek(offset)
 
371
    return size