comparison tests/test-filecache.py @ 14928:dca59d5be12d

scmutil: introduce filecache The idea is being able to associate a file with a property, and watch that file stat info for modifications when we decide it's important for it to be up-to-date. Once it changes, we recreate the object. On filesystems that can't uniquely identify a file, we always recreate. As a consequence, localrepo.invalidate() will become much less expensive in the case where nothing changed on-disk.
author Idan Kamara <idankk86@gmail.com>
date Sat, 09 Jul 2011 19:06:59 +0300
parents
children 0b3e57c1b8c0
comparison
equal deleted inserted replaced
14927:2aa3e07b2f07 14928:dca59d5be12d
1 import sys, os, subprocess
2
3 try:
4 subprocess.check_call(['%s/hghave' % os.environ['TESTDIR'], 'cacheable'])
5 except subprocess.CalledProcessError:
6 sys.exit(80)
7
8 from mercurial import util, scmutil, extensions
9
10 filecache = scmutil.filecache
11
12 class fakerepo(object):
13 def __init__(self):
14 self._filecache = {}
15
16 def join(self, p):
17 return p
18
19 def sjoin(self, p):
20 return p
21
22 @filecache('x')
23 def cached(self):
24 print 'creating'
25
26 def invalidate(self):
27 for k in self._filecache:
28 try:
29 delattr(self, k)
30 except AttributeError:
31 pass
32
33 def basic(repo):
34 # file doesn't exist, calls function
35 repo.cached
36
37 repo.invalidate()
38 # file still doesn't exist, uses cache
39 repo.cached
40
41 # create empty file
42 f = open('x', 'w')
43 f.close()
44 repo.invalidate()
45 # should recreate the object
46 repo.cached
47
48 f = open('x', 'w')
49 f.write('a')
50 f.close()
51 repo.invalidate()
52 # should recreate the object
53 repo.cached
54
55 repo.invalidate()
56 # stats file again, nothing changed, reuses object
57 repo.cached
58
59 # atomic replace file, size doesn't change
60 # hopefully st_mtime doesn't change as well so this doesn't use the cache
61 # because of inode change
62 f = scmutil.opener('.')('x', 'w', atomictemp=True)
63 f.write('b')
64 f.rename()
65
66 repo.invalidate()
67 repo.cached
68
69 def fakeuncacheable():
70 def wrapcacheable(orig, *args, **kwargs):
71 return False
72
73 def wrapinit(orig, *args, **kwargs):
74 pass
75
76 originit = extensions.wrapfunction(util.cachestat, '__init__', wrapinit)
77 origcacheable = extensions.wrapfunction(util.cachestat, 'cacheable', wrapcacheable)
78
79 try:
80 os.remove('x')
81 except:
82 pass
83
84 basic(fakerepo())
85
86 util.cachestat.cacheable = origcacheable
87 util.cachestat.__init__ = originit
88
89 print 'basic:'
90 print
91 basic(fakerepo())
92 print
93 print 'fakeuncacheable:'
94 print
95 fakeuncacheable()