]> git.evergreen-ils.org Git - working/random.git/blob - constrictor.py
added configurable dictionary file option, fixed comment typo
[working/random.git] / constrictor.py
1 #!/usr/bin/python
2 # -----------------------------------------------------------------------
3 # Copyright (C) 2007-2008  King County Library System
4 # Bill Erickson <erickson@esilibrary.com>
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 3
9 # of the License, or (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 # -----------------------------------------------------------------------
16
17 import sys, getopt, os, errno
18 from constrictor.properties import Properties
19 from constrictor.db import DBConnection
20 from constrictor.controller import DroneController
21 from constrictor.script import ScriptThread, ScriptManager
22 from constrictor.log import *
23 from constrictor.utils import loadProps, saveProps, initDirs, initDB, openScript, PROPS_FILENAME
24
25 props = None
26 droneController = None
27
28 def usage():
29     print '''
30 python %s [options]
31
32     By default, all options are read from the properties file constrictor.properties.
33     Arguments passed via the command line will override any properties
34     loaded from the properties file
35
36     Options:
37         -h show this help message
38         -s test script to run (property constrictor.script)
39         -t number of threads to launch (property constrictor.numThreads)
40         -i number of test iterations per thread (property constrictor.numIterations)
41         -d database file (property constrictor.dbFile)
42         -p port to listen for controller connections on
43         -l listen address for incoming controller connections
44 ''' % sys.argv[0]
45     sys.exit(0)
46
47
48
49 def onThreadsComplete(scriptManager):
50     global droneController
51     summary = ScriptThread.currentScriptThread().dbConnection.createTaskSummary()
52     droneController.sendResult(type='task_summary', **summary)
53
54
55 def readArgv():
56     # see if we have any command-line args that override the properties file
57     ops, args = getopt.getopt(sys.argv[1:], 's:t:i:d:p:l:h')
58     options = dict( (k,v) for k,v in ops )
59
60     if options.has_key('-h'):
61         usage()
62     if options.has_key('-s'):
63         props.setProperty('constrictor.script', options['-s'])
64     if options.has_key('-t'):
65         props.setProperty('constrictor.numThreads', options['-t'])
66     if options.has_key('-i'):
67         props.setProperty('constrictor.numIterations', options['-i'])
68     if options.has_key('-d'):
69         props.setProperty('constrictor.dbFile', options['-d'])
70     if options.has_key('-p'):
71         props.setProperty('constrictor.port', options['-p'])
72     if options.has_key('-l'):
73         props.setProperty('constrictor.listenAddress', options['-l'])
74
75
76
77 def onThreadsComplete(scriptManager):
78     global droneController
79     summary = ScriptThread.currentScriptThread().dbConnection.createTaskSummary()
80     droneController.sendResult(type='task_summary', **summary)
81
82 loadProps(PROPS_FILENAME)
83 props = Properties.getProperties()
84 readArgv()
85 initDirs()
86 initLog()
87 scriptDirs = props.getProperty('constrictor.scriptDirs').split(',')
88
89
90
91 if props.getProperty('constrictor.listen') == 'true':
92     
93     ''' This is the main controller listen loop.  Here, we
94         accept commands from the controller module, perform
95         the action, then go back to listening '''
96
97     droneController = DroneController(
98         props.getProperty('constrictor.address'), 
99         int(props.getProperty('constrictor.port')))
100
101     ScriptManager.setOnThreadsComplete(onThreadsComplete)
102
103     while True:
104         try:
105             command = droneController.recv()['command']
106     
107             if command['action'] == 'setprop':
108                 prop = str(command['prop'])
109                 val = str(command['val'])
110                 logInfo('setting property %s %s' % (prop, val))
111                 props.setProperty(prop, val)
112                 continue
113
114             if command['action'] == 'saveprops':
115                 logInfo("saving properties back to file")
116                 saveProps()
117                 continue
118     
119             if command['action'] == 'run':
120                 ScriptThread.resetThreadSeed()
121                 script = props.getProperty('constrictor.script')
122                 logInfo('running ' + script)
123                 f = openScript(scriptDirs, script)
124                 if f:
125                     initDB()
126                     try:
127                         exec(f)
128                     except Exception, e:
129                         logError("script execution failed: %s" % str(e))
130                         droneController.sendError(text=str(e))
131                     f.close()
132                 continue
133
134         except KeyboardInterrupt:
135             droneController.shutdown()
136
137 else:
138     initDB()
139     script = props.getProperty('constrictor.script') # execute the requested script
140     ScriptThread.resetThreadSeed()
141     f = openScript(scriptDirs, script)
142     if f:
143         exec(f)
144         f.close()
145
146