From 48f44d67a8402c74bbc00fbe73dd43ef190907c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20D=2E=20Ak=C4=B1n?= Date: Mon, 19 Mar 2007 11:25:12 +0000 Subject: [PATCH] Use real singletons, remove older code. Be consistent in initializing and finalizing db objects api.py --- pisi/api.py | 23 +++++++++++++---------- pisi/db/installdb.py | 23 ++++------------------- pisi/db/packagedb.py | 14 ++------------ pisi/db/repodb.py | 24 ++++-------------------- pisi/db/sourcedb.py | 19 ++----------------- pisi/oo.py | 10 ++++++++++ 6 files changed, 35 insertions(+), 78 deletions(-) diff --git a/pisi/api.py b/pisi/api.py index cfd0accf..6c4a20df 100644 --- a/pisi/api.py +++ b/pisi/api.py @@ -111,12 +111,12 @@ def init(database = True, write = True, ctx.database = database if database: shelve.init_dbenv(write=write) - ctx.repodb = pisi.db.repodb.init() - ctx.installdb = pisi.db.installdb.init() + ctx.repodb = pisi.db.repodb.RepoDB() + ctx.installdb = pisi.db.installdb.InstallDB() ctx.filesdb = pisi.db.filesdb.FilesDB() ctx.componentdb = pisi.db.componentdb.ComponentDB() - ctx.packagedb = packagedb.init_db() - ctx.sourcedb = pisi.db.sourcedb.init() + ctx.packagedb = pisi.db.packagedb.PackageDB() + ctx.sourcedb = pisi.db.sourcedb.SourceDB() else: ctx.repodb = None ctx.installdb = None @@ -134,9 +134,12 @@ def finalize(): if ctx.log: ctx.loghandler.flush() ctx.log.removeHandler(ctx.loghandler) - - pisi.db.repodb.finalize() - pisi.db.installdb.finalize() + if ctx.repodb != None: + ctx.repodb.close() + ctx.repodb = None + if ctx.installdb != None: + ctx.installdb.close() + ctx.installdb = None if ctx.filesdb != None: ctx.filesdb.close() ctx.filesdb = None @@ -144,10 +147,10 @@ def finalize(): ctx.componentdb.close() ctx.componentdb = None if ctx.packagedb: - packagedb.finalize_db() + ctx.packagedb.close() ctx.packagedb = None - if ctx.sourcedb: - pisi.db.sourcedb.finalize() + if ctx.sourcedb != None: + ctx.sourcedb.close() ctx.sourcedb = None if ctx.dbenv: ctx.dbenv.close() diff --git a/pisi/db/installdb.py b/pisi/db/installdb.py index 08468a68..9cb6c69a 100644 --- a/pisi/db/installdb.py +++ b/pisi/db/installdb.py @@ -24,12 +24,12 @@ _ = __trans.ugettext # PiSi import pisi import pisi.context as ctx +import pisi.oo import pisi.db.lockeddbshelve as dbshelve from pisi.files import Files import pisi.util as util from pisi.util import join_path - class InstallDBError(pisi.Error): pass @@ -70,7 +70,8 @@ class InstallInfo: class InstallDB: - + __metaclass__ = pisi.oo.Singleton + def __init__(self): self.d = dbshelve.LockedDBShelf('install') self.dp = dbshelve.LockedDBShelf('configpending') @@ -198,20 +199,4 @@ class InstallDB: def proc(txn): if self.d.has_key(pkg, txn): self.d.delete(pkg, txn) - self.d.txn_proc(proc, txn) - -db = None - -def init(): - global db - if db: - return db - - db = InstallDB() - return db - -def finalize(): - global db - if db: - db.close() - db = None + self.d.txn_proc(proc, txn) \ No newline at end of file diff --git a/pisi/db/packagedb.py b/pisi/db/packagedb.py index a90ee0e1..f4f952d9 100644 --- a/pisi/db/packagedb.py +++ b/pisi/db/packagedb.py @@ -28,6 +28,7 @@ _ = __trans.ugettext import pisi import pisi.util as util import pisi.context as ctx +import pisi.oo import pisi.db.itembyrepodb as itembyrepodb @@ -42,6 +43,7 @@ class NotfoundError(pisi.Error): class PackageDB(object): """PackageDB class provides an interface to the package database using shelf objects""" + __metaclass__ = pisi.oo.Singleton def __init__(self): self.d = itembyrepodb.ItemByRepoDB('package') @@ -152,21 +154,9 @@ class PackageDB(object): self.dr.remove_repo(repo, txn=txn) self.d.txn_proc(proc, txn) -pkgdb = None - def remove_tracking_package(name, txn = None): # remove the guy from the tracking databases if pkgdb.has_package(name, itembyrepodb.installed, txn=txn): pkgdb.remove_package(name, itembyrepodb.installed, txn=txn) if pkgdb.has_package(name, itembyrepodb.thirdparty, txn=txn): pkgdb.remove_package(name, itembyrepodb.thirdparty, txn=txn) - -def init_db(): - global pkgdb - pkgdb = PackageDB() - return pkgdb - -def finalize_db(): - global pkgdb - if pkgdb: - pkgdb.close() diff --git a/pisi/db/repodb.py b/pisi/db/repodb.py index dbea2ab9..7354d4fb 100644 --- a/pisi/db/repodb.py +++ b/pisi/db/repodb.py @@ -18,6 +18,7 @@ import pisi import pisi.db.lockeddbshelve as shelve import pisi.context as ctx import pisi.db.packagedb as packagedb +import pisi.oo class Error(pisi.Error): pass @@ -35,7 +36,8 @@ class Repo: class RepoDB(object): """RepoDB maps repo ids to repository information""" - + __metaclass__ = pisi.oo.Singleton + def __init__(self, txn = None): self.d = shelve.LockedDBShelf("repo") def proc(txn): @@ -104,22 +106,4 @@ class RepoDB(object): ctx.packagedb.remove_repo(name, txn=txn) ctx.sourcedb.remove_repo(name, txn=txn) ctx.componentdb.remove_repo(name, txn=txn) - self.d.txn_proc(proc, txn) - -db = None - -def init(): - global db - - if db: - return db - - db = RepoDB() - return db - -def finalize(): - global db - - if db: - db.close() - db = None + self.d.txn_proc(proc, txn) \ No newline at end of file diff --git a/pisi/db/sourcedb.py b/pisi/db/sourcedb.py index e692772b..541aa9b7 100644 --- a/pisi/db/sourcedb.py +++ b/pisi/db/sourcedb.py @@ -27,11 +27,13 @@ _ = __trans.ugettext import pisi.context as ctx import pisi.db.repodb import pisi.db.itembyrepodb as itembyrepodb +import pisi.oo class NotfoundError(pisi.Error): pass class SourceDB(object): + __metaclass__ = pisi.oo.Singleton def __init__(self): self.d = itembyrepodb.ItemByRepoDB('source') @@ -81,7 +83,6 @@ class SourceDB(object): for pkg in spec.packages: self.dpkgtosrc.remove_item_repo(pkg.name, repo, txn) ctx.componentdb.remove_spec(spec.source.partOf, spec.source.name, repo, txn) - self.d.txn_proc(proc, txn) def remove_repo(self, repo, txn = None): @@ -89,19 +90,3 @@ class SourceDB(object): self.d.remove_repo(repo, txn=txn) self.dpkgtosrc.remove_repo(repo, txn=txn) self.d.txn_proc(proc, txn) - -sourcedb = None - -def init(): - global sourcedb - if sourcedb: - return sourcedb - - sourcedb = SourceDB() - return sourcedb - -def finalize(): - global sourcedb - if sourcedb: - sourcedb.close() - sourcedb = None diff --git a/pisi/oo.py b/pisi/oo.py index 5b893f24..4605c9df 100644 --- a/pisi/oo.py +++ b/pisi/oo.py @@ -37,3 +37,13 @@ class Struct: def __init__(self, **entries): self.__dict__.update(entries) + +class Singleton(type): + def __init__(cls, name, bases, dic): + super(Singleton, cls).__init__(name, bases, dic) + cls.instance = None + + def __call__(cls, *args, **kwargs): + if not cls.instance: + cls.instance = super(Singleton, cls).__call__(*args, **kwargs) + return cls.instance