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

Last change on this file since 8750 was 8750, checked in by uli, 12 years ago

Add support for importing CSV files automatically into an existing instance.

File size: 3.9 KB
Line 
1"""Helpers when inside an instance context.
2"""
3import os
4import sys
5import transaction
6from zope.app.appsetup import database, config
7from zope.app.publication.zopepublication import ZopePublication
8from waeup.kofa.app import University
9
10#: The name under which any artificially created University instance
11#: will be put into the root folder
12APP_NAME = 'stress_app'
13
14IMPORTERS = {
15    'faculties.csv': 'facultyprocessor',
16    'departments.csv': 'departmentprocessor',
17    'courses.csv': 'courseprocessor',
18    'certificates.csv': 'certificateprocessor',
19    'certificatecourses.csv': 'certificatecourseprocessor',
20    'applicantcontainers.csv': 'applicants container processor',
21    'applicants.csv': 'applicantprocessor',
22    }
23
24def configure(instance_path=''):
25    config_file = os.path.join(
26        instance_path, 'parts', 'etc', 'site.zcml')
27    print "WAEUP.STRESS: configuring ZODB from %s" % (
28        config_file,)
29    context = config(config_file)
30    return context
31
32def open_zodb(instance_path=''):
33    """Open a ZODB and configure it using a site.zcml file.
34
35    If `instance_path` is not given, we assume to be in the root of
36    some Zope instance and compute relative paths to
37    'parts/etc/site.zcml' and 'var/filestorage/Data.fs'.
38
39    If `instance_path` is given, these paths are os.path.joined with
40    it.
41
42    Returns a tuple containing a database object, an open connection
43    to this DB and the root folder.
44    """
45    print "WAEUP.STRESS: opening ZODB"
46    zodb_file = os.path.join(
47        instance_path, 'var', 'filestorage', 'Data.fs')
48    db = database(zodb_file)
49    conn = db.open()
50    root = conn.root()
51    root_folder = root.get(ZopePublication.root_name, None)
52    return db, conn, root_folder
53
54def close_zodb(db):
55    print "WAEUP.STRESS: closing ZODB"
56    transaction.commit() # make sure changes are saved
57    db.close()
58    return
59
60def zodb(func):
61    """A decorator that does a ZODB setup and calls the decorated
62    function with the ZODB root folder as first argument.
63    """
64    def wrapped(*args, **kw):
65        instance_path = kw.get('instance_path', '')
66        db, conn, root_folder = open_zodb(instance_path)
67        try:
68            func(root_folder, *args, **kw)
69        finally:
70            close_zodb(db)
71    return wrapped
72
73@zodb
74def create_app(root_folder):
75    """Create a University instance named 'stress_app' in ZODB.
76
77    The `root_folder` arg is injected by the decorator and represents
78    the root folder of an opened, writable ZODB.
79    """
80    root_folder[APP_NAME] = University()
81    print "WAEUP.STRESS.SCRIPTS: created '%s' in root folder." % APP_NAME
82    return
83
84@zodb
85def import_files(root_folder, *args):
86    from zope.component.hooks import setSite
87    from zope.component import getUtility
88    from waeup.kofa.interfaces import IBatchProcessor
89    setSite(root_folder[APP_NAME])
90    for path in args:
91        if not os.path.isfile(path):
92            print "WAEUP.STRESS.SCRIPTS: no such file: %s" % path
93            continue
94        basename = os.path.basename(path)
95        util_name = IMPORTERS.get(basename, None)
96        if util_name is None:
97            print "WAEUP.STRESS.SCRIPTS: not a valid import filename: %s" % (
98                basename)
99            print "  valid names: %s" % IMPORTERS.keys()
100            continue
101        util = getUtility(IBatchProcessor, name=util_name)
102        for line in open(path, 'rb'):
103            break
104        headerfields = line.strip().split(',')
105        print "WAEUP.STRESS.SCRIPTS: importing %s" % os.path.basename(path)
106        results = util.doImport(path, headerfields)
107        print "WAEUP.STRESS.SCRIPTS: import done."
108    return
109
110def main(cmd, *args, **kw):
111    if cmd == 'install_app':
112        configure()
113        create_app()
114        return
115    elif cmd == 'import_csv':
116        configure()
117        create_app()
118        import_files(*args)
119        return
120
121if __name__ == '__main__':
122    cmd = sys.argv[1]
123    args = []
124    if len(sys.argv) > 2:
125        args = sys.argv[2:]
126    main(cmd, *args)
Note: See TracBrowser for help on using the repository browser.