~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/hashcache.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-03-20 05:08:48 UTC
  • mfrom: (4171.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20090320050848-c1wdgzf5kkfdt1ys
Content filters (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
import os, stat, time
33
33
 
 
34
from bzrlib.filters import internal_size_sha_file_byname
34
35
from bzrlib.osutils import sha_file, sha_string, pathjoin, safe_unicode
35
36
from bzrlib.trace import mutter, warning
36
37
from bzrlib.atomicfile import AtomicFile
79
80
    """
80
81
    needs_write = False
81
82
 
82
 
    def __init__(self, root, cache_file_name, mode=None):
83
 
        """Create a hash cache in base dir, and set the file mode to mode."""
 
83
    def __init__(self, root, cache_file_name, mode=None,
 
84
            content_filter_stack_provider=None):
 
85
        """Create a hash cache in base dir, and set the file mode to mode.
 
86
 
 
87
        :param content_filter_stack_provider: a function that takes a
 
88
            path (relative to the top of the tree) and a file-id as
 
89
            parameters and returns a stack of ContentFilters.
 
90
            If None, no content filtering is performed.
 
91
        """
84
92
        self.root = safe_unicode(root)
85
93
        self.root_utf8 = self.root.encode('utf8') # where is the filesystem encoding ?
86
94
        self.hit_count = 0
92
100
        self._cache = {}
93
101
        self._mode = mode
94
102
        self._cache_file_name = safe_unicode(cache_file_name)
 
103
        self._filter_provider = content_filter_stack_provider
95
104
 
96
105
    def cache_file_name(self):
97
106
        return self._cache_file_name
161
170
 
162
171
        mode = file_fp[FP_MODE_COLUMN]
163
172
        if stat.S_ISREG(mode):
164
 
            digest = self._really_sha1_file(abspath)
 
173
            if self._filter_provider is None:
 
174
                filters = []
 
175
            else:
 
176
                filters = self._filter_provider(path=path, file_id=None)
 
177
            digest = self._really_sha1_file(abspath, filters)
165
178
        elif stat.S_ISLNK(mode):
166
179
            digest = sha_string(os.readlink(abspath))
167
180
        else:
198
211
            self._cache[path] = (digest, file_fp)
199
212
        return digest
200
213
 
201
 
    def _really_sha1_file(self, abspath):
 
214
    def _really_sha1_file(self, abspath, filters):
202
215
        """Calculate the SHA1 of a file by reading the full text"""
203
 
        return sha_file(file(abspath, 'rb', buffering=65000))
 
216
        return internal_size_sha_file_byname(abspath, filters)[1]
204
217
 
205
218
    def write(self):
206
219
        """Write contents of cache to file."""