From 804606b721296a3dc6b4d386eaa3336ae772c9a2 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Wed, 28 Jan 2009 12:45:01 +0100 Subject: First 'db' layout --- portato/db/__init__.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 portato/db/__init__.py (limited to 'portato/db/__init__.py') diff --git a/portato/db/__init__.py b/portato/db/__init__.py new file mode 100644 index 0000000..5787c56 --- /dev/null +++ b/portato/db/__init__.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +# +# File: portato/db/__init__.py +# This file is part of the Portato-Project, a graphical portage-frontend. +# +# Copyright (C) 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 -- cgit v1.2.3-54-g00ecf From 816fbaf42407fbbb8466c0d08d64fc11f605e5b6 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Wed, 28 Jan 2009 13:13:56 +0100 Subject: Use the new database class layout --- portato/db/__init__.py | 14 ++++++++++++++ portato/db/database.py | 17 +++++++++++++++++ portato/db/dict.py | 16 +++++----------- portato/db/sql.py | 16 ++++------------ portato/gui/windows/main.py | 3 ++- 5 files changed, 42 insertions(+), 24 deletions(-) (limited to 'portato/db/__init__.py') diff --git a/portato/db/__init__.py b/portato/db/__init__.py index 5787c56..122940a 100644 --- a/portato/db/__init__.py +++ b/portato/db/__init__.py @@ -9,3 +9,17 @@ # There is NO WARRANTY, to the extent permitted by law. # # Written by René 'Necoro' Neumann + +from __future__ import absolute_import + +from ..constants import USE_SQL +from ..helper import debug + +if USE_SQL: + debug("Using SQLDatabase") + from .sql import SQLDatabase + Database = SQLDatabase +else: + debug("Using DictDatabase") + from .dict import DictDatabase + Database = DictDatabase diff --git a/portato/db/database.py b/portato/db/database.py index 7d8e378..941c3a3 100644 --- a/portato/db/database.py +++ b/portato/db/database.py @@ -12,6 +12,9 @@ from __future__ import absolute_import, with_statement +from threading import RLock +from functools import wraps + class PkgData (object): __slots__ = ("cat", "pkg", "inst") @@ -33,6 +36,20 @@ class Database (object): ALL = _("ALL") + def __init__ (self): + self._lock = RLock() + + @staticmethod + def lock (f): + @wraps(f) + def wrapper (self, *args, **kwargs): + with self._lock: + r = f(self, *args, **kwargs) + + return r + + return wrapper + def populate (self, category = None): """Populates the database. diff --git a/portato/db/dict.py b/portato/db/dict.py index 435cdd9..5c5ca49 100644 --- a/portato/db/dict.py +++ b/portato/db/dict.py @@ -14,7 +14,7 @@ from __future__ import absolute_import, with_statement import re from collections import defaultdict -from functools import wraps +from threading import RLock from ..backend import system from .database import Database, PkgData @@ -22,21 +22,15 @@ from .database import Database, PkgData class DictDatabase (Database): """An internal database which holds a simple dictionary cat -> [package_list].""" + lock = Database.lock + def __init__ (self): """Constructor.""" + Database.__init__(self) + self.__initialize() - self._lock = RLock() self.populate() - def lock (f): - @wraps(f) - def wrapper (self, *args, **kwargs): - with self._lock: - r = f(self, *args, **kwargs) - return r - - return wrapper - def __initialize (self): self._db = defaultdict(list) self.inst_cats = set([self.ALL]) diff --git a/portato/db/sql.py b/portato/db/sql.py index c80fd91..e7be91e 100644 --- a/portato/db/sql.py +++ b/portato/db/sql.py @@ -22,8 +22,8 @@ import hashlib import os from functools import wraps -from threading import RLock +from ..constants import SESSION_DIR from ..helper import info, error, debug from ..backend import system from .database import Database, PkgData @@ -31,11 +31,13 @@ from .database import Database, PkgData class SQLDatabase (Database): FORBIDDEN = (".bzr", ".svn", ".git", "CVS", ".hg", "_darcs") + lock = Database.lock def __init__ (self): """Constructor.""" + Database.__init__(self) + self._restrict = "" - self._lock = RLock() pkgdb = os.path.join(SESSION_DIR, "package.db") pkgdb_existed = os.path.exists(pkgdb) @@ -130,16 +132,6 @@ class SQLDatabase (Database): return changed - def lock (f): - @wraps(f) - def wrapper (self, *args, **kwargs): - with self._lock: - r = f(self, *args, **kwargs) - - return r - - return wrapper - def con (f): @wraps(f) def wrapper (*args, **kwargs): diff --git a/portato/gui/windows/main.py b/portato/gui/windows/main.py index 0882c2a..b8d3c17 100644 --- a/portato/gui/windows/main.py +++ b/portato/gui/windows/main.py @@ -26,11 +26,12 @@ from ...backend import flags, system # must be the first to avoid circular deps from ... import get_listener, plugin from ...helper import debug, warning, error, info, unique_array from ...session import Session +from ...db import Database from ...constants import CONFIG_LOCATION, VERSION, APP_ICON, ICON_DIR from ...backend.exceptions import PackageNotFoundException, BlockedException # more GUI stuff -from ..utils import Database, Config, GtkThread, get_color +from ..utils import Config, GtkThread, get_color from ..queue import EmergeQueue from ..session import SESSION_VERSION, SessionException, OldSessionException, NewSessionException from ..wrapper import GtkTree, GtkConsole -- cgit v1.2.3-54-g00ecf From 59792e7297d90cdead2e1c83e4537991b20dd11c Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Wed, 28 Jan 2009 22:27:59 +0100 Subject: Enable sql-db formats --- portato/db/__init__.py | 25 +++++++++++++++++-------- portato/db/dict.py | 3 ++- portato/db/sql.py | 21 ++++++++++++++++----- 3 files changed, 35 insertions(+), 14 deletions(-) (limited to 'portato/db/__init__.py') diff --git a/portato/db/__init__.py b/portato/db/__init__.py index 122940a..40bf9ee 100644 --- a/portato/db/__init__.py +++ b/portato/db/__init__.py @@ -12,14 +12,23 @@ from __future__ import absolute_import +from ..session import Session, SectionDict from ..constants import USE_SQL from ..helper import debug -if USE_SQL: - debug("Using SQLDatabase") - from .sql import SQLDatabase - Database = SQLDatabase -else: - debug("Using DictDatabase") - from .dict import DictDatabase - Database = DictDatabase +_SESSION = None + +def Database(): + global _SESSION + + if _SESSION is None: + _SESSION = Session("db.cfg", name = "DB") + + if USE_SQL: + debug("Using SQLDatabase") + from .sql import SQLDatabase + return SQLDatabase(SectionDict(_SESSION, "SQL")) + else: + debug("Using DictDatabase") + from .dict import DictDatabase + return DictDatabase(SectionDict(_SESSION, "dict")) diff --git a/portato/db/dict.py b/portato/db/dict.py index 5c5ca49..d230821 100644 --- a/portato/db/dict.py +++ b/portato/db/dict.py @@ -24,9 +24,10 @@ class DictDatabase (Database): lock = Database.lock - def __init__ (self): + def __init__ (self, session): """Constructor.""" Database.__init__(self) + self.session = session self.__initialize() self.populate() diff --git a/portato/db/sql.py b/portato/db/sql.py index e7be91e..3cffd88 100644 --- a/portato/db/sql.py +++ b/portato/db/sql.py @@ -30,15 +30,22 @@ from .database import Database, PkgData class SQLDatabase (Database): + FORMAT = "1" FORBIDDEN = (".bzr", ".svn", ".git", "CVS", ".hg", "_darcs") lock = Database.lock - def __init__ (self): + def __init__ (self, session): """Constructor.""" Database.__init__(self) self._restrict = "" + self.session = session + updateFormat = False + if "format" not in session or session["format"] != self.FORMAT: + session["format"] = self.FORMAT + updateFormat = True + pkgdb = os.path.join(SESSION_DIR, "package.db") pkgdb_existed = os.path.exists(pkgdb) @@ -49,18 +56,22 @@ class SQLDatabase (Database): pkg_conn = sql.connect(os.path.join(SESSION_DIR, "package.db")) pkg_conn.row_factory = sql.Row + if pkgdb_existed and updateFormat: + pkg_conn.execute("DROP TABLE packages") + pkg_conn.execute(""" CREATE TABLE IF NOT EXISTS packages ( name TEXT, cat TEXT, - inst INTEGER + inst INTEGER, + disabled INTEGER )""") pkg_conn.commit() self.was_updated = self.updated() - if self.was_updated or not pkgdb_existed: + if self.was_updated or not pkgdb_existed or updateFormat: info(_("Cleaning database...")) pkg_conn.execute("DELETE FROM packages") # empty db at beginning info(_("Populating database...")) @@ -153,9 +164,9 @@ class SQLDatabase (Database): for p in system.find_packages(key = category, with_version = False): cat, pkg = p.split("/") - yield (cat, pkg, p in inst) + yield (cat, pkg, p in inst, False) - connection.executemany("INSERT INTO packages (cat, name, inst) VALUES (?, ?, ?)", _get()) + connection.executemany("INSERT INTO packages (cat, name, inst, disabled) VALUES (?, ?, ?, ?)", _get()) connection.commit() @lock -- cgit v1.2.3-54-g00ecf From e3a7f1a2120f6bd20bdaf53afab5a5de1ae25554 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Mon, 2 Feb 2009 13:10:26 +0100 Subject: Remove the USE_SQL flag - use normal config instead --- portato/constants.py | 3 --- portato/db/__init__.py | 25 +++++++++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) (limited to 'portato/db/__init__.py') diff --git a/portato/constants.py b/portato/constants.py index 79ad0f2..3d7217f 100644 --- a/portato/constants.py +++ b/portato/constants.py @@ -22,8 +22,6 @@ These should be set during the installation. @type HOME: string @var SU_COMMAND: command to execute to "su" @type SU_COMMAND: string -@var USE_SQL: whether to use the sqlite db -@type USE_SQL: boolean @var CONFIG_DIR: The configuration directory. @type CONFIG_DIR: string @@ -58,7 +56,6 @@ APP = "portato" VERSION = "9999" HOME = os.environ["HOME"] SU_COMMAND = "gksu -D 'Portato'" -USE_SQL = True # config CONFIG_DIR = "/etc/portato/" diff --git a/portato/db/__init__.py b/portato/db/__init__.py index 40bf9ee..d77fc0d 100644 --- a/portato/db/__init__.py +++ b/portato/db/__init__.py @@ -13,22 +13,35 @@ from __future__ import absolute_import from ..session import Session, SectionDict -from ..constants import USE_SQL from ..helper import debug _SESSION = None +_TYPE = None + +def _set_type(t): + global _TYPE + _TYPE = t def Database(): - global _SESSION + global _SESSION, _TYPE if _SESSION is None: _SESSION = Session("db.cfg", name = "DB") + _SESSION.add_handler((["type"], _set_type, lambda: _TYPE), default = ["sql"]) + _SESSION.load() - if USE_SQL: + if _TYPE == "sql": debug("Using SQLDatabase") - from .sql import SQLDatabase - return SQLDatabase(SectionDict(_SESSION, "SQL")) - else: + try: + from .sql import SQLDatabase + except ImportError: + debug("Cannot load SQLDatabase.") + _TYPE = "dict" + return Database() + else: + return SQLDatabase(SectionDict(_SESSION, "SQL")) + + elif _TYPE == "dict": debug("Using DictDatabase") from .dict import DictDatabase return DictDatabase(SectionDict(_SESSION, "dict")) -- cgit v1.2.3-54-g00ecf From cffc4dbc7529d761600bcaf5fbc63bdb308b3194 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Mon, 2 Feb 2009 13:24:40 +0100 Subject: Use warning instead of debug, if the sql database could not be loaded --- portato/db/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'portato/db/__init__.py') diff --git a/portato/db/__init__.py b/portato/db/__init__.py index d77fc0d..e0919dd 100644 --- a/portato/db/__init__.py +++ b/portato/db/__init__.py @@ -13,7 +13,7 @@ from __future__ import absolute_import from ..session import Session, SectionDict -from ..helper import debug +from ..helper import debug, warning _SESSION = None _TYPE = None @@ -35,7 +35,7 @@ def Database(): try: from .sql import SQLDatabase except ImportError: - debug("Cannot load SQLDatabase.") + warning(_("Cannot load SQLDatabase.")) _TYPE = "dict" return Database() else: -- cgit v1.2.3-54-g00ecf