CmvrControlStation/scripts/package_taskflow.py

50 lines
3.1 KiB
Python
Raw Permalink Normal View History

"""Package the task JSON contract, schemas, catalog, reference parser and Proto closure."""
from pathlib import Path
import json, re, zipfile, os, subprocess, shutil, hashlib
root=Path.cwd(); docs=root/"docs/task-flow"; staging=root/"artifacts/TaskFlow_20261110"
staging.mkdir(parents=True,exist_ok=True)
files=[]
for p in sorted(docs.iterdir()):
if p.is_file():
dst=staging/p.name;shutil.copyfile(p,dst);files.append(dst)
verify=staging/"verify-example.mjs"
test=(root/"tests/task_flow_reference_test.mjs").read_text(encoding="utf-8").replace("../docs/task-flow/", "./")
verify.write_text(test,encoding="utf-8");files.append(verify)
catalog=json.loads((docs/"task_operations.json").read_text(encoding="utf-8"))
seen=set()
def copy_proto(name):
if name in seen:return
seen.add(name);source=root/"protos"/name
if not source.is_file():source=root/"include"/name
if not source.is_file():raise FileNotFoundError(name)
text=source.read_text(encoding="utf-8");dst=staging/"protos"/name;dst.parent.mkdir(parents=True,exist_ok=True);shutil.copyfile(source,dst);files.append(dst)
for dependency in re.findall(r'^import\s+(?:public\s+|weak\s+)?"([^"]+)"',text,re.M):copy_proto(dependency)
services=[]
for service in catalog["services"]:
name=service["service"].split(".")[-1]
candidates=[]
for p in (root/"protos/cmvr/api").glob("*service.proto"):
if re.search(r'\bservice\s+'+name+r'\s*\{',p.read_text(encoding="utf-8")):candidates.append(p)
assert len(candidates)==1,name
filename=candidates[0].relative_to(root/"protos").as_posix();services.append(filename);copy_proto(filename)
lines=["CMVR device RPC catalog / 2026-11-10",""]
for service in catalog["services"]:
lines.append(service["deviceType"]+" / "+service["service"])
for m in service["methods"]:
kind="Streaming: dedicated adapter required" if m["clientStreaming"] or m["serverStreaming"] else "Unary: structured task editor"
lines.append(" "+m["method"]+" | "+kind+" | "+m["requestType"]+" -> "+m["responseType"])
lines.append("")
listing=staging/"rpc_catalog.txt";listing.write_text("\n".join(lines),encoding="utf-8-sig");files.append(listing)
env=os.environ.copy();env["PATH"]=str(root/"lib")+os.pathsep+env.get("PATH","")
descriptor=staging/"api_descriptor.pb"
subprocess.run([str(root/"tools/protoc.exe"),"--experimental_allow_proto3_optional","--proto_path="+str(staging/"protos"),"--include_imports","--include_source_info","--descriptor_set_out="+str(descriptor),*services],env=env,check=True)
files.append(descriptor)
manifest={str(p.relative_to(staging)).replace("\\","/"):hashlib.sha256(p.read_bytes()).hexdigest() for p in files}
manifest_path=staging/"SHA256.json";manifest_path.write_text(json.dumps(manifest,ensure_ascii=False,indent=2),encoding="utf-8");files.append(manifest_path)
out=root/"artifacts/TaskFlow_JSON_Protocol_20261110.zip"
with zipfile.ZipFile(out,"w",zipfile.ZIP_DEFLATED) as z:
for p in files:z.write(p,p.relative_to(staging))
with zipfile.ZipFile(out) as z:assert z.testzip() is None
print("ZIP:",out,"files:",len(files),"bytes:",out.stat().st_size)
print("SHA256:",hashlib.sha256(out.read_bytes()).hexdigest())