diff --git a/roundup/instance.py b/roundup/instance.py
index 127590b2dc8ce72dfa4c6842a51ba7def64480c7..f2f100c1d9795432beb10108698f35f30380a3c1 100644 (file)
--- a/roundup/instance.py
+++ b/roundup/instance.py
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#
-"""Tracker handling (open tracker).
+"""Top-level tracker interface.
-Backwards compatibility for the old-style "imported" trackers.
+Open a tracker with:
+
+ >>> from roundup import instance
+ >>> db = instance.open('path to tracker home')
+
+The "db" handle you get back is the tracker's hyperdb which has the interface
+described in `roundup.hyperdb.Database`.
"""
__docformat__ = 'restructuredtext'
"""
self.tracker_home = tracker_home
self.optimize = optimize
+ # if set, call schema_hook after executing schema.py will get
+ # same variables (in particular db) as schema.py main purpose is
+ # for regression tests
+ self.schema_hook = None
self.config = configuration.CoreConfig(tracker_home)
self.actions = {}
self.cgi_actions = {}
sys.path.remove(libdir)
def get_backend_name(self):
- o = __builtins__['open']
- f = o(os.path.join(self.tracker_home, 'db', 'backend_name'))
+ f = file(os.path.join(self.config.DATABASE, 'backend_name'))
name = f.readline().strip()
f.close()
return name
'db': backend.Database(self.config, name)
}
+ libdir = os.path.join(self.tracker_home, 'lib')
+ if os.path.isdir(libdir):
+ sys.path.insert(1, libdir)
if self.optimize:
# execute preloaded schema object
exec(self.schema, vars)
+ if callable (self.schema_hook):
+ self.schema_hook(**vars)
# use preloaded detectors
detectors = self.detectors
else:
- libdir = os.path.join(self.tracker_home, 'lib')
- if os.path.isdir(libdir):
- sys.path.insert(1, libdir)
# execute the schema file
self._load_python('schema.py', vars)
+ if callable (self.schema_hook):
+ self.schema_hook(**vars)
# reload extensions and detectors
for extension in self.get_extensions('extensions'):
extension(self)
detectors = self.get_extensions('detectors')
- if libdir in sys.path:
- sys.path.remove(libdir)
+ if libdir in sys.path:
+ sys.path.remove(libdir)
db = vars['db']
# apply the detectors
for detector in detectors:
# or this is the first time the database is opened,
# do database upgrade checks
if not (self.optimize and self.db_open):
+ # As a consistency check, ensure that every link property is
+ # pointing at a defined class. Otherwise, the schema is
+ # internally inconsistent. This is an important safety
+ # measure as it protects against an accidental schema change
+ # dropping a table while there are still links to the table;
+ # once the table has been dropped, there is no way to get it
+ # back, so it is important to drop it only if we are as sure
+ # as possible that it is no longer needed.
+ classes = db.getclasses()
+ for classname in classes:
+ cl = db.getclass(classname)
+ for propname, prop in cl.getprops().iteritems():
+ if not isinstance(prop, (hyperdb.Link,
+ hyperdb.Multilink)):
+ continue
+ linkto = prop.classname
+ if linkto not in classes:
+ raise ValueError, \
+ ("property %s.%s links to non-existent class %s"
+ % (classname, propname, linkto))
+
db.post_init()
self.db_open = 1
return db