Imported Upstream version 1.39.3
[platform/upstream/gobject-introspection.git] / giscanner / cachestore.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008-2010  Johan Dahlin
4 #
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.
9 #
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.
14 #
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.
19 #
20
21 import errno
22 import cPickle
23 import glob
24 import hashlib
25 import os
26 import shutil
27 import sys
28 import tempfile
29
30 import giscanner
31
32 _CACHE_VERSION_FILENAME = '.cache-version'
33
34
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()
43
44
45 def _get_cachedir():
46     if 'GI_SCANNER_DISABLE_CACHE' in os.environ:
47         return None
48     homedir = os.environ.get('HOME')
49     if homedir is None:
50         return None
51     if not os.path.exists(homedir):
52         return None
53
54     cachedir = os.path.join(homedir, '.cache')
55     if not os.path.exists(cachedir):
56         try:
57             os.mkdir(cachedir, 0755)
58         except OSError:
59             return None
60
61     scannerdir = os.path.join(cachedir, 'g-ir-scanner')
62     if not os.path.exists(scannerdir):
63         try:
64             os.mkdir(scannerdir, 0755)
65         except OSError:
66             return None
67     # If it exists and is a file, don't cache at all
68     elif not os.path.isdir(scannerdir):
69         return None
70     return scannerdir
71
72
73 class CacheStore(object):
74
75     def __init__(self):
76         try:
77             self._directory = _get_cachedir()
78         except OSError as e:
79             if e.errno != errno.EPERM:
80                 raise
81             self._directory = None
82
83         self._check_cache_version()
84
85     def _check_cache_version(self):
86         if self._directory is None:
87             return
88
89         current_hash = _get_versionhash()
90         version = os.path.join(self._directory, _CACHE_VERSION_FILENAME)
91         try:
92             cache_hash = open(version).read()
93         except IOError as e:
94             # File does not exist
95             if e.errno == errno.ENOENT:
96                 cache_hash = 0
97             else:
98                 raise
99
100         if current_hash == cache_hash:
101             return
102
103         self._clean()
104         try:
105             fp = open(version, 'w')
106         except IOError as e:
107             # Permission denied
108             if e.errno == errno.EACCES:
109                 return
110             else:
111                 raise
112
113         fp.write(current_hash)
114
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:
120             return
121         hexdigest = hashlib.sha1(filename).hexdigest()
122         return os.path.join(self._directory, hexdigest)
123
124     def _cache_is_valid(self, store_filename, filename):
125         return (os.stat(store_filename).st_mtime >=
126                 os.stat(filename).st_mtime)
127
128     def _remove_filename(self, filename):
129         try:
130             os.unlink(filename)
131         except IOError as e:
132             # Permission denied
133             if e.errno == errno.EACCES:
134                 return
135             else:
136                 raise
137         except OSError as e:
138             # File does not exist
139             if e.errno == errno.ENOENT:
140                 return
141             else:
142                 raise
143
144     def _clean(self):
145         for filename in os.listdir(self._directory):
146             if filename == _CACHE_VERSION_FILENAME:
147                 continue
148             self._remove_filename(os.path.join(self._directory, filename))
149
150     def store(self, filename, data):
151         store_filename = self._get_filename(filename)
152         if store_filename is None:
153             return
154
155         if (os.path.exists(store_filename) and self._cache_is_valid(store_filename, filename)):
156             return None
157
158         tmp_fd, tmp_filename = tempfile.mkstemp(prefix='g-ir-scanner-cache-')
159         try:
160             cPickle.dump(data, os.fdopen(tmp_fd, 'w'))
161         except IOError as e:
162             # No space left on device
163             if e.errno == errno.ENOSPC:
164                 self._remove_filename(tmp_filename)
165                 return
166             else:
167                 raise
168
169         try:
170             shutil.move(tmp_filename, store_filename)
171         except IOError as e:
172             # Permission denied
173             if e.errno == errno.EACCES:
174                 self._remove_filename(tmp_filename)
175             else:
176                 raise
177
178     def load(self, filename):
179         store_filename = self._get_filename(filename)
180         if store_filename is None:
181             return
182         try:
183             fd = open(store_filename)
184         except IOError as e:
185             if e.errno == errno.ENOENT:
186                 return None
187             else:
188                 raise
189         if not self._cache_is_valid(store_filename, filename):
190             return None
191         try:
192             data = cPickle.load(fd)
193         except (AttributeError, EOFError, ValueError, cPickle.BadPickleGet):
194             # Broken cache entry, remove it
195             self._remove_filename(store_filename)
196             data = None
197         return data