Use real singletons, remove older code. Be consistent in initializing and finalizing db objects api.py

This commit is contained in:
Mehmet D. Akın
2007-03-19 11:25:12 +00:00
parent 4b55007379
commit 48f44d67a8
6 changed files with 35 additions and 78 deletions
+13 -10
View File
@@ -111,12 +111,12 @@ def init(database = True, write = True,
ctx.database = database ctx.database = database
if database: if database:
shelve.init_dbenv(write=write) shelve.init_dbenv(write=write)
ctx.repodb = pisi.db.repodb.init() ctx.repodb = pisi.db.repodb.RepoDB()
ctx.installdb = pisi.db.installdb.init() ctx.installdb = pisi.db.installdb.InstallDB()
ctx.filesdb = pisi.db.filesdb.FilesDB() ctx.filesdb = pisi.db.filesdb.FilesDB()
ctx.componentdb = pisi.db.componentdb.ComponentDB() ctx.componentdb = pisi.db.componentdb.ComponentDB()
ctx.packagedb = packagedb.init_db() ctx.packagedb = pisi.db.packagedb.PackageDB()
ctx.sourcedb = pisi.db.sourcedb.init() ctx.sourcedb = pisi.db.sourcedb.SourceDB()
else: else:
ctx.repodb = None ctx.repodb = None
ctx.installdb = None ctx.installdb = None
@@ -134,9 +134,12 @@ def finalize():
if ctx.log: if ctx.log:
ctx.loghandler.flush() ctx.loghandler.flush()
ctx.log.removeHandler(ctx.loghandler) ctx.log.removeHandler(ctx.loghandler)
if ctx.repodb != None:
pisi.db.repodb.finalize() ctx.repodb.close()
pisi.db.installdb.finalize() ctx.repodb = None
if ctx.installdb != None:
ctx.installdb.close()
ctx.installdb = None
if ctx.filesdb != None: if ctx.filesdb != None:
ctx.filesdb.close() ctx.filesdb.close()
ctx.filesdb = None ctx.filesdb = None
@@ -144,10 +147,10 @@ def finalize():
ctx.componentdb.close() ctx.componentdb.close()
ctx.componentdb = None ctx.componentdb = None
if ctx.packagedb: if ctx.packagedb:
packagedb.finalize_db() ctx.packagedb.close()
ctx.packagedb = None ctx.packagedb = None
if ctx.sourcedb: if ctx.sourcedb != None:
pisi.db.sourcedb.finalize() ctx.sourcedb.close()
ctx.sourcedb = None ctx.sourcedb = None
if ctx.dbenv: if ctx.dbenv:
ctx.dbenv.close() ctx.dbenv.close()
+4 -19
View File
@@ -24,12 +24,12 @@ _ = __trans.ugettext
# PiSi # PiSi
import pisi import pisi
import pisi.context as ctx import pisi.context as ctx
import pisi.oo
import pisi.db.lockeddbshelve as dbshelve import pisi.db.lockeddbshelve as dbshelve
from pisi.files import Files from pisi.files import Files
import pisi.util as util import pisi.util as util
from pisi.util import join_path from pisi.util import join_path
class InstallDBError(pisi.Error): class InstallDBError(pisi.Error):
pass pass
@@ -70,7 +70,8 @@ class InstallInfo:
class InstallDB: class InstallDB:
__metaclass__ = pisi.oo.Singleton
def __init__(self): def __init__(self):
self.d = dbshelve.LockedDBShelf('install') self.d = dbshelve.LockedDBShelf('install')
self.dp = dbshelve.LockedDBShelf('configpending') self.dp = dbshelve.LockedDBShelf('configpending')
@@ -198,20 +199,4 @@ class InstallDB:
def proc(txn): def proc(txn):
if self.d.has_key(pkg, txn): if self.d.has_key(pkg, txn):
self.d.delete(pkg, txn) self.d.delete(pkg, txn)
self.d.txn_proc(proc, 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
+2 -12
View File
@@ -28,6 +28,7 @@ _ = __trans.ugettext
import pisi import pisi
import pisi.util as util import pisi.util as util
import pisi.context as ctx import pisi.context as ctx
import pisi.oo
import pisi.db.itembyrepodb as itembyrepodb import pisi.db.itembyrepodb as itembyrepodb
@@ -42,6 +43,7 @@ class NotfoundError(pisi.Error):
class PackageDB(object): class PackageDB(object):
"""PackageDB class provides an interface to the package database """PackageDB class provides an interface to the package database
using shelf objects""" using shelf objects"""
__metaclass__ = pisi.oo.Singleton
def __init__(self): def __init__(self):
self.d = itembyrepodb.ItemByRepoDB('package') self.d = itembyrepodb.ItemByRepoDB('package')
@@ -152,21 +154,9 @@ class PackageDB(object):
self.dr.remove_repo(repo, txn=txn) self.dr.remove_repo(repo, txn=txn)
self.d.txn_proc(proc, txn) self.d.txn_proc(proc, txn)
pkgdb = None
def remove_tracking_package(name, txn = None): def remove_tracking_package(name, txn = None):
# remove the guy from the tracking databases # remove the guy from the tracking databases
if pkgdb.has_package(name, itembyrepodb.installed, txn=txn): if pkgdb.has_package(name, itembyrepodb.installed, txn=txn):
pkgdb.remove_package(name, itembyrepodb.installed, txn=txn) pkgdb.remove_package(name, itembyrepodb.installed, txn=txn)
if pkgdb.has_package(name, itembyrepodb.thirdparty, txn=txn): if pkgdb.has_package(name, itembyrepodb.thirdparty, txn=txn):
pkgdb.remove_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()
+4 -20
View File
@@ -18,6 +18,7 @@ import pisi
import pisi.db.lockeddbshelve as shelve import pisi.db.lockeddbshelve as shelve
import pisi.context as ctx import pisi.context as ctx
import pisi.db.packagedb as packagedb import pisi.db.packagedb as packagedb
import pisi.oo
class Error(pisi.Error): class Error(pisi.Error):
pass pass
@@ -35,7 +36,8 @@ class Repo:
class RepoDB(object): class RepoDB(object):
"""RepoDB maps repo ids to repository information""" """RepoDB maps repo ids to repository information"""
__metaclass__ = pisi.oo.Singleton
def __init__(self, txn = None): def __init__(self, txn = None):
self.d = shelve.LockedDBShelf("repo") self.d = shelve.LockedDBShelf("repo")
def proc(txn): def proc(txn):
@@ -104,22 +106,4 @@ class RepoDB(object):
ctx.packagedb.remove_repo(name, txn=txn) ctx.packagedb.remove_repo(name, txn=txn)
ctx.sourcedb.remove_repo(name, txn=txn) ctx.sourcedb.remove_repo(name, txn=txn)
ctx.componentdb.remove_repo(name, txn=txn) ctx.componentdb.remove_repo(name, txn=txn)
self.d.txn_proc(proc, 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
+2 -17
View File
@@ -27,11 +27,13 @@ _ = __trans.ugettext
import pisi.context as ctx import pisi.context as ctx
import pisi.db.repodb import pisi.db.repodb
import pisi.db.itembyrepodb as itembyrepodb import pisi.db.itembyrepodb as itembyrepodb
import pisi.oo
class NotfoundError(pisi.Error): class NotfoundError(pisi.Error):
pass pass
class SourceDB(object): class SourceDB(object):
__metaclass__ = pisi.oo.Singleton
def __init__(self): def __init__(self):
self.d = itembyrepodb.ItemByRepoDB('source') self.d = itembyrepodb.ItemByRepoDB('source')
@@ -81,7 +83,6 @@ class SourceDB(object):
for pkg in spec.packages: for pkg in spec.packages:
self.dpkgtosrc.remove_item_repo(pkg.name, repo, txn) self.dpkgtosrc.remove_item_repo(pkg.name, repo, txn)
ctx.componentdb.remove_spec(spec.source.partOf, spec.source.name, repo, txn) ctx.componentdb.remove_spec(spec.source.partOf, spec.source.name, repo, txn)
self.d.txn_proc(proc, txn) self.d.txn_proc(proc, txn)
def remove_repo(self, repo, txn = None): def remove_repo(self, repo, txn = None):
@@ -89,19 +90,3 @@ class SourceDB(object):
self.d.remove_repo(repo, txn=txn) self.d.remove_repo(repo, txn=txn)
self.dpkgtosrc.remove_repo(repo, txn=txn) self.dpkgtosrc.remove_repo(repo, txn=txn)
self.d.txn_proc(proc, 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
+10
View File
@@ -37,3 +37,13 @@ class Struct:
def __init__(self, **entries): def __init__(self, **entries):
self.__dict__.update(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