68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
"""Dispatch by persisted task engine; new tasks default to EasyEDA Pro."""
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import sys
|
|
|
|
|
|
def emit(value):
|
|
print(json.dumps(value, ensure_ascii=False), flush=True)
|
|
|
|
|
|
def task_engine(root, command):
|
|
if command['action'] == 'create':
|
|
engine = os.getenv('AI_PCB_EDA_BACKEND', 'easyeda')
|
|
else:
|
|
tid = command.get('payload', {}).get('task_id')
|
|
if not isinstance(tid, str) or not re.fullmatch(r'pcb-[a-z0-9]{12,32}', tid):
|
|
raise ValueError('Invalid task identity')
|
|
path = Path(root) / 'tasks' / tid / 'task.json'
|
|
snapshot = json.loads(path.read_text())
|
|
# Pre-migration EasyEDA snapshots had no engine field.
|
|
engine = snapshot.get('eda_engine', 'easyeda')
|
|
if engine not in {'easyeda', 'kicad'}:
|
|
raise ValueError('Unsupported PCB backend: ' + str(engine))
|
|
return engine
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--root', required=True)
|
|
parser.add_argument('--pack')
|
|
args = parser.parse_args()
|
|
command = json.load(sys.stdin)
|
|
backend = Path(__file__).resolve().parents[2]
|
|
sys.path.insert(0, str(backend))
|
|
engine = task_engine(args.root, command)
|
|
def on_event(event):
|
|
if engine=='easyeda':
|
|
from eda_runtime.devlog import task,log
|
|
task(event.get('task_id'))
|
|
log('Engineering event: '+event.get('kind',''),event.get('data',{}))
|
|
emit({'event': event})
|
|
if engine == 'kicad':
|
|
from kicad_backend.service import handle_request
|
|
result = handle_request(args.root, command, event_callback=on_event)
|
|
else:
|
|
pack = Path(args.pack or backend / 'easyeda_backend').resolve()
|
|
sys.path.insert(0, str(pack))
|
|
from eda_runtime.devlog import configure,task
|
|
configure(Path(args.root).parent)
|
|
task(command.get('payload',{}).get('task_id'))
|
|
from design_runtime.service import handle_request
|
|
result = handle_request(args.root, command, event_callback=on_event,
|
|
eda_executable=os.getenv('AI_PCB_EDA_EXECUTABLE'),
|
|
blender=os.getenv('AI_PCB_BLENDER'),
|
|
max_rounds=int(os.getenv('AI_PCB_MAX_ROUNDS', '48')))
|
|
emit({'result': result})
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
main()
|
|
except Exception as exc:
|
|
emit({'result': {'error': type(exc).__name__, 'message': str(exc)}})
|
|
raise SystemExit(1)
|