source: main/waeup.stress/trunk/src/waeup/stress/run.py @ 11150

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

Reorganize.

File size: 3.3 KB
Line 
1import os
2import subprocess
3import sys
4import waeup.stress
5from optparse import OptionParser
6from ConfigParser import SafeConfigParser
7
8VALID_MODES = ('paster', 'kofactl', 'zeo', 'profile')
9
10def configure_options():
11    usage = 'Usage: %prog <project name> [options]'
12    parser = OptionParser(usage=usage)
13    parser.add_option('-p', '--port', dest='port', type='int',
14                      help='rpc listener port')
15    parser.add_option('-r', '--results', dest='results_dir',
16                      help='results directory to reprocess')
17    parser.add_option('-b', '--bind-addr', dest='bind_addr',
18                      help='rpc bind address', default='localhost')
19    parser.add_option('-d', '--directory', dest='projects_dir',
20                      help='directory containing project folder', default='.')
21    cmd_opts, args = parser.parse_args()
22
23    try:
24        project_name = args[0]
25    except IndexError:
26        sys.stderr.write('\nERROR: no project specified\n\n')
27        sys.stderr.write('%s\n' % usage)
28        sys.stderr.write('Example: multimech-run my_project\n\n')
29        sys.exit(1)
30        pass
31
32    return cmd_opts, args
33
34DEFAULTS = dict(
35    instance='kofa', baseport='8080', server_port='8100',
36    zope_threads='10', cache='20MB', cached_objs='5000',
37    paster_threads='10', use_threadpool='true', buildout='',
38    instance_mode='paster', start_instance='true', host='localhost')
39
40def configure_project(name, path):
41    """Parse project in `path` with name `name`.
42
43    Extracts kofa-related settings and returns them as a dict.
44    """
45    print "CONFIGURE PROJECT: ", name, path
46    cfg_path = os.path.join(path, 'config.cfg')
47    cfg_defaults = dict([('kofa_' + name, value)
48                         for name, value in DEFAULTS.items()])
49    config = SafeConfigParser(cfg_defaults)
50    config.read(cfg_path)
51    params = dict()
52    params.update(DEFAULTS)
53    for name, value in config.items('global'):
54        if not name.startswith('kofa_'):
55            continue
56        name = name.split('_', 1)[-1]
57        print name, value
58        params.update(dict({name: value}))
59    if params['instance_mode'] not in VALID_MODES:
60        print "Warning: invalid instance_mode in config.cfg, picking 'paster'."
61        print "  valid would be: %s" % VALID_MODES
62        params['instance_mode'] = 'paster'
63    params['start_instance'] = config.getboolean(
64        'global', 'kofa_start_instance')
65    return params
66
67def configure():
68    cmd_opts, args = configure_options()
69    project_name = args[0]
70    path = os.path.abspath(os.path.join(cmd_opts.projects_dir, project_name))
71    cfg_opts = configure_project(project_name, path)
72    return path, project_name, cmd_opts, cfg_opts
73
74def main():
75    path, project_name, cmd_opts, cfg_opts = configure()
76    pre_run_path = os.path.join(path, 'pre_run.py')
77    post_run_path = os.path.join(path, 'post_run.py')
78    sys.path.append(path) # make project scripts available for import
79    if os.path.exists(pre_run_path):
80        print "WAEUP.STRESS.RUN: running pre_run %s" % pre_run_path
81        execfile(pre_run_path)
82    # late import as this scripts mangles sys.argv
83    from multimechanize.utilities import run
84    run.main()
85    if os.path.exists(post_run_path):
86        print "WAEUP.STRESS.RUN: running post_run %s" % post_run_path
87        execfile(post_run_path)
88    return
Note: See TracBrowser for help on using the repository browser.