From: Michael Tremer Date: Tue, 22 Feb 2011 18:23:13 +0000 (+0100) Subject: Fetch packages from cache if available. X-Git-Tag: 0.9.3~137 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7da489bea7bb85641e62b203cdf21e7d8ae46fb3;p=pakfire.git Fetch packages from cache if available. --- diff --git a/pakfire/index.py b/pakfire/index.py index dd7021c2e..841511e8c 100644 --- a/pakfire/index.py +++ b/pakfire/index.py @@ -185,12 +185,30 @@ class DatabaseIndex(Index): # Reopen the database self.db = database.RemotePackageDatabase(self.pakfire, cache.abspath(cache_filename)) + def __get_from_cache(self, pkg): + """ + Check if package is already in cache and return an instance of + BinaryPackage instead. + """ + if hasattr(self.repo, "cache"): + filename = os.path.join("packages", os.path.basename(pkg.filename)) + + if self.repo.cache.exists(filename): + filename = self.repo.cache.abspath(filename) + + pkg = packages.BinaryPackage(self.pakfire, self.repo, filename) + + return pkg + def get_all_by_name(self, name): c = self.db.cursor() c.execute("SELECT * FROM packages WHERE name = ?", name) for pkg in c: - yield package.DatabasePackage(self.pakfire, self.repo, self.db, pkg) + pkg = package.DatabasePackage(self.pakfire, self.repo, self.db, pkg) + + # Try to get package from cache. + yield self.__get_from_cache(pkg) c.close() @@ -210,7 +228,10 @@ class DatabaseIndex(Index): c.execute("SELECT * FROM packages") for pkg in c: - yield packages.DatabasePackage(self.pakfire, self.repo, self.db, pkg) + pkg = packages.DatabasePackage(self.pakfire, self.repo, self.db, pkg) + + # Try to get package from cache. + yield self.__get_from_cache(pkg) c.close()