source: main/waeup.stress/trunk/src/waeup/stress/scripts.py @ 8681

Last change on this file since 8681 was 8654, checked in by uli, 13 years ago

Put local stresstest stuff into SVN. Yet not too useable!

File size: 2.1 KB
Line 
1import os
2import sys
3import transaction
4from zope.app.appsetup import database, config
5from zope.app.publication.zopepublication import ZopePublication
6from waeup.kofa.app import University
7
8#: The name under which any artificially created University instance
9#: will be put into the root folder
10APP_NAME = 'stress_app'
11
12def open_zodb(instance_path=''):
13    """Open a ZODB and configure it using a site.zcml file.
14
15    If `instance_path` is not given, we assume to be in the root of
16    some Zope instance and compute relative paths to
17    'parts/etc/site.zcml' and 'var/filestorage/Data.fs'.
18
19    If `instance_path` is given, these paths are os.path.joined with
20    it.
21
22    Returns a tuple containing a database object, an open connection
23    to this DB and the root folder.
24    """
25    config_file = os.path.join(
26        instance_path, 'parts', 'etc', 'site.zcml')
27    zodb_file = os.path.join(
28        instance_path, 'var', 'filestorage', 'Data.fs')
29    print "WAEUP.STRESS: configuring ZODB from %s / %s" % (
30        config_file, zodb_file)
31    context = config(config_file)
32    db = database(zodb_file)
33    conn = db.open()
34    root = conn.root()
35    root_folder = root.get(ZopePublication.root_name, None)
36    return db, conn, root_folder
37
38def close_zodb(db):
39    print "WAEUP.STRESS: closing ZODB"
40    transaction.commit() # make sure changes are saved
41    db.close()
42    return
43
44def zodb(func):
45    """A decorator that does a ZODB setup and calls the decorated
46    function with the ZODB root folder as first argument.
47    """
48    def wrapped(*args, **kw):
49        instance_path = kw.get('instance_path', '')
50        db, conn, root_folder = open_zodb(instance_path)
51        try:
52            func(root_folder, *args, **kw)
53        finally:
54            close_zodb(db)
55    return wrapped
56
57@zodb
58def create_app(root_folder):
59    """Create a University instance named 'stress_app' in ZODB.
60
61    The `root_folder` arg is injected by the decorator and represents
62    the root folder of an opened, writable ZODB.
63    """
64    print [x for x in root_folder]
65    root_folder['stress_app'] = University()
66    print "WAEUP.STRESS.SCRIPTS: created 'stress_app' in root folder."
67    return
68
69create_app()
Note: See TracBrowser for help on using the repository browser.