Use real singletons, remove older code. Be consistent in initializing and finalizing db objects api.py
This commit is contained in:
+13
-10
@@ -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()
|
||||
|
||||
+4
-19
@@ -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)
|
||||
+2
-12
@@ -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()
|
||||
|
||||
+4
-20
@@ -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)
|
||||
+2
-17
@@ -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
|
||||
|
||||
+10
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user