~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tree.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-09 04:08:15 UTC
  • Revision ID: mbp@sourcefrog.net-20050309040815-13242001617e4a06
import from baz patch-364

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
#! /usr/bin/env python
 
2
# -*- coding: UTF-8 -*-
2
3
 
3
4
# This program is free software; you can redistribute it and/or modify
4
5
# it under the terms of the GNU General Public License as published by
20
21
from sets import Set
21
22
import os.path, os, fnmatch
22
23
 
23
 
from osutils import pumpfile, compare_files, 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
24
from inventory import Inventory
29
25
from trace import mutter, note
 
26
from osutils import pumpfile, compare_files, filesize, quotefn, sha_file, \
 
27
     joinpath, splitpath, appendpath, isdir, isfile, file_kind
30
28
from errors import bailout
31
29
import branch
 
30
from stat import S_ISREG, S_ISDIR, ST_MODE, ST_SIZE
32
31
 
33
32
import bzrlib
34
33
 
54
53
    trees or versioned trees.
55
54
    """
56
55
    
 
56
    def get_file(self, file_id):
 
57
        """Return an open file-like object for given file id."""
 
58
        raise NotImplementedError()
 
59
 
57
60
    def has_filename(self, filename):
58
61
        """True if the tree has given filename."""
59
62
        raise NotImplementedError()
75
78
                         doc="Inventory of this Tree")
76
79
 
77
80
    def _check_retrieved(self, ie, f):
78
 
        fp = fingerprint_file(f)
79
 
        f.seek(0)
80
 
        
81
 
        if ie.text_size != None:
82
 
            if ie.text_size != fp['size']:
 
81
        # TODO: Test this check by damaging the store?
 
82
        if ie.text_size is not None:
 
83
            fs = filesize(f)
 
84
            if fs != ie.text_size:
83
85
                bailout("mismatched size for file %r in %r" % (ie.file_id, self._store),
84
86
                        ["inventory expects %d bytes" % ie.text_size,
85
 
                         "file is actually %d bytes" % fp['size'],
 
87
                         "file is actually %d bytes" % fs,
86
88
                         "store is probably damaged/corrupt"])
87
89
 
88
 
        if ie.text_sha1 != fp['sha1']:
 
90
        f_hash = sha_file(f)
 
91
        f.seek(0)
 
92
        if ie.text_sha1 != f_hash:
89
93
            bailout("wrong SHA-1 for file %r in %r" % (ie.file_id, self._store),
90
94
                    ["inventory expects %s" % ie.text_sha1,
91
 
                     "file is actually %s" % fp['sha1'],
 
95
                     "file is actually %s" % f_hash,
92
96
                     "store is probably damaged/corrupt"])
93
97
 
94
98
 
95
 
    def print_file(self, fileid):
96
 
        """Print file with id `fileid` to stdout."""
97
 
        import sys
98
 
        pumpfile(self.get_file(fileid), sys.stdout)
99
 
        
100
 
        
101
 
    def export(self, dest):        
 
99
    def export(self, dest):
102
100
        """Export this tree to a new directory.
103
101
 
104
102
        `dest` should not exist, and will be created holding the
105
103
        contents of this tree.
106
104
 
107
 
        TODO: To handle subdirectories we need to create the
 
105
        :todo: To handle subdirectories we need to create the
108
106
               directories first.
109
107
 
110
108
        :note: If the export fails, the destination directory will be
121
119
            elif kind == 'file':
122
120
                pumpfile(self.get_file(ie.file_id), file(fullpath, 'wb'))
123
121
            else:
124
 
                bailout("don't know how to export {%s} of kind %r" % (fid, kind))
 
122
                bailout("don't know how to export {%s} of kind %r", fid, kind)
125
123
            mutter("  export {%s} kind %s to %s" % (ie.file_id, kind, fullpath))
126
124
 
127
125
 
144
142
        return "<%s of %s>" % (self.__class__.__name__,
145
143
                               self.basedir)
146
144
 
147
 
    def abspath(self, filename):
 
145
    def _rel(self, filename):
148
146
        return os.path.join(self.basedir, filename)
149
147
 
150
148
    def has_filename(self, filename):
151
 
        return os.path.exists(self.abspath(filename))
 
