]> git.evergreen-ils.org Git - working/random.git/blob - contrib/evergreen/eg_batch.py
fixed bug in property reading from eg_data module. more python style tweaks
[working/random.git] / contrib / evergreen / eg_batch.py
1 #!/usr/bin/python
2 import random, traceback, os
3 from constrictor.script import Script, ScriptThread
4 from constrictor.properties import Properties
5 import constrictor.log as log
6 import eg_utils
7 import eg_data
8 import eg_workflow
9 import eg_tasks
10 import osrf.json
11
12 eg_utils.init()
13 props = Properties.get_properties()
14
15
16 class BatchMethodTask(eg_tasks.AbstractMethodTask):
17     def __init__(self, service, method):
18         eg_tasks.AbstractMethodTask.__init__(self, method)
19         self.service = service
20         self.method = method
21
22     def run(self, **kw):
23         log.log_info("Batch call: %s %s" % (self.method, osrf.json.to_json(kw['params'])))
24         return self.run_method(*kw['params'])
25
26
27 class BatchScript(Script):
28
29     def __init__(self):
30         Script.__init__(self)
31         self.calls = []
32         self.loaded = False
33     
34     def load_file(self):
35         
36         batch_file = props.get_property('evergreen.batchAPIFile')
37         log.log_info("loading " + batch_file)
38         file = open(batch_file)
39         dm = eg_data.DataManager()
40
41         for line in file.readlines():
42             line = line[0:len(line)-1] # chomp
43             parts = line.split(None, 2)
44             weight = parts[0]
45             method = parts[1]
46             params = '[]'
47             if len(parts) > 2:
48                 params = "[%s]" % parts[2]
49
50             params = params.replace('AUTHTOKEN', '"%s"' % eg_utils.authtoken())
51
52             trying = True
53             while trying:
54                 try: # get past some encoding oddities in the dict files
55                     params = params.replace('DICT_KEYWORD', '"%s"' % eg_utils.random_phrase(1))
56                     params = params.replace('DICT_SUBJECT', '"%s"' % eg_utils.random_phrase(1))
57                     params = params.replace('DICT_USER', '"%s"' % eg_utils.random_phrase(1))
58                     params = params.replace('DICT_ISBN', '"%s"' % eg_utils.random_phrase(1))
59                     params = params.replace('DICT_KEYWORD', '"%s"' % eg_utils.random_phrase(1))
60                 except:
61                     continue
62                 trying = False
63
64             # TODO: fill in props values for all of these, pre-collecting data as necessary
65             for prop in [
66                     'COPY_BARCODE',
67                     'PATRON_BARCODE',
68                     'CONTAINER_ID',
69                     'CIRC_ID',
70                     'COPY_ID',
71                     'HOLD_ID',
72                     'ORG_ID',
73                     'USER_ID',
74                     'TITLE_ID',
75                     'USER_ADDRESS_ID',
76                     'XACT_ID',
77                     'CALLNUMBER_ID',
78                     'COPY_LOCATION_ID',
79                     'METARECORD_ID',
80                     'ORG_ADDRESS_ID',
81                     'ORG_SHORTNAME' ]:
82
83                 value = 1
84                 prop_name = 'PROP_%s' % prop
85                 if hasattr(eg_data, prop_name):
86                     value = dm.get_thread_data(getattr(eg_data, prop_name))
87
88                 params = params.replace(prop, '"%s"' % value)
89
90             params = osrf.json.to_object(params)
91             for idx in range(0, int(weight)):
92                 self.calls.append({
93                     'service' : '.'.join(method.split('.')[0:2]), 
94                     'method' : method, 
95                     'params' : params
96                 })
97
98         file.close()
99
100     def run(self):
101
102         if not self.loaded:
103             self.load_file()
104
105         for idx in range(0, len(self.calls)):
106             call = self.calls[int(random.random() * len(self.calls))]
107             try:
108                 BatchMethodTask(call['service'], call['method']).start(params=call['params'])
109             except:
110                 log.log_error("Batch Method Error")
111                 traceback.print_exc()
112
113
114
115 ScriptManager.go(BatchScript())