/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/hashcache.py

  • Committer: Jelmer Vernooij
  • Date: 2017-11-13 20:08:37 UTC
  • mfrom: (6813.3.1 py3_hashcache)
  • Revision ID: jelmer@jelmer.uk-20171113200837-fo1w7jxpl2gf74dc
Merge lp:~gz/brz/py3_hashcache

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
 
31
31
 
32
 
CACHE_HEADER = "### bzr hashcache v5\n"
 
32
CACHE_HEADER = b"### bzr hashcache v5\n"
33
33
 
34
34
import os
35
35
import stat
229
229
            outf.write(CACHE_HEADER)
230
230
 
231
231
            for path, c  in viewitems(self._cache):
232
 
                line_info = [path.encode('utf-8'), '// ', c[0], ' ']
233
 
                line_info.append(' '.join([str(fld) for fld in c[1]]))
234
 
                line_info.append('\n')
235
 
                outf.write(''.join(line_info))
 
232
                line_info = [path.encode('utf-8'), b'// ', c[0], b' ']
 
233
                line_info.append(b'%d %d %d %d %d %d' % c[1])
 
234
                line_info.append(b'\n')
 
235
                outf.write(b''.join(line_info))
236
236
            outf.commit()
237
237
            self.needs_write = False
238
238
            ## mutter("write hash cache: %s hits=%d misses=%d stat=%d recent=%d updates=%d",
253
253
 
254
254
        fn = self.cache_file_name()
255
255
        try:
256
 
            inf = file(fn, 'rb', buffering=65000)
 
256
            inf = open(fn, 'rb', buffering=65000)
257
257
        except IOError as e:
258
258
            trace.mutter("failed to open %s: %s", fn, e)
259
259
            # better write it now so it is valid
260
260
            self.needs_write = True
261
261
            return
262
262
 
263
 
        hdr = inf.readline()
264
 
        if hdr != CACHE_HEADER:
265
 
            trace.mutter('cache header marker not found at top of %s;'
266
 
                         ' discarding cache', fn)
267
 
            self.needs_write = True
268
 
            return
269
 
 
270
 
        for l in inf:
271
 
            pos = l.index('// ')
272
 
            path = l[:pos].decode('utf-8')
273
 
            if path in self._cache:
274
 
                trace.warning('duplicated path %r in cache' % path)
275
 
                continue
276
 
 
277
 
            pos += 3
278
 
            fields = l[pos:].split(' ')
279
 
            if len(fields) != 7:
280
 
                trace.warning("bad line in hashcache: %r" % l)
281
 
                continue
282
 
 
283
 
            sha1 = fields[0]
284
 
            if len(sha1) != 40:
285
 
                trace.warning("bad sha1 in hashcache: %r" % sha1)
286
 
                continue
287
 
 
288
 
            fp = tuple(map(int, fields[1:]))
289
 
 
290
 
            self._cache[path] = (sha1, fp)
291
 
 
292
 
        # GZ 2009-09-20: Should really use a try/finally block to ensure close
293
 
        inf.close()
 
263
        with inf:
 
264
            hdr = inf.readline()
 
265
            if hdr != CACHE_HEADER:
 
266
                trace.mutter('cache header marker not found at top of %s;'
 
267
                             ' discarding cache', fn)
 
268
                self.needs_write = True
 
269
                return
 
270
 
 
271
            for l in inf:
 
272
                pos = l.index(b'// ')
 
273
                path = l[:pos].decode('utf-8')
 
274
                if path in self._cache:
 
275
                    trace.warning('duplicated path %r in cache' % path)
 
276
                    continue
 
277
 
 
278
                pos += 3
 
279
                fields = l[pos:].split(b' ')
 
280
                if len(fields) != 7:
 
281
                    trace.warning("bad line in hashcache: %r" % l)
 
282
                    continue
 
283
 
 
284
                sha1 = fields[0]
 
285
                if len(sha1) != 40:
 
286
                    trace.warning("bad sha1 in hashcache: %r" % sha1)
 
287
                    continue
 
288
 
 
289
                fp = tuple(map(int, fields[1:]))
 
290
 
 
291
                self._cache[path] = (sha1, fp)
294
292
 
295
293
        self.needs_write = False
296
294