149
        return os.path.exists(self._rel(filename))
152
150
 
153
151
    def get_file(self, file_id):
154
 
        return self.get_file_byname(self.id2path(file_id))
155
 
 
156
 
    def get_file_byname(self, filename):
157
 
        return file(self.abspath(filename), 'rb')
 
152
        return file(self._get_store_filename(file_id), 'rb')
158
153
 
159
154
    def _get_store_filename(self, file_id):
160
 
        ## XXX: badly named; this isn't in the store at all
161
 
        return self.abspath(self.id2path(file_id))
162
 
 
163
 
    def has_id(self, file_id):
164
 
        # files that have been deleted are excluded
165
 
        if not self.inventory.has_id(file_id):
166
 
            return False
167
 
        return os.access(self.abspath(self.inventory.id2path(file_id)), os.F_OK)
 
155
        return self._rel(self.id2path(file_id))
168
156
 
169
157
    def get_file_size(self, file_id):
170
158
        return os.stat(self._get_store_filename(file_id))[ST_SIZE]
183
171
            return '?'
184
172
 
185
173
 
 
174
    def file_kind(self, filename):
 
175
        if isfile(self._rel(filename)):
 
176
            return 'file'
 
177
        elif isdir(self._rel(filename)):
 
178
            return 'directory'
 
179
        else:
 
180
            return 'unknown'
 
181
 
 
182
 
186
183
    def list_files(self):
187
184
        """Recursively list all files as (path, class, kind, id).
188
185
 
195
192
        """
196
193
        inv = self.inventory
197
194
 
198
 
        def descend(from_dir_relpath, from_dir_id, dp):
 
195
        def descend(from_dir, from_dir_id, dp):
199
196
            ls = os.listdir(dp)
200
197
            ls.sort()
201
198
            for f in ls:
202
 
                ## TODO: If we find a subdirectory with its own .bzr
203
 
                ## directory, then that is a separate tree and we
204
 
                ## should exclude it.
205
199
                if bzrlib.BZRDIR == f:
206
200
                    continue
207
201
 
208
202
                # path within tree
209
 
                fp = appendpath(from_dir_relpath, f)
 
203
                fp = appendpath(from_dir, f)
210
204
 
211
205
                # absolute path
212
206
                fap = appendpath(dp, f)
238
232
                for ff in descend(fp, f_ie.file_id, fap):
239
233
                    yield ff
240
234
 
241
 
        for f in descend('', inv.root.file_id, self.basedir):
 
235
        for f in descend('', None, self.basedir):
242
236
            yield f
243
237
            
244
238
 
245
239
 
246
 
    def unknowns(self):
247
 
        for subp in self.extras():
248
 
            if not self.is_ignored(subp):
249
 
                yield subp
250
 
 
251
 
 
252
 
    def extras(self):
253
 
        """Yield all unknown files in this WorkingTree.
 
240
    def unknowns(self, path='', dir_id=None):
 
241
        """Yield names of unknown files in this WorkingTree.
254
242
 
255
243
        If there are any unknown directories then only the directory is
256
244
        returned, not all its children.  But if there are unknown files
258
246
 
259
247
        Currently returned depth-first, sorted by name within directories.
260
248
        """
261
 
        ## TODO: Work from given directory downwards
262
 
        
263
 
        for path, dir_entry in self.inventory.directories():
264
 
            mutter("search for unknowns in %r" % path)
265
 
            dirabs = self.abspath(path)
266
 
            if not isdir(dirabs):
267
 
                # e.g. directory deleted
268
 
                continue
269
 
 
270
 
            fl = []
271
 
            for subf in os.listdir(dirabs):
272
 
                if (subf != '.bzr'
273
 
                    and (subf not in dir_entry.children)):
274
 
                    fl.append(subf)
275
 
            
276
 
            fl.sort()
277
 
            for subf in fl:
278
 
                subp = appendpath(path, subf)
279
 
                yield subp
280
 
 
 
249
        for fpath, fclass, fkind, fid in self.list_files():
 
250
            if fclass == '?':
 
251
                yield fpath
 
252
                
281
253
 
282
254
    def ignored_files(self):
283
 
        """Yield list of PATH, IGNORE_PATTERN"""
284
 
        for subp in self.extras():
285
 
            pat = self.is_ignored(subp)
286
 
            if pat != None:
287
 
                yield subp, pat
288
 
 
289
 
 
290
 
    def get_ignore_list(self):
291
 
        """Return list of ignore patterns.
