2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008-2010 Johan Dahlin
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
32 _CACHE_VERSION_FILENAME = '.cache-version'
35 def _get_versionhash():
36 toplevel = os.path.dirname(giscanner.__file__)
37 # Use pyc instead of py to avoid extra IO
38 sources = glob.glob(os.path.join(toplevel, '*.pyc'))
39 sources.append(sys.argv[0])
40 # Using mtimes is a bit (5x) faster than hashing the file contents
41 mtimes = (str(os.stat(source).st_mtime) for source in sources)
42 return hashlib.sha1(''.join(mtimes)).hexdigest()
46 if 'GI_SCANNER_DISABLE_CACHE' in os.environ:
48 homedir = os.environ.get('HOME')
51 if not os.path.exists(homedir):
54 cachedir = os.path.join(homedir, '.cache')
55 if not os.path.exists(cachedir):
57 os.mkdir(cachedir, 0755)
61 scannerdir = os.path.join(cachedir, 'g-ir-scanner')
62 if not os.path.exists(scannerdir):
64 os.mkdir(scannerdir, 0755)
67 # If it exists and is a file, don't cache at all
68 elif not os.path.isdir(scannerdir):
73 class CacheStore(object):
77 self._directory = _get_cachedir()
79 if e.errno != errno.EPERM:
81 self._directory = None
83 self._check_cache_version()
85 def _check_cache_version(self):
86 if self._directory is None:
89 current_hash = _get_versionhash()
90 version = os.path.join(self._directory, _CACHE_VERSION_FILENAME)
92 cache_hash = open(version).read()
95 if e.errno == errno.ENOENT:
100 if current_hash == cache_hash:
105 fp = open(version, 'w')
108 if e.errno == errno.EACCES:
113 fp.write(current_hash)
115 def _get_filename(self, filename):
116 # If we couldn't create the directory we're probably
117 # on a read only home directory where we just disable
118 # the cache all together.
119 if self._directory is None:
121 hexdigest = hashlib.sha1(filename).hexdigest()
122 return os.path.join(self._directory, hexdigest)
124 def _cache_is_valid(self, store_filename, filename):
125 return (os.stat(store_filename).st_mtime >=
126 os.stat(filename).st_mtime)
128 def _remove_filename(self, filename):
133 if e.errno == errno.EACCES:
138 # File does not exist
139 if e.errno == errno.ENOENT:
145 for filename in os.listdir(self._directory):
146 if filename == _CACHE_VERSION_FILENAME:
148 self._remove_filename(os.path.join(self._directory, filename))
150 def store(self, filename, data):
151 store_filename = self._get_filename(filename)
152 if store_filename is None:
155 if (os.path.exists(store_filename) and self._cache_is_valid(store_filename, filename)):
158 tmp_fd, tmp_filename = tempfile.mkstemp(prefix='g-ir-scanner-cache-')
160 cPickle.dump(data, os.fdopen(tmp_fd, 'w'))
162 # No space left on device
163 if e.errno == errno.ENOSPC:
164 self._remove_filename(tmp_filename)
170 shutil.move(tmp_filename, store_filename)
173 if e.errno == errno.EACCES:
174 self._remove_filename(tmp_filename)
178 def load(self, filename):
179 store_filename = self._get_filename(filename)
180 if store_filename is None:
183 fd = open(store_filename)
185 if e.errno == errno.ENOENT:
189 if not self._cache_is_valid(store_filename, filename):
192 data = cPickle.load(fd)
193 except (AttributeError, EOFError, ValueError, cPickle.BadPickleGet):
194 # Broken cache entry, remove it
195 self._remove_filename(store_filename)