3cbf7bda9a
CI / typecheck + test + build (push) Has been cancelled
Integrate origin/main, including feature/new-ai-gate, with the saved local PCB work. Preserve project covers, packaging, platform authentication, macOS KiCad support and the AI disable switch. Mount only the new session workspace, align proxy defaults with port 8003, and resolve cross-platform and test integration issues.
71 lines
3.0 KiB
Python
71 lines
3.0 KiB
Python
"""Start the bundled API with private environment configuration and external data."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import sys
|
|
import shutil
|
|
|
|
|
|
def load_env(path):
|
|
if not path.is_file():
|
|
return
|
|
for number, raw in enumerate(path.read_text().splitlines(), 1):
|
|
line = raw.strip()
|
|
if not line or line.startswith('#'):
|
|
continue
|
|
if '=' not in line:
|
|
raise SystemExit(f'Invalid backend environment line {number}')
|
|
key, value = line.split('=', 1)
|
|
key, value = key.strip(), value.strip()
|
|
if not re.fullmatch(r'[A-Z][A-Z0-9_]*', key):
|
|
raise SystemExit(f'Invalid backend environment variable on line {number}')
|
|
if value.startswith('"'):
|
|
value = json.loads(value)
|
|
elif value.startswith("'") and value.endswith("'"):
|
|
value = value[1:-1]
|
|
os.environ.setdefault(key, value)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
root = Path(__file__).resolve().parent
|
|
load_env(root / '.env.local')
|
|
sys.path[:0] = [str(root), str(root / 'api')]
|
|
os.environ.setdefault('AI_PCB_MODE', 'real')
|
|
os.environ.setdefault('AI_PCB_API_DATA', str(Path.home() / '.local/share/vibemake-pcb'))
|
|
from providers.config import current_binding
|
|
from providers.factory import check_configuration
|
|
check_configuration(current_binding())
|
|
from tools.fetch_router import main as fetch_router
|
|
fetch_router()
|
|
engine = os.getenv('AI_PCB_EDA_BACKEND', 'easyeda')
|
|
if engine == 'easyeda':
|
|
pack = Path(os.getenv('AI_PCB_PACK_DIR', str(root / 'easyeda_backend'))).resolve()
|
|
sys.path.insert(0, str(pack))
|
|
from eda_runtime.adapter import find_eda, cli_call, EDA_EXTRA_ARGS
|
|
from eda_startup import ensure_bridge
|
|
executable = find_eda(os.getenv('AI_PCB_EDA_EXECUTABLE'))
|
|
try:
|
|
health = ensure_bridge(executable, cli_call, EDA_EXTRA_ARGS)
|
|
except Exception as exc:
|
|
raise SystemExit(str(exc)) from None
|
|
if not shutil.which('java'):
|
|
raise SystemExit('Install Java 17+ for local routing')
|
|
if not shutil.which(os.getenv('AI_PCB_BLENDER', 'blender')):
|
|
raise SystemExit('Install Blender for component 3D previews')
|
|
elif engine == 'kicad':
|
|
from kicad_backend.runtime import KiCadRuntime
|
|
from kicad_backend.routing import executable
|
|
health = KiCadRuntime().doctor()
|
|
if not health['available']:
|
|
raise SystemExit('KiCad backend prerequisites failed: ' + '; '.join(health['findings']))
|
|
executable()
|
|
if sys.platform != 'darwin' and not shutil.which('Xvfb'):
|
|
raise SystemExit('Install Xvfb for unattended local routing')
|
|
else:
|
|
raise SystemExit('AI_PCB_EDA_BACKEND must be easyeda or kicad')
|
|
import uvicorn
|
|
uvicorn.run('app.main:create_app', factory=True, host=os.getenv('AI_PCB_API_HOST', '127.0.0.1'),
|
|
port=int(os.getenv('AI_PCB_API_PORT', '8000')), timeout_graceful_shutdown=8,
|
|
access_log=os.getenv('AI_PCB_HTTP_ACCESS_LOG','0')=='1')
|