-
Notifications
You must be signed in to change notification settings - Fork 0
/
slurm_runner.py
125 lines (91 loc) · 3.87 KB
/
slurm_runner.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
"""Run all jobs locally."""
import os
import shlex
import click
import dtoolcore
from analysis import Analysis
class SlurmRunner(object):
def __init__(self, analysis, base_output_path):
self.analysis = analysis
base_output_path = os.path.abspath(base_output_path)
self.base_output_path = base_output_path
self.scripts_path = os.path.join(base_output_path, "scripts")
dtoolcore.utils.mkdir_parents(self.scripts_path)
self.logs_path = os.path.join(base_output_path, "logs")
self.logs_relpath = os.path.relpath(
self.logs_path,
self.base_output_path
)
dtoolcore.utils.mkdir_parents(self.logs_path)
self.master_script = ""
def construct_single_process_template(self, identifier):
variables = {
"name": "autotest",
"stdout": os.path.join(
self.logs_relpath,
"{}.out".format(identifier)
),
"stderr": os.path.join(
self.logs_relpath,
"{}.err".format(identifier)
),
"input_dataset_uri": self.analysis.input_dataset.uri,
"output_dataset_uri": self.analysis.output_dataset.uri,
"identifier": identifier,
}
slurm_template = self.analysis.config["slurm_run_template"]
return slurm_template.format(**variables)
def process_single_identifier(self, identifier):
script_contents = self.construct_single_process_template(identifier)
script_name = "process_{}.slurm".format(identifier)
script_fpath = os.path.join(self.scripts_path, script_name)
script_relpath = os.path.relpath(script_fpath, self.base_output_path)
with open(script_fpath, "w") as fh:
fh.write(script_contents)
master_script_line = "sbatch {}\n".format(script_relpath)
self.master_script += master_script_line
def finalise(self):
variables = {
"name": "autotest",
"stdout": os.path.join(self.logs_relpath, "freeze.out"),
"stderr": os.path.join(self.logs_relpath, "freeze.err"),
"output_dataset_uri": self.analysis.output_dataset.uri,
}
slurm_template = self.analysis.config["slurm_freeze_template"]
script_contents = slurm_template.format(**variables)
script_name = "freeze_dataset.slurm"
script_fpath = os.path.join(self.scripts_path, script_name)
script_relpath = os.path.relpath(script_fpath, self.base_output_path)
with open(script_fpath, "w") as fh:
fh.write(script_contents)
master_script_line = "sbatch --dependency=singleton {}\n".format(
script_relpath
)
self.master_script += master_script_line
master_script_fpath = os.path.join(self.base_output_path, "runme.sh")
with open(master_script_fpath, "w") as fh:
fh.write(self.master_script)
# Add README content to the output dataset.
readme_content = self.analysis.input_dataset.get_readme_content()
readme_content += "\nderived_from_UUID: {}".format(
self.analysis.input_dataset.uuid
)
readme_content += "\nderived_from_URI: '{}'".format(
self.analysis.input_dataset.uri
)
self.analysis.output_dataset.put_readme(readme_content)
@click.command()
@click.argument('analysis_fpath')
@click.argument('output_path')
def main(analysis_fpath, output_path):
analysis = Analysis(analysis_fpath)
runner = SlurmRunner(analysis, output_path)
analysis.initialise()
for identifier in runner.analysis.identifiers_to_process:
runner.process_single_identifier(identifier)
runner.finalise()
# analysis.finalise()
# click.secho("Created: ", nl=False)
# click.secho("{}".format(analysis.output_dataset.uri), fg='green')
if __name__ == '__main__':
main()