forked from yigbt/uap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
uap.py
576 lines (490 loc) · 17.1 KB
/
uap.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import traceback
import sys
import logging
import argparse
import os
uap_path = os.path.dirname(os.path.realpath(__file__))
activate_this_file = '%s/python_env/bin/activate_this.py' % uap_path
exec(
compile(
open(activate_this_file).read(),
activate_this_file,
'exec'),
dict(
__file__=activate_this_file))
'''
Adjust sys.path so everything we need can be found
'''
uap_version = 2.0
if uap_path not in sys.path:
sys.path.append(uap_path)
include_path = '%s/include' % uap_path
if include_path not in sys.path:
sys.path.append(include_path)
sources_path = '%s/include/sources' % uap_path
if sources_path not in sys.path:
sys.path.append(sources_path)
steps_path = '%s/include/steps' % uap_path
if steps_path not in sys.path:
sys.path.append(steps_path)
subcommand_path = '%s/include/subcommands' % uap_path
if subcommand_path not in sys.path:
sys.path.append(subcommand_path)
from uaperrors import *
from include.subcommands import *
def main():
'''
Set environment variables for git, so it does use the correct repository
'''
os.environ["GIT_DIR"] = "%s/.git" % uap_path
os.environ["GIT_WORK_TREE"] = uap_path
'''
This script allows access to all commands which are provided by the pipeline.
'''
# Definition of common parser(s)
common_parser = argparse.ArgumentParser(
add_help=False,
formatter_class=argparse.RawTextHelpFormatter,
prog='uap'
)
common_parser.add_argument(
"--even-if-dirty",
dest="even_if_dirty",
action="store_true",
default=False,
help="This option must be set if the local git repository "
"contains uncommited changes.\n"
"Otherwise uap will not run.")
common_parser.add_argument(
"--no-tool-checks",
dest="no_tool_checks",
action="store_true",
default=False,
help="This option disables the otherwise mandatory checks for "
"tool availability and version")
# Definition of the final parser
parser = argparse.ArgumentParser(
description="This script starts and controls analysis for 'uap'.",
epilog="For complete documentation see: "
"http://uap.readthedocs.org/en/latest/\n"
"For citation use: ...\n"
"For source code see: https://github.com/yigbt/uap",
formatter_class=argparse.RawTextHelpFormatter,
prog='uap'
)
parser.add_argument(
"config",
help="Path to YAML file that contains the pipeline configuration.\n"
"The content of that file needs to follow the documentation.",
metavar="<project-config>.yaml",
nargs='?',
type=argparse.FileType('r'))
parser.add_argument(
"-v", "--verbose",
dest="verbose",
action="count",
default=1,
help="Increase output verbosity")
parser.add_argument(
"--path",
action="version",
version=uap_path,
help="Report the path of the UAP installation and exit.")
parser.add_argument(
"--debugging",
dest="debugging",
action="store_true",
default=False,
help="Print traceback on UAPError and keep backup of all ping files.")
parser.add_argument(
"--profiling",
dest="profiling",
action="store_true",
default=False,
help="Enable profiling save report in uap.cprof.")
parser.add_argument(
"--version",
dest="version",
action="version",
version="%%(prog)s %s" % uap_version,
help="Display version information.")
subparsers = parser.add_subparsers(
title="subcommands",
dest="subcommand",
description="Available subcommands.")
subparsers.required=True
'''
The argument parser for 'fix-problems.py' is created here."
'''
fix_problems_parser = subparsers.add_parser(
"fix-problems",
help="Fixes problematic states by removing stall files.",
description="",
formatter_class=argparse.RawTextHelpFormatter,
parents=[common_parser])
fix_problems_parser.add_argument(
"--cluster",
dest="cluster",
type=str,
default="auto",
help="Specify the cluster type. Default: [auto].")
fix_problems_parser.add_argument(
"--first-error",
dest="first_error",
action="store_true",
default=False,
help="Print stderr of the first failed cluster job.")
fix_problems_parser.add_argument(
"--file-modification-date",
dest="file_modification_date",
action="store_true",
default=False,
help="If the sha256sum of the result is correct, reset the modification date.")
fix_problems_parser.add_argument(
"--details",
dest="details",
action="store_true",
default=False,
help="Displays information about the files causing problems.")
fix_problems_parser.add_argument(
"--srsly",
dest="srsly",
action="store_true",
default=False,
help="Delete problematic files or do change modification dates.")
fix_problems_parser.set_defaults(func=fix_problems.main)
'''
The argument parser for 'render.py' is created here."
'''
render_parser = subparsers.add_parser(
"render",
help="Renders DOT-graphs displaying information of the analysis.",
description="'render' generates DOT-graphs. Without arguments\n"
"it takes the annotation file of each run and generates a graph,\n"
"showing details of the computation.",
formatter_class=argparse.RawTextHelpFormatter,
parents=[common_parser])
render_parser.add_argument(
"--files",
dest="files",
action="store_true",
default=False,
help="Renders a graph showing all files of the analysis. "
"[Not implemented yet!]")
render_parser.add_argument(
"--steps",
dest="steps",
action="store_true",
default=False,
help="Renders a graph showing all steps of the analysis and their "
"connections.")
render_parser.add_argument(
"--simple",
dest="simple",
action="store_true",
default=False,
help="Simplify rendered graphs.")
render_parser.add_argument(
"--orientation",
choices=['left-to-right', 'right-to-left', 'top-to-bottom'],
dest="orientation",
default='top-to-bottom',
help="Defines orientation of the graph. Default: 'top-to-bottom'",
type=str
)
render_parser.add_argument(
"run",
nargs='*',
default=list(),
type=str,
help="Render only graphs for these runs.")
render_parser.set_defaults(func=render.main)
'''
The argument parser for 'run_locally.py' is created here."
'''
run_locally_parser = subparsers.add_parser(
"run-locally",
help="Executes the analysis on the local machine.",
description="This command starts 'uap' on the local machine. "
"It can be used to start:\n"
" * all runs of the pipeline as configured in <project-config>.yaml\n"
" * all runs defined by a specific step in <project-config>.yaml\n"
" * one or more steps\n"
"To start the complete pipeline as configured in <project-config>.yaml "
"execute:\n"
"$ uap <project-config>.yaml run-locally\n"
"To start a specific step execute:\n"
"$ uap <project-config>.yaml run-locally <step_name>\n"
"To start a specific run execute:\n"
"$ uap <project-config>.yaml run-locally <step/run>\n"
"The step_name is the name of an entry in the 'steps:' section "
"as defined in '<project-config>.yaml'. A specific run is defined via "
"its run ID 'step/run'. To get a list of all run IDs please run:\n"
"$ uap <project-config>.yaml status",
formatter_class=argparse.RawTextHelpFormatter,
parents=[common_parser])
run_locally_parser.add_argument(
"--force",
dest="force",
action="store_true",
default=False,
help="Force to overwrite changed tasks.")
run_locally_parser.add_argument(
"--ignore",
dest="ignore",
action="store_true",
default=False,
help="Ignore chages of tasks and consider them finished.")
run_locally_parser.add_argument(
"run",
nargs='*',
default=list(),
type=str,
help="These runs are processed on the local machine.")
run_locally_parser.set_defaults(func=run_locally.main)
'''
The argument parser for 'status.py' is created here.
'''
status_parser = subparsers.add_parser(
"status",
help="Displays information about the status of the analysis.",
description="This script displays by default information about all "
"runs of the pipeline as configured in '<project-config>.yaml'. But "
"the displayed information can be narrowed down via command line "
"options.\n"
"IMPORTANT: Hints given by this script are just valid if the jobs were "
"submitted to the cluster.",
formatter_class=argparse.RawTextHelpFormatter,
parents=[common_parser])
status_parser.add_argument(
"--cluster",
dest="cluster",
type=str,
default="auto",
help="Specify the cluster type. Default: [auto].")
status_parser.add_argument(
"--details",
dest="details",
action="store_true",
default=False,
help="Displays more information about task states.")
status_parser.add_argument(
"--job-ids",
dest="job_ids",
action="store_true",
default=False,
help="Prints space seperated cluster job ids of all submitted jobs.")
status_parser.add_argument(
"--summarize",
dest="summarize",
action="store_true",
default=False,
help="Displays summarized information of the analysis.")
status_parser.add_argument(
"--graph",
dest="graph",
action="store_true",
default=False,
help="Displays the dependency graph of the analysis.")
status_parser.add_argument(
"--hash",
dest="hash",
action="store_true",
default=False,
help="Validate sha256sums to detect file changes.")
status_parser.add_argument(
"--sources",
dest="sources",
action="store_true",
default=False,
help="Displays only information about the source runs.")
status_parser.add_argument(
"run",
nargs='*',
default=list(),
type=str,
help="The status of these runs are displayed.")
status_parser.set_defaults(func=status.main)
'''
The argument parser for 'steps.py' is created here.
'''
steps_parser = subparsers.add_parser(
"steps",
help="Displays information about the steps available in uap.",
description="This script displays by default a list of all "
"steps the pipeline can use.\n",
formatter_class=argparse.RawTextHelpFormatter,
parents=[common_parser])
steps_parser.add_argument(
"--details",
dest="details",
action="store_true",
default=False,
help="Display description per step.")
steps_parser.add_argument(
"--show",
dest="step",
type=str,
default="",
help="Show the details of a specific step.")
steps_parser.set_defaults(func=steps.main)
'''
The argument parser for 'submit-to-cluster.py' is created here."
'''
submit_to_cluster_parser = subparsers.add_parser(
"submit-to-cluster",
help="Submits the jobs created by uap to a cluster",
description="This script submits all runs configured in "
"<project-config>.yaml to a cluster. "
"The configuration for the available cluster types is stored at "
"/<path-to-uap>/cluster/cluster-specific-commands.yaml. "
"The list of runs can be narrowed down to specific steps. "
"All runs of the specified step will be submitted to the cluster. "
"Also, individual runs IDs (step/run) can be used for submission.",
formatter_class=argparse.RawTextHelpFormatter,
parents=[common_parser])
submit_to_cluster_parser.add_argument(
"--cluster",
dest="cluster",
type=str,
default="auto",
help="Specify the cluster type. Default: [auto].")
submit_to_cluster_parser.add_argument(
"--legacy",
dest="legacy",
action="store_true",
default=False,
help="Use none array cluster submission.")
submit_to_cluster_parser.add_argument(
"--force",
dest="force",
action="store_true",
default=False,
help="Force to overwrite changed tasks.")
submit_to_cluster_parser.add_argument(
"--ignore",
dest="ignore",
action="store_true",
default=False,
help="Ignore chages of tasks and consider them finished.")
submit_to_cluster_parser.add_argument(
"run",
nargs='*',
default=list(),
type=str,
help="Submit only these runs to the cluster.")
submit_to_cluster_parser.set_defaults(func=submit_to_cluster.main)
'''
The argument parser for 'run-info.py' is created here."
'''
run_info_parser = subparsers.add_parser(
"run-info",
help="Displays information about certain source or processing runs.",
description="",
formatter_class=argparse.RawTextHelpFormatter,
parents=[common_parser])
run_info_parser.add_argument(
"--sources",
dest="sources",
action="store_true",
default=False,
help="Displays only information about the source runs.")
run_info_parser.add_argument(
"run",
nargs='*',
default=list(),
type=str,
help="Display run-info for these runs.")
run_info_parser.set_defaults(func=run_info.main)
'''
The argument parser for 'volatilize.py' is created here."
'''
volatilize_parser = subparsers.add_parser(
"volatilize",
help="Saves disk space by volatilizing intermediate results",
description="Save disk space by volatilizing intermediate results. "
"Only steps marked with '_volatile: True' are considered.",
formatter_class=argparse.RawTextHelpFormatter,
parents=[common_parser])
volatilize_parser.add_argument(
"--details",
dest="details",
action="store_true",
default=False,
help="Shows which files can be volatilized.")
volatilize_parser.add_argument(
"--srsly",
dest="srsly",
action="store_true",
default=False,
help="Replaces files marked for volatilization with a placeholder.")
volatilize_parser.set_defaults(func=volatilize.main)
# get arguments and call the appropriate function
args = parser.parse_args()
# Add the path to this very file
args.uap_path = uap_path
args.uap_version = uap_version
# create logger object
logger = _configure_logger(args.verbose)
# tracback output in case of verbosity
if args.verbose > 1:
args.debugging = True
# call subcommand
try:
if args.profiling is True:
import cProfile
cProfile.runctx('args.func(args)', {'args': args}, {},
filename='uap.cprof')
else:
args.func(args)
except (Exception, KeyboardInterrupt) as e:
error = traceback.format_exception(*sys.exc_info())[-1]
logger.error(error.strip())
if args.debugging is True:
raise
else:
sys.exit(1)
def _configure_logger(verbosity):
logger = logging.getLogger("uap_logger")
# create formatter for different log level
debug_formatter = logging.Formatter(
fmt='[uap][%(levelname)s] %(funcName)s in %(filename)s: %(message)s '
)
info_formatter = logging.Formatter(
fmt='[uap][%(levelname)s]: %(message)s '
)
# create console handler
ch = logging.StreamHandler()
# set handler logging level
ch.setLevel(logging.NOTSET)
# Instantiate logger
if verbosity == 0:
# add formatter to ch
ch.setFormatter(info_formatter)
# set logger logging level
logger.setLevel(logging.ERROR)
lvl = 'ERROR'
elif verbosity == 1:
# add formatter to ch
ch.setFormatter(info_formatter)
# set logger logging level
logger.setLevel(logging.WARNING)
lvl = 'WARNING'
elif verbosity == 2:
# add formatter to ch
ch.setFormatter(info_formatter)
# set logger logging level
logger.setLevel(logging.INFO)
lvl = 'INFO'
elif verbosity >= 3:
# add formatter to ch
ch.setFormatter(debug_formatter)
# set logger logging level
logger.setLevel(logging.DEBUG)
lvl = 'DEBUG'
# add ch to logger
logger.addHandler(ch)
logger.info("Set log level to %s" % lvl)
return logger
if __name__ == '__main__':
main()