-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiperunner.py
386 lines (329 loc) · 12.2 KB
/
piperunner.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import subprocess
import os
from os import path
import re
import sys
from itertools import product
from collections import Iterable
class GEJob(object):
default_mail = 'n'
default_mail_address = None
@staticmethod
def has_path(command):
for p in os.getenv("PATH").split(path.pathsep):
if path.exists(path.join(p, command)):
return True
return False
@staticmethod
def iter_or_item2list(val):
if val is None:
return []
elif isinstance(val, Iterable) and not isinstance(val, basestring):
return reduce(lambda a, b: a+b, map(GEJob.iter_or_item2list, val), [])
else:
return [val]
def __init__(self,
command,
args=None,
optionfile=None,
exectime=None,
additional_contexts=None,
ar_id=None,
account_string=None,
binary=None,
binding=None,
checkpoint=None,
ckpt=None,
clear=None,
cwd=None,
prefix_string=None,
delete_contexts=None,
display=None,
deadline=None,
stderr=None,
hard=None,
hold=None,
hold_jid=None,
hold_jid_ad=None,
stdin=None,
join=None,
job_share=None,
jsv=None,
resource=None,
mem=None, # specific option
mail=None,
mail_address=None,
masterq=None,
notify=None,
now=None,
name=None,
stdout=None,
project_name=None,
priority=None,
parallel_env=None,
slot=None, # specific option
pty=None,
queue=None,
reservation=None,
rerun=None,
set_contexts=None,
shell=None,
soft=None,
sync=None,
interpreter=None,
array=None,
max_running=None,
terse=None, # will be ignored
username=None,
var=None,
verbose=None,
verify=None,
allval=None,
validation_level=None,
working_dir=None,
**kwargs):
if not (path.isfile(command) or self.has_path(command)):
raise IOError("Script/Binary not found: {}".format(command))
self.command = command
self.args = [] if args is None else args
self.optionfile = optionfile
self.exectime = exectime
self.additional_contexts = additional_contexts
self.ar_id = ar_id
self.account_string = account_string
self.binary = binary
self.binding = binding
self.checkpoint = checkpoint
self.ckpt = ckpt
self.clear = clear
self.cwd = cwd
self.prefix_string = prefix_string
self.delete_contexts = self.iter_or_item2list(delete_contexts)
self.display = display
self.deadline = deadline
self.stderr = self.iter_or_item2list(stderr)
self.hard = hard
self.hold = hold
self.hold_jid = [] if hold_jid is None else self.iter_or_item2list(hold_jid)
self.hold_jid_ad = [] if hold_jid_ad is None else self.iter_or_item2list(hold_jid_ad)
self.stdin = self.iter_or_item2list(stdin)
self.join = join
self.job_share = job_share
self.jsv = jsv
self.resource = {} if resource is None else resource
if mem:
self.resource["s_vmem"] = mem
self.resource["mem_req"] = mem
self.mail = GEJob.default_mail if mail is None else mail
self.mail_address = self.iter_or_item2list(GEJob.default_mail_address if mail_address is None else mail_address)
self.masterq = self.iter_or_item2list(masterq)
self.notify = notify
self.now = now
self.name = path.basename(self.command) if name is None else name
if re.match("^\d", self.name):
raise ValueError("Invalid job name (cannot start with a digit): {}".format(self.name))
self.stdout = self.iter_or_item2list(stdout)
self.project_name = project_name
self.priority = priority
self.parallel_env = parallel_env if parallel_env else {}
if slot:
self.parallel_env = dict(def_slot=[slot])
self.pty = pty
self.queue = self.iter_or_item2list(queue)
self.reservation = reservation
self.rerun = rerun
self.set_contexts = set_contexts
self.shell = shell
self.soft = soft
self.sync = sync
self.interpreter = self.iter_or_item2list(interpreter)
self.array = array
self.max_running = max_running
self.terse = True # terse will be ignored
self.username = self.iter_or_item2list(username)
self.var = {} if var is None else var
self.verbose = verbose
self.verify = verify
self.allval = allval
self.validation_level = validation_level
self.working_dir = working_dir
self.additionals = kwargs
self.job_id = None
self.next_job = []
def append_hold_jid(self, jid):
self.hold_jid += self.iter_or_item2list(jid)
def append_hold_jid_ad(self, jid):
self.hold_jid_ad += self.iter_or_item2list(jid)
def append_next_job(self, job, as_array=False):
self.next_job.append((job, as_array))
def _build_command(self):
self.commandline = ["qsub"]
simpleargs = (
("-@", "optionfile"),
("-a", "exectime"),
("-ar", "ar_id"),
("-A", "account_string"),
("-c", "checkpoint"),
("-ckpt", "ckpt"),
("-C", "prefix_string"),
("-display", "display"),
("-dl", "deadline"),
("-js", "job_share"),
("-jsv", "jsv"),
("-m", "mail"),
("-N", "name"),
("-P", "project_name"),
("-p", "priority"),
("-t", "array"),
("-tc", "max_running"),
("-w", "validation_level"),
("-wd", "working_dir")
)
for arg, name in simpleargs:
val = getattr(self, name)
if val is not None:
self.commandline += [arg, val]
ynargs = (
("-b", "binary"),
("-j", "join"),
("-now", "now"),
("-pty", "pty"),
("-R", "reservation"),
("-r", "rerun"),
("-shell", "shell"),
("-sync", "sync")
)
for arg, name in ynargs:
val = getattr(self, name)
if val is not None:
if val is True:
self.commandline += [arg, "yes"]
elif val is False:
self.commandline += [arg, "no"]
flagvals = (
("-clear", "clear"),
("-cwd", "cwd"),
("-hard", "hard"),
("-h", "hold"),
("-notify", "notify"),
("-soft", "soft"),
("-terse", "terse"),
("-verify", "verify"),
("-V", "allval")
)
for arg, name in flagvals:
val = getattr(self, name)
if val is True:
self.commandline.append(arg)
commasepargs = (
("-dc", "delete_contexts"),
("-e", "stderr"),
("-hold_jid", "hold_jid"),
("-hold_jid_ad", "hold_jid_ad"),
("-i", "stdin"),
("-M", "mail_address"),
("-masterq", "masterq"),
("-o", "stdout"),
("-q", "queue"),
("-S", "interpreter"),
("-u", "username")
)
for arg, name in commasepargs:
val = getattr(self, name)
if val:
self.commandline += [arg, ','.join(map(str, val))]
kvargs = (
("-sc", "set_contexts"),
("-v", "var"),
("-ac", "additional_contexts"),
("-l", "resource")
)
for arg, name in kvargs:
val = getattr(self, name)
if val:
self.commandline += [arg, ','.join([("{}={}".format(k, v) if v else k) for k, v in val.items()])]
if self.binding:
self.commandline += ["-binding"] + self.binding
if self.parallel_env:
k, v = self.parallel_env.items()[0]
self.commandline += ["-pe", k, ','.join(map(str, v))]
self.commandline.append(self.command)
if self.args:
self.commandline += self.args
self.commandline = map(str, self.commandline)
def submit(self):
if self.job_id is not None:
raise IOError("This job has already been submitted as {}".format(self.job_id))
sys.stdout.write("{}\t-> ".format(self.name))
self._build_command()
stdout = subprocess.check_output(self.commandline)
if '.' in stdout:
job_id, self.array = stdout.rstrip().split('.')
else:
job_id, self.array = stdout.rstrip(), None
self.job_id = int(job_id)
sys.stdout.write("{}{} [ SUBMITTED ]".format(self.job_id, ('.' + self.array) if self.array else ''))
holds = []
holds += self.hold_jid if self.hold_jid else []
holds += self.hold_jid_ad if self.hold_jid_ad else []
print " (waiting {})".format(','.join(map(str, holds))) if holds else ''
for next_job, as_array in self.next_job:
if as_array:
next_job.append_hold_jid_ad(self.job_id)
else:
next_job.append_hold_jid(self.job_id)
next_job.submit()
return self.job_id
class GEArrayJob(GEJob):
INTERPRETER = "python"
ARRAYRUNNER = path.join(path.dirname(path.abspath(__file__)), "arrayrunner.py")
def __init__(self, command, make_combination=False, **kwargs):
super(GEArrayJob, self).__init__(command, **kwargs)
args = tuple(kwargs["arg{}".format(i)] for i in range(1, 100) if "arg{}".format(i) in kwargs)
if make_combination:
args = zip(*product(*args))
else:
args = zip(*zip(*args))
self.cwd = True
self.binary = True
if not self.array:
self.array = "1-{}".format(len(args[0]))
self.var["PATH"] = os.environ.get("PATH", '')
self.var["LD_LIBRARY_PATH"] = os.environ.get("LD_LIBRARY_PATH", '')
arraycommand = [self.ARRAYRUNNER]
for i, arg in enumerate(args):
arraycommand += ["-{}".format(i+1)] + ['"{}"'.format(str(x).replace('"', '\"')) for x in arg]
self.args = arraycommand + ["--", self.command] + ['"{}"'.format(x.replace('"', '\"')) for x in self.args]
self.command = self.INTERPRETER
class GESeriesJob(object):
def __init__(self, jobs, as_array=False):
self.jobs = list(jobs)
self.as_array = as_array
def append_hold_jid(self, jid):
self.jobs[0].append_hold_jid(jid)
def append_hold_jid_ad(self, jid):
self.jobs[0].append_hold_jid_ad(jid)
def submit(self):
for job, next_job in zip(self.jobs, self.jobs[1:]+[None]):
hold_jid = job.submit()
if next_job is not None:
if self.as_array:
next_job.append_hold_jid_ad(hold_jid)
else:
next_job.append_hold_jid(hold_jid)
return hold_jid
class GEParallelJob(object):
def __init__(self, jobs, as_array=False):
self.jobs = jobs
self.as_array = as_array
def append_hold_jid(self, jid):
for job in self.jobs:
job.append_hold_jid(jid)
def append_hold_jid_ad(self, jid):
for job in self.jobs:
job.append_hold_jid_ad(jid)
def submit(self):
return tuple(job.submit() for job in self.jobs)
if __name__ == "__main__":
t = GEArrayJob("cat", args="{1}".split(), arg1=["arrayrunner.py", "piperunner.py", "test.py"])
t.submit()