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'
34 def _get_versionhash():
35 toplevel = os.path.dirname(giscanner.__file__)
36 # Use pyc instead of py to avoid extra IO
37 sources = glob.glob(os.path.join(toplevel, '*.pyc'))
38 sources.append(sys.argv[0])
39 # Using mtimes is a bit (5x) faster than hashing the file contents
40 mtimes = (str(os.stat(source).st_mtime) for source in sources)
41 return hashlib.sha1(''.join(mtimes)).hexdigest()
44 if 'GI_SCANNER_DISABLE_CACHE' in os.environ:
46 homedir = os.environ.get('HOME')
49 if not os.path.exists(homedir):
52 cachedir = os.path.join(homedir, '.cache')
53 if not os.path.exists(cachedir):
55 os.mkdir(cachedir, 0755)
59 scannerdir = os.path.join(cachedir, 'g-ir-scanner')
60 if not os.path.exists(scannerdir):
62 os.mkdir(scannerdir, 0755)
65 # If it exists and is a file, don't cache at all
66 elif not os.path.isdir(scannerdir):
71 class CacheStore(object):
75 self._directory = _get_cachedir()
77 if e.errno != errno.EPERM:
79 self._directory = None
81 self._check_cache_version()
83 def _check_cache_version(self):
84 if self._directory is None:
87 current_hash = _get_versionhash()
88 version = os.path.join(self._directory, _CACHE_VERSION_FILENAME)
90 cache_hash = open(version).read()
93 if e.errno == errno.ENOENT:
98 if current_hash == cache_hash:
103 fp = open(version, 'w')
106 if e.errno == errno.EACCES:
111 fp.write(current_hash)
113 def _get_filename(self, filename):
114 # If we couldn't create the directory we're probably
115 # on a read only home directory where we just disable
116 # the cache all together.
117 if self._directory is None:
119 hexdigest = hashlib.sha1(filename).hexdigest()
120 return os.path.join(self._directory, hexdigest)
122 def _cache_is_valid(self, store_filename, filename):
123 return (os.stat(store_filename).st_mtime >=
124 os.stat(filename).st_mtime)
126 def _remove_filename(self, filename):
131 if e.errno == errno.EACCES:
136 # File does not exist
137 if e.errno == errno.ENOENT:
143 for filename in os.listdir(self._directory):
144 if filename == _CACHE_VERSION_FILENAME:
146 self._remove_filename(os.path.join(self._directory, filename))
148 def store(self, filename, data):
149 store_filename = self._get_filename(filename)
150 if store_filename is None:
153 if (os.path.exists(store_filename) and
154 self._cache_is_valid(store_filename, filename)):
157 tmp_fd, tmp_filename = tempfile.mkstemp(prefix='g-ir-scanner-cache-')
159 cPickle.dump(data, os.fdopen(tmp_fd, 'w'))
161 # No space left on device
162 if e.errno == errno.ENOSPC:
163 self._remove_filename(tmp_filename)
169 shutil.move(tmp_filename, store_filename)
172 if e.errno == errno.EACCES:
173 self._remove_filename(tmp_filename)
177 def load(self, filename):
178 store_filename = self._get_filename(filename)
179 if store_filename is None:
182 fd = open(store_filename)
184 if e.errno == errno.ENOENT:
188 if not self._cache_is_valid(store_filename, filename):
191 data = cPickle.load(fd)
192 except (AttributeError, EOFError, ValueError, cPickle.BadPickleGet):
193 # Broken cache entry, remove it
194 self._remove_filename(store_filename)