2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008 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.
28 cachedir = os.path.join(os.environ['HOME'], '.cache')
29 if not os.path.exists(cachedir):
30 os.mkdir(cachedir, 0755)
32 scannerdir = os.path.join(cachedir, 'g-ir-scanner')
33 if not os.path.exists(scannerdir):
34 os.mkdir(scannerdir, 0755)
35 # If it exists and is a file, don't cache at all
36 elif not os.path.isdir(scannerdir):
41 class CacheStore(object):
45 self._directory = _get_cachedir()
47 if e.errno != errno.EPERM:
49 self._directory = None
51 def _get_filename(self, filename):
52 # If we couldn't create the directory we're probably
53 # on a read only home directory where we just disable
54 # the cache all together.
55 if self._directory is None:
57 hexdigest = hashlib.sha1(filename).hexdigest()
58 return os.path.join(self._directory, hexdigest)
60 def _cache_is_valid(self, store_filename, filename):
61 return (os.stat(store_filename).st_mtime >=
62 os.stat(filename).st_mtime)
64 def _purge_cache(self, filename):
69 if e.errno == errno.EACCES:
72 elif e.errno == errno.ENOENT:
77 def store(self, filename, data):
78 store_filename = self._get_filename(filename)
79 if store_filename is None:
81 if (os.path.exists(store_filename) and
82 self._cache_is_valid(store_filename, filename)):
84 fd = open(store_filename, 'w')
86 cPickle.dump(data, fd)
88 # No space left on device
89 if e.errno == e.ENOSPC:
94 def load(self, filename):
95 store_filename = self._get_filename(filename)
96 if store_filename is None:
99 fd = open(store_filename)
101 if e.errno == errno.ENOENT:
105 if not self._cache_is_valid(store_filename, filename):
108 data = cPickle.load(fd)
109 except (EOFError, cPickle.BadPickleGet):
110 # Broken cache entry, remove it
111 self._purge_cache(store_filename)