-
Notifications
You must be signed in to change notification settings - Fork 10
/
run.py
344 lines (294 loc) · 10.2 KB
/
run.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
import argparse
import os
import yaml
import logging
import coloredlogs
import signal
import sys
from pippin.config import (
mkdirs,
get_logger,
get_output_dir,
chown_file,
get_config,
chown_dir,
)
from pippin.manager import Manager
from colorama import init
import socket
class MessageStore(logging.Handler):
store = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.store = {}
def emit(self, record):
l = record.levelname
if l not in self.store:
self.store[l] = []
self.store[l].append(record)
def get_warnings(self):
return self.store.get("WARNING", []) + []
def get_errors(self):
return self.store.get("CRITICAL", []) + self.store.get("ERROR", [])
def setup_logging(config_filename, logging_folder, args):
level = logging.DEBUG if args.verbose else logging.INFO
logging_filename = f"{logging_folder}/{config_filename}.log"
message_store = MessageStore()
message_store.setLevel(logging.WARNING)
NOTICE_LEVELV_NUM = 25
logging.addLevelName(NOTICE_LEVELV_NUM, "NOTICE")
def notice(self, message, *args, **kws):
if self.isEnabledFor(NOTICE_LEVELV_NUM):
self._log(NOTICE_LEVELV_NUM, message, args, **kws)
logging.Logger.notice = notice
fmt_verbose = "[%(levelname)8s |%(filename)21s:%(lineno)3d] %(message)s"
fmt_info = "%(message)s"
fmt = fmt_verbose if args.verbose else fmt_info
logger = get_logger()
handlers = [message_store]
if not args.check:
handlers.append(logging.FileHandler(logging_filename, mode="w"))
handlers[-1].setLevel(logging.DEBUG)
handlers[-1].setFormatter(logging.Formatter(fmt_verbose))
# logging.basicConfig(level=level, format=fmt, handlers=handlers)
for h in handlers:
logger.addHandler(h)
coloredlogs.install(
level=level,
fmt=fmt,
reconfigure=True,
level_styles=coloredlogs.parse_encoded_styles(
"debug=8;notice=green;warning=yellow;error=red,bold;critical=red,inverse"
),
)
logging.getLogger("matplotlib").setLevel(logging.ERROR)
logger.info(f"Logging streaming out, also saving to {logging_filename}")
return message_store, logging_filename
def load_yaml(yaml_path):
with open(yaml_path, "r") as f:
raw = f.read()
logging.info("Preprocessing yaml")
yaml_str = preprocess(raw)
info = f"# Original input file: {yaml_path}\n# Submitted on login node: {socket.gethostname()}\n"
yaml_str = info + yaml_str
config = yaml.safe_load(yaml_str)
return yaml_str, config
def preprocess(raw):
lines = raw.split("\n")
# Get all lines which start with #
comment_lines = [line[1:] for line in lines if (len(line) > 0) and (line[0] == "#")]
# Now get all lines which start and end with %
preprocess_lines = [
line
for line in comment_lines
if (len(line.split()) > 0)
and (line.split()[0][0] == line.split()[-1][-1] == "%")
]
if len(preprocess_lines) == 0:
logging.info("No preprocessing found")
return raw
logging.debug(f"Found preprocessing lines:\n{preprocess_lines}")
for preprocess in preprocess_lines:
# Remove % from preprocess
preprocess = preprocess.replace("%", "")
# Run preprocess step
name, *value = preprocess.split()
if name == "include:":
logging.info(f"Including {value[0]}")
lines = preprocess_include(value, lines)
else:
logging.warning(f"Unknown preprocessing step: {name}, skipping")
yaml_str = "\n".join(lines)
return yaml_str
def preprocess_include(value, lines):
include_path = os.path.abspath(os.path.expandvars(value[0]))
assert os.path.exists(
include_path
), f"Attempting to include {include_path}, but file cannot be found."
with open(include_path, "r") as f:
include_yaml = f.read()
include_yaml = include_yaml.split("\n")
index = [i for i, l in enumerate(lines) if value[0] in l][0]
info = [f"# Anchors included from {include_path}"]
return lines[:index] + info + include_yaml + lines[index + 1 :]
def run(args):
if args is None:
return None
init()
# Load YAML config file
yaml_path = os.path.abspath(os.path.expandvars(args.yaml))
assert os.path.exists(yaml_path), f"File {yaml_path} cannot be found."
config_raw, config = load_yaml(yaml_path)
# with open(yaml_path, "r") as f:
# config = yaml.safe_load(f)
overwrites = config.get("GLOBAL")
if config.get("GLOBALS") is not None:
logging.warning(
"Your config file has a GLOBALS section in it. If you're trying to overwrite cfg.yml, rename this to GLOBAL"
)
cfg = None
if overwrites:
cfg = overwrites.get("CFG_PATH")
if cfg is None:
cfg = args.config
global_config = get_config(initial_path=cfg, overwrites=overwrites)
config_filename = os.path.basename(args.yaml).split(".")[0].upper()
output_dir = get_output_dir()
logging_folder = os.path.abspath(os.path.join(output_dir, config_filename))
if not args.check:
mkdirs(logging_folder)
if os.path.exists(logging_folder):
chown_dir(logging_folder, walk=args.permission)
if args.permission:
return
message_store, logging_filename = setup_logging(
config_filename, logging_folder, args
)
for i, d in enumerate(global_config["DATA_DIRS"]):
logging.debug(f"Data directory {i + 1} set as {d}")
assert (
d is not None
), "Data directory is none, which means it failed to resolve. Check the error message above for why."
logging.info(
f"Running on: {os.environ.get('HOSTNAME', '$HOSTNAME not set')} login node."
)
manager = Manager(config_filename, yaml_path, config_raw, config, message_store)
# Gracefully hand Ctrl-c
def handler(signum, frame):
logging.error("Ctrl-c was pressed.")
logging.warning("All remaining tasks will be killed and their hash reset")
manager.kill_remaining_tasks()
exit(1)
signal.signal(signal.SIGINT, handler)
if args.start is not None:
args.refresh = True
manager.set_start(args.start)
manager.set_finish(args.finish)
manager.set_force_refresh(args.refresh)
manager.set_force_ignore_stage(args.ignore)
num_errs = manager.execute(args.check, args.compress, args.uncompress)
manager.num_errs = num_errs or 0
chown_file(logging_filename)
return manager
def get_syntax(task):
taskname = [
"DATAPREP",
"SIM",
"LCFIT",
"CLASSIFY",
"AGG",
"MERGE",
"BIASCOR",
"CREATE_COV",
"COSMOFIT",
"ANALYSE",
]
if task == "options":
print(f"Possible tasks are: ({[(i, task) for i, task in enumerate(taskname)]})")
return None
try:
task = taskname[int(task)]
except:
pass
assert task in taskname, f"Unknown task {task}"
base = os.path.dirname(os.path.realpath(__file__))
with open(f"{base}/docs/src/tasks/{task.lower()}.md", "r") as f:
print(f.read())
def get_args(test=False):
# Set up command line arguments
parser = argparse.ArgumentParser()
parser.add_argument(
"yaml",
help="the name of the yml config file to run. For example: configs/default.yml",
type=str,
nargs="*",
)
parser.add_argument(
"--config",
help="Location of global config (i.e. cfg.yml)",
default=None,
type=str,
)
parser.add_argument(
"-v", "--verbose", help="increase output verbosity", action="store_true"
)
parser.add_argument(
"-s",
"--start",
help="Stage to start and force refresh. Accepts either the stage number or name (i.e. 1 or SIM)",
default=None,
)
parser.add_argument(
"-f",
"--finish",
help="Stage to finish at (it runs this stage too). Accepts either the stage number or name (i.e. 1 or SIM)",
default=None,
)
parser.add_argument(
"-r",
"--refresh",
help="Refresh all tasks, do not use hash",
action="store_true",
)
parser.add_argument(
"-c",
"--check",
help="Check if config is valid",
action="store_true",
default=False,
)
parser.add_argument(
"-p",
"--permission",
help="Fix permissions and groups on all output, don't rerun",
action="store_true",
default=False,
)
parser.add_argument(
"-i",
"--ignore",
help="Dont rerun tasks with this stage or less. Accepts either the stage number of name (i.e. 1 or SIM)",
default=None,
)
parser.add_argument(
"-S",
"--syntax",
help="Get the syntax of the given stage. Accepts either the stage number or name (i.e. 1 or SIM). If run without argument, will tell you all stage numbers / names.",
default=None,
const="options",
type=str,
nargs="?",
)
command_group = parser.add_mutually_exclusive_group()
command_group.add_argument(
"-C",
"--compress",
help="Compress pippin output during job. Combine with -c / --check in order to compress completed pippin job.",
action="store_true",
default=False,
)
command_group.add_argument(
"-U",
"--uncompress",
help="Do not compress pippin output during job. Combine with -c / --check in order to uncompress completed pippin job. Mutually exclusive with -C / --compress",
action="store_true",
default=False,
)
args = parser.parse_args()
if args.syntax is not None:
s = args.syntax
get_syntax(s)
return None
elif not test:
if len(args.yaml) == 0:
parser.error("You must specify a yaml file!")
else:
args.yaml = args.yaml[0]
return args
if __name__ == "__main__":
args = get_args()
if args is not None:
manager = run(args)
sys.stdout.flush()
if manager.num_errs > 0:
raise (ValueError(f"{manager.num_errs} Errors found"))