292
 
 
293
 
        Cached in the Tree object after the first call.
294
 
        """
295
 
        if hasattr(self, '_ignorelist'):
296
 
            return self._ignorelist
297
 
 
298
 
        l = bzrlib.DEFAULT_IGNORE[:]
299
 
        if self.has_filename(bzrlib.IGNORE_FILENAME):
300
 
            f = self.get_file_byname(bzrlib.IGNORE_FILENAME)
301
 
            l.extend([line.rstrip("\n\r") for line in f.readlines()])
302
 
        self._ignorelist = l
303
 
        return l
 
255
        for fpath, fclass, fkind, fid in self.list_files():
 
256
            if fclass == 'I':
 
257
                yield fpath
304
258
 
305
259
 
306
260
    def is_ignored(self, filename):
307
 
        r"""Check whether the filename matches an ignore pattern.
308
 
 
309
 
        Patterns containing '/' or '\' need to match the whole path;
310
 
        others match against only the last component.
311
 
 
312
 
        If the file is ignored, returns the pattern which caused it to
313
 
        be ignored, otherwise None.  So this can simply be used as a
314
 
        boolean if desired."""
315
 
 
316
 
        # TODO: Use '**' to match directories, and other extended
317
 
        # globbing stuff from cvs/rsync.
318
 
 
319
 
        # XXX: fnmatch is actually not quite what we want: it's only
320
 
        # approximately the same as real Unix fnmatch, and doesn't
321
 
        # treat dotfiles correctly and allows * to match /.
322
 
        # Eventually it should be replaced with something more
323
 
        # accurate.
324
 
        
325
 
        for pat in self.get_ignore_list():
326
 
            if '/' in pat or '\\' in pat:
327
 
                
328
 
                # as a special case, you can put ./ at the start of a
329
 
                # pattern; this is good to match in the top-level
330
 
                # only;
331
 
                
332
 
                if (pat[:2] == './') or (pat[:2] == '.\\'):
333
 
                    newpat = pat[2:]
334
 
                else:
335
 
                    newpat = pat
336
 
                if fnmatch.fnmatchcase(filename, newpat):
337
 
                    return pat
338
 
            else:
339
 
                if fnmatch.fnmatchcase(splitpath(filename)[-1], pat):
340
 
                    return pat
341
 
        return None
 
261
        """Check whether the filename matches an ignore pattern."""
 
262
        ## TODO: Take them from a file, not hardcoded
 
263
        ## TODO: Use extended zsh-style globs maybe?
 
264
        ## TODO: Use '**' to match directories?
 
265
        ## TODO: Patterns without / should match in subdirectories?
 
266
        for i in bzrlib.DEFAULT_IGNORE:
 
267
            if fnmatch.fnmatchcase(filename, i):
 
268
                return True
 
269
        return False
342
270
        
343
271
 
344
272
        
349
277
 
350
278
    File text can be retrieved from the text store.
351
279
 
352
 
    TODO: Some kind of `__repr__` method, but a good one
 
280
    :todo: Some kind of `__repr__` method, but a good one
353
281
           probably means knowing the branch and revision number,
354
282
           or at least passing a description to the constructor.
355
283
    """
362
290
        ie = self._inventory[file_id]
363
291
        f = self._store[ie.text_id]
364
292
        mutter("  get fileid{%s} from %r" % (file_id, self))
 
293
        fs = filesize(f)
 
294
        if ie.text_size is None:
 
295
            note("warning: no text size recorded on %r" % ie)
365
296
        self._check_retrieved(ie, f)
366
297
        return f
367
298
 
449
380
 
450
381
    
451
382
 
452
 
def find_renames(old_inv, new_inv):
453
 
    for file_id in old_inv:
454
 
        if file_id not in new_inv:
455
 
            continue
456
 
        old_name = old_inv.id2path(file_id)
457
 
        new_name = new_inv.id2path(file_id)
458
 
        if old_name != new_name:
459
 
            yield (old_name, new_name)
460