diff --git a/pisi/__init__.py b/pisi/__init__.py index 0972ebbb..31634d8b 100644 --- a/pisi/__init__.py +++ b/pisi/__init__.py @@ -62,10 +62,6 @@ def _cleanup(): if filesdb.is_initialized(): filesdb.close() - installdb = pisi.db.installdb.InstallDB() - if installdb.is_initialized(): - installdb.close() - if ctx.build_leftover and os.path.exists(ctx.build_leftover): os.unlink(ctx.build_leftover) diff --git a/pisi/api.py b/pisi/api.py index 8a4ca2ff..ebabf73c 100644 --- a/pisi/api.py +++ b/pisi/api.py @@ -65,8 +65,10 @@ def locked(func): raise pisi.errors.AnotherInstanceError(_("Another instance of PiSi is running. Only one instance is allowed.")) try: + pisi.db.invalidate_caches() return func(*__args,**__kw) finally: + pisi.db.update_caches() lock.close() return wrapper @@ -646,6 +648,7 @@ def add_repo(name, indexuri, at = None): else: repo = pisi.db.repodb.Repo(pisi.uri.URI(indexuri)) repodb.add_repo(name, repo, at = at) + pisi.db.flush_caches() ctx.ui.info(_('Repo %s added to system.') % name) @locked @@ -653,6 +656,7 @@ def remove_repo(name): repodb = pisi.db.repodb.RepoDB() if repodb.has_repo(name): repodb.remove_repo(name) + pisi.db.flush_caches() ctx.ui.info(_('Repo %s removed from system.') % name) else: raise pisi.Error(_('Repository %s does not exist. Cannot remove.') @@ -663,14 +667,13 @@ def update_repos(repos, force=False): pisi.db.historydb.HistoryDB().create_history("repoupdate") for repo in repos: __update_repo(repo, force) - pisi.db.reload() - + pisi.db.flush_caches() @locked def update_repo(repo, force=False): pisi.db.historydb.HistoryDB().create_history("repoupdate") __update_repo(repo, force) - pisi.db.reload() + pisi.db.flush_caches() def __update_repo(repo, force=False): ctx.ui.info(_('* Updating repository: %s') % repo) diff --git a/pisi/db/__init__.py b/pisi/db/__init__.py index b3849d97..3896a3f5 100644 --- a/pisi/db/__init__.py +++ b/pisi/db/__init__.py @@ -10,9 +10,19 @@ # Please read the COPYING file. # -import pisi +def invalidate_caches(): + # Invalidates pisi caches in use and forces to re-fill caches from disk when needed + for db in [packagedb.PackageDB(), sourcedb.SourceDB(), componentdb.ComponentDB(), installdb.InstallDB()]: + db.invalidate() -def reload(): - pisi.db.packagedb.PackageDB().reload() - pisi.db.sourcedb.SourceDB().reload() - pisi.db.componentdb.ComponentDB().reload() +def flush_caches(): + # Invalidate and flush caches to re-generate them when needed + for db in [packagedb.PackageDB(), sourcedb.SourceDB(), componentdb.ComponentDB()]: + db.invalidate() + db.cache_flush() + +def update_caches(): + # Updates ondisk caches + for db in [packagedb.PackageDB(), sourcedb.SourceDB(), componentdb.ComponentDB(), installdb.InstallDB()]: + if db.is_initialized(): + db.cache_save() diff --git a/pisi/db/lazydb.py b/pisi/db/lazydb.py index 16bd0d64..c618753e 100644 --- a/pisi/db/lazydb.py +++ b/pisi/db/lazydb.py @@ -16,10 +16,17 @@ import time import pisi.context as ctx class Singleton(object): + _the_instances = {} def __new__(type): - if not '_the_instance' in type.__dict__: - type._the_instance = object.__new__(type) - return type._the_instance + if not type.__name__ in Singleton._the_instances: + Singleton._the_instances[type.__name__] = object.__new__(type) + return Singleton._the_instances[type.__name__] + + def _instance(self): + return self._the_instances[type(self).__name__] + + def _delete(self): + del self._the_instances[type(self).__name__] class LazyDB(Singleton): def __init__(self, cacheable=False): @@ -32,12 +39,12 @@ class LazyDB(Singleton): def cache_save(self): if os.access("/var/cache/pisi", os.W_OK) and self.cacheable: - cPickle.dump(self.__class__._the_instance.__dict__, + cPickle.dump(self._instance().__dict__, file('/var/cache/pisi/%s.cache' % self.__class__.__name__.lower(), 'wb'), 1) def cache_load(self): if os.path.exists("/var/cache/pisi/%s.cache" % self.__class__.__name__.lower()): - self.__class__._the_instance.__dict__ = cPickle.load(file('/var/cache/pisi/%s.cache' % self.__class__.__name__.lower(), 'rb')) + self._instance().__dict__ = cPickle.load(file('/var/cache/pisi/%s.cache' % self.__class__.__name__.lower(), 'rb')) return True return False @@ -46,17 +53,12 @@ class LazyDB(Singleton): if os.path.exists(cache_file): os.unlink(cache_file) - def reload(self): - self.cache_flush() - self.__init() - - def close(self): - self.cache_save() + def invalidate(self): + self._delete() def __init(self): if not self.cache_load(): self.init() - self.cache_save() def __getattr__(self, attr): if not attr == "__setstate__" and not self.initialized: