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.
35 _CACHE_VERSION_FILENAME = '.cache-version'
38 def _get_versionhash():
39 toplevel = os.path.dirname(giscanner.__file__)
40 sources = glob.glob(os.path.join(toplevel, '*.py'))
41 sources.append(sys.argv[0])
42 # Using mtimes is a bit (5x) faster than hashing the file contents
43 mtimes = (str(os.stat(source).st_mtime) for source in sources)
44 # ASCII encoding is sufficient since we are only dealing with numbers.
45 return hashlib.sha1(''.join(mtimes).encode('ascii')).hexdigest()
48 class CacheStore(object):
51 self._directory = self._get_cachedir()
52 self._check_cache_version()
54 def _get_cachedir(self):
55 if 'GI_SCANNER_DISABLE_CACHE' in os.environ:
58 cachedir = utils.get_user_cache_dir('g-ir-scanner')
61 def _check_cache_version(self):
62 if self._directory is None:
65 current_hash = _get_versionhash()
66 version = os.path.join(self._directory, _CACHE_VERSION_FILENAME)
68 with open(version, 'r', encoding='utf-8') as version_file:
69 cache_hash = version_file.read()
70 except (IOError, OSError) as e:
72 if e.errno == errno.ENOENT:
77 if current_hash == cache_hash:
82 tmp_fd, tmp_filename = tempfile.mkstemp(prefix='g-ir-scanner-cache-version-')
84 with os.fdopen(tmp_fd, 'w', encoding='utf-8') as tmp_file:
85 tmp_file.write(current_hash)
87 # On Unix, this would just be os.rename() but Windows
89 shutil.move(tmp_filename, version)
90 except (IOError, OSError) as e:
92 if e.errno == errno.EACCES:
97 def _get_filename(self, filename):
98 # If we couldn't create the directory we're probably
99 # on a read only home directory where we just disable
100 # the cache all together.
101 if self._directory is None:
103 # Assume UTF-8 encoding for the filenames. This doesn't matter so much
104 # as long as the results of this method always produce the same hash.
105 hexdigest = hashlib.sha1(filename.encode('utf-8')).hexdigest()
106 return os.path.join(self._directory, hexdigest)
108 def _cache_is_valid(self, store_filename, filename):
110 store_mtime = os.stat(store_filename).st_mtime
111 except FileNotFoundError:
114 return store_mtime >= os.stat(filename).st_mtime
116 def _remove_filename(self, filename):
119 except (IOError, OSError) as e:
120 # Ignore "permission denied", "file does not exist"
121 if e.errno in (errno.EACCES, errno.ENOENT):
127 for filename in os.listdir(self._directory):
128 if filename == _CACHE_VERSION_FILENAME:
130 self._remove_filename(os.path.join(self._directory, filename))
132 def store(self, filename, data):
133 store_filename = self._get_filename(filename)
134 if store_filename is None:
137 if self._cache_is_valid(store_filename, filename):
140 tmp_fd, tmp_filename = tempfile.mkstemp(prefix='g-ir-scanner-cache-')
142 with os.fdopen(tmp_fd, 'wb') as tmp_file:
143 pickle.dump(data, tmp_file)
144 except (IOError, OSError) as e:
145 # No space left on device
146 if e.errno == errno.ENOSPC:
147 self._remove_filename(tmp_filename)
153 shutil.move(tmp_filename, store_filename)
154 except (IOError, OSError) as e:
156 if e.errno == errno.EACCES:
157 self._remove_filename(tmp_filename)
161 def load(self, filename):
162 store_filename = self._get_filename(filename)
163 if store_filename is None:
166 fd = open(store_filename, 'rb')
167 except (IOError, OSError) as e:
168 if e.errno == errno.ENOENT:
174 if not self._cache_is_valid(store_filename, filename):
177 data = pickle.load(fd)
179 # Broken cache entry, remove it
180 self._remove_filename(store_filename)