diff options
Diffstat (limited to '')
-rw-r--r-- | portato/db/__init__.py | 43 | ||||
-rw-r--r-- | portato/db/eix_sql.py | 69 | ||||
-rw-r--r-- | portato/db/hash.py (renamed from portato/db/dict.py) | 4 | ||||
-rw-r--r-- | portato/db/sql.py | 19 |
4 files changed, 122 insertions, 13 deletions
diff --git a/portato/db/__init__.py b/portato/db/__init__.py index da8a81e..9d21d3b 100644 --- a/portato/db/__init__.py +++ b/portato/db/__init__.py @@ -19,34 +19,59 @@ class UnknownDatabaseTypeError (Exception): pass _SESSION = None +_TYPE = None +_DEFAULT = "dict" +_DATABASE = None types = { "sql": (_("SQLite"), _("Uses an SQLite-database to store package information.\nMay take longer to generate at the first time, but has advantages if portato is re-started with an unchanged portage tree. Additionally it allows to use fast SQL expressions for fetching the data.")), - "dict": (_("Hashmap"), _("Uses an in-memory hashmap to store package information.\nHas been used since at least version 0.3.3, but all information has to be regenerated on each startup.")) + "dict": (_("Hashmap"), _("Uses an in-memory hashmap to store package information.\nHas been used since at least version 0.3.3, but all information has to be regenerated on each startup.")), + "eixsql" : (_("eix + SQLite"), _("Similar to SQLite, but now uses the eix database to get the package information.\nThis should be much faster on startup, but requires that your eix database is always up-to-date.")) } -def Database(type): - global _SESSION +def Database(type = None): + global _SESSION, _TYPE, _DATABASE + if type is None: + if _DATABASE is None: + warning("No database type specified! Falling back to default.") + return Database(_DEFAULT) + else: + return _DATABASE + if _SESSION is None: _SESSION = Session("db.cfg", name = "DB") _SESSION.load() + _TYPE = type + if type == "sql": debug("Using SQLDatabase") try: from .sql import SQLDatabase except ImportError: - warning(_("Cannot load SQLDatabase.")) - return Database("dict") + warning(_("Cannot load %s."), "SQLDatabase") + _DATABASE = Database("dict") else: - return SQLDatabase(SectionDict(_SESSION, "SQL")) + _DATABASE = SQLDatabase(SectionDict(_SESSION, type)) elif type == "dict": - debug("Using DictDatabase") - from .dict import DictDatabase - return DictDatabase(SectionDict(_SESSION, "dict")) + debug("Using HashDatabase") + from .hash import HashDatabase + _DATABASE = HashDatabase(SectionDict(_SESSION, type)) + elif type == "eixsql": + debug("Using EixSQLDatabase") + try: + from .eix_sql import EixSQLDatabase + except ImportError: + warning(_("Cannot load %s."), "EixSQLDatabase.") + _DATABASE = Database("sql") + else: + _DATABASE = EixSQLDatabase(SectionDict(_SESSION, type)) + else: error(_("Unknown database type: %s"), type) raise UnknownDatabaseTypeError, type + + return _DATABASE diff --git a/portato/db/eix_sql.py b/portato/db/eix_sql.py new file mode 100644 index 0000000..c2d2292 --- /dev/null +++ b/portato/db/eix_sql.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- +# +# File: portato/db/eix_sql.py +# This file is part of the Portato-Project, a graphical portage-frontend. +# +# Copyright (C) 2006-2009 René 'Necoro' Neumann +# This is free software. You may redistribute copies of it under the terms of +# the GNU General Public License version 2. +# There is NO WARRANTY, to the extent permitted by law. +# +# Written by René 'Necoro' Neumann <necoro@necoro.net> + +from __future__ import absolute_import, with_statement + +try: + import sqlite3 as sql +except ImportError: + from pysqlite2 import dbapi2 as sql + +import os + +from .sql import SQLDatabase +from ..eix import EixReader +from ..helper import debug, warning +from ..backend import system + +class EixSQLDatabase (SQLDatabase): + + CACHE_FILE = "/var/cache/eix" + + def __init__ (self, session): + + self.cache = session.get("cache", self.CACHE_FILE) + if not os.path.exists(self.cache): + warning(_("Cache file '%s' does not exist. Using default instead."), self.cache) + self.cache = self.CACHE_FILE + + debug("Using '%s' as eix cache file.", self.cache) + + session["cache"] = self.cache + + SQLDatabase.__init__(self, session) + + def updated (self): + mtime = os.stat(self.cache).st_mtime + old = self.session.get("mtime", 0) + + self.session["mtime"] = str(mtime) + + return old < mtime + + def generate_cat_expr (self, cat): + # be a noop + return cat + + @SQLDatabase.con + def populate (self, category = None, connection = None): + inst = set(system.find_packages(pkgSet = system.SET_INSTALLED, key = category, with_version = False)) + + def _get(): + with EixReader(self.cache) as eix: + for cat in eix.categories: + if category is None or cat.name == category: + for pkg in cat.packages: + p = "%s/%s" % (cat.name, pkg.name) + yield (cat.name, pkg.name, p in inst, False) + + connection.executemany("INSERT INTO packages (cat, name, inst, disabled) VALUES (?, ?, ?, ?)", _get()) + connection.commit() diff --git a/portato/db/dict.py b/portato/db/hash.py index 279ab97..8cea6f2 100644 --- a/portato/db/dict.py +++ b/portato/db/hash.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# File: portato/db/dict.py +# File: portato/db/hash.py # This file is part of the Portato-Project, a graphical portage-frontend. # # Copyright (C) 2006-2009 René 'Necoro' Neumann @@ -19,7 +19,7 @@ from ..helper import info from ..backend import system from .database import Database, PkgData -class DictDatabase (Database): +class HashDatabase (Database): """An internal database which holds a simple dictionary cat -> [package_list].""" lock = Database.lock diff --git a/portato/db/sql.py b/portato/db/sql.py index db66764..fbc01e6 100644 --- a/portato/db/sql.py +++ b/portato/db/sql.py @@ -166,7 +166,7 @@ class SQLDatabase (Database): def populate (self, category = None, connection = None): def _get(): # get the lists - inst = system.find_packages(pkgSet = system.SET_INSTALLED, key=category, with_version = False) + inst = set(system.find_packages(pkgSet = system.SET_INSTALLED, key=category, with_version = False)) for p in system.find_packages(key = category, with_version = False): cat, pkg = p.split("/") @@ -213,12 +213,26 @@ class SQLDatabase (Database): for cat in l: yield cat["cat"] + def generate_cat_expr (self, cat): + """ + Generates an expression from a category name to match all packages of the category. + + E.g. as SQLDatabase uses regexps internally, return cat/* + + @param cat: the category + @type cat: string + + @returns: expression + """ + + return cat+"/*" + @con def reload (self, cat = None, connection = None): if cat: connection.execute("DELETE FROM packages WHERE cat = ?", (cat,)) connection.commit() - self.populate(cat+"/*", connection = connection) + self.populate(self.generate_cat_expr(cat), connection = connection) else: connection.execute("DELETE FROM packages") connection.commit() @@ -247,3 +261,4 @@ class SQLDatabase (Database): self._restrict = "AND (name LIKE '%%%(restrict)s%%' OR cat LIKE '%(restrict)s%%')" % {"restrict":restrict} restrict = property(get_restrict, set_restrict) + con = staticmethod(con) |