-
Notifications
You must be signed in to change notification settings - Fork 5
/
qsub
319 lines (240 loc) · 9.86 KB
/
qsub
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
#!/bin/env python
#qsub for SGE->SLURM -- Matt Maurano
import sys
if sys.version_info[0] < 3:
print("Package requires Python 3")
sys.exit(1)
import os
import re
import subprocess
import argparse
version="1.4"
createTMPDIR = True
def quote_argument(argument):
return '%s' % (
argument
.replace('\\', '\\\\')
# .replace('"', '\\"')
.replace('$', '\\$')
.replace('`', '\\`')
)
parser = argparse.ArgumentParser(prog = "qsub", description = "", allow_abbrev=False)
sge_group = parser.add_argument_group(title="SGE options")
sge_group.add_argument("cmd", type = str, default=None, nargs="?", help = "")
sge_group.add_argument("-hold_jid", action = "store", type = str, help = " [%(default)s]")
sge_group.add_argument("-pe", action = "store", type = str, nargs=2, help= " [%(default)s]")
sge_group.add_argument("-j", action = "store", type = str, choices=['y', 'yes', 'n', 'no'], help = "[%(default)s]")
sge_group.add_argument("-b", action = "store", type = str, choices=['y', 'yes', 'n', 'no'], help = "ignored [%(default)s]")
sge_group.add_argument("-r", action = "store", type = str, choices=['y', 'yes', 'n', 'no'], help = "[%(default)s]")
sge_group.add_argument("-w", action = "store", type = str, choices=['e', 'w', 'n', 'v'], help = "ignored [%(default)s]")
sge_group.add_argument("-o", action = "store", type = str, help = "[%(default)s]")
#sge_group.add_argument("-e", action = "store", type = str, help = "[%(default)s]")
sge_group.add_argument("-t", action = "store", type = str, help = "[%(default)s]")
sge_group.add_argument("-tc", action = "store", type = int, help = "[%(default)s]")
sge_group.add_argument("-p", action = "store", type = int, help = "[%(default)s]")
sge_group.add_argument("-q", action = "store", type = str, help = "[%(default)s] queue name")
sge_group.add_argument("-m", action = "store", type = str, choices=['a', 'b', 'e', 'n'], help = "[%(default)s] email type, s not supported")
sge_group.add_argument("-M", action = "store", type = str, help = "[%(default)s] email address")
sge_group.add_argument("-S", action = "store", type = str, help = "[%(default)s]")
sge_group.add_argument("-N", action = "store", type = str, help = "[%(default)s]")
sge_group.add_argument("-V", action = "store_true", default = False, help = " [%(default)s]")
sge_group.add_argument("-cwd", action = "store_true", default = False, help = "no effect for now [%(default)s]")
sge_group.add_argument("-now", action = "store_true", default = False, help = "[%(default)s]")
sge_group.add_argument("-terse", action = "store_true", default = False, help = "[%(default)s]")
slurm_group = parser.add_argument_group(title="SLURM passthrough options")
slurm_group.add_argument("--mem-per-cpu", action = "store", type = str, help = "[%(default)s]")
slurm_group.add_argument("--mem", action = "store", type = str, help = "[%(default)s]")
slurm_group.add_argument("--time", action = "store", type = str, help = "[%(default)]")
slurm_group.add_argument("--export", action = "store", type = str, help = "[%(default)s]")
slurm_group.add_argument("--share", action = "store_true", default = False, help = "[%(default)s]")
slurm_group.add_argument("--oversubscribe", action = "store_true", default = False, help = "[%(default)s]")
slurm_group.add_argument("--qos", action = "store", type = str, help = "[%(default)s]")
qsub_group = parser.add_argument_group(title="qsub SGE->SLURM options")
qsub_group.add_argument("--verbose", action='store_true', default=False, help = "Verbose mode")
qsub_group.add_argument('--version', action='version', version='%(prog)s ' + version)
#argparse does not set an exit code upon this error
#https://stackoverflow.com/questions/5943249/python-argparse-and-controlling-overriding-the-exit-status-code
try:
args = parser.parse_args()
except argparse.ArgumentError as exc:
print(exc.message, '\n', exc.argument)
sys.exit(2)
if args.verbose:
print("[qsub -> SLURM] " + ' '.join([str(cur) for cur in sys.argv]), file=sys.stderr)
print("[qsub -> SLURM]", args, file=sys.stderr)
sys.stderr.flush()
###Generate sbatch command
theCmd = "sbatch"
###translate SGE options
if args.V:
theCmd += " --export=ALL"
if args.terse:
theCmd += " --parsable"
if args.now:
theCmd += " --immediate"
name = None
if args.N is not None:
name = args.N
else:
if args.cmd is None:
name = "STDIN"
else:
if os.path.isfile(args.cmd):
name = os.path.basename(args.cmd)
else:
name = args.cmd
theCmd += " -J \"" + name + "\""
#stdout
if args.o is not None:
stdoutFilename = args.o
#SGE allows passing a directory whereupon we use the job name as the filename
if os.path.isdir(args.o):
if re.search("\/$", args.o) is None:
stdoutFilename += "/"
stdoutFilename += name
else:
stdoutFilename = name
theCmd += " -o \"" + stdoutFilename + ".o%A"
if args.t is not None:
theCmd += ".%a"
theCmd += "\""
#stderr
if args.j != "y" and args.j != "yes":
theCmd += " -e \"" + stdoutFilename + ".e%A"
if args.t is not None:
theCmd += ".%a"
theCmd += "\""
if args.r == "y" or args.r == "yes":
theCmd += " --requeue"
elif args.r == "n" or args.r == "no":
theCmd += " --no-requeue"
if args.t is not None:
theCmd += " --array=" + args.t
if args.tc is not None:
theCmd += "%" + str(args.tc)
if args.M is not None or args.m is not None:
if args.M is not None and args.m is not None:
theCmd += " --mail-user=" + args.M
if args.m == 'a':
theCmd += " --mail-type=FAIL"
elif args.m == 'b':
theCmd += " --mail-type=BEGIN"
elif args.m == 'e':
theCmd += " --mail-type=END"
elif args.m == 'n':
theCmd += " --mail-type=NONE"
else:
raise Exception("Don't know how to handle -m " + args.m)
else:
raise Exception("Don't know how to handle -m/-M as specified")
if args.p is not None:
theCmd += " --nice=" + str(-1 * args.p)
if args.q is not None:
theCmd += " --partition=" + args.q
def sjobname2id(jobname):
jobids = subprocess.check_output("squeue -o '%F %j' | awk '$2==\"" + jobname + "\" {print $1}' | sort | uniq", shell=True).decode().rstrip()
return(",".join([str(int(x)) for x in jobids.split('\n')]))
if args.hold_jid is not None:
#SLURM only takes ids, so look up any names
hold_jid_list = [x.strip() for x in args.hold_jid.split(',')]
#drop empty element in case of trailing ","
hold_jid_list = [x for x in hold_jid_list if x]
for i in range(len(hold_jid_list)):
job = hold_jid_list[i]
#"" in case of trailing ","
if not job.isdigit():
#better error checking? right now bad name gives ValueError on conv to int.
#BUGBUG can't handle a name corresponding to multiple IDs
hold_jid_list[i] = sjobname2id(job)
if args.verbose:
print("Converted job name " + job + " to id " + hold_jid_list[i], file=sys.stderr)
#NB use afterok even though SGE only blocks dependencies upon a 100 return code
theCmd += " --depend=afterok:" + ":".join(hold_jid_list)
if args.pe is not None:
if args.pe[0] != "threads":
raise Exception("Can't handle pe " + args.pe[0])
if not args.pe[1].isdigit():
raise Exception(args.pe[1] + "is not an int -- SLURM can't handle ranges")
theCmd += " --cpus-per-task=" + args.pe[1]
##SLURM pass-through options
if args.mem_per_cpu is not None:
theCmd += " --mem-per-cpu=" + args.mem_per_cpu
if args.mem is not None:
theCmd += " --mem=" + args.mem
if args.time is not None:
theCmd += " --time=" + args.time
#BUGBUG does this collide with -V?
if args.export is not None:
theCmd += " --export=\"" + args.export + '"'
if args.share is not False:
theCmd += " --share"
#Newer versions of SLURM changed the name of --share
if args.oversubscribe is not False:
theCmd += " --oversubscribe"
if args.qos is not None:
theCmd += " --qos=" + args.qos
###Do job script
theCmd += " <<EOF\n#!/bin/bash\n"
if createTMPDIR:
theCmd += quote_argument("""\n
#Initialize TMPDIR
if [[ $HOSTNAME == isglpdcstorage01 ]]; then
export TMPDIR=/adams/tmp
else
export TMPDIR=/tmp
fi
export TMPDIR=${TMPDIR}/slurm.tmp.${SLURM_JOB_NAME:0:80}.${SLURM_JOB_ID}
if [[ ! -z ${SLURM_ARRAY_TASK_ID} ]]; then
export TMPDIR=${TMPDIR}.${SLURM_ARRAY_TASK_ID}
fi
mkdir -p $TMPDIR""")
theCmd += quote_argument("""\n
#For SGE compatibility
export JOB_ID=${SLURM_JOB_ID}
if [[ -z ${SLURM_CPUS_PER_TASK} ]]; then
export NSLOTS=1
else
export NSLOTS=${SLURM_CPUS_PER_TASK}
fi
if [[ -z ${SLURM_ARRAY_TASK_ID} ]]; then
export SGE_TASK_ID="undefined"
else
export SGE_TASK_ID=${SLURM_ARRAY_TASK_ID}
fi\n\n""")
#Breaks when run in batch cluster job
#use_stdin = not sys.stdin.isatty()
use_stdin = args.cmd is None
if use_stdin:
if args.cmd is not None:
raise Exception("Appear to have command on both command line and stdin (" + sys.stdin.name + ")")
try:
with sys.stdin as source:
for line in source:
theCmd += quote_argument(line) + "\n"
except:
pass
finally:
sys.stdin.close()
else:
if args.cmd is None:
raise Exception
if args.S=="/bin/bash" and re.search(" ", args.cmd) is None and os.path.isfile(args.cmd):
theCmd += "/bin/bash " + args.cmd + "\n"
elif args.S == "/bin/bash":
theCmd += quote_argument(args.cmd) + "\n"
elif args.S is None:
theCmd += quote_argument(args.cmd) + "\n"
else:
theCmd += args.S + " " + quote_argument(args.cmd) + "\n"
if createTMPDIR:
theCmd += quote_argument("""
#Retain exit status of last command to pass on after cleaning up
EXITCODE=$?
if [[ ! -z ${TMPDIR} ]]; then
rm -rf ${TMPDIR}
fi
exit ${EXITCODE}\n""")
theCmd += "\nEOF\n"
if args.verbose:
print("[qsub -> SLURM] " + theCmd, file=sys.stderr)
subprocess.check_call(theCmd, shell=True)