-
Notifications
You must be signed in to change notification settings - Fork 0
/
resubmit_jobs.py
80 lines (65 loc) · 2.26 KB
/
resubmit_jobs.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
import sys, os
import argparse
import glob
import pdb
import json
# Resubmit all the jobs in a given directory or a file
def resubmit_jobs(directory, jobnums = None):
# Collect all sbatch files
sbatch_files = glob.glob('uoicorr/%s/sbatch*' % directory)
for sbf in sbatch_files:
if jobnums is not None:
jobnum = int(sbf.split('.sh')[0].split('sbatch')[1])
if jobnum in jobnums:
os.system('sbatch %s' % sbf)
else:
os.system('sbatch %s' % sbf)
# Change a parameter given by key to value in jobs given by jobnums. If jobnums is none, the change
# will be applied to all jobs in directory. Flag sbatch tells whether we should look for parameter
# key in the job_param file or the sbatch file (for example to change the job clock time, we would
# set sbatch = True)
def change_param(key, value, directory, sbatch = False, jobnums = None):
# Grab all files
if sbatch:
files = glob.glob('uoicorr/%s/sbatch*' % directory)
for file in files:
if jobnums is not None:
jobnum = int(file.split('.sh')[0].split('sbatch')[1])
if jobnum in jobnums:
edit_sbatch(file, key, value)
else:
edit_sbatch(file, key, value)
else:
files = glob.glob('uoicorr/%s/*.json' % directory)
for file in files:
if jobnums is not None:
jobnum = int(file.split('.sh')[0].split('sbatch')[1])
if jobnum in jobnums:
with open(file, 'r+') as f:
# change parameter and add sbatch to list
params = json.load(f)
params[key] = value
with open(file, 'w') as f:
json.dump(params, f)
else:
with open(file, 'r+') as f:
# change parameter and add sbatch to list
params = json.load(f)
params[key] = value
with open(file, 'w') as f:
json.dump(params, f)
# Given an sbatch file, find parameter given by flag and set to value
def edit_sbatch(file, flag, value):
with open(file, 'r') as fobject:
fcontent = fobject.read()
paramstring = '#SBATCH %s' % flag
start_loc = fcontent.find(paramstring)
if start_loc != -1:
end_loc = start_loc + len(paramstring) + 1
endofline = fcontent.find('\n', end_loc)
current_value = fcontent[end_loc:endofline]
fcontent = fcontent.replace(current_value, value)
with open(file, 'w') as fobject:
fobject.write(fcontent)
else:
print('Could not find flag in sbatch file')