chronaxis.batch.cli
源码基线:8904c2f744951857659abd40aaa3535d57faf53c
1"""从严格批量配置调用公共API,打印精简计数并保留完整逐任务报告。23成功退出0、任一任务失败退出1、取消退出130;配置错误沿外层CLI退出2。4输出失败保已完成任务,原异常恢复报告在传播前尝试关闭,不清除诊断文件。5"""67from dataclasses import replace8import json9import os10import sys11from ..planning.checks import PlanValidationError12from ..results.spool import _cleanup_owner13from .api import run_batch14from .config import load_batch_plan15from .runner import _new_root161718def run_batch_cli(arguments) -> int:19 """执行真实批量入口。参数: argparse参数。返回: 0/1/130。异常: 外层CLI处理的原错或配置错。"""20 plan = load_batch_plan(arguments.plan)21 try:22 if arguments.process_count is not None:23 plan = replace(plan, process_count=arguments.process_count)24 _new_root(arguments.output)25 except ValueError as error:26 raise PlanValidationError('$.cli', str(error)) from error27 label = os.environ.get('CHRONAXIS_DEVICE_LABEL', '').strip() or 'pending'28 try:29 report = run_batch(plan, arguments.output, device_label=label)30 except BaseException as error:31 report = getattr(error, 'batch_report', None)32 if report is not None:33 _cleanup_owner(report, error, 'batch_report', '批量异常报告')34 raise35 # 一般失败已经清理完;持续故障再尝试,完整报告仍保存初始失败及恢复位置。36 cleanup_failed = False37 try:38 report.close()39 except Exception as error:40 cleanup_failed = True41 print('批量结果清理失败:' + type(error).__name__ + ': ' + str(error), file=sys.stderr)42 document = report.to_dict()43 print(json.dumps(dict(status=report.status, counts=document['counts'],44 output=str(report.output_dir), report=str(report.report_path),45 cleanup_pending=cleanup_failed, device_label=label), ensure_ascii=False, allow_nan=False))46 if cleanup_failed or report.status == 'failed':47 return 148 return 130 if report.status == 'cancelled' else 049