diff --git a/.gitignore b/.gitignore index 5350dd6..f5140ec 100644 --- a/.gitignore +++ b/.gitignore @@ -53,5 +53,10 @@ __pycache__/ /backend/runtime/ /backend/.venv/ +# Optional local Linux EDA application used by the backend container +/vendor/lceda-pro/ +/codex_pcb/build-assets/* +!/codex_pcb/build-assets/README.md + # Local backend source backups /backend/**/*.bak-* diff --git a/CODEX-SESSION.md b/CODEX-SESSION.md index 4df0cdc..35eb4cc 100644 --- a/CODEX-SESSION.md +++ b/CODEX-SESSION.md @@ -10,12 +10,12 @@ Review has no Shell or EDA editing access. Production starts the same thread wit ## Runtime -- Model: exact `gpt-6.1-sol`, reasoning `high`; no model substitution. +- Model: `PCB_MODEL` (default `gpt-6.1-sol`), reasoning `PCB_REASONING_EFFORT` (default `high`); no automatic model substitution. API projects must have access to the configured model. - Service: `vibemake-pcb2-session.service`, FastAPI on loopback `8003`. - Web: `vibemake-pcb2-web.service`, HTTPS `15182`, `/pcb/pcb-api/` proxy. -- Source: `/home/wpz/auto_pcb_2/vibemake-pcb-studio/codex_pcb`. -- Persistent runtime: `/home/wpz/auto_pcb_2/persistent-runtime`. -- Dedicated Codex home stores native session history. Authentication links to the existing local Codex login; credentials are not sent to the browser or copied into the repository. +- Source: the installed checkout's `codex_pcb/` directory. +- Persistent runtime: `PCB_SESSION_ROOT` (default `~/.local/share/vibemake-pcb`; use a persistent mount such as `/data` in a container). +- Dedicated Codex home: `PCB_CODEX_HOME` (default `$PCB_SESSION_ROOT/codex-home`) stores native session history. Authentication can use a server-side OpenAI API key or an existing Codex login; credentials are not sent to the browser or copied into the repository. - Each task owns a workspace, native EDA session, full EDA scripts/results and Codex event evidence. Official standalone EDA API references come from the installed `~/.codex/skills/easyeda-api`; no legacy design workflow is loaded. - The original `~/auto_pcb` service and user-opened EDA windows are not modified. @@ -27,6 +27,32 @@ When an agent turn ends without accepted delivery, the service asks the same ses At approval the service hashes its Python source and instructions. Any subsequent change blocks production tool calls/resume. Therefore an acceptance run cannot silently continue with a patched backend. A new version requires a new task/run. +The approval hash also covers non-secret runtime settings, including the model, reasoning effort, provider and executable paths. Rotating an API key does not change this hash. An existing thread cannot be switched between the login and API providers; create a new task after changing provider/model settings. Deploy source changes only after active tasks have finished; already-approved tasks from an earlier source version cannot be resumed by rewriting their hash. + +## Portable paths and OpenAI API authentication + +For the backend-only Docker/1Panel upload bundle, build commands and EDA mount layout, see [codex_pcb/DEPLOY.md](codex_pcb/DEPLOY.md). Generate the bundle with `python3 scripts/package-pcb-backend.py`. + +Backend settings are documented in `codex_pcb/.env.example`; they are separate from the frontend/proxy's `.env.pcb.example`. Copy the backend example to an ignored `.env.local`, fill in the key on the server, and inject it into the **backend process** using Compose `env_file`, systemd `EnvironmentFile`, or Uvicorn `--env-file` (requires `python-dotenv`). Merely creating an env file does not load it. + +Existing installations must explicitly keep `PCB_SESSION_ROOT` pointing at their original runtime to retain access to task records and Codex history; the new portable default does not migrate old directories. + +For API authentication set `PCB_CODEX_AUTH=apikey` and either `OPENAI_API_KEY` or `OPENAI_API_KEY_FILE`. The environment value takes precedence; the file is useful with Docker secrets. The service configures a dedicated Codex provider using the OpenAI Responses API at `https://api.openai.com/v1`, with `env_key=OPENAI_API_KEY`. It passes the key only to the Codex process, without adding it to CLI arguments or writing an `auth.json`. No interactive Codex login is required in this mode. Existing login credentials are not used as a fallback. API use is billed to the OpenAI API project; authentication does not guarantee access to the configured model. + +To retain the earlier login setup, explicitly set `PCB_CODEX_AUTH=chatgpt` (also the default for compatibility) and provision the service's `PCB_CODEX_HOME` with the existing login. This mode removes API keys from the Codex process environment. Use a dedicated service home rather than your IDE's session directory. Both `thread/start` and `thread/resume` select the same provider. + +Set `PCB_CODEX_BIN`, `PCB_EDA_BIN` and `PCB_BLENDER_BIN` to installed executables, or leave them unset to search `PATH` for `codex`, `lceda-pro` and `blender`. Set `PCB_FREEROUTING_JAR` when the JAR is outside `$PCB_SESSION_ROOT/resources/freerouting.jar`; both the production prompt and standalone launcher honor it. EDA subprocesses preserve an explicitly configured `XDG_RUNTIME_DIR` and `DISPLAY`; neither setting starts a display server. Create a private runtime directory owned by the service user with mode `0700`, and start/configure Xvfb separately on headless servers. The EDA application, Java, Xvfb/xauth and Blender still need to be installed and validated. + +For example, after injecting the settings, start one backend instance from the checkout root: + +```sh +python -m uvicorn codex_pcb.app:app --host 0.0.0.0 --port 8003 --workers 1 +``` + +Persist `PCB_SESSION_ROOT`, any separately configured `PCB_CODEX_HOME`, and the native EDA application's user/project data. A key alone does not install CAD dependencies. A successful `/api/health` checks the HTTP service, not API billing/model permissions or native EDA execution; validate with a new proposal and a real approved design. Never use a key pasted into chat; revoke it and supply a replacement only through server-side configuration. + +See [Codex authentication](https://developers.openai.com/codex/auth/) and [provider configuration](https://developers.openai.com/codex/config-advanced/). + ## Delivery The agent supplies real native project, schematic, routed layout, extracted assembled STEP and 3D render, plus optional BOM/reports. Delivery independently focuses the supplied native documents, saves them, exports native netlists and runs schematic/PCB checks. Both native gate booleans must be true. Basic file signatures and path boundaries are checked, then files are copied to a separate delivery directory with hashes and a ZIP. Downloads verify hashes again. @@ -70,7 +96,7 @@ The root `start.sh --services` starts the new systemd services on a configured L ### 新服务器部署与使用 1. 安装 Python 3、可运行此 JAR 的 Java(1.9.0 官方要求 Java 17)、`xvfb` 和 `xauth`。Debian/Ubuntu 可以安装 `openjdk-17-jre xvfb xauth`;若已有兼容 Java,可保留现有版本。 -2. 将已验证的 JAR 放到 `$PCB_SESSION_ROOT/resources/freerouting.jar`,或使用 `PCB_FREEROUTING_JAR` / `--jar` 指定绝对路径。未配置运行根目录时,默认仍是当前部署的 `/home/wpz/auto_pcb_2/persistent-runtime`,迁移必须修改它。 +2. 将已验证的 JAR 放到 `$PCB_SESSION_ROOT/resources/freerouting.jar`,或使用 `PCB_FREEROUTING_JAR` / `--jar` 指定绝对路径。未配置运行根目录时默认使用 `~/.local/share/vibemake-pcb`;容器中应显式设置为持久化挂载路径,例如 `/data`。 3. 生产会话应使用批准消息提供的启动命令。下面路径需要按目标服务器替换: ```sh diff --git a/codex_pcb/.env.example b/codex_pcb/.env.example new file mode 100644 index 0000000..24b79e3 --- /dev/null +++ b/codex_pcb/.env.example @@ -0,0 +1,23 @@ +# Backend only. Copy to .env.local and inject with Compose env_file, systemd +# EnvironmentFile, or uvicorn --env-file (requires python-dotenv). +# Never put a real key in this example or a VITE_* variable. +PCB_CODEX_AUTH=apikey +OPENAI_API_KEY= +# Alternatively mount a secret file; leave OPENAI_API_KEY empty when using it. +# OPENAI_API_KEY_FILE=/run/secrets/openai_api_key + +# Container examples: set these to the actual installed/mounted paths. +PCB_SESSION_ROOT=/data +PCB_CODEX_HOME=/data/codex-home +PCB_CODEX_BIN=/usr/local/bin/codex +PCB_EDA_BIN=/opt/lceda-pro/lceda-pro +PCB_BLENDER_BIN=/usr/bin/blender +PCB_FREEROUTING_JAR=/opt/pcb-resources/freerouting.jar + +# Must be a model and reasoning effort supported by your OpenAI API project. +# The existing model is retained by default; it is not an entitlement check. +PCB_MODEL=gpt-6.1-sol +PCB_REASONING_EFFORT=high + +DISPLAY=:99 +XDG_RUNTIME_DIR=/tmp/pcb-xdg diff --git a/codex_pcb/DEPLOY.md b/codex_pcb/DEPLOY.md new file mode 100644 index 0000000..e1e985a --- /dev/null +++ b/codex_pcb/DEPLOY.md @@ -0,0 +1,195 @@ +# 在 1Panel 部署 PCB AI 后端 + +后端可独立于前端部署。仅上传 `codex_pcb/` 会缺少独立 EDA API 文档;推荐使用下述部署包,不需要上传整个仓库或运行旧版 `backend/` 服务。 + +## 1. 在本地生成上传包 + +在仓库根目录执行: + +```bash +python3 scripts/package-pcb-backend.py +``` + +如果服务器访问 GitHub 慢,在本地网络可访问 GitHub 时改用: + +```bash +python3 scripts/package-pcb-backend.py --with-freerouting +``` + +该选项将固定版本的 Freerouting JAR 和许可一起放入部署包,并核对 JAR 的 SHA-256。下载缓存在本地 `dist/pcb-backend-downloads/`,重复打包不再下载;程序会优先使用系统 curl 的正常证书验证。服务器构建时直接使用包内文件,无需下载这两个 GitHub 文件。 + +生成 `dist/vibemake-pcb-backend.tar.gz`,包含当前后端源码、Dockerfile、Compose、启动脚本、Python 依赖、独立 API 参考文档和许可说明。打包按明确的文件清单进行,不包含 `.env.local`、任务历史、个人登录文件、前端、旧设计流程或嘉立创程序。 + +上传压缩包到 Linux 服务器,在 1Panel 文件管理器中解压到 `/opt`,结果应为: + +```text +/opt/vibemake-pcb-backend/ +├── LICENSE +├── codex_pcb/ +│ ├── Dockerfile +│ ├── Dockerfile.dockerignore +│ ├── compose.yaml +│ ├── docker-entrypoint.sh +│ ├── requirements.txt +│ ├── .env.example +│ └── ...Python 后端源码 +├── backend/easyeda_backend/skills/easyeda-api/ +│ ├── SKILL.md +│ └── references/ +└── vendor/lceda-pro/ # 需自行放入完整的 Linux EDA 程序 +``` + +其中 `backend/` 只包含官方 API 文档和来源元数据,容器不启动旧版服务。 + +## 2. 准备嘉立创 EDA 专业版 Linux 程序 + +从[官网下载页](https://lceda.cn/page/download)获取与**服务器** CPU 架构一致的专业版 Linux ZIP。服务器 `uname -m` 的 `x86_64` 对应 x64,`aarch64` 对应 arm64。不要上传 macOS/Windows 程序。CLI 要求支持 `open`、`session list`、`invoke --code-file` 的专业版客户端,运行前请核对其 `--help`。 + +按压缩包内说明解压,找到实际包含 `lceda-pro` 可执行文件的**完整应用目录**,将该目录的内容放入: + +```text +/opt/vibemake-pcb-backend/vendor/lceda-pro/ +``` + +确保 `vendor/lceda-pro/lceda-pro` 是可执行程序;保留应用全部资源、共享库、辅助程序及符号链接,不要只复制一个二进制文件。若外层还有一层目录,需要展开到上述位置。程序以只读方式挂载到容器 `/opt/lceda-pro`,不打进镜像。 + +```bash +cd /opt/vibemake-pcb-backend +chmod +x vendor/lceda-pro/lceda-pro +``` + +如果发行包采用其他入口名称,修改 `.env.local` 的 `PCB_EDA_BIN` 为挂载目录中实际路径。首次激活、账号登录、组件库访问以及该版本在虚拟显示上的运行仍需在目标服务器验证;OpenAI Key 不代替嘉立创客户端所需的配置。 + +## 3. 填写后端环境变量 + +```bash +cp codex_pcb/.env.example codex_pcb/.env.local +chmod 600 codex_pcb/.env.local +``` + +在 1Panel 文件编辑器中填写: + +```dotenv +PCB_CODEX_AUTH=apikey +OPENAI_API_KEY=填入新生成的服务端Key +PCB_SESSION_ROOT=/data +PCB_CODEX_HOME=/data/codex-home +PCB_CODEX_BIN=/usr/local/bin/codex +PCB_EDA_BIN=/opt/lceda-pro/lceda-pro +PCB_BLENDER_BIN=/usr/bin/blender +PCB_FREEROUTING_JAR=/opt/pcb-resources/freerouting.jar +DISPLAY=:99 +XDG_RUNTIME_DIR=/tmp/pcb-xdg +``` + +`PCB_MODEL` 必须是 OpenAI API 项目有权限使用的准确模型 ID;示例保留现有的 `gpt-6.1-sol`,这不代表每个 API 项目均可访问。`PCB_REASONING_EFFORT` 也必须受所选模型支持。真实 Key 只放服务器的 `.env.local`,不要放进 `.env.example`、Dockerfile 或 `VITE_*`。 + +Compose 的 `env_file` 会将 `.env.local` 注入后端。Dockerfile 的专用 ignore 文件只允许后端源码和 API 文档进入构建上下文,排除密钥、程序包及运行数据。不要使用 `docker compose config` 的完整输出对外分享配置,其中可能包含已展开的环境变量。 + +## 4. 构建并启动 + +在服务器终端执行: + +```bash +cd /opt/vibemake-pcb-backend +docker compose -f codex_pcb/compose.yaml up -d --build +docker compose -f codex_pcb/compose.yaml logs --tail=100 -f pcb-backend +``` + +或者在 1Panel「容器 → 编排 → 创建编排 → 路径选择」选择: + +```text +/opt/vibemake-pcb-backend/codex_pcb/compose.yaml +``` + +需要预先在「容器 → 镜像」构建时,Dockerfile 选择 `codex_pcb/Dockerfile`,**构建上下文必须是 `/opt/vibemake-pcb-backend`,不是其 `codex_pcb/` 子目录**;镜像名用 `vibemake-pcb-backend:local`。 + +镜像使用 Node 22 / Debian Bookworm,安装 Python、Codex CLI(固定版本)、Java 17、Blender、Xvfb/xauth、字体及常用 Electron 库,并下载或读取包内的 Freerouting 1.9.0,始终校验其 SHA-256。构建需要服务器能访问基础镜像仓库及所选软件源;未预装路由器资源时,还需要访问 GitHub。入口先检查依赖和凭据是否存在,再启动虚拟显示及一个 Uvicorn worker。它不会自动检查 Key 额度或模型权限,也不会自动完成客户端激活。 + +可用以下命令独立构建: + +```bash +docker build -f codex_pcb/Dockerfile -t vibemake-pcb-backend:local . +``` + +### 国内下载源与缓存 + +Dockerfile 和 Compose 默认使用以下国内源,可在 `codex_pcb/compose.yaml` 的 `build.args` 中修改: + +| 参数 | 默认值 | 下载内容 | +|---|---|---| +| `DEBIAN_MIRROR` | `http://mirrors.tuna.tsinghua.edu.cn/debian` | Python、Blender、Java、图形运行库 | +| `DEBIAN_SECURITY_MIRROR` | `http://mirrors.tuna.tsinghua.edu.cn/debian-security` | Debian 安全更新 | +| `NPM_REGISTRY` | `https://registry.npmmirror.com` | Codex CLI 及匹配平台的原生包 | +| `PIP_INDEX_URL` | `https://mirrors.ustc.edu.cn/pypi/simple` | FastAPI、Uvicorn 及 Python 依赖 | + +Debian 12 的 DEB822 `.sources` 文件及传统 `.list` 都会换源;仍使用 Debian 自带的包签名校验。APT 默认使用 HTTP 以支持尚未安装 CA 证书的 slim 基础镜像,npm/pip 保持 HTTPS 验证。不要通过关闭验证或添加 `trusted-host` 处理网络问题。 + +这些是**构建参数**,放在后端运行时 `.env.local` 中不会自动作用于镜像构建。修改 Compose 的 `build.args`,或直接构建时传 `--build-arg`。例如回到官方源: + +```bash +docker build -f codex_pcb/Dockerfile -t vibemake-pcb-backend:local \ + --build-arg DEBIAN_MIRROR=http://deb.debian.org/debian \ + --build-arg DEBIAN_SECURITY_MIRROR=http://deb.debian.org/debian-security \ + --build-arg NPM_REGISTRY=https://registry.npmjs.org \ + --build-arg PIP_INDEX_URL=https://pypi.org/simple . +``` + +某个固定版本尚未同步到镜像站时,可单独切换对应源,无需更改版本。`BASE_IMAGE` 可指定你自己可信仓库中的 Node 22 Bookworm slim 镜像;`FREEROUTING_URL` 和 `FREEROUTING_LICENSE_URL` 可指向自己维护的下载地址,JAR 校验保持不变。 + +基础镜像 `node:22-bookworm-slim` 在执行 Dockerfile 的 `RUN` 之前拉取,不受上述 apt/npm/pip 源控制。若这一步慢,在 1Panel「容器 → 配置 → 镜像加速」中配置镜像加速器;[1Panel 官方文档](https://1panel-dev.github.io/docs/v2/user_manual/containers/setting/)当前列出 `https://docker.1panel.live`。已成功拉取的基础镜像会复用本地缓存。 + +镜像构建使用 BuildKit 缓存 APT、npm 和 pip 下载,并将三类安装拆成独立步骤;npm 或 Python 安装失败时不必重新安装已经完成的系统依赖。换源仅对下一次构建生效,已经运行的构建不会自动切换下载地址。更新部署文件后重新执行 `docker compose -f codex_pcb/compose.yaml up -d --build` 即可,不要加 `--no-cache`。 + +来源:[清华 Debian 镜像说明](https://mirrors.tuna.tsinghua.edu.cn/help/debian/)、[中科大 PyPI 镜像说明](https://mirrors.ustc.edu.cn/help/pypi.html)、[npmmirror 使用说明](https://npmmirror.com/)。 + +## 5. 验收并接上现有网站 + +先检查服务器本机接口: + +```bash +curl -f http://127.0.0.1:8003/api/health +docker compose -f codex_pcb/compose.yaml exec pcb-backend codex --version +docker compose -f codex_pcb/compose.yaml exec pcb-backend codex sandbox -- /bin/true +docker compose -f codex_pcb/compose.yaml exec pcb-backend /opt/lceda-pro/lceda-pro --help --no-sandbox +docker compose -f codex_pcb/compose.yaml exec pcb-backend /opt/lceda-pro/lceda-pro session list --no-sandbox +``` + +`codex sandbox -- /bin/true` 不调用模型,用于检查目标 Linux/Docker 是否允许 Codex 创建本机沙箱。若出现 namespace、bubblewrap、Landlock 或 seccomp 错误,需要先核对宿主机内核与容器策略;这与 Key 无关。此模板保留原生沙箱,没有添加 `privileged` 或自动关闭 Codex 沙箱。该项必须在目标服务器验收,当前本机的配置解析不能替代它。 + +如果使用 `scripts/serve.mjs`,同机前端服务设置 `PCB_API_TARGET=http://127.0.0.1:8003`。若前端也在 Docker 的桥接网络中,改为同网络下的 `http://pcb-backend:8003`。 + +1Panel OpenResty 使用宿主机网络时,网站增加以下规则;若已有同路径代理,请更新原规则,避免重复: + +```nginx +location ^~ /pcb/pcb-api/ { + proxy_pass http://127.0.0.1:8003/api/; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_buffering off; + proxy_read_timeout 3600s; +} +``` + +OpenResty 若使用桥接网络,其 `127.0.0.1` 指向 OpenResty 自身。此时把 PCB 和 OpenResty 加入同一个 Docker 网络,用 `http://pcb-backend:8003/api/` 转发。例如确认 OpenResty 已在 `1panel-network` 后,在 Compose 最外层添加: + +```yaml +networks: + default: + external: true + name: 1panel-network +``` + +网络名以面板实际配置为准。改动后重建容器;无需将 8003 公开到互联网。当前 API 为共享任务服务,对外开放网站前仍需服务端认证和任务归属校验。 + +最后通过网站新建方案、审核启动,验证完整的绘板及交付下载。HTTP 健康并不等于原生绘板成功。 + +## 数据、备份与升级 + +`pcb-data` 命名卷挂载到 `/data`,包含任务、交付、Codex 会话和以 `/data/home` 为 HOME 的 EDA 用户数据。默认 Compose 项目名为 `vibemake-pcb`,卷名通常为 `vibemake-pcb_pcb-data`。不要执行 `docker compose down -v`;该操作会删除数据卷。改用宿主机 bind mount 时,目录需允许 UID/GID `10001:10001` 写入。 + +更新代码后重新生成上传包,替换源码、文档及部署文件,保留服务器的 `.env.local`、`vendor/` 和命名卷,然后重新执行 `up -d --build`。有已批准任务时先完成该任务再升级;旧任务的源码冻结校验不能通过修改哈希绕过。 + +新增空卷时入口会将 `/data/resources` 链接到镜像中的 `/opt/pcb-resources`,文档随镜像升级;如果迁移现有运行目录,则保留原有 `resources/` 并检查其文档和路由器路径。独立更改 `PCB_CODEX_HOME` 或 EDA 工程数据位置时,也要额外挂载相应数据。 + +参考:[1Panel 编排](https://1panel-dev.github.io/docs/v2/user_manual/containers/compose/)、[Docker 构建上下文](https://docs.docker.com/build/concepts/context/)、[嘉立创 CLI](https://prodocs.lceda.cn/cn/api/guide/cli.html)。 diff --git a/codex_pcb/Dockerfile b/codex_pcb/Dockerfile new file mode 100644 index 0000000..e18ea96 --- /dev/null +++ b/codex_pcb/Dockerfile @@ -0,0 +1,95 @@ +# Build from the repository/deployment-bundle root: +# docker build -f codex_pcb/Dockerfile -t vibemake-pcb-backend:local . +ARG BASE_IMAGE=node:22-bookworm-slim +FROM ${BASE_IMAGE} + +ARG DEBIAN_MIRROR=http://mirrors.tuna.tsinghua.edu.cn/debian +ARG DEBIAN_SECURITY_MIRROR=http://mirrors.tuna.tsinghua.edu.cn/debian-security + +ENV DEBIAN_FRONTEND=noninteractive \ + PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 \ + PATH=/opt/venv/bin:$PATH + +RUN set -eu; \ + for source in /etc/apt/sources.list /etc/apt/sources.list.d/*.sources /etc/apt/sources.list.d/*.list; do \ + [ -f "$source" ] || continue; \ + sed -i \ + -e "s|https://deb[.]debian[.]org/debian-security|${DEBIAN_SECURITY_MIRROR%/}|g" \ + -e "s|http://deb[.]debian[.]org/debian-security|${DEBIAN_SECURITY_MIRROR%/}|g" \ + -e "s|https://security[.]debian[.]org/debian-security|${DEBIAN_SECURITY_MIRROR%/}|g" \ + -e "s|http://security[.]debian[.]org/debian-security|${DEBIAN_SECURITY_MIRROR%/}|g" \ + -e "s|https://deb[.]debian[.]org/debian|${DEBIAN_MIRROR%/}|g" \ + -e "s|http://deb[.]debian[.]org/debian|${DEBIAN_MIRROR%/}|g" \ + "$source"; \ + done + +# BuildKit caches survive a failed build; no package cache is added to the image. +RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ + --mount=type=cache,target=/var/lib/apt/lists,sharing=locked \ + rm -f /etc/apt/apt.conf.d/docker-clean \ + && apt-get -o Acquire::Retries=3 update \ + && apt-get install -y --no-install-recommends \ + python3 python3-venv ca-certificates curl tini \ + openjdk-17-jre blender xvfb xauth x11-utils dbus-x11 \ + libasound2 libatk-bridge2.0-0 libgtk-3-0 libnss3 libxss1 \ + libgbm1 libdrm2 libx11-xcb1 libxtst6 libsecret-1-0 libfuse2 \ + fonts-noto-cjk \ + && python3 -m venv /opt/venv + +ARG CODEX_VERSION=0.169.0-alpha.11.1 +ARG NPM_REGISTRY=https://registry.npmmirror.com +RUN --mount=type=cache,target=/root/.npm \ + npm install -g --registry="${NPM_REGISTRY}" "@openai/codex@${CODEX_VERSION}" + +COPY codex_pcb/requirements.txt /tmp/pcb-requirements.txt +ARG PIP_INDEX_URL=https://mirrors.ustc.edu.cn/pypi/simple +RUN --mount=type=cache,target=/root/.cache/pip \ + python -m pip install --index-url "${PIP_INDEX_URL}" -r /tmp/pcb-requirements.txt + +COPY backend/easyeda_backend/skills/easyeda-api/references/ /opt/pcb-resources/references/ +COPY backend/easyeda_backend/skills/easyeda-api/SKILL.md /usr/share/doc/vibemake-pcb/easyeda-api-provenance.md +COPY LICENSE codex_pcb/THIRD-PARTY.md /usr/share/doc/vibemake-pcb/ +COPY codex_pcb/build-assets/ /tmp/pcb-build-assets/ +ARG FREEROUTING_SHA256=9084a4888937a7f31f857ecc12aa7a37407f51160e4d2892dff9c9bb47ae3102 +ARG FREEROUTING_URL=https://github.com/freerouting/freerouting/releases/download/v1.9.0/freerouting-1.9.0.jar +ARG FREEROUTING_LICENSE_URL=https://raw.githubusercontent.com/freerouting/freerouting/v1.9.0/LICENSE +RUN if [ -s /tmp/pcb-build-assets/freerouting.jar ]; then \ + cp /tmp/pcb-build-assets/freerouting.jar /opt/pcb-resources/freerouting.jar; \ + else \ + curl --fail --location --retry 3 "${FREEROUTING_URL}" \ + --output /opt/pcb-resources/freerouting.jar; \ + fi \ + && echo "${FREEROUTING_SHA256} /opt/pcb-resources/freerouting.jar" | sha256sum --check - \ + && if [ -s /tmp/pcb-build-assets/LICENSE.freerouting ]; then \ + cp /tmp/pcb-build-assets/LICENSE.freerouting /usr/share/doc/vibemake-pcb/LICENSE.freerouting; \ + else \ + curl --fail --location --retry 3 "${FREEROUTING_LICENSE_URL}" \ + --output /usr/share/doc/vibemake-pcb/LICENSE.freerouting; \ + fi \ + && useradd --uid 10001 --create-home --shell /bin/bash pcb \ + && mkdir -p /data/home /data/codex-home /data/tasks \ + && chown -R pcb:pcb /data + +WORKDIR /app +COPY codex_pcb/*.py codex_pcb/instructions.md /app/codex_pcb/ +COPY --chmod=755 codex_pcb/docker-entrypoint.sh /usr/local/bin/pcb-entrypoint + +ENV HOME=/data/home \ + PCB_SESSION_ROOT=/data \ + PCB_CODEX_HOME=/data/codex-home \ + PCB_CODEX_AUTH=apikey \ + PCB_CODEX_BIN=/usr/local/bin/codex \ + PCB_EDA_BIN=/opt/lceda-pro/lceda-pro \ + PCB_BLENDER_BIN=/usr/bin/blender \ + PCB_FREEROUTING_JAR=/opt/pcb-resources/freerouting.jar \ + DISPLAY=:99 \ + XDG_RUNTIME_DIR=/tmp/pcb-xdg \ + LIBGL_ALWAYS_SOFTWARE=1 + +USER pcb +EXPOSE 8003 +HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \ + CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8003/api/health', timeout=4).read()"] +ENTRYPOINT ["/usr/bin/tini", "-g", "--", "/usr/local/bin/pcb-entrypoint"] +CMD ["python", "-m", "uvicorn", "codex_pcb.app:app", "--host", "0.0.0.0", "--port", "8003", "--workers", "1"] diff --git a/codex_pcb/Dockerfile.dockerignore b/codex_pcb/Dockerfile.dockerignore new file mode 100644 index 0000000..f823229 --- /dev/null +++ b/codex_pcb/Dockerfile.dockerignore @@ -0,0 +1,21 @@ +# Allow only backend source and standalone API references into the build. +** +!LICENSE +!codex_pcb/ +!codex_pcb/*.py +!codex_pcb/instructions.md +!codex_pcb/requirements.txt +!codex_pcb/Dockerfile +!codex_pcb/docker-entrypoint.sh +!codex_pcb/THIRD-PARTY.md +!codex_pcb/build-assets/ +!codex_pcb/build-assets/README.md +!codex_pcb/build-assets/freerouting.jar +!codex_pcb/build-assets/LICENSE.freerouting +!backend/ +!backend/easyeda_backend/ +!backend/easyeda_backend/skills/ +!backend/easyeda_backend/skills/easyeda-api/ +!backend/easyeda_backend/skills/easyeda-api/SKILL.md +!backend/easyeda_backend/skills/easyeda-api/references/ +!backend/easyeda_backend/skills/easyeda-api/references/** diff --git a/codex_pcb/THIRD-PARTY.md b/codex_pcb/THIRD-PARTY.md new file mode 100644 index 0000000..3e6e478 --- /dev/null +++ b/codex_pcb/THIRD-PARTY.md @@ -0,0 +1,24 @@ +# Backend deployment dependencies + +VibeMake PCB Studio source is distributed under the repository's GPL-3.0 license; +the deployment bundle includes the original `LICENSE`. + +The standalone EDA API references are copied from the bundled +`backend/easyeda_backend/skills/easyeda-api` 1.1.56 snapshot. Upstream: +https://github.com/easyeda/easyeda-api-skill . Its SKILL.md declares MIT and +attributes the package to JLCEDA; the image preserves that source metadata. +The legacy design runtime is not included. + +Freerouting 1.9.0 is downloaded when building the image, or included in the upload +archive with `package-pcb-backend.py --with-freerouting`; it is not committed to Git. +Upstream and corresponding source: https://github.com/freerouting/freerouting/tree/v1.9.0 +Binary: https://github.com/freerouting/freerouting/releases/download/v1.9.0/freerouting-1.9.0.jar +SHA-256: `9084a4888937a7f31f857ecc12aa7a37407f51160e4d2892dff9c9bb47ae3102`. +License: GPL-3.0. The image includes the upstream license in +`/usr/share/doc/vibemake-pcb/LICENSE.freerouting`. + +Codex CLI: https://github.com/openai/codex (Apache-2.0), installed from its pinned +npm package. Debian packages retain their respective notices under +`/usr/share/doc/`. The EasyEDA application is supplied separately by the deployer +and mounted read-only; its commercial application and component libraries are +not part of this source bundle. diff --git a/codex_pcb/app.py b/codex_pcb/app.py index f8d181c..c3ee5e5 100644 --- a/codex_pcb/app.py +++ b/codex_pcb/app.py @@ -1,6 +1,7 @@ import asyncio import contextlib import json +import shlex import time import uuid from contextlib import asynccontextmanager @@ -8,6 +9,7 @@ from pathlib import Path from fastapi import FastAPI, HTTPException from fastapi.responses import FileResponse from pydantic import BaseModel, Field +from .config import BLENDER_BIN, FREEROUTING_JAR, PROVIDER from .storage import SOURCE, ROOT, TASKS, MODEL, EFFORT, task_dir, workspace, load, save, write, event, events, fingerprint, digest, inside from .rpc import AppServer from .eda import execute @@ -43,7 +45,7 @@ def public(t): def frozen(t): if t.get('approval') and fingerprint()!=t['frozen_source']: - raise RuntimeError('Backend source changed after approval. This acceptance run is invalid; start a new task.') + raise RuntimeError('Backend source or runtime settings changed after approval. This acceptance run is invalid; start a new task.') async def handle(t, method, p): if method != 'item/tool/call': @@ -112,10 +114,12 @@ async def handle(t, method, p): async def run_turn(t, prompt, production): frozen(t) + if t.get('model', MODEL) != MODEL or t.get('effort', EFFORT) != EFFORT or (t.get('thread_id') and t.get('provider', 'openai') != PROVIDER): + raise RuntimeError('Task model, effort or provider differs from the current runtime; restore the previous settings or start a new task.') directory=workspace(t) async with AppServer(directory, production, lambda m,p:handle(t,m,p)) as client: CLIENTS[t['id']]=client - common={'model':MODEL,'modelProvider':'openai','cwd':str(directory),'approvalPolicy':'never','sandbox':'workspace-write' if production else 'read-only','developerInstructions':(SOURCE/'instructions.md').read_text(),'serviceTier':'default'} + common={'model':MODEL,'modelProvider':PROVIDER,'cwd':str(directory),'approvalPolicy':'never','sandbox':'workspace-write' if production else 'read-only','developerInstructions':(SOURCE/'instructions.md').read_text(),'serviceTier':'default'} if t.get('thread_id'): result=await client.call('thread/resume',dict(common,threadId=t['thread_id'],excludeTurns=True)) if result['thread']['id']!=t['thread_id']: @@ -257,7 +261,7 @@ async def list_sessions(): @app.post('/api/sessions',status_code=201) async def create(req:Request): tid='pcb-'+uuid.uuid4().hex[:24] - t={'id':tid,'request':req.text,'created_at':time.time(),'state':'planning','cursor':0,'model':MODEL,'effort':EFFORT,'busy':False} + t={'id':tid,'request':req.text,'created_at':time.time(),'state':'planning','cursor':0,'model':MODEL,'effort':EFFORT,'provider':PROVIDER,'busy':False} directory=workspace(t);directory.mkdir(parents=True) for name in ('artifacts','evidence','scripts','tmp'): (directory/name).mkdir() @@ -303,8 +307,8 @@ async def approve(tid:str,req:Approval): This server-owned approval authorizes production of the accepted proposal in this same persistent session. Earlier requests to wait applied only to review. Workspace: {workspace(t)} Official API documentation: {workspace(t)}/api-reference/references/_index.md -External router launcher: python3 {SOURCE}/freerouting.py --input --output --jar {ROOT}/resources/freerouting.jar --timeout 900 -- -mp 30 -mt 1 -Use this launcher for Freerouting, including --help. It validates paths, skips the startup countdown and runs on a private virtual display. Choose routing options and timeout according to the board; do not invoke the JAR directly. Blender: /snap/bin/blender. +External router launcher: python3 {shlex.quote(str(SOURCE/'freerouting.py'))} --input --output --jar {shlex.quote(str(FREEROUTING_JAR))} --timeout 900 -- -mp 30 -mt 1 +Use this launcher for Freerouting, including --help. It validates paths, skips the startup countdown and runs on a private virtual display. Choose routing options and timeout according to the board; do not invoke the JAR directly. Blender command: {shlex.quote(BLENDER_BIN)}. Now use native Codex tools and pcb_eda to implement, inspect, fix, route, verify and export the real board. You write the design scripts yourself. No fixed backend design pipeline exists. Keep working until pcb_deliver accepts native project, schematic, layout, assembled STEP and real 3D render. Do not edit backend code or use legacy PCB flows. Backend source is frozen for this run. Accepted proposal:\n{p['markdown']}''' schedule(t,prompt,True) diff --git a/codex_pcb/build-assets/README.md b/codex_pcb/build-assets/README.md new file mode 100644 index 0000000..acc3026 --- /dev/null +++ b/codex_pcb/build-assets/README.md @@ -0,0 +1,12 @@ +# Optional local build downloads + +The Dockerfile uses `freerouting.jar` and `LICENSE.freerouting` from this directory +when present, otherwise it downloads the pinned release. The JAR is always +verified against the SHA-256 in the Dockerfile. + +Run `python3 scripts/package-pcb-backend.py --with-freerouting` on a machine that +can reach GitHub to include these files in the upload archive. The download is +cached under `dist/pcb-backend-downloads`, not committed into source control. + +Debian, Python and npm downloads use the configurable mirrors in +`codex_pcb/compose.yaml`. No API keys belong in this directory or build args. diff --git a/codex_pcb/compose.yaml b/codex_pcb/compose.yaml new file mode 100644 index 0000000..3a526a5 --- /dev/null +++ b/codex_pcb/compose.yaml @@ -0,0 +1,36 @@ +name: vibemake-pcb + +services: + pcb-backend: + image: vibemake-pcb-backend:local + build: + context: .. + dockerfile: codex_pcb/Dockerfile + args: + DEBIAN_MIRROR: http://mirrors.tuna.tsinghua.edu.cn/debian + DEBIAN_SECURITY_MIRROR: http://mirrors.tuna.tsinghua.edu.cn/debian-security + NPM_REGISTRY: https://registry.npmmirror.com + PIP_INDEX_URL: https://mirrors.ustc.edu.cn/pypi/simple + restart: unless-stopped + env_file: + - .env.local + ports: + - "127.0.0.1:8003:8003" + volumes: + - pcb-data:/data + - type: bind + source: ../vendor/lceda-pro + target: /opt/lceda-pro + read_only: true + bind: + create_host_path: false + shm_size: "1gb" + stop_grace_period: 30s + logging: + driver: json-file + options: + max-size: "10m" + max-file: "3" + +volumes: + pcb-data: diff --git a/codex_pcb/config.py b/codex_pcb/config.py new file mode 100644 index 0000000..e231eaa --- /dev/null +++ b/codex_pcb/config.py @@ -0,0 +1,36 @@ +"""Deployment settings; credentials are read separately and never fingerprinted.""" +import os +from pathlib import Path +import shutil + + +def path_setting(name, default): + return Path(os.environ.get(name) or default).expanduser().resolve() + + +def executable(name, default): + value = os.environ.get(name) or default + return shutil.which(os.path.expanduser(value)) or os.path.expanduser(value) + + +ROOT = path_setting('PCB_SESSION_ROOT', Path.home() / '.local/share/vibemake-pcb') +MODEL = os.environ.get('PCB_MODEL') or 'gpt-6.1-sol' +EFFORT = os.environ.get('PCB_REASONING_EFFORT') or 'high' +# Preserve existing installations unless API authentication is explicitly selected. +AUTH_MODE = os.environ.get('PCB_CODEX_AUTH') or 'chatgpt' +PROVIDER = 'vibemake_openai' if AUTH_MODE == 'apikey' else 'openai' +CODEX_HOME = path_setting('PCB_CODEX_HOME', ROOT / 'codex-home') +CODEX_BIN = executable('PCB_CODEX_BIN', 'codex') +EDA_BIN = executable('PCB_EDA_BIN', 'lceda-pro') +BLENDER_BIN = executable('PCB_BLENDER_BIN', 'blender') +FREEROUTING_JAR = path_setting('PCB_FREEROUTING_JAR', ROOT / 'resources/freerouting.jar') + + +def frozen_settings(): + # Key rotation is allowed; changing the runtime of an approved design is not. + return {name: str(value) for name, value in { + 'root': ROOT, 'model': MODEL, 'effort': EFFORT, 'auth_mode': AUTH_MODE, + 'provider': PROVIDER, 'codex_home': CODEX_HOME, 'codex_bin': CODEX_BIN, + 'eda_bin': EDA_BIN, 'blender_bin': BLENDER_BIN, + 'freerouting_jar': FREEROUTING_JAR, + }.items()} diff --git a/codex_pcb/docker-entrypoint.sh b/codex_pcb/docker-entrypoint.sh new file mode 100755 index 0000000..92ffaba --- /dev/null +++ b/codex_pcb/docker-entrypoint.sh @@ -0,0 +1,63 @@ +#!/bin/sh +set -eu + +fail() { + printf '%s\n' "PCB backend: $*" >&2 + exit 1 +} + +# Allow diagnostics such as `docker compose run --rm pcb-backend codex --version`. +if [ "${1:-}" != python ] || [ "${2:-}" != -m ] || [ "${3:-}" != uvicorn ]; then + exec "$@" +fi + +mkdir -p "$PCB_SESSION_ROOT" "$PCB_CODEX_HOME" "$HOME" "$XDG_RUNTIME_DIR" +chmod 700 "$XDG_RUNTIME_DIR" +[ -w "$PCB_SESSION_ROOT" ] || fail 'Runtime volume is not writable by UID 10001.' + +if [ ! -e "$PCB_SESSION_ROOT/resources" ] && [ ! -L "$PCB_SESSION_ROOT/resources" ]; then + ln -s /opt/pcb-resources "$PCB_SESSION_ROOT/resources" +fi +[ -r "$PCB_SESSION_ROOT/resources/references/_index.md" ] || fail 'Missing EDA API references.' +[ -r "$PCB_FREEROUTING_JAR" ] || fail 'Missing Freerouting JAR.' +command -v "$PCB_CODEX_BIN" >/dev/null 2>&1 || fail 'Codex CLI not found; check PCB_CODEX_BIN.' +command -v "$PCB_EDA_BIN" >/dev/null 2>&1 || fail 'Mount the complete Linux EDA directory and check PCB_EDA_BIN.' +command -v "$PCB_BLENDER_BIN" >/dev/null 2>&1 || fail 'Blender not found; check PCB_BLENDER_BIN.' + +# Validate credential presence without printing or writing its value. +python -c 'from codex_pcb.rpc import process_config; process_config(False)' + +Xvfb "$DISPLAY" -screen 0 1920x1080x24 -nolisten tcp > /tmp/pcb-xvfb.log 2>&1 & +xvfb_pid=$! +app_pid= +cleanup() { + if [ -n "$app_pid" ]; then + kill -TERM "$app_pid" 2>/dev/null || true + wait "$app_pid" 2>/dev/null || true + fi + kill -TERM "$xvfb_pid" 2>/dev/null || true + wait "$xvfb_pid" 2>/dev/null || true +} +trap cleanup EXIT +trap 'exit 143' TERM +trap 'exit 130' INT + +ready=0 +for attempt in 1 2 3 4 5 6 7 8 9 10; do + kill -0 "$xvfb_pid" 2>/dev/null || fail 'Xvfb exited; inspect /tmp/pcb-xvfb.log.' + if xdpyinfo -display "$DISPLAY" >/dev/null 2>&1; then + ready=1 + break + fi + sleep 1 +done +[ "$ready" = 1 ] || fail 'Virtual display did not become ready.' + +"$@" & +app_pid=$! +set +e +wait "$app_pid" +status=$? +set -e +app_pid= +exit "$status" diff --git a/codex_pcb/eda.py b/codex_pcb/eda.py index 881c2a2..d87ea8d 100644 --- a/codex_pcb/eda.py +++ b/codex_pcb/eda.py @@ -6,17 +6,20 @@ import json import os import time from pathlib import Path +from .config import EDA_BIN from .storage import ROOT, workspace, task_dir, inside, save, write, event, digest EDA_LOCK = asyncio.Lock() async def cli(args, code=None, timeout=210): - env = dict(os.environ, DISPLAY=os.environ.get('DISPLAY',':0'), XDG_RUNTIME_DIR=f'/run/user/{os.getuid()}') + env = dict(os.environ, DISPLAY=os.environ.get('DISPLAY',':0'), XDG_RUNTIME_DIR=os.environ.get('XDG_RUNTIME_DIR') or f'/run/user/{os.getuid()}') + for key in ('OPENAI_API_KEY', 'OPENAI_API_KEY_FILE', 'DEEPSEEK_API_KEY', 'CODEX_API_KEY'): + env.pop(key, None) if not env.get('XAUTHORITY'): auth = list(Path(f'/run/user/{os.getuid()}').glob('.mutter-Xwaylandauth.*')) if auth: env['XAUTHORITY'] = str(auth[0]) - p = await asyncio.create_subprocess_exec('/usr/local/bin/lceda-pro', *args, '--no-sandbox', stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=env, cwd=ROOT) + p = await asyncio.create_subprocess_exec(EDA_BIN, *args, '--no-sandbox', stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, env=env, cwd=ROOT) try: out, err = await asyncio.wait_for(p.communicate(code.encode() if code is not None else None),timeout) except (asyncio.TimeoutError, asyncio.CancelledError): diff --git a/codex_pcb/freerouting.py b/codex_pcb/freerouting.py index 514aef5..861d2ed 100644 --- a/codex_pcb/freerouting.py +++ b/codex_pcb/freerouting.py @@ -59,7 +59,8 @@ def main(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--input', required=True, type=Path, help='Existing DSN input') parser.add_argument('--output', required=True, type=Path, help='New SES output; existing files are refused') - default_jar = Path(os.environ.get('PCB_SESSION_ROOT', '/home/wpz/auto_pcb_2/persistent-runtime')) / 'resources/freerouting.jar' + root = Path(os.environ.get('PCB_SESSION_ROOT') or Path.home() / '.local/share/vibemake-pcb').expanduser().resolve() + default_jar = root / 'resources/freerouting.jar' parser.add_argument('--jar', type=Path, default=os.environ.get('PCB_FREEROUTING_JAR', str(default_jar))) parser.add_argument('--timeout', type=float, default=900, help='Maximum process seconds (default 900); partial output is not accepted') parser.add_argument('--visible', action='store_true', help='Diagnostic only: use the current desktop instead of private Xvfb') @@ -67,7 +68,7 @@ def main(argv=None): args = parser.parse_args(argv) try: options = router_options(args.router_args) - source, target, jar = args.input.resolve(), args.output.resolve(), args.jar.resolve() + source, target, jar = args.input.resolve(), args.output.resolve(), args.jar.expanduser().resolve() if not math.isfinite(args.timeout) or args.timeout <= 0: raise ValueError('--timeout must be a positive finite number') if not source.is_file() or source.suffix.lower() != '.dsn': diff --git a/codex_pcb/requirements.txt b/codex_pcb/requirements.txt new file mode 100644 index 0000000..22057fe --- /dev/null +++ b/codex_pcb/requirements.txt @@ -0,0 +1,2 @@ +fastapi==0.162.0 +uvicorn==0.54.0 diff --git a/codex_pcb/rpc.py b/codex_pcb/rpc.py index ffb56d3..d8c1dc1 100644 --- a/codex_pcb/rpc.py +++ b/codex_pcb/rpc.py @@ -3,9 +3,56 @@ import asyncio import json import os from pathlib import Path -from .storage import ROOT, MODEL, EFFORT +from .config import AUTH_MODE, CODEX_BIN, CODEX_HOME, MODEL, EFFORT, PROVIDER + + +def api_key(): + key = os.environ.get('OPENAI_API_KEY', '').strip() + if not key and os.environ.get('OPENAI_API_KEY_FILE'): + try: + with Path(os.environ['OPENAI_API_KEY_FILE']).expanduser().open() as stream: + key = stream.read(16385).strip() + except (OSError, UnicodeError): + raise ValueError('Cannot read OPENAI_API_KEY_FILE') from None + if not key: + raise ValueError('PCB_CODEX_AUTH=apikey requires OPENAI_API_KEY or OPENAI_API_KEY_FILE') + if len(key) > 16384 or any(c.isspace() for c in key): + raise ValueError('OpenAI API key must be a single non-empty value') + return key + + +def process_config(production): + if AUTH_MODE not in {'apikey', 'chatgpt'}: + raise ValueError('PCB_CODEX_AUTH must be apikey or chatgpt') + config = { + 'model': MODEL, 'model_provider': PROVIDER, 'model_reasoning_effort': EFFORT, + 'approval_policy': 'never', 'web_search': 'live', + 'sandbox_mode': 'workspace-write' if production else 'read-only', + 'features.shell_tool': production, 'features.multi_agent': False, + 'sandbox_workspace_write.network_access': True, + 'sandbox_workspace_write.exclude_slash_tmp': True, + 'sandbox_workspace_write.exclude_tmpdir_env_var': True, + 'shell_environment_policy.ignore_default_excludes': False, + } + env = dict(os.environ, CODEX_HOME=str(CODEX_HOME)) + for key in ('OPENAI_API_KEY', 'OPENAI_API_KEY_FILE', 'OPENAI_BASE_URL', + 'DEEPSEEK_API_KEY', 'CODEX_API_KEY'): + env.pop(key, None) + if AUTH_MODE == 'apikey': + env['OPENAI_API_KEY'] = api_key() + prefix = 'model_providers.' + PROVIDER + '.' + config.update({ + prefix + 'name': 'VibeMake OpenAI API', + prefix + 'base_url': 'https://api.openai.com/v1', + prefix + 'wire_api': 'responses', + prefix + 'env_key': 'OPENAI_API_KEY', + prefix + 'requires_openai_auth': False, + }) + args = [CODEX_BIN, 'app-server', '--listen', 'stdio://'] + for key, value in config.items(): + args += ['-c', key + '=' + json.dumps(value)] + return args, env -CODEX = os.environ.get('PCB_CODEX_BIN', '/home/wpz/auto_pcb_2/vibemake-pcb-studio/backend/.cache/codex-cli/node_modules/.bin/codex') class AppServer: def __init__(self, directory, production, handler): @@ -13,29 +60,21 @@ class AppServer: self.pending, self.requests, self.counter = {}, set(), 0 self.events = asyncio.Queue() self.proc = None + self.reader = None + self.err = None async def __aenter__(self): - config = { - 'model': MODEL, 'model_reasoning_effort': EFFORT, - 'approval_policy': 'never', 'web_search': 'live', - 'sandbox_mode': 'workspace-write' if self.production else 'read-only', - 'features.shell_tool': self.production, - 'features.multi_agent': False, - 'sandbox_workspace_write.network_access': True, - 'sandbox_workspace_write.exclude_slash_tmp': True, - 'sandbox_workspace_write.exclude_tmpdir_env_var': True, - } - args = [CODEX, 'app-server', '--listen', 'stdio://'] - for k,v in config.items(): - args += ['-c', k + '=' + json.dumps(v)] - env = dict(os.environ, CODEX_HOME=str(ROOT / 'codex-home')) - for key in ('OPENAI_API_KEY','DEEPSEEK_API_KEY','CODEX_API_KEY'): - env.pop(key, None) + args, env = process_config(self.production) + CODEX_HOME.mkdir(parents=True, exist_ok=True, mode=0o700) self.err = (self.directory / 'codex-stderr.log').open('ab') - self.proc = await asyncio.create_subprocess_exec(*args, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=self.err, env=env, cwd=self.directory, limit=64*1024*1024) - self.reader = asyncio.create_task(self._read()) - await self.call('initialize', {'clientInfo': {'name':'vibemake_persistent_pcb','title':'VibeMake Persistent PCB','version':'1.0.0'}, 'capabilities':{'experimentalApi':True}}) - await self.send({'method':'initialized'}) + try: + self.proc = await asyncio.create_subprocess_exec(*args, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=self.err, env=env, cwd=self.directory, limit=64*1024*1024) + self.reader = asyncio.create_task(self._read()) + await self.call('initialize', {'clientInfo': {'name':'vibemake_persistent_pcb','title':'VibeMake Persistent PCB','version':'1.0.0'}, 'capabilities':{'experimentalApi':True}}) + await self.send({'method':'initialized'}) + except BaseException: + await self.__aexit__() + raise return self async def send(self, message): @@ -100,6 +139,8 @@ class AppServer: except asyncio.TimeoutError: self.proc.kill() await self.proc.wait() - self.reader.cancel() - await asyncio.gather(self.reader,return_exceptions=True) - self.err.close() + if self.reader: + self.reader.cancel() + await asyncio.gather(self.reader,return_exceptions=True) + if self.err: + self.err.close() diff --git a/codex_pcb/storage.py b/codex_pcb/storage.py index 0d7c4f3..03f04ad 100644 --- a/codex_pcb/storage.py +++ b/codex_pcb/storage.py @@ -4,18 +4,17 @@ import os import re import time from pathlib import Path +from .config import ROOT, MODEL, EFFORT, frozen_settings SOURCE = Path(__file__).parent.resolve() -ROOT = Path(os.environ.get('PCB_SESSION_ROOT', '/home/wpz/auto_pcb_2/persistent-runtime')).resolve() TASKS = ROOT / 'tasks' -MODEL = 'gpt-6.1-sol' -EFFORT = 'high' def digest(data): return hashlib.sha256(data).hexdigest() def fingerprint(): - return digest(b''.join(p.name.encode() + p.read_bytes() for p in sorted(SOURCE.iterdir()) if p.suffix in {'.py', '.md'})) + source = b''.join(p.name.encode() + p.read_bytes() for p in sorted(SOURCE.iterdir()) if p.suffix in {'.py', '.md'}) + return digest(source + json.dumps(frozen_settings(), sort_keys=True).encode()) def write(path, value): path = Path(path) diff --git a/codex_pcb/tests/test_container_startup.py b/codex_pcb/tests/test_container_startup.py new file mode 100644 index 0000000..f3422dd --- /dev/null +++ b/codex_pcb/tests/test_container_startup.py @@ -0,0 +1,128 @@ +"""Exercise startup/shutdown using fake programs; no Docker or EDA required.""" +import os +from pathlib import Path +import signal +import subprocess +import sys +import tempfile +import time +import unittest + + +ENTRYPOINT = Path(__file__).resolve().parents[1] / 'docker-entrypoint.sh' +REPOSITORY = ENTRYPOINT.parents[1] + + +class ContainerStartup(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory() + self.addCleanup(self.temporary.cleanup) + self.root = Path(self.temporary.name) + self.bin = self.root / 'bin' + self.bin.mkdir() + resources = self.root / 'data/resources/references' + resources.mkdir(parents=True) + (resources / '_index.md').write_text('Test reference index') + self.jar = self.root / 'router.jar' + self.jar.touch() + self.env = { + 'PATH': str(self.bin) + os.pathsep + os.defpath, + 'HOME': str(self.root / 'home'), + 'PYTHONPATH': str(REPOSITORY), + 'PCB_SESSION_ROOT': str(self.root / 'data'), + 'PCB_CODEX_HOME': str(self.root / 'codex-home'), + 'PCB_CODEX_AUTH': 'apikey', + 'OPENAI_API_KEY': 'unit-test-api-credential', + 'PCB_FREEROUTING_JAR': str(self.jar), + 'PCB_CODEX_BIN': str(self.bin / 'codex'), + 'PCB_EDA_BIN': str(self.bin / 'lceda-pro'), + 'PCB_BLENDER_BIN': str(self.bin / 'blender'), + 'DISPLAY': ':99', 'XDG_RUNTIME_DIR': str(self.root / 'xdg'), + 'TEST_ROOT': str(self.root), 'APP_EXIT_CODE': '7', + } + for name in ('codex', 'lceda-pro', 'blender'): + self.program(name, 'pass\n') + self.program('python', '''import os,sys,time +from pathlib import Path +if sys.argv[1:2] == ['-c']: + os.execv(sys.executable, [sys.executable, *sys.argv[1:]]) +Path(os.environ['TEST_ROOT'],'app.pid').write_text(str(os.getpid())) +if os.environ.get('APP_WAIT'): + while True:time.sleep(1) +sys.exit(int(os.environ['APP_EXIT_CODE'])) +''') + self.program('Xvfb', '''import os,time +from pathlib import Path +Path(os.environ['TEST_ROOT'],'xvfb.pid').write_text(str(os.getpid())) +while True:time.sleep(1) +''') + self.program('xdpyinfo', '''import os,sys +from pathlib import Path +sys.exit(0 if Path(os.environ['TEST_ROOT'],'xvfb.pid').exists() else 1) +''') + + def program(self, name, body): + path = self.bin / name + path.write_text(f'#!{sys.executable}\n' + body) + path.chmod(0o755) + + def command(self): + return ['/bin/sh', str(ENTRYPOINT), 'python', '-m', 'uvicorn', 'codex_pcb.app:app'] + + def assert_process_gone(self, filename): + pid = int((self.root / filename).read_text()) + with self.assertRaises(ProcessLookupError): + os.kill(pid, 0) + + def test_diagnostic_command_bypasses_runtime_requirements(self): + result = subprocess.run(['/bin/sh', str(ENTRYPOINT), '/bin/echo', 'diagnostic'], + env={}, capture_output=True, text=True, timeout=5) + self.assertEqual(result.returncode, 0) + self.assertEqual(result.stdout.strip(), 'diagnostic') + + def test_missing_eda_fails_before_display_or_app_start(self): + (self.bin / 'lceda-pro').unlink() + result = subprocess.run(self.command(), env=self.env, cwd=REPOSITORY, + capture_output=True, text=True, timeout=5) + self.assertEqual(result.returncode, 1) + self.assertIn('Mount the complete Linux EDA directory', result.stderr) + self.assertNotIn('unit-test-api-credential', result.stderr) + self.assertFalse((self.root / 'xvfb.pid').exists()) + self.assertFalse((self.root / 'app.pid').exists()) + + def test_application_exit_preserves_status_and_stops_display(self): + result = subprocess.run(self.command(), env=self.env, cwd=REPOSITORY, + capture_output=True, text=True, timeout=8) + self.assertEqual(result.returncode, 7, result.stderr) + self.assert_process_gone('xvfb.pid') + self.assert_process_gone('app.pid') + self.assertEqual((self.root / 'xdg').stat().st_mode & 0o777, 0o700) + + def test_shutdown_signal_stops_app_and_display(self): + self.env['APP_WAIT'] = '1' + process = subprocess.Popen(self.command(), env=self.env, cwd=REPOSITORY, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + try: + deadline = time.monotonic() + 5 + while not (self.root / 'app.pid').exists() and time.monotonic() < deadline: + time.sleep(0.02) + self.assertTrue((self.root / 'app.pid').exists()) + process.send_signal(signal.SIGTERM) + stdout, stderr = process.communicate(timeout=5) + self.assertEqual(process.returncode, 143, stderr) + self.assert_process_gone('xvfb.pid') + self.assert_process_gone('app.pid') + finally: + if process.poll() is None: + process.kill() + process.communicate(timeout=5) + for filename in ('app.pid', 'xvfb.pid'): + if (self.root / filename).exists(): + try: + os.kill(int((self.root / filename).read_text()), signal.SIGTERM) + except ProcessLookupError: + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/codex_pcb/tests/test_runtime_config.py b/codex_pcb/tests/test_runtime_config.py new file mode 100644 index 0000000..804c78c --- /dev/null +++ b/codex_pcb/tests/test_runtime_config.py @@ -0,0 +1,186 @@ +"""Deployment/authentication boundaries; no real credentials or model calls.""" +import asyncio +import importlib +import json +import os +from pathlib import Path +import sys +import tempfile +import tomllib +import unittest +from unittest.mock import AsyncMock, patch + +from codex_pcb import config, eda, rpc, storage + + +FAKE_KEY = 'unit-test-api-credential' + + +def command_settings(args): + return tomllib.loads('\n'.join(args[i + 1] for i, value in enumerate(args) if value == '-c')) + + +class RuntimeConfig(unittest.IsolatedAsyncioTestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.root = Path(self.temp.name).resolve() + environment = { + 'HOME': str(self.root), 'PATH': os.defpath, + 'PCB_CODEX_AUTH': 'apikey', + 'PCB_SESSION_ROOT': str(self.root / 'runtime'), + 'PCB_CODEX_BIN': str(self.root / 'codex'), + 'PCB_EDA_BIN': str(self.root / 'native eda'), + 'PCB_BLENDER_BIN': str(self.root / 'native blender'), + } + self.environment = patch.dict(os.environ, environment, clear=True) + self.environment.start() + self.addCleanup(self.restore_environment) + self.reload() + + def reload(self): + for module in (config, storage, rpc, eda): + importlib.reload(module) + + def restore_environment(self): + self.environment.stop() + self.reload() + + def test_api_key_provider_does_not_put_credentials_in_arguments(self): + os.environ.update(OPENAI_API_KEY=FAKE_KEY, CODEX_API_KEY='unrelated', + DEEPSEEK_API_KEY='unrelated', OPENAI_BASE_URL='https://unused.invalid') + args, env = rpc.process_config(True) + settings = command_settings(args) + provider = settings['model_providers'][config.PROVIDER] + self.assertEqual(provider['base_url'], 'https://api.openai.com/v1') + self.assertEqual(provider['env_key'], 'OPENAI_API_KEY') + self.assertEqual(provider['wire_api'], 'responses') + self.assertFalse(provider['requires_openai_auth']) + self.assertEqual(settings['model_provider'], config.PROVIDER) + self.assertFalse(settings['shell_environment_policy']['ignore_default_excludes']) + self.assertEqual(env['OPENAI_API_KEY'], FAKE_KEY) + self.assertNotIn(FAKE_KEY, ' '.join(args)) + for name in ('CODEX_API_KEY', 'DEEPSEEK_API_KEY', 'OPENAI_BASE_URL'): + self.assertNotIn(name, env) + + def test_file_secret_does_not_create_login_file(self): + secret = self.root / 'api-key' + secret.write_text(FAKE_KEY + '\n') + os.environ['OPENAI_API_KEY_FILE'] = str(secret) + args, env = rpc.process_config(False) + self.assertEqual(env['OPENAI_API_KEY'], FAKE_KEY) + self.assertNotIn('OPENAI_API_KEY_FILE', env) + self.assertNotIn(FAKE_KEY, ' '.join(args)) + self.assertFalse(config.CODEX_HOME.exists()) + + def test_environment_key_takes_precedence_over_file(self): + os.environ.update(OPENAI_API_KEY=FAKE_KEY, OPENAI_API_KEY_FILE='/missing/file') + self.assertEqual(rpc.api_key(), FAKE_KEY) + + def test_missing_and_invalid_secrets_fail_without_login_fallback(self): + with self.assertRaisesRegex(ValueError, 'requires OPENAI_API_KEY'): + rpc.process_config(False) + os.environ['OPENAI_API_KEY_FILE'] = str(self.root / 'missing') + with self.assertRaisesRegex(ValueError, 'Cannot read OPENAI_API_KEY_FILE'): + rpc.process_config(False) + for value in ('two values', 'x' * 16385): + os.environ['OPENAI_API_KEY'] = value + with self.assertRaisesRegex(ValueError, 'single non-empty value'): + rpc.process_config(False) + + def test_chatgpt_mode_removes_api_credentials(self): + os.environ.update(PCB_CODEX_AUTH='chatgpt', OPENAI_API_KEY=FAKE_KEY, + OPENAI_API_KEY_FILE='/missing/file') + self.reload() + args, env = rpc.process_config(False) + settings = command_settings(args) + self.assertEqual(settings['model_provider'], 'openai') + self.assertNotIn('model_providers', settings) + self.assertNotIn('OPENAI_API_KEY', env) + self.assertNotIn('OPENAI_API_KEY_FILE', env) + self.assertEqual(env['CODEX_HOME'], str(config.ROOT / 'codex-home')) + + def test_invalid_auth_mode_is_rejected(self): + os.environ['PCB_CODEX_AUTH'] = 'typo' + self.reload() + with self.assertRaisesRegex(ValueError, 'must be apikey or chatgpt'): + rpc.process_config(False) + + def test_portable_defaults_and_explicit_model_paths(self): + for name in ('PCB_SESSION_ROOT', 'PCB_CODEX_AUTH'): + del os.environ[name] + self.reload() + self.assertEqual(config.ROOT, self.root / '.local/share/vibemake-pcb') + self.assertEqual(config.AUTH_MODE, 'chatgpt') + os.environ.update(PCB_SESSION_ROOT='~/data', PCB_CODEX_HOME='~/history', + PCB_MODEL='project-model', PCB_REASONING_EFFORT='medium', + PCB_FREEROUTING_JAR='~/router.jar') + self.reload() + self.assertEqual(config.ROOT, self.root / 'data') + self.assertEqual(config.CODEX_HOME, self.root / 'history') + self.assertEqual(config.FREEROUTING_JAR, self.root / 'router.jar') + self.assertEqual(storage.MODEL, 'project-model') + self.assertEqual(storage.EFFORT, 'medium') + + def test_approval_hash_tracks_runtime_but_allows_key_rotation(self): + before = storage.fingerprint() + os.environ['OPENAI_API_KEY'] = FAKE_KEY + self.assertEqual(before, storage.fingerprint()) + os.environ['OPENAI_API_KEY'] = 'rotated-unit-test-credential' + self.assertEqual(before, storage.fingerprint()) + os.environ['PCB_MODEL'] = 'different-model' + self.reload() + self.assertNotEqual(before, storage.fingerprint()) + + async def test_eda_preserves_display_settings_without_api_credentials(self): + os.environ.update(OPENAI_API_KEY=FAKE_KEY, OPENAI_API_KEY_FILE='/secret', + DISPLAY=':99', XDG_RUNTIME_DIR=str(self.root / 'xdg')) + process = unittest.mock.Mock(returncode=0) + process.communicate = AsyncMock(return_value=(b'{"ok":true,"value":{"alive":true}}', b'')) + with patch.object(asyncio, 'create_subprocess_exec', new=AsyncMock(return_value=process)) as spawn: + self.assertEqual(await eda.cli(['session', 'list']), {'alive': True}) + args, kwargs = spawn.call_args + self.assertEqual(args[0], str(self.root / 'native eda')) + self.assertEqual(kwargs['env']['DISPLAY'], ':99') + self.assertEqual(kwargs['env']['XDG_RUNTIME_DIR'], str(self.root / 'xdg')) + self.assertNotIn('OPENAI_API_KEY', kwargs['env']) + self.assertNotIn('OPENAI_API_KEY_FILE', kwargs['env']) + + async def test_rpc_starts_with_environment_auth_and_persistent_home(self): + os.environ['OPENAI_API_KEY'] = FAKE_KEY + binary = Path(config.CODEX_BIN) + binary.write_text(f'#!{sys.executable}\n' + '''import json,os,sys +assert os.environ.get('OPENAI_API_KEY') == 'unit-test-api-credential' +assert 'unit-test-api-credential' not in ' '.join(sys.argv) +for line in sys.stdin: + message=json.loads(line) + if 'id' in message: + result={'home':os.environ['CODEX_HOME']} if message['method']=='probe' else {} + print(json.dumps({'id':message['id'],'result':result}),flush=True) +''') + binary.chmod(0o755) + async with rpc.AppServer(self.root, False, AsyncMock()) as client: + reply = await client.call('probe', {}, timeout=5) + self.assertEqual(reply['home'], str(config.CODEX_HOME)) + self.assertIsNotNone(client.proc.returncode) + self.assertTrue(client.err.closed) + self.assertFalse((config.CODEX_HOME / 'auth.json').exists()) + + async def test_failed_initialize_closes_process_and_log(self): + os.environ['OPENAI_API_KEY'] = FAKE_KEY + binary = Path(config.CODEX_BIN) + binary.write_text(f'#!{sys.executable}\n' + '''import json,sys +for line in sys.stdin: + message=json.loads(line) + print(json.dumps({'id':message['id'],'error':{'message':'invalid configuration'}}),flush=True) +''') + binary.chmod(0o755) + client = rpc.AppServer(self.root, False, AsyncMock()) + with self.assertRaisesRegex(RuntimeError, 'invalid configuration'): + await client.__aenter__() + self.assertIsNotNone(client.proc.returncode) + self.assertTrue(client.err.closed) + + +if __name__ == '__main__': + unittest.main() diff --git a/integration/vibemake-pcb2-session.service b/integration/vibemake-pcb2-session.service index 197ebf1..6f1d12b 100644 --- a/integration/vibemake-pcb2-session.service +++ b/integration/vibemake-pcb2-session.service @@ -3,11 +3,12 @@ Description=VibeMake PCB2 persistent Codex sessions StartLimitIntervalSec=300 StartLimitBurst=5 [Service] -WorkingDirectory=/home/wpz/auto_pcb_2/vibemake-pcb-studio -Environment=PATH=/home/wpz/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/bin:/home/wpz/.local/bin:/usr/local/bin:/usr/bin:/bin:/snap/bin -Environment=PCB_SESSION_ROOT=/home/wpz/auto_pcb_2/persistent-runtime +WorkingDirectory=/opt/vibemake-pcb-studio +Environment=PATH=/usr/local/bin:/usr/bin:/bin:/snap/bin Environment=DISPLAY=:0 -ExecStart=/home/wpz/auto_pcb_2/vibemake-pcb-studio/backend/.venv/bin/python -m uvicorn codex_pcb.app:app --host 127.0.0.1 --port 8003 +# Copy codex_pcb/.env.example here and adapt paths/display to this host. +EnvironmentFile=-%h/.config/vibemake-pcb/backend.env +ExecStart=/opt/vibemake-pcb-studio/.venv/bin/python -m uvicorn codex_pcb.app:app --host 127.0.0.1 --port 8003 --workers 1 Restart=on-failure RestartSec=5 TimeoutStopSec=25 diff --git a/scripts/package-pcb-backend.py b/scripts/package-pcb-backend.py new file mode 100755 index 0000000..04a5042 --- /dev/null +++ b/scripts/package-pcb-backend.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 +"""Create a backend-only upload bundle; exclude credentials, tasks and EDA binaries.""" +import argparse +import hashlib +from pathlib import Path +import re +import shutil +import subprocess +import tarfile +import tempfile +import urllib.request + + +ROOT = Path(__file__).resolve().parents[1] +DEPLOY_FILES = ( + '.env.example', 'Dockerfile', 'Dockerfile.dockerignore', 'compose.yaml', + 'docker-entrypoint.sh', 'requirements.txt', 'instructions.md', + 'DEPLOY.md', 'THIRD-PARTY.md', + 'build-assets/README.md', +) +API_ROOT = Path('backend/easyeda_backend/skills/easyeda-api') + + +def routing_assets(): + # Read the release pin from the Dockerfile so packaging and building agree. + dockerfile = (ROOT / 'codex_pcb/Dockerfile').read_text() + checksum = re.search(r'^ARG FREEROUTING_SHA256=([0-9a-f]{64})$', dockerfile, re.M).group(1) + cache = ROOT / 'dist/pcb-backend-downloads' + cache.mkdir(parents=True, exist_ok=True) + files = [] + for name, argument in (('freerouting.jar', 'FREEROUTING_URL'), ('LICENSE.freerouting', 'FREEROUTING_LICENSE_URL')): + path = cache / name + if path.is_file() and (name != 'freerouting.jar' or hashlib.sha256(path.read_bytes()).hexdigest() == checksum): + files.append(path) + continue + url = re.search(r'^ARG ' + argument + r'=(https://\S+)$', dockerfile, re.M).group(1) + print(f'Downloading {name} from the pinned upstream release...', flush=True) + with tempfile.NamedTemporaryFile(dir=cache, delete=False) as stream: + pending = Path(stream.name) + try: + curl = shutil.which('curl') + if curl: + subprocess.run([curl, '--fail', '--location', '--silent', '--show-error', + '--retry', '3', '--connect-timeout', '15', '--max-time', '180', + '--output', str(pending), url], check=True) + else: + with urllib.request.urlopen(url, timeout=60) as response, pending.open('wb') as stream: + shutil.copyfileobj(response, stream) + if name == 'freerouting.jar' and hashlib.sha256(pending.read_bytes()).hexdigest() != checksum: + raise ValueError('Freerouting checksum mismatch; no upload bundle was changed') + if not pending.stat().st_size: + raise ValueError(f'Empty download: {name}') + if name == 'LICENSE.freerouting' and b'GNU GENERAL PUBLIC LICENSE' not in pending.read_bytes(): + raise ValueError('Freerouting license download is not a GPL license') + pending.replace(path) + finally: + pending.unlink(missing_ok=True) + files.append(path) + return files + + +def package(output, with_freerouting=False): + assets = routing_assets() if with_freerouting else [] + output = output.expanduser().resolve() + output.parent.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(prefix='pcb-backend-package-') as temporary: + bundle = Path(temporary) / 'vibemake-pcb-backend' + code = bundle / 'codex_pcb' + code.mkdir(parents=True) + sources = [ROOT / 'codex_pcb' / name for name in DEPLOY_FILES] + sources += sorted((ROOT / 'codex_pcb').glob('*.py')) + for source in sources: + target = code / source.relative_to(ROOT / 'codex_pcb') + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(source, target) + for source in assets: + shutil.copy2(source, code / 'build-assets' / source.name) + shutil.copy2(ROOT / 'LICENSE', bundle / 'LICENSE') + api = bundle / API_ROOT + api.mkdir(parents=True) + shutil.copytree(ROOT / API_ROOT / 'references', api / 'references') + shutil.copy2(ROOT / API_ROOT / 'SKILL.md', api / 'SKILL.md') + if not (api / 'references/_index.md').is_file(): + raise ValueError('The standalone EDA API reference index is missing') + vendor = bundle / 'vendor/lceda-pro' + vendor.mkdir(parents=True) + (vendor / 'PLACE-LINUX-APP-HERE.txt').write_text( + 'Place the complete Linux EasyEDA Pro application directory here.\n' + 'The configured executable must be /opt/lceda-pro/lceda-pro.\n' + 'See codex_pcb/DEPLOY.md. No application binary is bundled.\n' + ) + # Write next to the destination, so the final replacement is atomic. + with tempfile.NamedTemporaryFile(dir=output.parent, suffix='.tar.gz', delete=False) as stream: + pending = Path(stream.name) + try: + with tarfile.open(pending, 'w:gz') as archive: + archive.add(bundle, arcname=bundle.name) + pending.replace(output) + finally: + pending.unlink(missing_ok=True) + return output + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--output', type=Path, default=ROOT / 'dist/vibemake-pcb-backend.tar.gz') + parser.add_argument('--with-freerouting', action='store_true', help='Include the verified JAR and license so the server need not download from GitHub') + args = parser.parse_args() + output = package(args.output, with_freerouting=args.with_freerouting) + print(f'Backend upload bundle: {output} ({output.stat().st_size:,} bytes)') + + +if __name__ == '__main__': + main()