summaryrefslogtreecommitdiff
path: root/giscanner/cachestore.py
diff options
context:
space:
mode:
authorJohan Dahlin <jdahlin@async.com.br>2008-10-30 17:12:51 +0000
committerJohan Dahlin <johan@src.gnome.org>2008-10-30 17:12:51 +0000
commit53d089628f98fca0ca7984a95902134a7d7c11b8 (patch)
treeb410c743aed159768cb275a00cf4f36867c8b192 /giscanner/cachestore.py
parent0fcde9ead2e60751268dde5300a7a3e4f13f5c58 (diff)
downloadgobject-introspection-53d089628f98fca0ca7984a95902134a7d7c11b8.tar.gz
Remove arguments from the constructor, move them to separate accessors.
2008-10-30 Johan Dahlin <jdahlin@async.com.br> * giscanner/girparser.py: Remove arguments from the constructor, move them to separate accessors. Add a new parse_tree method which takes an element tree instance. * tools/g-ir-scanner: Update callsite for this * giscanner/Makefile.am: * giscanner/cachestore.py: * giscanner/transformer.py: Cache the include parsing. Saves ~25% time when creating vte (which includes everything up to gtk+). svn path=/trunk/; revision=842
Diffstat (limited to 'giscanner/cachestore.py')
-rw-r--r--giscanner/cachestore.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/giscanner/cachestore.py b/giscanner/cachestore.py
new file mode 100644
index 00000000..ddbbc059
--- /dev/null
+++ b/giscanner/cachestore.py
@@ -0,0 +1,87 @@
+# -*- Mode: Python -*-
+# GObject-Introspection - a framework for introspecting GObject libraries
+# Copyright (C) 2008 Johan Dahlin
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+#
+
+import cPickle
+import hashlib
+import os
+import errno
+
+
+def _get_cachedir():
+ cachedir = os.path.join(os.environ['HOME'], '.cache')
+ if not os.path.exists(cachedir):
+ os.mkdir(cachedir, 0755)
+
+ scannerdir = os.path.join(cachedir, 'g-ir-scanner')
+ if not os.path.exists(scannerdir):
+ os.mkdir(scannerdir, 0755)
+ # If it exists and is a file, don't cache at all
+ elif not os.path.isdir(scannerdir):
+ return None
+ return scannerdir
+
+
+class CacheStore(object):
+
+ def __init__(self):
+ try:
+ self._directory = _get_cachedir()
+ except OSError, e:
+ if e.errno != errno.EPERM:
+ raise
+ self._directory = None
+
+ def _get_filename(self, filename):
+ # If we couldn't create the directory we're probably
+ # on a read only home directory where we just disable
+ # the cache all together.
+ if self._directory is None:
+ return
+ hexdigest = hashlib.sha1(filename).hexdigest()
+ return os.path.join(self._directory, hexdigest)
+
+ def _cache_is_valid(self, store_filename, filename):
+ return (os.stat(store_filename).st_mtime >=
+ os.stat(filename).st_mtime)
+
+ def store(self, filename, data):
+ store_filename = self._get_filename(filename)
+ if store_filename is None:
+ return
+ if (os.path.exists(store_filename) and
+ self._cache_is_valid(store_filename, filename)):
+ return None
+ fd = open(store_filename, 'w')
+ cPickle.dump(data, fd)
+
+ def load(self, filename):
+ store_filename = self._get_filename(filename)
+ if store_filename is None:
+ return
+ try:
+ fd = open(store_filename)
+ except IOError, e:
+ if e.errno == errno.ENOENT:
+ return None
+ raise
+ if not self._cache_is_valid(store_filename, filename):
+ return None
+ data = cPickle.load(fd)
+ return data