OpenPi_POC_Dev/ws_common/report_utils.py
2026-10-09 16:41:13 +08:00

61 lines
2.0 KiB
Python

"""产物清单与进度标记 (design_v5 §11 report_utils / §12 收尾协议)。
- write_artifacts: 业务收尾时把产物清单 (name/type/portId/path/sizeBytes 实测)
写入 $OUTPUT_DIR/.artifacts.json, report.sh 读取它拼入 RESULT_OUTPUT_PATH 的
artifacts 字段。portId 与注册侧 outputs 端口 id 一致。
- emit_progress: 长任务逐 episode/逐文件打印 DROBOTICFLOW_PROGRESS + JSON
(含 RFC3339 updatedAt), 平台监控页取最后一条。
"""
from __future__ import annotations
import json
import os
from datetime import datetime, timezone
from pathlib import Path
def _rfc3339_now() -> str:
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
def emit_progress(stage: str, **fields) -> None:
payload = {"stage": stage}
payload.update(fields)
payload["updatedAt"] = _rfc3339_now()
print("DROBOTICFLOW_PROGRESS " + json.dumps(payload, ensure_ascii=False), flush=True)
def _path_size(path: Path) -> int:
if path.is_file():
return path.stat().st_size
total = 0
for root, _dirs, files in os.walk(path):
for name in files:
try:
total += (Path(root) / name).stat().st_size
except OSError:
pass
return total
def write_artifacts(output_dir, artifacts: list[dict]) -> None:
"""artifacts 每项: {"name","type","portId","path"}; sizeBytes 实测补齐。"""
output_dir = Path(output_dir)
resolved = []
for item in artifacts:
path = Path(item["path"])
size = _path_size(path) if path.exists() else 0
resolved.append(
{
"name": item["name"],
"type": item.get("type", "file"),
"portId": item["portId"],
"path": str(path),
"sizeBytes": size,
}
)
target = output_dir / ".artifacts.json"
target.write_text(json.dumps(resolved, ensure_ascii=False, indent=2), encoding="utf-8")
print(f"[report] artifacts manifest written: {target} ({len(resolved)} items)")