| 1 |
""" |
| 2 |
New Commandline interface that supports ResolvedToolContracts and emitting ToolContracts |
| 3 |
|
| 4 |
There's three use cases. |
| 5 |
|
| 6 |
- running from an argparse instance |
| 7 |
- running from a Resolved Tool Contract (RTC) |
| 8 |
- emitting a ToolContract (TC) |
| 9 |
|
| 10 |
Going to do this in a new steps. |
| 11 |
|
| 12 |
- de-serializing of RTC (I believe this should be done via avro, not a new random JSON file. With avro, the java, c++, classes can be generated. Python can load the RTC via a structure dict that has a well defined schema) |
| 13 |
- get loading and running of RTC from commandline to call main func in a report. |
| 14 |
- generate/emit TC from a a common commandline parser interface that builds the TC and the standard argparse instance |
| 15 |
""" |
| 16 |
|
| 17 |
import argparse |
| 18 |
import errno |
| 19 |
import json |
| 20 |
import logging |
| 21 |
import os |
| 22 |
import shutil |
| 23 |
import sys |
| 24 |
import time |
| 25 |
import traceback |
| 26 |
|
| 27 |
import pbcommand |
| 28 |
from pbcommand.models import ResourceTypes, PacBioAlarm |
| 29 |
from pbcommand.models.report import Report, Attribute |
| 30 |
from pbcommand.common_options import add_base_options, add_nproc_option |
| 31 |
from pbcommand.utils import get_parsed_args_log_level, get_peak_memory_usage |
| 32 |
|
| 33 |
|
| 34 |
def _add_version(p, version): |
| 35 |
p.version = version |
| 36 |
p.add_argument('--version', |
| 37 |
action="version", |
| 38 |
help="show program's version number and exit") |
| 39 |
return p |
| 40 |
|
| 41 |
|
| 42 |
def get_default_argparser(version, description): |
| 43 |
""" |
| 44 |
Everyone should use this to create an instance on a argparser python parser. |
| 45 |
|
| 46 |
|
| 47 |
*This should be replaced updated to have the required base options* |
| 48 |
|
| 49 |
:param version: Version of your tool |
| 50 |
:param description: Description of your tool |
| 51 |
:return: |
| 52 |
:rtype: ArgumentParser |
| 53 |
""" |
| 54 |
p = argparse.ArgumentParser(description=description, |
| 55 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 56 |
|
| 57 |
return _add_version(p, version) |
| 58 |
|
| 59 |
|
| 60 |
def get_default_argparser_with_base_opts( |
| 61 |
version, description, default_level="INFO", nproc=None): |
| 62 |
"""Return a parser with the default log related options |
| 63 |
|
| 64 |
If you don't want the default log behavior to go to stdout, then set |
| 65 |
the default log level to be "ERROR". This will essentially suppress all |
| 66 |
output to stdout. |
| 67 |
|
| 68 |
Default behavior will only emit to stderr. This is essentially a '--quiet' |
| 69 |
default mode. |
| 70 |
|
| 71 |
my-tool --my-opt=1234 file_in.txt |
| 72 |
|
| 73 |
To override the default behavior and add a chatty-er stdout |
| 74 |
|
| 75 |
my-tool --my-opt=1234 --log-level=INFO file_in.txt |
| 76 |
|
| 77 |
Or write the console output to write the log file to an explict file and |
| 78 |
leave the stdout untouched. |
| 79 |
|
| 80 |
my-tool --my-opt=1234 --log-level=DEBUG --log-file=file.log file_in.txt |
| 81 |
|
| 82 |
""" |
| 83 |
p = add_base_options( |
| 84 |
get_default_argparser( |
| 85 |
version, |
| 86 |
description), |
| 87 |
default_level=default_level) |
| 88 |
if nproc is not None: |
| 89 |
p = add_nproc_option(p) |
| 90 |
return p |
| 91 |
|
| 92 |
|
| 93 |
def write_task_report(run_time, nproc, exit_code, maxrss): |
| 94 |
attributes = [ |
| 95 |
Attribute("host", value=os.uname()[1]), |
| 96 |
Attribute("system", value=os.uname()[0]), |
| 97 |
Attribute("nproc", value=nproc), |
| 98 |
Attribute("run_time", value=run_time), |
| 99 |
Attribute("exit_code", value=exit_code), |
| 100 |
Attribute("maxrss", value=maxrss) |
| 101 |
] |
| 102 |
report = Report("workflow_task", |
| 103 |
title="Workflow Task Report", |
| 104 |
attributes=attributes, |
| 105 |
tags=("internal",)) |
| 106 |
report.write_json("task-report.json") |
| 107 |
|
| 108 |
|
| 109 |
def _pacbio_main_runner(alog, setup_log_func, exe_main_func, *args, **kwargs): |
| 110 |
""" |
| 111 |
Runs a general func and logs results. The return type is expected to be an (int) return code. |
| 112 |
|
| 113 |
:param alog: a log instance |
| 114 |
|
| 115 |
:param func: a cli exe func, must return an int exit code. func(args) => Int, where args is parsed from p.parse_args() |
| 116 |
|
| 117 |
:param args: parsed args from parser |
| 118 |
|
| 119 |
:param setup_log_func: F(alog, level=value, file_name=value, formatter=value) or None |
| 120 |
|
| 121 |
:return: Exit code of callable func |
| 122 |
:rtype: int |
| 123 |
""" |
| 124 |
|
| 125 |
started_at = time.time() |
| 126 |
|
| 127 |
pargs = args[0] |
| 128 |
|
| 129 |
level = logging.INFO |
| 130 |
|
| 131 |
if 'level' in kwargs: |
| 132 |
level = kwargs.pop('level') |
| 133 |
else: |
| 134 |
level = get_parsed_args_log_level(pargs) |
| 135 |
|
| 136 |
|
| 137 |
log_file = getattr(pargs, 'log_file', None) |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
log_options = dict(level=level, file_name=log_file) |
| 143 |
|
| 144 |
base_dir = os.getcwd() |
| 145 |
|
| 146 |
dump_alarm_on_error = False |
| 147 |
if "dump_alarm_on_error" in kwargs: |
| 148 |
dump_alarm_on_error = kwargs.pop("dump_alarm_on_error") |
| 149 |
is_cromwell_environment = bool( |
| 150 |
os.environ.get( |
| 151 |
"SMRT_PIPELINE_BUNDLE_DIR", |
| 152 |
None)) and "cromwell-executions" in base_dir |
| 153 |
dump_alarm_on_error = dump_alarm_on_error and is_cromwell_environment |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
if setup_log_func is not None and alog is not None: |
| 160 |
setup_log_func(alog, **log_options) |
| 161 |
alog.info("Using pbcommand v{v}".format(v=pbcommand.get_version())) |
| 162 |
alog.info( |
| 163 |
"completed setting up logger with {f}".format( |
| 164 |
f=setup_log_func)) |
| 165 |
alog.info("log opts {d}".format(d=log_options)) |
| 166 |
|
| 167 |
if dump_alarm_on_error: |
| 168 |
alog.info( |
| 169 |
"This command appears to be running as part of a Cromwell workflow") |
| 170 |
alog.info("Additional output files may be generated") |
| 171 |
|
| 172 |
try: |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
return_code = exe_main_func(*args, **kwargs) |
| 177 |
run_time = time.time() - started_at |
| 178 |
except Exception as e: |
| 179 |
run_time = time.time() - started_at |
| 180 |
if alog is not None: |
| 181 |
alog.error(e, exc_info=True) |
| 182 |
else: |
| 183 |
traceback.print_exc(sys.stderr) |
| 184 |
if dump_alarm_on_error: |
| 185 |
PacBioAlarm.dump_error( |
| 186 |
file_name=os.path.join(base_dir, "alarms.json"), |
| 187 |
exception=e, |
| 188 |
info="".join(traceback.format_exc()), |
| 189 |
message=str(e), |
| 190 |
name=e.__class__.__name__, |
| 191 |
severity=logging.ERROR) |
| 192 |
|
| 193 |
|
| 194 |
if isinstance(e, IOError): |
| 195 |
return_code = 1 |
| 196 |
else: |
| 197 |
return_code = 2 |
| 198 |
|
| 199 |
maxrss = get_peak_memory_usage() |
| 200 |
if is_cromwell_environment: |
| 201 |
alog.info("Writing task report to task-report.json") |
| 202 |
nproc = getattr(pargs, "nproc", 1) |
| 203 |
write_task_report(run_time, nproc, return_code, maxrss) |
| 204 |
_d = dict(r=return_code, s=run_time) |
| 205 |
if alog is not None: |
| 206 |
alog.info(f"Max RSS (kB): {maxrss}") |
| 207 |
alog.info("exiting with return code {r} in {s:.2f} sec.".format(**_d)) |
| 208 |
return return_code |
| 209 |
|
| 210 |
|
| 211 |
def pacbio_args_runner(argv, parser, args_runner_func, alog, setup_log_func, |
| 212 |
dump_alarm_on_error=True): |
| 213 |
|
| 214 |
args = parser.parse_args(argv) |
| 215 |
return _pacbio_main_runner(alog, setup_log_func, args_runner_func, args, |
| 216 |
dump_alarm_on_error=dump_alarm_on_error) |