import os import sys import transaction from zope.app.appsetup import database, config from zope.app.publication.zopepublication import ZopePublication from waeup.kofa.app import University #: The name under which any artificially created University instance #: will be put into the root folder APP_NAME = 'stress_app' def open_zodb(instance_path=''): """Open a ZODB and configure it using a site.zcml file. If `instance_path` is not given, we assume to be in the root of some Zope instance and compute relative paths to 'parts/etc/site.zcml' and 'var/filestorage/Data.fs'. If `instance_path` is given, these paths are os.path.joined with it. Returns a tuple containing a database object, an open connection to this DB and the root folder. """ config_file = os.path.join( instance_path, 'parts', 'etc', 'site.zcml') zodb_file = os.path.join( instance_path, 'var', 'filestorage', 'Data.fs') print "WAEUP.STRESS: configuring ZODB from %s / %s" % ( config_file, zodb_file) context = config(config_file) db = database(zodb_file) conn = db.open() root = conn.root() root_folder = root.get(ZopePublication.root_name, None) return db, conn, root_folder def close_zodb(db): print "WAEUP.STRESS: closing ZODB" transaction.commit() # make sure changes are saved db.close() return def zodb(func): """A decorator that does a ZODB setup and calls the decorated function with the ZODB root folder as first argument. """ def wrapped(*args, **kw): instance_path = kw.get('instance_path', '') db, conn, root_folder = open_zodb(instance_path) try: func(root_folder, *args, **kw) finally: close_zodb(db) return wrapped @zodb def create_app(root_folder): """Create a University instance named 'stress_app' in ZODB. The `root_folder` arg is injected by the decorator and represents the root folder of an opened, writable ZODB. """ print [x for x in root_folder] root_folder['stress_app'] = University() print "WAEUP.STRESS.SCRIPTS: created 'stress_app' in root folder." return create_app()