~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Martin Pool
  • Date: 2005-07-22 23:58:11 UTC
  • Revision ID: mbp@sourcefrog.net-20050722235811-bebdd984c5aec42e
- add some passing tests for is_inside_any

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
# TODO: Don't allow WorkingTrees to be constructed for remote branches.
 
18
 
 
19
# FIXME: I don't know if writing out the cache from the destructor is really a
 
20
# good idea, because destructors are considered poor taste in Python, and
 
21
# it's not predictable when it will be written out.
17
22
 
18
23
import os
19
24
    
30
35
    It is possible for a `WorkingTree` to have a filename which is
31
36
    not listed in the Inventory and vice versa.
32
37
    """
33
 
    _statcache = None
34
 
    
35
38
    def __init__(self, basedir, inv):
 
39
        from bzrlib.hashcache import HashCache
 
40
        from bzrlib.trace import note, mutter
 
41
 
36
42
        self._inventory = inv
37
43
        self.basedir = basedir
38
44
        self.path2id = inv.path2id
39
 
        self._update_statcache()
 
45
 
 
46
        # update the whole cache up front and write to disk if anything changed;
 
47
        # in the future we might want to do this more selectively
 
48
        hc = self._hashcache = HashCache(basedir)
 
49
        hc.read()
 
50
        hc.scan()
 
51
 
 
52
        if hc.needs_write:
 
53
            mutter("write hc")
 
54
            hc.write()
 
55
            
 
56
            
 
57
    def __del__(self):
 
58
        if self._hashcache.needs_write:
 
59
            self._hashcache.write()
 
60
 
40
61
 
41
62
    def __iter__(self):
42
63
        """Iterate through file_ids for this tree.
45
66
        and the working file exists.
46
67
        """
47
68
        inv = self._inventory
48
 
        for file_id in self._inventory:
49
 
            # TODO: This is slightly redundant; we should be able to just
50
 
            # check the statcache but it only includes regular files.
51
 
            # only include files which still exist on disk
52
 
            ie = inv[file_id]
53
 
            if ie.kind == 'file':
54
 
                if ((file_id in self._statcache)
55
 
                    or (os.path.exists(self.abspath(inv.id2path(file_id))))):
56
 
                    yield file_id
57
 
 
 
69
        for path, ie in inv.iter_entries():
 
70
            if os.path.exists(self.abspath(path)):
 
71
                yield ie.file_id
58
72
 
59
73
 
60
74
    def __repr__(self):
61
75
        return "<%s of %s>" % (self.__class__.__name__,
62
 
                               self.basedir)
 
76
                               getattr(self, 'basedir', None))
 
77
 
 
78
 
63
79
 
64
80
    def abspath(self, filename):
65
81
        return os.path.join(self.basedir, filename)
80
96
                
81
97
    def has_id(self, file_id):
82
98
        # files that have been deleted are excluded
83
 
        if not self.inventory.has_id(file_id):
 
99
        inv = self._inventory
 
100
        if not inv.has_id(file_id):
84
101
            return False
85
 
        if file_id in self._statcache:
86
 
            return True
87
 
        return os.path.exists(self.abspath(self.id2path(file_id)))
 
102
        path = inv.id2path(file_id)
 
103
        return os.path.exists(self.abspath(path))
88
104
 
89
105
 
90
106
    __contains__ = has_id
91
107
    
92
108
 
93
 
    def _update_statcache(self):
94
 
        if not self._statcache:
95
 
            from bzrlib.statcache import update_cache
96
 
            self._statcache = update_cache(self.basedir, self.inventory)
97
 
 
98
109
    def get_file_size(self, file_id):
99
 
        import os, stat
100
 
        return os.stat(self._get_store_filename(file_id))[stat.ST_SIZE]
 
110
        # is this still called?
 
111
        raise NotImplementedError()
101
112
 
102
113
 
103
114
    def get_file_sha1(self, file_id):
104
 
        from bzrlib.statcache import SC_SHA1
105
 
        return self._statcache[file_id][SC_SHA1]
 
115
        path = self._inventory.id2path(file_id)
 
116
        return self._hashcache.get_sha1(path)
106
117
 
107
118
 
108
119
    def file_class(self, filename):
127
138
        from osutils import appendpath, file_kind
128
139
        import os
129
140
 
130
 
        inv = self.inventory
 
141
        inv = self._inventory
131
142
 
132
143
        def descend(from_dir_relpath, from_dir_id, dp):
133
144
            ls = os.listdir(dp)
279
290
                    return pat
280
291
        else:
281
292
            return None
282
 
        
283
 
 
284
 
        
285
 
        
286
 
 
 
293
        
 
 
b'\\ No newline at end of file'