-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSV.py
224 lines (222 loc) · 9.56 KB
/
JSV.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/python
import re, sys, tempfile, datetime
class JSV( object ):
"Control the Job Submission Verification steps"
_jsv_cli_params = ( "a", "ar", "A", "b", "ckpt", "cwd", "C", "display",
"dl", "e", "hard", "h", "hold_jid", "hold_jid_ad", "i",
"inherit", "j", "js", "m", "M", "masterq", "notify",
"now", "N", "noshell", "nostdin", "o", "ot", "P", "p",
"pty", "R", "r", "shell", "sync", "S", "t", "tc",
"terse", "u", "w", "wd" )
_jsv_mod_params = ( "ac", "l_hard", "l_soft", "q_hard", "q_soft", "pe_min",
"pe_max", "pe_name", "binding_strategy", "binding_type",
"binding_amount", "binding_socket", "binding_core",
"binding_step", "binding_exp_n" )
_jsv_add_params = ( "CLIENT", "CONTEXT", "GROUP", "VERSION", "JOB_ID",
"SCRIPT", "CMDARGS", "USER" )
def __init__(self, logging=False) :
self.Log = False
if logging is True :
self.Log = True
self.tempfile = tempfile.NamedTemporaryFile(delete=False)
self.state = "initialized"
self.env = {}
self.param = {}
def jsv_script_log(self, error) :
if self.Log:
error = "{}\n".format(error)
if sys.version_info.major > 2:
error = bytes(error, encoding='utf-8')
self.tempfile.write(error)
def jsv_main( self ) :
self.jsv_script_log("{} started on date {}".format(sys.argv[0], datetime.datetime.now()))
self.jsv_script_log("")
self.jsv_script_log("This file contains logging output from a GE JSV script. Lines beginning")
self.jsv_script_log("with >>> contain the data which was sent by a command line client or")
self.jsv_script_log("sge_qmaster to the JSV script. Lines beginning with <<< contain data")
self.jsv_script_log("which is sent for this JSV script to the client or sge_qmaster")
self.jsv_script_log("")
_jsv_quit = False
try :
while (not _jsv_quit) :
iput = sys.stdin.readline().strip('\n')
if len(iput) == 0:
continue
self.jsv_script_log(">>> {}".format(iput))
jsv_line = re.split('\s*', iput, maxsplit=3)
if jsv_line[0] == "QUIT" :
_jsv_quit = True
elif jsv_line[0] == "PARAM" :
self.jsv_handle_param_command(jsv_line[1], jsv_line[2])
elif jsv_line[0] == "ENV" :
jsv_data = re.split('\s*', jsv_line[2], maxsplits=2)
self.jsv_handle_env_command(jsv_line[1], jsv_data[0], jsv_data[1])
elif jsv_line[0] == "START" :
self.jsv_handle_start_command()
elif jsv_line[0] == "BEGIN" :
self.jsv_handle_begin_command()
elif jsv_line[0] == "SHOW" :
self.jsv_show_params()
self.jsv_show_envs()
else:
self.jsv_send_command("ERROR JSV script got unknown command " + jsv_line[0])
except (EOFError) :
pass
self.jsv_script_log("{} is terminating on {}".format(sys.argv[0], datetime.datetime.now()))
if self.Log:
self.tempfile.close()
def jsv_send_command(self, command):
print(command)
sys.stdout.flush()
self.jsv_script_log("<<< {}".format(command))
def jsv_handle_start_command(self):
if self.state == "initialized":
self.jsv_on_start()
self.jsv_send_command("STARTED")
self.state = "started"
else:
self.jsv_send_command("ERROR JSV script got START but is in state {}".format(self.state))
def jsv_handle_begin_command(self):
if self.state == "started":
self.state = "verifying"
self.jsv_on_verify()
self.jsv_clear_params()
self.jsv_clear_envs()
else:
self.jsv_send_command("ERROR JSV script got BEGIN but is in state {}".format(self.state))
def jsv_handle_param_command(self, param, value) :
self.param[param] = {}
if ',' in value:
values = value.split(',')
for v in values:
if '=' in v:
t = v.split('=')
self.param[param][t[0]] = t[1]
else:
self.param[param][v] = None
else:
if '=' in value:
t = value.split('=')
self.param[param][t[0]] = t[1]
else:
self.param[param][value] = None
def jsv_handle_env_command(self, action, name, data) :
if action == 'DEL':
if name in self.env.keys():
del(self.env[name])
else:
self.env[name] = data
def jsv_on_verify(self):
self.jsv_accept()
pass
def jsv_on_start(self):
pass
def jsv_accept(self, reason):
if self.state == "verifying":
self.jsv_send_command("RESULT STATE ACCEPT {}".format(reason))
self.state = "initialized"
else:
self.jsv_send_command("ERROR JSV script tried to send RESULT but was in state {}".format(self.state))
def jsv_correct(self, reason):
if self.state == "verifying":
self.jsv_send_command("RESULT STATE CORRECT {}".format(reason))
self.state = "initialized"
else:
self.jsv_send_command("ERROR JSV script tried to send RESULT but was in state {}".format(self.state))
def jsv_reject(self, reason):
if self.state == "verifying":
self.jsv_send_command("RESULT STATE REJECT {}".format(reason))
self.state = "initialized"
else:
self.jsv_send_command("ERROR JSV script tried to send RESULT but was in state {}".format(self.state))
def jsv_reject_wait(self, reason):
if self.state == "verifying":
self.jsv_send_command("RESULT STATE REJECT_WAIT {}".format(reason))
self.state = "initialized"
else:
self.jsv_send_command("ERROR JSV script tried to send RESULT but was in state {}".format(self.state))
def jsv_clear_envs(self):
self.env = {}
def jsv_clear_params(self):
self.param = {}
def jsv_is_env(self, var):
return var in self.env.keys()
def jsv_get_env(self, var):
if self.jsv_is_env(var):
return self.env['var']
return None
def jsv_add_env(self, var, val):
if not self.jsv_is_env(var):
self.env[var] = val
self.jsv_send_command('ENV ADD {} {}'.format(var, val))
def jsv_mod_env(self, var, val):
if self.jsv_is_env(var):
self.env[var] = val
self.jsv_send_command('ENV MOD {} {}'.format(var, val))
def jsv_del_env(self, var):
if self.jsv_is_env(var):
del(self.env[var])
self.jsv_send_command('ENV DEL {}'.format(var))
def jsv_show_params(self):
for k, v in self.param:
self.jsv_log_info("got param {}={}".format(k,v))
def jsv_show_envs(self):
for k, v in self.env:
self.jsv_log_info("got env {}={}".format(k,v))
def jsv_log_info(self, message):
self.jsv_send_command("LOG INFO {}".format(message))
def jsv_log_warning(self, message):
self.jsv_send_command("LOG WARNING {}".format(message))
def jsv_log_error(self, message):
self.jsv_send_command("LOG ERROR {}".format(message))
def jsv_send_env(self):
self.jsv_send_command("SEND ENV")
def jsv_is_param(self, param):
return param in self.param.keys()
def jsv_get_param(self, param):
if self.jsv_is_param(param):
if len(list(self.param[param].keys())) == 1 and self.param[param][list(self.param[param].keys())[0]] is None:
return list(self.param[param].keys())[0]
return self.param[param]
return None
def jsv_set_param(self, param, val):
self.param[param] = val
self.jsv_send_command("PARAM {} {}".format(param, val))
def jsv_del_param(self, param):
if self.jsv_is_param(param):
del(self.param[param])
self.jsv_send_command("PARAM {}".format(param))
def jsv_sub_is_param(self, param, var):
if self.jsv_is_param(param):
v = self.jsv_get_param(param)
if type(v) is dict:
return var in v.keys()
return False
def jsv_sub_get_param(self, param, var):
if self.jsv_sub_is_param(param, var):
return self.jsv_get_param(param)[var]
return None
def jsv_sub_add_param(self, param, var, val):
if self.jsv_is_param(param):
self.param[param][var] = val
else:
self.param[param] = {var: val}
args = []
for item in self.param[param].items():
if item[1] is None:
args.append(item[0])
else:
args.append('='.join(item))
args = ','.join(args)
self.jsv_send_command("PARAM {} {}".format(param, args))
def jsv_sub_del_param(self, param, var):
if self.jsv_is_param(param) and self.jsv_sub_is_param(param, var):
del(self.param[param][var])
args = []
for item in self.param[param].items():
if item[1] is None:
args.append(item[0])
else:
args.append('='.join(item))
args = ','.join(args)
self.jsv_send_command("PARAM {} {}".format(param, args))