Add portable Mentor schematic batch exporter
This commit is contained in:
commit
40fbb1893a
26 files changed
+3622
No files matched your search
@@ -0,0 +1,8 @@
|
||||
runtime/
|
||||
dist/
|
||||
build/
|
||||
__pycache__/
|
||||
.pytest_cache/
|
||||
.superpowers/
|
||||
*.pyc
|
||||
settings.json
|
||||
@@ -0,0 +1,51 @@
|
||||
# MentorExport 原理图批量导出
|
||||
|
||||
解压整个工具文件夹后,双击「导出.cmd」,或在命令提示符中运行它。Python 和必要包已经预置,使用者无需安装 Python、pip 或设置环境变量。需要本机已有 Mentor 安装及对应可用许可。
|
||||
|
||||
## 第一次使用
|
||||
|
||||
1. 保存需要导出的原理图。
|
||||
2. 启动「导出.cmd」。自动发现 Mentor;有多套时选择版本。
|
||||
3. 输入工程根目录,递归扫描 .prj;输入结果根目录。
|
||||
4. 核对数量与原理图快照后确认。完成后可直接打开汇总、结果目录,或重试未完成项。
|
||||
|
||||
默认读取工程指定的原理图快照,通常是 DxD。工具不打开 Designer,不执行换料或 PCB 同步。源数据库只用于捕获稳定副本,各导出器在私有副本中运行。
|
||||
|
||||
## 命令行
|
||||
|
||||
导出.cmd
|
||||
导出.cmd --check
|
||||
导出.cmd --input "F:\工程目录" --output "F:\导出结果" --yes
|
||||
导出.cmd --mentor-home "D:\Mentor\7.9.5EE\SDD_HOME" --input "F:\工程目录" --yes
|
||||
导出.cmd --retry "F:\导出结果\批次_..." --yes
|
||||
|
||||
不指定参数进入问答。--yes 适合使用固定参数重复运行;发现多套 Mentor 时需要 --mentor-home 指定。--timeout 可以调整单工具等待上限,默认 60 秒。
|
||||
|
||||
按 Ctrl+C 请求停止,等待工具收尾;不要直接关闭窗口。下次使用 --retry 指定批次,可恢复未完成项。成功产物只有在输入、工具和输出摘要仍匹配时才复用;源工程变化会完整重新导出该工程。
|
||||
|
||||
退出码:0 全部请求项成功;1 含扫描问题/部分成功/失败;2 环境或输入配置错误;130 已取消。
|
||||
|
||||
## 输出内容
|
||||
|
||||
每次建立独立批次,不覆盖旧结果。工程目录包含:
|
||||
|
||||
- 01_连接网表:KeyinNetlist、RinfNetlist、EDIF Netlist、PXR。
|
||||
- 02_器件与引脚属性:ASCII 平铺有效值、分层值、CSV 关联表、WIR/OAT。
|
||||
- 03_位置与显示:EDIF Schematic。
|
||||
- 04_电气约束:CMS 约束记录、单位、默认值及覆盖标记。
|
||||
- 05_关联库件与封装:当前工程用到的库件/封装数据和索引。同库数据按指纹在批次内共享。
|
||||
- 90_运行日志:工具输出、参数、错误及收尾记录。
|
||||
|
||||
工具、模式、快照、输入/输出摘要分别保存。同名字段不互相覆盖;分层属性、平铺有效值、网表身份表和当前中央库是不同来源。先看「导出汇总.html」,再进入工程的逐项说明。
|
||||
|
||||
“成功”表示请求的导出项生成并通过产物检查,不代表换料正确、ERC 通过或全部制造几何已核验。缺工具、COM 不匹配、缺库或不支持的内容均明确列出,不使用空文件冒充成功。
|
||||
|
||||
## 兼容与边界
|
||||
|
||||
面向 Windows 10/11 x64。本地原生库读取 ABI 首先支持实测 EE7.9.5 DLL 指纹;其他安装可被发现,但不盲调未验证的库 ABI。Python 运行时、依赖与完整性清单见 runtime-dependencies.json、package-manifest.json。不要只复制启动脚本,须保留整个文件夹。
|
||||
|
||||
多根配置暂列为需明确处理的工程,不静默选择第一个。装配变体、完整字体族、钻孔/阻焊几何、复杂多 gate/可交换脚的覆盖以报告为准,不能仅凭文件生成判断已取得全部内部信息。
|
||||
|
||||
## 开发与复现
|
||||
|
||||
源码为 export.py、mentorexport 和 helpers。Python 测试使用 pytest;packaging/build_portable.py 构建预置环境和 ZIP。构建过程中从 Python.org/PyPI 下载锁版本组件,使用者运行时无需网络。厂商 EXE/DLL 不随工具分发。
|
||||
@@ -0,0 +1,57 @@
|
||||
# Mentor Batch Export Implementation Plan
|
||||
|
||||
> For agentic workers: use superpowers:subagent-driven-development. Steps use checkbox syntax.
|
||||
|
||||
Goal: ship an offline portable, interactive Windows folder exporter for saved Mentor schematics, with tool provenance and recoverable results.
|
||||
|
||||
Architecture: a standalone Python package, bundled embedded Python and pywin32, vendor tools discovered locally. The CLI owns batching and reports; the exporter owns stable clones, isolated tool execution, validation and shared library reads.
|
||||
|
||||
Tech Stack: Python 3.12 x64, pywin32 312, x86 native library helper, 32-bit Windows cscript, stdlib HTML/JSON.
|
||||
|
||||
Spec: ../specs/2026-11-06-batch-export.md, approved by the user in chat.
|
||||
|
||||
## Global Constraints
|
||||
|
||||
- No Designer launch, no mutation of source projects/libraries or permanent environment/COM registration changes.
|
||||
- Detailed schematic exports by default. Tool and mode provenance never merged away.
|
||||
- Portable Python with preset dependencies; no user-side pip.
|
||||
- Source copies exclude server address files. Owned native process cleanup is mandatory.
|
||||
- Failure/partial/unavailable/cancelled must be visible; output validation required even with exit code zero.
|
||||
- Retrying a changed source starts a complete new project attempt, never mixes source generations.
|
||||
|
||||
## Interfaces shared by tasks
|
||||
|
||||
core.read_project(path) returns a dict with string path, name, database, root_block, snapshot, library (str or None).
|
||||
core.sha256(path), core.read_text(path), core.write_json(path, value) are stdlib utilities.
|
||||
environment.discover_installations(explicit=None, cached=None) returns root/version/tools/native_compatible/com dicts.
|
||||
tools keys are ascii, csv, keyin, rinf, edif, wir, pxr, constraints_xml; values are absolute EXE paths.
|
||||
installation.com maps cms and central to dicts containing registered_path and matches.
|
||||
exports.export_project(project, installation, project_dir, shared_dir, *, previous=None, stop_event=None, timeout=45, progress=print) returns a JSON-serializable record containing status, source_fingerprint, items, and source_hashes.
|
||||
Each item has key, status (success/failed/unavailable/cancelled), reason, outputs (absolute file paths), and provenance.
|
||||
previous is an earlier exporter result; project_dir is always a new attempt directory. Successful prior items are reused only when hashes, snapshot and tool identity still match.
|
||||
exports.list_snapshots(project, installation, work_dir) may return names, without changing default selection.
|
||||
|
||||
## Task 1: export backend and helpers
|
||||
|
||||
- [x] Write behavioral tests for exit-zero/no-output, stable clones, cancellation, tool provenance and changed-input retries; run red.
|
||||
- [x] Implement exports.py, helpers, and private process boundary by reusing verified research code; no fixed sample root/path.
|
||||
- [x] Preserve ASCII flat/hier, CSV tables, WIR/OAT, KYN/RINF/PXR, EDIF net/schematic, CMS constraints with level identity.
|
||||
- [x] Export associated library data for used parts/cells once per matching library fingerprint; native ABI guard and clear partial coverage.
|
||||
- [x] Run tests green and report touched files and limitations.
|
||||
|
||||
## Task 2: environment, scanning, CLI, reporting
|
||||
|
||||
- [x] Write failing filesystem/behavior tests before implementation.
|
||||
- [x] Implement core.py with project options, recursive discovery, collision-safe names, bounded identity and stable capture utilities.
|
||||
- [x] Implement environment.py with explicit/cache/env/registry/limited conventional paths; same-install tools and COM matching.
|
||||
- [x] Implement export.py interactive/noninteractive CLI, settings, per-batch and per-project manifests, cancellation, retries and HTML summaries.
|
||||
- [x] Verify output-root recursion exclusion, duplicate names, invalid sources, changed sources and late interruption records.
|
||||
|
||||
## Task 3: portable package and acceptance
|
||||
|
||||
- [x] Build embedded Python 3.12.10 x64 from official distributable, preset locked pywin32 wheel, include runtime/third-party licenses.
|
||||
- [x] Compile custom helper x86 with static CRT; include source, no Mentor binaries.
|
||||
- [x] Create CMD entry using only relative bundled runtime; integrity validation and offline smoke check.
|
||||
- [x] Run a real two-project export, compare source hashes, inspect provenance and required artifacts, retry selected failed/unavailable items.
|
||||
- [x] Test moved folder with Python/PATH environment stripped; timeout/cancel/error tests; independent review.
|
||||
- [x] Deliver folder and ZIP with README, dependency/compatibility manifest and validation report.
|
||||
@@ -0,0 +1,154 @@
|
||||
# Mentor 批量原理图导出工具设计草案
|
||||
|
||||
日期:2026-11-06。状态:已获用户确认并实施;最终验收见 ../../validation-2026-11-06.md。
|
||||
|
||||
## 已确认要求
|
||||
|
||||
- 独立、可复制的工具文件夹,以命令行启动,中文问答式交互。
|
||||
- 递归扫描用户指定目录中的 Mentor 工程。
|
||||
- 默认 A「工程详细包」,默认读取工程配置指定的原理图快照,通常是 DxD。
|
||||
- 不同工具、同一工具的不同模式分别保存,不能把同名字段直接合并为同一事实。
|
||||
- 导出集中到一个结果根目录,按批次、工程名称及重要内容分类。
|
||||
- 自动发现 Mentor 环境。
|
||||
- Python 环境及必要依赖预置好,用户端无需 pip 安装。
|
||||
- 注重重复运行、结果核实、失败说明及重试。
|
||||
|
||||
以下为建议细则,不将其标记为用户已经逐项确认。
|
||||
|
||||
## 交付和运行环境
|
||||
|
||||
交付 ZIP,解压为工具文件夹:
|
||||
|
||||
MentorExport/
|
||||
导出.cmd
|
||||
export.py
|
||||
runtime/ 便携 Python 3.12 x64 及预装依赖
|
||||
helpers/ 自有 x86 库读取 EXE、VBS 和进程隔离模块
|
||||
licenses/
|
||||
使用说明.md
|
||||
|
||||
第一版以 Windows 10/11 x64 为目标,脚本可读、可维护。命令入口始终调用随包 runtime/python.exe,不依赖系统 Python、PATH 或 Python Launcher。必要的 pywin32 模块及原生 DLL 一起预置并固定版本;不引入界面框架。自有库读取程序按 x86 构建,CMS 和 CentralLib 的 VBS 使用 Windows 自带 32 位 cscript。
|
||||
|
||||
构建时形成依赖版本、文件和 SHA256 清单;启动验证运行环境,缺失/损坏时给出具体文件和恢复方法,不在用户端自动联网安装。发布包不携带 Mentor 厂商 EXE/DLL;目标机器必须已有可用 Mentor 安装及相应许可。必要运行时依赖在预检中实际检查。
|
||||
|
||||
保留一个用户配置文件,记住上次输入/输出路径及已选择的 Mentor 安装。优先保存到可写的工具目录;工具目录不可写时使用 LOCALAPPDATA/MentorExport。复制到另一电脑后,安装路径必须重新验证。设置仅限本工具,不永久修改用户环境变量或系统 COM 注册。
|
||||
|
||||
## Mentor 环境自动发现
|
||||
|
||||
按以下来源收集候选,规范化路径后去重:
|
||||
|
||||
1. 本次命令指定路径、上次选定且仍有效的路径。
|
||||
2. 当前进程的 SDD_HOME。
|
||||
3. 32/64 位注册表中的相关安装项、卸载信息和已注册组件路径。
|
||||
4. 常见安装根目录下的有限层级目录查找,不默认递归扫描所有磁盘。
|
||||
5. 自动发现失败时,提示用户输入安装目录。
|
||||
|
||||
每个候选核验安装根、工具文件、版本、必要依赖和导出参数能力。单个 EXE 存在不代表该安装完整。工具和依赖必须来自同一套安装,不能拼凑多个版本的 PATH。
|
||||
|
||||
只找到一套可用安装时默认选用;多套时展示版本、路径和可用导出项,让用户选一次并记住。只有本机已验证的 EE7.9.5 接口能够默认使用原生库读取 helper;其他版本需通过明确的兼容准入,未验证项展示为不可用,不盲调本机 ABI。
|
||||
|
||||
CMS/CentralLib 的 32 位 COM 注册路径要与选定安装匹配。发现其他版本注册时,不自动重新注册,相关导出项返回可解释的不可用状态;其他独立导出继续。
|
||||
|
||||
环境预检只调用已有帮助/能力接口,不启动 Designer;不因探测而打开用户工程。
|
||||
|
||||
## 交互流程
|
||||
|
||||
1. 启动自检,显示本工具、运行环境与 Mentor 版本,以及缺失能力。
|
||||
2. 输入扫描根目录,可回车沿用上次路径。
|
||||
3. 递归发现 .prj,验证其数据库、根块及原理图快照;展示发现数、可导出数和问题工程。
|
||||
4. 输入输出根目录,默认给出独立的导出结果目录;避免将输出放进源数据库。
|
||||
5. 显示摘要:工程数、输出位置、原理图快照策略、可用/缺失导出项。用户确认后开始。
|
||||
6. 按工程、按导出项顺序执行,持续显示当前工程、步骤和已完成数量。
|
||||
7. 完成后显示成功、部分成功、失败、取消数量、汇总报告路径和下一步菜单:打开结果目录、查看失败、仅重试失败项、退出。
|
||||
|
||||
命令行同时支持指定输入、输出和 Mentor 安装路径,以减少重复输入;未提供的必要参数继续问答。完整脚本重跑与失败项重试是明确区分的操作。暂不增加无人值守调度、任务数据库或界面。
|
||||
|
||||
## 工程发现与命名
|
||||
|
||||
以规范化后的 .prj 路径识别工程,不把 .dxp 与旁边 .prj 重复计数。只发现 .dxp 时提示未找到对应可用 .prj,不自动启动 Designer 做转换。
|
||||
|
||||
同名工程保留原工程名并追加源相对路径摘要,避免覆盖或混淆。扫描不进入本工具已有导出目录、私有临时目录、.git 或目录联接/符号链接;其他目录不按宽泛名称随意排除。访问失败单独列出,不静默漏掉子目录。
|
||||
|
||||
默认从工程配置取得原理图快照及根块;其他快照仅记录名称。一个 .prj 下存在多个根块/配置时要显式枚举和区分,无法确定原理图上下文时列为需选择/不支持,不能静默挑第一个。快照不能称为装配变体。
|
||||
|
||||
## 结果目录
|
||||
|
||||
导出结果/
|
||||
批次_日期时间_短标识/
|
||||
导出汇总.html
|
||||
批次记录.json
|
||||
环境信息.json
|
||||
工程A/
|
||||
工程说明.html
|
||||
工程记录.json
|
||||
01_连接网表/
|
||||
KeyinNetlist/
|
||||
RinfNetlist/
|
||||
EDIF_Netlist/
|
||||
PXR/
|
||||
02_器件与引脚属性/
|
||||
icdb2ascii_平铺有效值/
|
||||
icdb2ascii_分层值/
|
||||
icdb2csv/
|
||||
WIR_OAT/
|
||||
03_位置与显示/
|
||||
EDIF_Schematic/
|
||||
04_电气约束/
|
||||
CMS/
|
||||
05_关联库件与封装/
|
||||
工程引用与数据索引.json
|
||||
90_运行日志/
|
||||
_共享库数据/
|
||||
库指纹/
|
||||
|
||||
目录示意是完整能力集;某一项不可用时,在记录及工程说明中明确写原因,不生成会误导用户的空成功文件。
|
||||
|
||||
所有原始输出按工具和模式保留;同一字段来自工程平铺、分层、网表身份列或中央库时,分别标注。报告可以引用和并列展示,不能自动覆盖差异或把旧网表身份列改写成当前料号。
|
||||
|
||||
工程记录保存:源路径、捕获时间、输入哈希、根块/快照/配置、工具版本与指纹、参数、退出码、耗时、产物路径/大小/哈希、完整性检查、覆盖限制和错误。
|
||||
|
||||
默认结果使用已验证、可读的导出路线。编码的 HKP/约束 XML、CCZ 等研究产物不默认加入,避免用文件存在冒充字段已覆盖。
|
||||
|
||||
## 工程详细包的范围
|
||||
|
||||
连接:KYN、RINF、EDIF Netlist、PXR。属性:平铺/分层 ASCII、CSV 关联表、WIR/OAT。显示:EDIF Schematic 的实际导出内容。约束:CMS 原始记录、列约束定义、单位及默认/覆盖标记。
|
||||
|
||||
关联库以本批工程实际使用的料号和封装为范围;同一库按指纹共享读取与结果,避免每工程保存整套中央库。缺库、库件重名、关联不明确或不支持的脚号映射分别记为未取得/无法确认。不能将未验证的多 gate、多 slot、可交换脚或历史库版本映射标为通过。
|
||||
|
||||
脚本只执行导出与覆盖检查,输出不等于换料前后比较,也不等于 ERC/制造正确性认证。第一版不加入自动修图、换料或 PCB 同步。
|
||||
|
||||
## 隔离、重复运行和失败闭环
|
||||
|
||||
- 每工程先捕获稳定的已保存输入,工具只访问短路径私有副本;不复制数据库服务器地址文件。所有导出项使用同一捕获输入,必要时每工具再建运行副本。
|
||||
- 校验源文件读取前后摘要与副本摘要。数据库不可读或捕获期间变化时,该工程失败并给出保存后重试提示,不强行读取编辑中的不一致状态。
|
||||
- 子进程环境临时设置 SDD_HOME、SDD_PLATFORM、同安装 DLL 路径。私有桌面和 Job 管理自有进程,保留超时、退出及资源收尾证据。
|
||||
- 一个导出项失败,独立项继续;一个工程失败,其他工程继续。停止时先收尾当前工具,未执行项显示取消。
|
||||
- 新运行创建新批次,旧结果不覆盖。重试建立独立尝试目录,旧失败证据保留;未变的成功输出可以引用,不能冒充重新导出。
|
||||
- 仅当工程和所需库指纹仍匹配,才允许只重试失败项;输入已变时创建该工程的新完整尝试,避免混合前后状态。
|
||||
- 启动时识别未完成尝试并显示中断状态;确认旧自有进程已退出后,允许重试,不默认自动续跑。
|
||||
- 临时目录只清理本工具确认为自有、且进程已收尾的路径;日志和已发布产物保留。
|
||||
|
||||
## 结果判定
|
||||
|
||||
导出项:成功、失败、不可用、取消。成功必须同时满足可接受退出码、预期产物存在、非空/格式要求、对应快照和最小完整性检查;不能只看退出码为 0。
|
||||
|
||||
工程:全部请求项成功才为成功;有成功也有失败/不可用为部分成功;没有可用结果为失败;被停止且未执行完为取消。覆盖限制始终单独展示。
|
||||
|
||||
进程退出码建议:0 全部请求成功;1 已完成但含部分成功/失败;2 启动配置或必需环境错误;130 用户取消。HTML 和 JSON 结果始终与退出码一致。
|
||||
|
||||
## 实施顺序草案
|
||||
|
||||
1. 搭好独立交付目录和预置运行环境;完成可从任意工作目录启动的命令入口、离线依赖自检及 Mentor 发现。
|
||||
2. 完成问答、目录扫描、工程身份/配置解析和稳定输入副本。
|
||||
3. 按来源接入现有可读导出路线;补齐工程根块、快照和路径参数,移除研究脚本的固定样例路径。
|
||||
4. 完成工程所用库件/封装读取与共享指纹索引、来源标注及覆盖报告。
|
||||
5. 完成汇总页、状态、取消/超时、失败重试和中断识别。
|
||||
6. 打包并在清理 Python/PATH 环境的子进程中验收便携启动;使用真实工程副本完成端到端验证。
|
||||
|
||||
正式实施计划将在本草案定稿后细化到文件、接口、测试与验收步骤。
|
||||
|
||||
## 验收重点
|
||||
|
||||
干净环境启动(无系统 Python/pip/PATH 依赖)、中文/空格/长路径、单套/多套/失效 Mentor 路径、32 位 COM 路径不匹配、无工程、重名工程、不可访问目录、源输入变化、缺库、非零退出、退出 0 无产物、超时、取消、中断、仅重试失败、输入变更后的重试。
|
||||
|
||||
真实导出验收须逐项核对报告、原始产物、来源标识与输入哈希,确认没有启动 Designer,没有修改源工程/源库,并证明复制整个交付文件夹到另一目录后仍能运行。
|
||||
@@ -0,0 +1,35 @@
|
||||
# MentorExport 0.1.0 发布验收
|
||||
|
||||
日期:2026-11-06。本机实际环境:Windows x64、Mentor EE7.9.5。
|
||||
|
||||
交付:`F:\MentorExport-0.1.0-final` 和同名 ZIP,约 18.3 MB。Python 3.12.10 amd64、pywin32 312 已预置;Mentor 程序和 DLL 由本机发现,不随包分发。
|
||||
|
||||
## 已验证
|
||||
|
||||
- 完整自动测试:`python -X utf8 -m pytest -q`,40 passed in 19.66s。
|
||||
- 独立审查最初发现的源路径保护、清理不确定性、坐标空值、错误证据保留等问题均已修正;末轮审查无待处理发现。
|
||||
- 将最终包复制到中文及空格路径,从 `C:\Windows\Temp` 调用真实 `导出.cmd`。PATH 仅为 `C:\WINDOWS\System32`,PYTHONHOME/PYTHONPATH 设置为无效目录,实际使用随包 Python,环境自动发现及预检成功。
|
||||
- 驱动无参数中文问答:输入扫描目录、结果目录、确认和退出。递归找到两份同名工程,目录名通过摘要区分。
|
||||
- 两个真实原理图工程:Audio-D2128、Audio-T605A。输入路径长度分别为 80、315 字符,包含中文及空格。
|
||||
- 完整导出耗时 128.75 秒;两工程各 11 项成功,退出码 0。ASCII 平铺/分层、CSV、WIR/OAT、KYN、RINF、PXR、两种 EDIF、CMS、关联库件均经过产物检查。
|
||||
- 当前快照为 DxD;另行记录数据库快照名称,不切换默认原理图。
|
||||
- 导出保留 Part Label、Part Number 等原始属性及各工具来源,不合并覆盖字段。
|
||||
- 再次使用 `--retry --yes`,耗时 14.063 秒,22 项成功产物经输入、工具和输出摘要核对后全部复用。
|
||||
- 14 个工程副本文件逐字节摘要未变化;各工程记录中的原始数据库和中央库前后摘要一致。
|
||||
- 664 个发行包文件与 package-manifest.json 的摘要一致。仅显式启动导出程序和自有 helper,不启动 Designer。
|
||||
|
||||
机器可读证据:
|
||||
|
||||
`F:\MentorExport-dev\build\release-acceptance-20261106-144501\acceptance.json`
|
||||
|
||||
完整实际结果与报告:
|
||||
|
||||
`F:\MentorExport-dev\build\release-acceptance-20261106-144501\导出结果\批次_20261106_144509_6fdd91\导出汇总.html`
|
||||
|
||||
验证脚本为 `packaging/accept_portable.py`;输入引用本机研究工程副本,重复验收会建立新的时间戳目录,保留旧证据。
|
||||
|
||||
## 兼容范围
|
||||
|
||||
已验证 EE7.9.5;其他版本能参与环境发现,但原生库读取要求已验证 DLL 指纹。多根配置暂明确报告不支持。装配变体、完整制造几何、字体、多 gate/slot 和可交换脚映射未完整核验。导出成功不等于 ERC 或换料校验通过,也不承诺厂商所有内部字段均可导出。
|
||||
|
||||
便携包启动问题已在真实入口验收中修正:发布 CMD 使用 CRLF 换行,避免 chcp 65001 后 LF 文件被 CMD 错读。失败的早期验收记录保留,最终结果以以上 passed 记录为准。
|
||||
@@ -0,0 +1,16 @@
|
||||
"""Portable script bootstrap; always resolves dependencies from its own folder."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import sys
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
_dll_handles = []
|
||||
for folder in (ROOT / "runtime/Lib/site-packages/pywin32_system32",
|
||||
ROOT / "runtime/Lib/site-packages/win32"):
|
||||
if folder.is_dir() and hasattr(os, "add_dll_directory"):
|
||||
_dll_handles.append(os.add_dll_directory(str(folder)))
|
||||
|
||||
if __name__ == "__main__":
|
||||
from mentorexport.cli import main
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,6 @@
|
||||
@echo off
|
||||
setlocal
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars32.bat" >nul
|
||||
if errorlevel 1 exit /b %errorlevel%
|
||||
cl /std:c++17 /nologo /W4 /EHsc /O2 /MT "%~dp0native_read.cpp" /Fe"%~dp0native_read.exe" /Fo"%TEMP%\mentorexport-native.obj"
|
||||
exit /b %errorlevel%
|
||||
@@ -0,0 +1,91 @@
|
||||
Option Explicit
|
||||
Dim db, initResult, sheet, obj, cons, fso, output, total, seen, column, definition
|
||||
Set fso = CreateObject("Scripting.FileSystemObject")
|
||||
Set output = fso.CreateTextFile(WScript.Arguments(1), True, True)
|
||||
Set db = CreateObject("CMS.CeObjectDefsICDB")
|
||||
initResult = db.InitFromProjectFile(WScript.Arguments(2), "r", WScript.Arguments(0), WScript.Arguments(3), WScript.Arguments(4), "DxDesigner")
|
||||
output.WriteLine "{""kind"":""initialization"",""result"":" & V(initResult) & ",""read_only"":" & V(db.ReadOnly) & ",""project"":" & Q(db.PrjFile) & "}"
|
||||
If Not initResult Or LCase(db.PrjFile) <> LCase(WScript.Arguments(0)) Then
|
||||
output.Close
|
||||
WScript.Quit 3
|
||||
End If
|
||||
total = 0
|
||||
Set seen = CreateObject("Scripting.Dictionary")
|
||||
db.LoadAllDeferredPages
|
||||
For Each sheet In db.Sheets
|
||||
output.WriteLine "{""kind"":""sheet"",""name"":" & Q(sheet.Name) & "}"
|
||||
For Each column In sheet.Columns
|
||||
Set definition = column.ConstraintDef
|
||||
output.WriteLine "{""kind"":""definition"",""sheet"":" & Q(sheet.Name) & ",""name"":" & Q(definition.Name) & ",""title"":" & Q(definition.Title) & ",""description"":" & Q(definition.Description) & ",""data_type"":" & V(definition.DataType) & ",""unit_type"":" & V(definition.UnitType) & ",""unit"":" & V(definition.Unit) & ",""display_unit"":" & V(definition.DisplayUnit) & ",""default"":" & V(definition.Default) & "}"
|
||||
Next
|
||||
For Each obj In sheet.Objects
|
||||
DumpObject sheet.Name, obj
|
||||
Next
|
||||
Next
|
||||
output.WriteLine "{""kind"":""complete"",""count"":" & CStr(total) & ",""objects"":" & CStr(seen.Count) & "}"
|
||||
output.Close
|
||||
Set db = Nothing
|
||||
WScript.Echo "Constraints read: " & CStr(total)
|
||||
|
||||
Sub DumpObject(sheetName, current)
|
||||
Dim key, constraint, child, children, actual, defaultValue, overridden, resolvedDefault, errorText, unitValue, def
|
||||
key = sheetName & vbTab & current.Level & vbTab & CStr(current.AthenaId) & vbTab & current.Name
|
||||
If seen.Exists(key) Then Exit Sub
|
||||
seen.Add key, True
|
||||
If seen.Count > 100000 Then WScript.Quit 4
|
||||
For Each constraint In current.Constraints
|
||||
actual = "" : defaultValue = "" : overridden = "" : resolvedDefault = "" : errorText = ""
|
||||
unitValue = ""
|
||||
On Error Resume Next
|
||||
actual = constraint.Value
|
||||
If Err.Number <> 0 Then errorText = errorText & "Value: " & Err.Description & "; " : Err.Clear
|
||||
defaultValue = constraint.DefaultValue
|
||||
If Err.Number <> 0 Then errorText = errorText & "DefaultValue: " & Err.Description & "; " : Err.Clear
|
||||
overridden = constraint.IsOverridden
|
||||
If Err.Number <> 0 Then errorText = errorText & "IsOverridden: " & Err.Description & "; " : Err.Clear
|
||||
resolvedDefault = current.IsResolvedDefaultValue(constraint.Name, CStr(actual))
|
||||
If Err.Number <> 0 Then errorText = errorText & "IsResolvedDefaultValue: " & Err.Description & "; " : Err.Clear
|
||||
Set def = constraint.ConstraintDef
|
||||
unitValue = constraint.GetStringValue(def.Unit)
|
||||
If Err.Number <> 0 Then errorText = errorText & "GetStringValue: " & Err.Description & "; " : Err.Clear
|
||||
On Error GoTo 0
|
||||
output.WriteLine "{""kind"":""constraint"",""sheet"":" & Q(sheetName) & ",""object"":" & Q(current.Name) & ",""level"":" & Q(current.Level) & ",""athena_id"":" & Q(current.AthenaId) & ",""name"":" & Q(constraint.Name) & ",""value"":" & V(actual) & ",""default_value"":" & V(defaultValue) & ",""is_overridden"":" & V(overridden) & ",""equals_resolved_default"":" & V(resolvedDefault) & ",""value_in_definition_unit"":" & Q(unitValue) & ",""unit"":" & V(def.Unit) & ",""unit_type"":" & V(def.UnitType) & ",""error"":" & Q(errorText) & "}"
|
||||
total = total + 1
|
||||
Next
|
||||
Set children = current.GetChildObjects
|
||||
If Not children Is Nothing Then
|
||||
For Each child In children
|
||||
DumpObject sheetName, child
|
||||
Next
|
||||
End If
|
||||
Set child = Nothing
|
||||
Set children = Nothing
|
||||
End Sub
|
||||
|
||||
Function Q(value)
|
||||
Dim s
|
||||
If IsNull(value) Or IsEmpty(value) Then
|
||||
s = ""
|
||||
Else
|
||||
s = CStr(value)
|
||||
End If
|
||||
s = Replace(s, "\", "\\")
|
||||
s = Replace(s, Chr(34), "\" & Chr(34))
|
||||
s = Replace(s, vbCr, "\r")
|
||||
s = Replace(s, vbLf, "\n")
|
||||
s = Replace(s, vbTab, "\t")
|
||||
Q = Chr(34) & s & Chr(34)
|
||||
End Function
|
||||
|
||||
Function V(value)
|
||||
Select Case VarType(value)
|
||||
Case 0, 1
|
||||
V = "null"
|
||||
Case 11
|
||||
V = LCase(CStr(value))
|
||||
Case 2, 3, 4, 5, 6, 14, 17
|
||||
V = Replace(CStr(value), ",", ".")
|
||||
Case Else
|
||||
V = Q(value)
|
||||
End Select
|
||||
End Function
|
||||
@@ -0,0 +1,216 @@
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <windows.h>
|
||||
#include <bcrypt.h>
|
||||
#pragma comment(lib, "bcrypt.lib")
|
||||
// Disposable research client for the locally verified EE7.9.5 x86 PartsDB40 ABI.
|
||||
// Query a copied PDB. No CAD process or private replacement address is invoked.
|
||||
static_assert(sizeof(void *) == 4, "This research ABI requires an x86 build.");
|
||||
struct ObjectGuard {
|
||||
void *value;
|
||||
ULONG(__stdcall *release)(void *);
|
||||
~ObjectGuard() {
|
||||
if (value)
|
||||
release(value);
|
||||
}
|
||||
};
|
||||
struct ModuleGuard {
|
||||
HMODULE value;
|
||||
~ModuleGuard() {
|
||||
if (value)
|
||||
FreeLibrary(value);
|
||||
}
|
||||
};
|
||||
static void str(const char *p) {
|
||||
std::string utf8;
|
||||
if (p && *p) {
|
||||
int count =
|
||||
MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, p, -1, nullptr, 0);
|
||||
if (!count)
|
||||
throw std::runtime_error(
|
||||
"Metadata encoding is invalid for the native ANSI code page");
|
||||
std::vector<wchar_t> wide(count);
|
||||
if (!MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS, p, -1, wide.data(),
|
||||
count))
|
||||
throw std::runtime_error("ANSI decoding failed");
|
||||
int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wide.data(),
|
||||
count - 1, nullptr, 0, nullptr, nullptr);
|
||||
if (!size)
|
||||
throw std::runtime_error("UTF-8 conversion failed");
|
||||
utf8.resize(size);
|
||||
if (!WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, wide.data(),
|
||||
count - 1, utf8.data(), size, nullptr, nullptr))
|
||||
throw std::runtime_error("UTF-8 conversion failed");
|
||||
}
|
||||
putchar('"');
|
||||
for (unsigned char c : utf8) {
|
||||
if (c == '"' || c == '\\')
|
||||
printf("\\%c", c);
|
||||
else if (c < 32)
|
||||
printf("\\u%04x", c);
|
||||
else
|
||||
putchar(c);
|
||||
}
|
||||
putchar('"');
|
||||
}
|
||||
template <class T> T proc(HMODULE h, const char *name) {
|
||||
auto p = GetProcAddress(h, name);
|
||||
if (!p)
|
||||
throw std::runtime_error(name);
|
||||
return reinterpret_cast<T>(p);
|
||||
}
|
||||
using ReadString = HRESULT(__stdcall *)(void *, int, const char **);
|
||||
using ReadInt = HRESULT(__stdcall *)(void *, int, int *);
|
||||
using ReadList = HRESULT(__stdcall *)(void *, int, const int **, int *);
|
||||
using ReadPair = HRESULT(__stdcall *)(void *, int, int *, int *);
|
||||
using ReadPoint = HRESULT(__stdcall *)(void *, int, const int **);
|
||||
|
||||
static void check(HRESULT hr) {
|
||||
if (FAILED(hr)) throw std::runtime_error("Native getter failed");
|
||||
}
|
||||
static void string_field(HMODULE module, void *db, int id, const char *symbol,
|
||||
const char *field) {
|
||||
const char *value = nullptr;
|
||||
HRESULT hr = proc<ReadString>(module, symbol)(db, id, &value);
|
||||
putchar(','); str(field); printf(":{\"hr\":%ld,\"value\":", hr);
|
||||
if (SUCCEEDED(hr)) str(value); else printf("null");
|
||||
putchar('}');
|
||||
}
|
||||
static std::vector<int> ids(HMODULE module, void *db, int id, const char *symbol) {
|
||||
const int *data = nullptr; int count = 0;
|
||||
check(proc<ReadList>(module, symbol)(db, id, &data, &count));
|
||||
if (count < 0 || count > 100000 || (count && !data))
|
||||
throw std::runtime_error("Invalid native list");
|
||||
return count ? std::vector<int>(data, data + count) : std::vector<int>();
|
||||
}
|
||||
static bool verified_dll(const std::string &path, const char *expected) {
|
||||
FILE *file = nullptr;
|
||||
if (fopen_s(&file, path.c_str(), "rb") || !file) return false;
|
||||
BCRYPT_ALG_HANDLE algorithm = nullptr;
|
||||
BCRYPT_HASH_HANDLE hash = nullptr;
|
||||
unsigned char result[32], block[65536];
|
||||
bool ok = BCryptOpenAlgorithmProvider(&algorithm, BCRYPT_SHA256_ALGORITHM, nullptr, 0) == 0;
|
||||
if (ok) ok = BCryptCreateHash(algorithm, &hash, nullptr, 0, nullptr, 0, 0) == 0;
|
||||
size_t count;
|
||||
while (ok && (count = fread(block, 1, sizeof(block), file)) != 0)
|
||||
ok = BCryptHashData(hash, block, static_cast<ULONG>(count), 0) == 0;
|
||||
if (ferror(file)) ok = false;
|
||||
if (ok) ok = BCryptFinishHash(hash, result, sizeof(result), 0) == 0;
|
||||
fclose(file);
|
||||
if (hash) BCryptDestroyHash(hash);
|
||||
if (algorithm) BCryptCloseAlgorithmProvider(algorithm, 0);
|
||||
if (!ok) return false;
|
||||
char hex[65];
|
||||
for (unsigned i = 0; i < 32; ++i) sprintf_s(hex + i * 2, 65 - i * 2, "%02x", result[i]);
|
||||
return strcmp(hex, expected) == 0;
|
||||
}
|
||||
int main(int argc, char **argv) {
|
||||
if (argc != 4) return 64;
|
||||
bool cell = !strcmp(argv[1], "cell");
|
||||
if (!cell && strcmp(argv[1], "parts")) return 64;
|
||||
SetDllDirectoryA(argv[2]);
|
||||
std::string filename = std::string(argv[2]) + (cell ? "\\CellDB40.dll" : "\\PartsDB40.dll");
|
||||
if (!verified_dll(filename, cell ? "e3fb93a0b5e5d2d027a82f71d1ee13d497598f709ced54c9145a781af6a54d26"
|
||||
: "f24d44eaa13f28550317711df6a6241e227ae56805baa73c8f3020bde513ad0d")) {
|
||||
fprintf(stderr, "Unverified DLL ABI: SHA256 does not match the validated EE7.9.5 library.\n");
|
||||
return 65;
|
||||
}
|
||||
HMODULE h = LoadLibraryA(filename.c_str()); ModuleGuard module{h};
|
||||
if (!h) return 2;
|
||||
try {
|
||||
using Factory = HRESULT(__cdecl *)(void **);
|
||||
using Loader = HRESULT(__stdcall *)(void *, const char *, int);
|
||||
using GetEnum = HRESULT(__stdcall *)(void *, void **);
|
||||
using Next = HRESULT(__stdcall *)(void *, ULONG, int *, ULONG *);
|
||||
using Release = ULONG(__stdcall *)(void *);
|
||||
const char *klass = cell ? "CellDB" : "PartsDB";
|
||||
auto decorated = [klass](const char *method, const char *suffix) {
|
||||
return std::string("?") + method + "@" + klass + "@@" + suffix;
|
||||
};
|
||||
auto symbol = [&](const char *method, const char *suffix) {
|
||||
return GetProcAddress(h, decorated(method, suffix).c_str());
|
||||
};
|
||||
auto release = reinterpret_cast<Release>(symbol("Release", "UAGKXZ"));
|
||||
auto load = reinterpret_cast<Loader>(symbol("Loader", "UAGJPBDH@Z"));
|
||||
if (!release || !load) throw std::runtime_error("Missing required exports");
|
||||
void *db = nullptr;
|
||||
check(proc<Factory>(h, cell ? "GetCellDBPtr" : "GetPartsDBPtr")(&db));
|
||||
ObjectGuard db_guard{db, release}; if (!db) return 3;
|
||||
check(load(db, argv[3], 0));
|
||||
auto enumerate = reinterpret_cast<GetEnum>(symbol(cell ? "GetCellEnum" : "GetPartEnum", "UAGJPAPAUIEnumInt@@@Z"));
|
||||
auto next = proc<Next>(h, cell ? "?Next@EnumCellDBCell@@UAGJKPAHPAK@Z" : "?Next@EnumPartsDBPart@@UAGJKPAHPAK@Z");
|
||||
auto release_enum = proc<Release>(h, cell ? "?Release@EnumCellDBCell@@UAGKXZ" : "?Release@EnumPartsDBPart@@UAGKXZ");
|
||||
if (!enumerate) throw std::runtime_error("Missing enumerator export");
|
||||
void *e = nullptr; check(enumerate(db, &e)); ObjectGuard enum_guard{e, release_enum};
|
||||
auto name = reinterpret_cast<ReadString>(symbol(cell ? "GetName" : "GetPartNumber", "UAGJHPAPBD@Z"));
|
||||
if (!e || !name) return 4;
|
||||
printf("{\"mode\":\"%s\"", argv[1]);
|
||||
if (cell) {
|
||||
using BaseUnit = HRESULT(__stdcall *)(void *, int *);
|
||||
int unit = -1;
|
||||
HRESULT unit_hr = proc<BaseUnit>(h, "?GetCellDBBaseUnit@CellDB@@UAGJPAH@Z")(db, &unit);
|
||||
printf(",\"base_unit_hr\":%ld,\"base_unit_code\":%d", unit_hr, unit);
|
||||
}
|
||||
printf(",\"records\":[");
|
||||
unsigned count = 0;
|
||||
for (;;) {
|
||||
int id = -1; ULONG fetched = 0; HRESULT hr = next(e, 1, &id, &fetched);
|
||||
check(hr); if (!fetched) break;
|
||||
if (++count > 100000) return 6;
|
||||
const char *n = nullptr; check(name(db, id, &n));
|
||||
if (count > 1) putchar(','); printf("{\"id\":%d,\"name\":", id); str(n);
|
||||
if (cell) {
|
||||
string_field(h, db, id, "?GetDescription@CellDB@@UAGJHPAPBD@Z", "description");
|
||||
auto pins = ids(h, db, id, "?GetPins@CellDB@@UAGJHPAPBHPAH@Z");
|
||||
printf(",\"pins\":["); bool first = true;
|
||||
for (int pin : pins) {
|
||||
if (!first) putchar(','); first = false; printf("{\"id\":%d", pin);
|
||||
string_field(h, db, pin, "?GetPinName@CellDB@@UAGJHPAPBD@Z", "name");
|
||||
string_field(h, db, pin, "?GetPinPadstackName@CellDB@@UAGJHPAPBD@Z", "padstack");
|
||||
const int *point = nullptr; int rotation = 0;
|
||||
HRESULT hp = proc<ReadPoint>(h, "?GetPinLocation@CellDB@@UAGJHPAPBH@Z")(db, pin, &point);
|
||||
HRESULT rotation_hr = proc<ReadInt>(h, "?GetPinRotation@CellDB@@UAGJHPAH@Z")(db, pin, &rotation);
|
||||
printf(",\"location_hr\":%ld,\"rotation_hr\":%ld,\"rotation_raw\":%d,\"xy_raw\":", hp, rotation_hr, rotation);
|
||||
if (SUCCEEDED(hp) && point) printf("[%d,%d]", point[0], point[1]); else printf("null");
|
||||
putchar('}');
|
||||
}
|
||||
putchar(']');
|
||||
} else {
|
||||
string_field(h, db, id, "?GetPartName@PartsDB@@UAGJHPAPBD@Z", "part_name");
|
||||
string_field(h, db, id, "?GetPartLabel@PartsDB@@UAGJHPAPBD@Z", "part_label");
|
||||
string_field(h, db, id, "?GetTopCellName@PartsDB@@UAGJHPAPBD@Z", "top_cell_name");
|
||||
string_field(h, db, id, "?GetBottomCellName@PartsDB@@UAGJHPAPBD@Z", "bottom_cell_name");
|
||||
printf(",\"properties\":["); bool first = true;
|
||||
for (int property : ids(h, db, id, "?GetPartProperties@PartsDB@@UAGJHPAPBHPAH@Z")) {
|
||||
int registry = -1, value = -1;
|
||||
check(proc<ReadPair>(h, "?GetProperty@PartsDB@@UAGJHPAH0@Z")(db, property, ®istry, &value));
|
||||
if (!first) putchar(','); first = false;
|
||||
printf("{\"property_id\":%d,\"registry_id\":%d,\"value_id\":%d", property, registry, value);
|
||||
string_field(h, db, registry, "?GetPrpName@PartsDB@@UAGJHPAPBD@Z", "name");
|
||||
string_field(h, db, value, "?GetValueText@PartsDB@@UAGJHPAPBD@Z", "text"); putchar('}');
|
||||
}
|
||||
printf("],\"symbols\":["); first = true;
|
||||
for (int sym : ids(h, db, id, "?GetSymDfnList@PartsDB@@UAGJHPAPBHPAH@Z")) {
|
||||
if (!first) putchar(','); first = false; printf("{\"id\":%d", sym);
|
||||
string_field(h, db, sym, "?GetSymDfnName@PartsDB@@UAGJHPAPBD@Z", "name");
|
||||
printf(",\"pins\":["); bool first_pin = true;
|
||||
for (int pin : ids(h, db, sym, "?GetSymPinList@PartsDB@@UAGJHPAPBHPAH@Z")) {
|
||||
if (!first_pin) putchar(','); first_pin = false; printf("{\"id\":%d", pin);
|
||||
string_field(h, db, pin, "?GetSymPinName@PartsDB@@UAGJHPAPBD@Z", "name");
|
||||
int ordinal = -1; HRESULT ordinal_hr = proc<ReadInt>(h, "?GetSymPinOrdinal@PartsDB@@UAGJHPAH@Z")(db, pin, &ordinal);
|
||||
printf(",\"ordinal_hr\":%ld,\"ordinal\":%d}", ordinal_hr, ordinal);
|
||||
}
|
||||
printf("]}");
|
||||
}
|
||||
printf("]");
|
||||
}
|
||||
putchar('}');
|
||||
}
|
||||
printf("],\"count\":%u}\n", count); return 0;
|
||||
} catch (const std::exception &e) {
|
||||
fprintf(stderr, "read failed: %s\n", e.what()); return 9;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,2 @@
|
||||
"""Portable Mentor saved-schematic exports."""
|
||||
__version__ = "0.1.0"
|
||||
@@ -0,0 +1,698 @@
|
||||
"""Owned Windows private-desktop process boundary for CAD and research workers.
|
||||
|
||||
This module deliberately contains no CAD automation. It only creates a
|
||||
desktop, starts a process on that desktop, and closes resources it owns.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from contextlib import ExitStack
|
||||
from copy import deepcopy
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from threading import get_native_id
|
||||
from typing import Mapping, Sequence
|
||||
|
||||
from ._errors import TaskCenterError
|
||||
|
||||
|
||||
DESKTOP_RELEASE_TIMEOUT_SECONDS = 5.0
|
||||
DESCENDANT_EXIT_GRACE_SECONDS = 2.0
|
||||
|
||||
|
||||
def _native_handle(handle):
|
||||
try:
|
||||
return int(handle) if handle is not None else None
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _resource_event(operation, handle, success, error=None):
|
||||
return {'operation': operation, 'handle': _native_handle(handle), 'success': success,
|
||||
'pid': os.getpid(), 'thread_id': get_native_id(), 'time_ns': time.time_ns(),
|
||||
'error': str(error) if error is not None else None}
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class StartupInfo:
|
||||
lpDesktop: str
|
||||
|
||||
|
||||
def build_startup_info(desktop_name: str) -> StartupInfo:
|
||||
if not desktop_name or "\\" in desktop_name or "\x00" in desktop_name:
|
||||
raise TaskCenterError("private_desktop", "invalid private desktop name")
|
||||
return StartupInfo(lpDesktop=f"winsta0\\{desktop_name}")
|
||||
|
||||
|
||||
class _Win32Api:
|
||||
def __init__(self) -> None:
|
||||
try:
|
||||
import win32api, win32con, win32event, win32job, win32process, win32service
|
||||
except ImportError as error:
|
||||
raise TaskCenterError("private_desktop", "pywin32 is required for private desktop supervision") from error
|
||||
self.con, self.job, self.process, self.service = win32con, win32job, win32process, win32service
|
||||
self.event = win32event
|
||||
self.open_process = win32api.OpenProcess
|
||||
self._pending_cleanup = []
|
||||
|
||||
def create_desktop(self, name: str):
|
||||
return self.service.CreateDesktop(name, 0, self.con.GENERIC_ALL, None)
|
||||
|
||||
def confirm_desktop_released(self, name: str):
|
||||
started = time.monotonic()
|
||||
# USER references can outlive the owned processes briefly. Allow a
|
||||
# bounded grace period, without delaying the usual immediate release
|
||||
# or accepting a desktop that still has a persistent external owner.
|
||||
budget = DESKTOP_RELEASE_TIMEOUT_SECONDS
|
||||
deadline = started + budget
|
||||
self.desktop_release_check = {'desktop': name, 'released': None,
|
||||
'elapsed_seconds': 0.0, 'budget_seconds': budget}
|
||||
while True:
|
||||
# EnumDesktops avoids reopening/closing a handle which Windows may
|
||||
# share with another desktop handle held by this process.
|
||||
names = self.service.GetProcessWindowStation().EnumDesktops()
|
||||
present = name.casefold() in {value.casefold() for value in names}
|
||||
self.desktop_release_check = {'desktop': name, 'released': not present,
|
||||
'elapsed_seconds': time.monotonic() - started, 'budget_seconds': budget,
|
||||
'caller_pid': os.getpid(), 'caller_thread_id': get_native_id()}
|
||||
if not present:
|
||||
return
|
||||
if time.monotonic() >= deadline:
|
||||
try:
|
||||
from ._identity import current_desktop_name
|
||||
self.desktop_release_check['caller_desktop'] = current_desktop_name()
|
||||
except Exception as error:
|
||||
self.desktop_release_check['caller_desktop_error'] = str(error)
|
||||
try:
|
||||
self.desktop_release_check['remaining_windows'] = self._desktop_windows(name)
|
||||
except Exception as error:
|
||||
self.desktop_release_check['window_query_error'] = str(error)
|
||||
raise TaskCenterError('private_desktop_cleanup', 'owned private desktop still exists after cleanup')
|
||||
time.sleep(min(0.05, max(0.0, deadline - time.monotonic())))
|
||||
|
||||
def _desktop_windows(self, name):
|
||||
"""Diagnose residue without switching desktops or terminating foreign owners."""
|
||||
import win32gui
|
||||
|
||||
desktop = self.service.OpenDesktop(name, 0, False, 1) # DESKTOP_READOBJECTS
|
||||
windows = []
|
||||
try:
|
||||
user = ctypes.WinDLL('user32', use_last_error=True)
|
||||
callback_type = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
|
||||
enumerate_windows = user.EnumDesktopWindows
|
||||
enumerate_windows.argtypes = [wintypes.HANDLE, callback_type, wintypes.LPARAM]
|
||||
enumerate_windows.restype = wintypes.BOOL
|
||||
handles = []
|
||||
callback = callback_type(lambda handle, _: handles.append(handle) or True)
|
||||
# An empty desktop can return FALSE with no last error. Our
|
||||
# callback always continues, so only a nonzero error is a failure.
|
||||
ctypes.set_last_error(0)
|
||||
if not enumerate_windows(int(desktop), callback, 0) and ctypes.get_last_error():
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
for handle in handles:
|
||||
thread, pid = self.process.GetWindowThreadProcessId(handle)
|
||||
windows.append({'hwnd': int(handle), 'pid': pid, 'thread_id': thread,
|
||||
'class': win32gui.GetClassName(handle)})
|
||||
finally:
|
||||
desktop.CloseDesktop()
|
||||
return windows
|
||||
|
||||
def create_process(self, command: Sequence[str], desktop: str, cwd: Path | None, env: Mapping[str, str] | None,
|
||||
*, stdout=None, stderr=None):
|
||||
# Windows venv python.exe redirects to a second process. Launch the
|
||||
# interpreter directly with its venv identity, so the handle we own is
|
||||
# also the process that admits the request and creates desktop windows.
|
||||
if (not getattr(sys, 'frozen', False) and sys.prefix != sys.base_prefix
|
||||
and os.path.normcase(os.path.abspath(str(command[0]))) == os.path.normcase(sys.executable)
|
||||
and getattr(sys, '_base_executable', None)):
|
||||
command = (sys._base_executable, *command[1:])
|
||||
env = dict(os.environ if env is None else env)
|
||||
env['__PYVENV_LAUNCHER__'] = sys.executable
|
||||
startup = self.process.STARTUPINFO()
|
||||
startup.lpDesktop = desktop
|
||||
flags = self.con.CREATE_NO_WINDOW | self.con.CREATE_SUSPENDED
|
||||
job = self.job.CreateJobObject(None, "")
|
||||
process = None
|
||||
try:
|
||||
info = self.job.QueryInformationJobObject(job, self.job.JobObjectExtendedLimitInformation)
|
||||
info["BasicLimitInformation"]["LimitFlags"] |= self.job.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
|
||||
self.job.SetInformationJobObject(job, self.job.JobObjectExtendedLimitInformation, info)
|
||||
streams = (None, None)
|
||||
if stdout is not None or stderr is not None:
|
||||
(handle, thread, pid, _), streams = _create_redirected_process(command, desktop, cwd, env, flags, stdout, stderr)
|
||||
else:
|
||||
handle, thread, pid, _ = self.process.CreateProcess(
|
||||
str(command[0]), subprocess.list2cmdline([str(item) for item in command]),
|
||||
None, None, False, flags, env, str(cwd) if cwd else None, startup,
|
||||
)
|
||||
process = _Win32Process(self.process, self.job, self.event, handle, job, int(pid), command,
|
||||
open_process=getattr(self, 'open_process', None))
|
||||
process.stdout, process.stderr = streams
|
||||
process.thread = thread
|
||||
self.job.AssignProcessToJobObject(job, handle)
|
||||
process.job_assigned = True
|
||||
if not self.job.IsProcessInJob(handle, job):
|
||||
raise TaskCenterError('private_desktop', 'Owned process Job membership was not confirmed')
|
||||
process.cleanup_record['job_membership_verified'] = True
|
||||
self.process.ResumeThread(thread)
|
||||
thread.Close()
|
||||
process.thread = None
|
||||
except Exception as error:
|
||||
try:
|
||||
if process is not None:
|
||||
# Assignment can fail before the job owns this suspended process.
|
||||
process.close()
|
||||
else:
|
||||
job.Close()
|
||||
except Exception as cleanup_error:
|
||||
pending = getattr(self, '_pending_cleanup', None)
|
||||
if pending is None:
|
||||
self._pending_cleanup = pending = []
|
||||
pending.append(process if process is not None else job)
|
||||
raise TaskCenterError('private_desktop', f'launch failed: {error}; cleanup failed: {cleanup_error}') from error
|
||||
raise
|
||||
return process
|
||||
|
||||
def close_pending(self):
|
||||
errors, remaining = [], []
|
||||
for resource in getattr(self, '_pending_cleanup', []):
|
||||
try:
|
||||
resource.close() if isinstance(resource, _Win32Process) else resource.Close()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
remaining.append(resource)
|
||||
self._pending_cleanup = remaining
|
||||
_raise_cleanup_errors(errors)
|
||||
|
||||
|
||||
def _raise_cleanup_errors(errors):
|
||||
if errors:
|
||||
raise TaskCenterError('private_desktop_cleanup', '; '.join(str(error) for error in errors))
|
||||
|
||||
|
||||
def _stop_and_wait(process):
|
||||
errors = []
|
||||
running = True
|
||||
try:
|
||||
running = process.poll() is None
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
if running:
|
||||
try:
|
||||
if isinstance(process, _Win32Process):
|
||||
process.terminate(reason='root_process_active')
|
||||
else:
|
||||
process.terminate()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
try:
|
||||
process.wait(timeout=5.0)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
return errors
|
||||
|
||||
|
||||
def _create_redirected_process(command, desktop, cwd, env, flags, stdout, stderr):
|
||||
"""Use an explicit inheritance list; never inherit unrelated parent handles."""
|
||||
import msvcrt
|
||||
import pywintypes
|
||||
|
||||
class NativeStartup(ctypes.Structure):
|
||||
_fields_ = [
|
||||
('cb', wintypes.DWORD), ('lpReserved', wintypes.LPWSTR),
|
||||
('lpDesktop', wintypes.LPWSTR), ('lpTitle', wintypes.LPWSTR),
|
||||
('dwX', wintypes.DWORD), ('dwY', wintypes.DWORD),
|
||||
('dwXSize', wintypes.DWORD), ('dwYSize', wintypes.DWORD),
|
||||
('dwXCountChars', wintypes.DWORD), ('dwYCountChars', wintypes.DWORD),
|
||||
('dwFillAttribute', wintypes.DWORD), ('dwFlags', wintypes.DWORD),
|
||||
('wShowWindow', wintypes.WORD), ('cbReserved2', wintypes.WORD),
|
||||
('lpReserved2', ctypes.POINTER(ctypes.c_byte)),
|
||||
('hStdInput', wintypes.HANDLE), ('hStdOutput', wintypes.HANDLE), ('hStdError', wintypes.HANDLE),
|
||||
]
|
||||
class ExtendedStartup(ctypes.Structure):
|
||||
_fields_ = [('StartupInfo', NativeStartup), ('lpAttributeList', ctypes.c_void_p)]
|
||||
class ProcessInformation(ctypes.Structure):
|
||||
_fields_ = [('hProcess', wintypes.HANDLE), ('hThread', wintypes.HANDLE),
|
||||
('dwProcessId', wintypes.DWORD), ('dwThreadId', wintypes.DWORD)]
|
||||
|
||||
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
initialize = kernel.InitializeProcThreadAttributeList
|
||||
initialize.argtypes = [ctypes.c_void_p, wintypes.DWORD, wintypes.DWORD, ctypes.POINTER(ctypes.c_size_t)]
|
||||
initialize.restype = wintypes.BOOL
|
||||
update = kernel.UpdateProcThreadAttribute
|
||||
update.argtypes = [ctypes.c_void_p, wintypes.DWORD, ctypes.c_size_t, ctypes.c_void_p,
|
||||
ctypes.c_size_t, ctypes.c_void_p, ctypes.c_void_p]
|
||||
update.restype = wintypes.BOOL
|
||||
delete = kernel.DeleteProcThreadAttributeList
|
||||
delete.argtypes = [ctypes.c_void_p]
|
||||
delete.restype = None
|
||||
create = kernel.CreateProcessW
|
||||
create.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, ctypes.c_void_p, ctypes.c_void_p,
|
||||
wintypes.BOOL, wintypes.DWORD, ctypes.c_void_p, wintypes.LPCWSTR,
|
||||
ctypes.POINTER(ExtendedStartup), ctypes.POINTER(ProcessInformation)]
|
||||
create.restype = wintypes.BOOL
|
||||
readers = [None, None]
|
||||
try:
|
||||
with ExitStack() as children:
|
||||
def output_handle(target, channel):
|
||||
if target == subprocess.PIPE:
|
||||
read_fd, write_fd = os.pipe()
|
||||
readers[channel] = os.fdopen(read_fd, 'rb')
|
||||
writer = children.enter_context(os.fdopen(write_fd, 'wb'))
|
||||
elif target is None or target == subprocess.DEVNULL:
|
||||
writer = children.enter_context(open(os.devnull, 'wb'))
|
||||
else:
|
||||
writer = children.enter_context(os.fdopen(os.dup(target.fileno()), 'wb'))
|
||||
handle = msvcrt.get_osfhandle(writer.fileno())
|
||||
os.set_handle_inheritable(handle, True)
|
||||
return handle
|
||||
|
||||
stdin_file = children.enter_context(open(os.devnull, 'rb'))
|
||||
stdin_handle = msvcrt.get_osfhandle(stdin_file.fileno())
|
||||
os.set_handle_inheritable(stdin_handle, True)
|
||||
stdout_handle = output_handle(stdout, 0)
|
||||
stderr_handle = stdout_handle if stderr == subprocess.STDOUT else output_handle(stderr, 1)
|
||||
handles = list(dict.fromkeys((stdin_handle, stdout_handle, stderr_handle)))
|
||||
inherited = (wintypes.HANDLE * len(handles))(*handles)
|
||||
size = ctypes.c_size_t()
|
||||
initialize(None, 1, 0, ctypes.byref(size))
|
||||
attributes = ctypes.create_string_buffer(size.value)
|
||||
if not initialize(attributes, 1, 0, ctypes.byref(size)):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
try:
|
||||
if not update(attributes, 0, 0x20002, inherited, ctypes.sizeof(inherited), None, None):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
startup = ExtendedStartup()
|
||||
startup.StartupInfo.cb = ctypes.sizeof(startup)
|
||||
startup.StartupInfo.lpDesktop = desktop
|
||||
startup.StartupInfo.dwFlags = 0x100 # STARTF_USESTDHANDLES
|
||||
startup.StartupInfo.hStdInput = stdin_handle
|
||||
startup.StartupInfo.hStdOutput = stdout_handle
|
||||
startup.StartupInfo.hStdError = stderr_handle
|
||||
startup.lpAttributeList = ctypes.addressof(attributes)
|
||||
information = ProcessInformation()
|
||||
environment = None if env is None else ctypes.create_unicode_buffer(
|
||||
'\0'.join(f'{key}={value}' for key, value in sorted(env.items(), key=lambda item: item[0].upper())) + '\0\0')
|
||||
command_line = ctypes.create_unicode_buffer(subprocess.list2cmdline([str(item) for item in command]))
|
||||
if not create(str(command[0]), command_line, None, None, True,
|
||||
flags | 0x80000 | 0x400, environment, str(cwd) if cwd else None,
|
||||
ctypes.byref(startup), ctypes.byref(information)):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
result = (pywintypes.HANDLE(information.hProcess), pywintypes.HANDLE(information.hThread),
|
||||
information.dwProcessId, information.dwThreadId)
|
||||
finally:
|
||||
delete(attributes)
|
||||
return result, readers
|
||||
except BaseException:
|
||||
for reader in readers:
|
||||
if reader is not None:
|
||||
reader.close()
|
||||
raise
|
||||
|
||||
|
||||
class _Win32Process:
|
||||
def __init__(self, process_api, job_api, event_api, handle, job, pid: int, args, *, open_process=None):
|
||||
self._process_api, self._job_api = process_api, job_api
|
||||
self._event_api = event_api
|
||||
self._open_process = open_process
|
||||
self._descendant_handles = []
|
||||
self._descendant_pids = set()
|
||||
self.handle, self.job, self.pid = handle, job, pid
|
||||
self.args = args
|
||||
self.thread = None
|
||||
self.job_assigned = False
|
||||
self.returncode = None
|
||||
self.stdout = self.stderr = None
|
||||
self.cleanup_record = {'pid': self.pid, 'forced_termination': False,
|
||||
'forced_root_termination': False, 'forced_descendant_pids': [],
|
||||
'termination_reason': None}
|
||||
|
||||
def poll(self):
|
||||
if self.returncode is not None or self.handle is None:
|
||||
return self.returncode
|
||||
status = self._event_api.WaitForSingleObject(self.handle, 0)
|
||||
if status == 258: # WAIT_TIMEOUT: 259 is also a valid *completed* exit code.
|
||||
return None
|
||||
if status != 0:
|
||||
raise OSError(f'process wait failed with status {status}')
|
||||
self.returncode = self._process_api.GetExitCodeProcess(self.handle)
|
||||
return self.returncode
|
||||
|
||||
def tree_exited(self):
|
||||
if self.poll() is None:
|
||||
return False
|
||||
if self.job is not None and self.job_assigned:
|
||||
active = self._job_api.QueryInformationJobObject(
|
||||
self.job, self._job_api.JobObjectBasicAccountingInformation)['ActiveProcesses']
|
||||
if active:
|
||||
return False
|
||||
return all(self._event_api.WaitForSingleObject(handle, 0) == 0
|
||||
for handle in self._descendant_handles)
|
||||
|
||||
def wait(self, timeout=None):
|
||||
if self.returncode is not None:
|
||||
return self.returncode
|
||||
if self.handle is None:
|
||||
raise OSError('owned process handle is closed before exit was confirmed')
|
||||
milliseconds = 0xFFFFFFFF if timeout is None else min(max(math.ceil(timeout * 1000), 0), 0xFFFFFFFE)
|
||||
status = self._event_api.WaitForSingleObject(self.handle, milliseconds)
|
||||
if status == 258:
|
||||
raise subprocess.TimeoutExpired(self.args, timeout)
|
||||
if status != 0:
|
||||
raise OSError(f'process wait failed with status {status}')
|
||||
self.returncode = self._process_api.GetExitCodeProcess(self.handle)
|
||||
return self.returncode
|
||||
|
||||
def terminate(self, *, reason='explicit_termination'):
|
||||
if self.handle is None:
|
||||
return
|
||||
errors = []
|
||||
root_active = True
|
||||
try:
|
||||
root_active = self.poll() is None
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
self.cleanup_record.setdefault('root_exit_before_cleanup', not root_active)
|
||||
if not self.job_assigned:
|
||||
self._process_api.TerminateProcess(self.handle, 1)
|
||||
self._record_termination(reason, root_active, ())
|
||||
_raise_cleanup_errors(errors)
|
||||
return
|
||||
descendants = set()
|
||||
try:
|
||||
# Keep confirmed members of this snapshot even if opening a later
|
||||
# member fails; the Job termination below still includes them.
|
||||
self._remember_descendants(active=descendants)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
try:
|
||||
self._job_api.TerminateJobObject(self.job, 1)
|
||||
self._record_termination(reason, root_active, descendants)
|
||||
except Exception:
|
||||
self._process_api.TerminateProcess(self.handle, 1)
|
||||
self._record_termination(reason, root_active, ())
|
||||
_raise_cleanup_errors(errors)
|
||||
|
||||
kill = terminate
|
||||
|
||||
def _record_termination(self, reason, root_active, descendants):
|
||||
self.cleanup_record.update(forced_termination=True,
|
||||
forced_root_termination=self.cleanup_record['forced_root_termination'] or root_active,
|
||||
forced_descendant_pids=sorted(set(self.cleanup_record['forced_descendant_pids']) | set(descendants)),
|
||||
termination_reason=self.cleanup_record['termination_reason'] or reason)
|
||||
|
||||
def _remember_descendants(self, *, active=None):
|
||||
if active is None:
|
||||
active = set()
|
||||
if self._open_process is None or self.job is None:
|
||||
return active
|
||||
for pid in self._job_api.QueryInformationJobObject(self.job, self._job_api.JobObjectBasicProcessIdList):
|
||||
if pid == self.pid:
|
||||
continue
|
||||
if pid in self._descendant_pids:
|
||||
active.add(pid)
|
||||
continue
|
||||
try:
|
||||
self._descendant_handles.append(self._open_process(0x100000, False, pid))
|
||||
self._descendant_pids.add(pid)
|
||||
active.add(pid)
|
||||
except Exception as error:
|
||||
# pywintypes.error is not an OSError. A child that exited
|
||||
# between the job snapshot and OpenProcess reports error 87.
|
||||
code = getattr(error, 'winerror', None)
|
||||
if code is None and error.args:
|
||||
code = error.args[0]
|
||||
if code != 87:
|
||||
raise
|
||||
return active
|
||||
|
||||
def close(self):
|
||||
errors = []
|
||||
job_empty = not self.job_assigned or self.job is None
|
||||
if self.handle is not None:
|
||||
if 'root_exit_before_cleanup' not in self.cleanup_record:
|
||||
try:
|
||||
self.cleanup_record['root_exit_before_cleanup'] = self.poll() is not None
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
errors.extend(_stop_and_wait(self))
|
||||
deadline = time.monotonic() + 5.0
|
||||
if self.job is not None and self.job_assigned:
|
||||
try:
|
||||
grace_started = time.monotonic()
|
||||
grace_deadline = grace_started + DESCENDANT_EXIT_GRACE_SECONDS
|
||||
# A completed root may leave children finishing normal shutdown.
|
||||
# An empty job takes no grace delay; an active root was already
|
||||
# stopped above and does not receive this normal-exit allowance.
|
||||
while True:
|
||||
active = self._job_api.QueryInformationJobObject(
|
||||
self.job, self._job_api.JobObjectBasicAccountingInformation)['ActiveProcesses']
|
||||
self.cleanup_record['job_active_processes'] = active
|
||||
if not active:
|
||||
job_empty = True
|
||||
break
|
||||
self._remember_descendants()
|
||||
if (self.cleanup_record.get('root_exit_before_cleanup') is not True
|
||||
or self.cleanup_record['forced_termination'] or time.monotonic() >= grace_deadline):
|
||||
self.terminate(reason='descendant_grace_expired')
|
||||
break
|
||||
time.sleep(min(0.01, max(0, grace_deadline - time.monotonic())))
|
||||
self.cleanup_record['descendant_grace_seconds'] = time.monotonic() - grace_started
|
||||
deadline = time.monotonic() + 5.0
|
||||
while True:
|
||||
active = self._job_api.QueryInformationJobObject(
|
||||
self.job, self._job_api.JobObjectBasicAccountingInformation)['ActiveProcesses']
|
||||
self.cleanup_record['job_active_processes'] = active
|
||||
if not active:
|
||||
job_empty = True
|
||||
break
|
||||
if time.monotonic() >= deadline:
|
||||
raise TimeoutError('owned job descendants did not exit within 5 seconds')
|
||||
time.sleep(0.01)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
retained = []
|
||||
for descendant in self._descendant_handles:
|
||||
try:
|
||||
# Job accounting can reach zero before a terminated process is signaled.
|
||||
remaining = max(0, math.ceil((deadline - time.monotonic()) * 1000))
|
||||
if self._event_api.WaitForSingleObject(descendant, remaining) != 0:
|
||||
raise TimeoutError('owned job descendant exit was not confirmed within 5 seconds')
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
retained.append(descendant)
|
||||
continue
|
||||
try:
|
||||
descendant.Close()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
retained.append(descendant)
|
||||
self._descendant_handles = retained
|
||||
for name in ('job', 'thread', 'handle'):
|
||||
handle = getattr(self, name)
|
||||
if name in ('job', 'handle') and not job_empty:
|
||||
# Closing a KILL_ON_JOB_CLOSE job would silently force remaining
|
||||
# children and discard the capability needed for cleanup retry.
|
||||
continue
|
||||
if name == 'handle' and self.returncode is None:
|
||||
# Keep the only termination/wait capability until cleanup can retry.
|
||||
continue
|
||||
if handle is not None:
|
||||
try:
|
||||
handle.Close()
|
||||
setattr(self, name, None)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
for stream in (self.stdout, self.stderr):
|
||||
if stream is not None:
|
||||
try:
|
||||
stream.close()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
self.cleanup_record.update(exit_code=self.returncode, descendant_pids=sorted(self._descendant_pids),
|
||||
unconfirmed_descendant_handles=len(self._descendant_handles),
|
||||
process_handle_closed=self.handle is None, job_handle_closed=self.job is None,
|
||||
errors=[str(error) for error in errors])
|
||||
_raise_cleanup_errors(errors)
|
||||
|
||||
|
||||
class PrivateDesktop:
|
||||
"""Own a private Windows desktop and processes started on it."""
|
||||
|
||||
def __init__(self, name: str, *, enabled: bool = False, api=None) -> None:
|
||||
self.name = name
|
||||
self.enabled = enabled
|
||||
self._handle = None
|
||||
self._processes: list[subprocess.Popen[str]] = []
|
||||
self._api = api
|
||||
self.cleanup_record = None
|
||||
self._desktop_handle_history = []
|
||||
self._process_history = []
|
||||
|
||||
def __enter__(self) -> "PrivateDesktop":
|
||||
if not self.enabled:
|
||||
raise TaskCenterError("private_desktop_opt_in", "private desktop backend requires explicit opt-in")
|
||||
self._create()
|
||||
if self._handle is None:
|
||||
raise TaskCenterError("private_desktop", "private desktop could not be created")
|
||||
return self
|
||||
|
||||
def __exit__(self, *_exc: object) -> None:
|
||||
self.close()
|
||||
|
||||
def inspect_dialogs(self, pid):
|
||||
if self._handle is None or not any(process.pid == pid and process.poll() is None for process in self._processes):
|
||||
return ()
|
||||
return tuple(window for window in self._api._desktop_windows(self.name) if window['pid'] == pid)
|
||||
|
||||
def _create(self) -> None:
|
||||
if os.name != "nt" and self._api is None:
|
||||
return
|
||||
self._api = self._api or _Win32Api()
|
||||
try:
|
||||
self._handle = self._api.create_desktop(self.name)
|
||||
except Exception as error:
|
||||
self._desktop_handle_history.append(_resource_event('create', None, False, error))
|
||||
raise
|
||||
self._desktop_handle_history.append(_resource_event('create', self._handle, self._handle is not None))
|
||||
|
||||
def start_process(
|
||||
self,
|
||||
command: Sequence[str],
|
||||
*,
|
||||
cwd: Path | None = None,
|
||||
env: Mapping[str, str] | None = None,
|
||||
stdout=None,
|
||||
stderr=None,
|
||||
) -> subprocess.Popen[str]:
|
||||
if not self.enabled or self._handle is None:
|
||||
raise TaskCenterError("private_desktop", "process launch requires an owned private desktop")
|
||||
if not command:
|
||||
raise TaskCenterError("private_desktop", "private desktop command is empty")
|
||||
if self._api is None:
|
||||
raise TaskCenterError("private_desktop", "private desktop process API is unavailable")
|
||||
options = {} if stdout is None and stderr is None else {'stdout': stdout, 'stderr': stderr}
|
||||
process = self._api.create_process(command, build_startup_info(self.name).lpDesktop, cwd, env, **options)
|
||||
self._processes.append(process)
|
||||
self._process_history.append({'pid': getattr(process, 'pid', None),
|
||||
'owner_pid': os.getpid(), 'owner_thread_id': get_native_id(),
|
||||
'started_time_ns': time.time_ns(), 'process_handle': _native_handle(getattr(process, 'handle', None)),
|
||||
'job_handle': _native_handle(getattr(process, 'job', None)), 'forgotten': False})
|
||||
return process
|
||||
|
||||
def _remember_process_cleanup(self, process, *, forgotten=False):
|
||||
for record in reversed(self._process_history):
|
||||
if record['pid'] == getattr(process, 'pid', None) and not record['forgotten']:
|
||||
record['cleanup'] = deepcopy(getattr(process, 'cleanup_record', {}))
|
||||
record['forgotten'] = forgotten
|
||||
record['observed_time_ns'] = time.time_ns()
|
||||
break
|
||||
|
||||
def forget_closed_process(self, process):
|
||||
"""Drop a worker only after its owning wrapper confirmed all cleanup."""
|
||||
record = getattr(process, 'cleanup_record', {})
|
||||
if (record.get('process_handle_closed') is not True
|
||||
or record.get('job_handle_closed') is not True
|
||||
or record.get('unconfirmed_descendant_handles', 0)
|
||||
or record.get('errors') or process.poll() is None):
|
||||
raise OSError('cannot forget a worker with unconfirmed owned resources')
|
||||
self._processes = [item for item in self._processes if item is not process]
|
||||
self._remember_process_cleanup(process, forgotten=True)
|
||||
|
||||
def close(self) -> None:
|
||||
errors, remaining = [], []
|
||||
self.cleanup_record = {'desktop': self.name, 'processes': []}
|
||||
for process in reversed(self._processes):
|
||||
process_errors = _stop_and_wait(process)
|
||||
errors.extend(process_errors)
|
||||
failed = bool(process_errors)
|
||||
try:
|
||||
process.close()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
failed = True
|
||||
self.cleanup_record['processes'].append(getattr(process, 'cleanup_record', {
|
||||
'pid': getattr(process, 'pid', None), 'exit_code': getattr(process, 'returncode', None)}))
|
||||
self._remember_process_cleanup(process)
|
||||
if failed:
|
||||
remaining.append(process)
|
||||
self._processes = list(reversed(remaining))
|
||||
close_pending = getattr(self._api, 'close_pending', None)
|
||||
if close_pending:
|
||||
try:
|
||||
close_pending()
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
close_desktop = getattr(self._handle, "CloseDesktop", None)
|
||||
if close_desktop:
|
||||
event = _resource_event('close', self._handle, False)
|
||||
try:
|
||||
close_desktop()
|
||||
event['success'] = True
|
||||
self._handle = None
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
event['error'] = str(error)
|
||||
self._desktop_handle_history.append(event)
|
||||
confirm = getattr(self._api, 'confirm_desktop_released', None)
|
||||
if confirm and self._handle is None:
|
||||
try:
|
||||
confirm(self.name)
|
||||
except Exception as error:
|
||||
errors.append(error)
|
||||
self.cleanup_record.update(desktop_handle_closed=self._handle is None,
|
||||
desktop_confirmation=getattr(self._api, 'desktop_release_check', None),
|
||||
desktop_handle_history=deepcopy(self._desktop_handle_history),
|
||||
process_history=deepcopy(self._process_history),
|
||||
diagnostic_scope={'external_handle_holders': 'unknown',
|
||||
'thread_desktops': 'unknown unless observed through residual windows; no exhaustive holder scan',
|
||||
'empty_windows_prove_no_holders': False},
|
||||
errors=[str(error) for error in errors])
|
||||
if errors:
|
||||
self.cleanup_record['owned_resource_diagnostics'] = self._owned_resource_diagnostics()
|
||||
_raise_cleanup_errors(errors)
|
||||
|
||||
def _owned_resource_diagnostics(self):
|
||||
"""Bounded queries of our handles only; never terminate/query foreign PIDs.
|
||||
|
||||
GetThreadDesktop can introduce borrowed USER references. Do not run a
|
||||
global thread scan in this long-lived owner while retrying release.
|
||||
Window observations identify only window-owning threads, not every
|
||||
thread or an external OpenDesktop handle holder.
|
||||
"""
|
||||
resources = []
|
||||
for process in self._processes:
|
||||
item = {'pid': getattr(process, 'pid', None),
|
||||
'process_handle': _native_handle(getattr(process, 'handle', None)),
|
||||
'job_handle': _native_handle(getattr(process, 'job', None)),
|
||||
'thread_handle': _native_handle(getattr(process, 'thread', None)),
|
||||
'cleanup': deepcopy(getattr(process, 'cleanup_record', {}))}
|
||||
try:
|
||||
item['exit_code'] = process.poll()
|
||||
except Exception as error:
|
||||
item['poll_error'] = str(error)
|
||||
job = getattr(process, 'job', None)
|
||||
job_api = getattr(process, '_job_api', None)
|
||||
if job is not None and job_api is not None:
|
||||
try:
|
||||
item['job_accounting'] = job_api.QueryInformationJobObject(
|
||||
job, job_api.JobObjectBasicAccountingInformation)
|
||||
except Exception as error:
|
||||
item['job_query_error'] = str(error)
|
||||
resources.append(item)
|
||||
confirmation = getattr(self._api, 'desktop_release_check', None) or {}
|
||||
return {'owned_processes': resources,
|
||||
'pending_launch_cleanup_count': len(getattr(self._api, '_pending_cleanup', [])),
|
||||
'residual_windows': deepcopy(confirmation.get('remaining_windows')),
|
||||
'window_query_error': confirmation.get('window_query_error'),
|
||||
'external_handle_holders': 'unknown',
|
||||
'thread_desktops': {'observations': deepcopy(confirmation.get('remaining_windows', [])),
|
||||
'complete': False, 'unobserved_threads': 'unknown'},
|
||||
'empty_windows_prove_no_holders': False}
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Stable, user-facing errors for trial task-center operations."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
import secrets
|
||||
|
||||
|
||||
class TaskCenterError(ValueError):
|
||||
"""An operation failure with a durable support identifier and safe user text."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
code: str,
|
||||
message: str,
|
||||
*,
|
||||
error_id: str | None = None,
|
||||
traceback_detail: str = "",
|
||||
) -> None:
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.error_id = error_id or self._new_error_id()
|
||||
self.traceback_detail = traceback_detail
|
||||
super().__init__(f"{self.error_id} {code}: {message}")
|
||||
|
||||
@staticmethod
|
||||
def _new_error_id() -> str:
|
||||
date = datetime.now(timezone.utc).strftime("%Y%m%d")
|
||||
return f"DXT-{date}-{secrets.token_hex(4).upper()}"
|
||||
@@ -0,0 +1,77 @@
|
||||
"""Identity certificate shared by the private desktop worker and native helper."""
|
||||
from dataclasses import dataclass
|
||||
import ctypes
|
||||
from ctypes import wintypes
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
def current_desktop_name():
|
||||
user = ctypes.WinDLL('user32', use_last_error=True)
|
||||
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
|
||||
user.GetThreadDesktop.argtypes = [wintypes.DWORD]
|
||||
user.GetThreadDesktop.restype = wintypes.HANDLE
|
||||
user.GetUserObjectInformationW.argtypes = [wintypes.HANDLE, ctypes.c_int, ctypes.c_void_p,
|
||||
wintypes.DWORD, ctypes.POINTER(wintypes.DWORD)]
|
||||
desktop = user.GetThreadDesktop(kernel.GetCurrentThreadId())
|
||||
value, required = ctypes.create_unicode_buffer(256), wintypes.DWORD()
|
||||
if not user.GetUserObjectInformationW(desktop, 2, value, ctypes.sizeof(value), ctypes.byref(required)):
|
||||
raise ctypes.WinError(ctypes.get_last_error())
|
||||
return value.value
|
||||
|
||||
|
||||
def window_pid(handle):
|
||||
import win32process
|
||||
return win32process.GetWindowThreadProcessId(handle)[1]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class CadBinding:
|
||||
desktop: str
|
||||
pid: int
|
||||
guid: str
|
||||
token: str
|
||||
|
||||
@classmethod
|
||||
def from_environment(cls, environment=None):
|
||||
environment = os.environ if environment is None else environment
|
||||
fields = ('DXPART_DESKTOP', 'DXPART_CAD_PID', 'DXPART_CAD_GUID', 'DXPART_SESSION_TOKEN')
|
||||
values = [environment.get(field, '') for field in fields]
|
||||
if not any(values):
|
||||
return None
|
||||
if not all(values):
|
||||
raise ValueError('incomplete private CAD identity')
|
||||
desktop, pid, guid, token = values
|
||||
if not re.fullmatch(r'dx-[A-Za-z0-9-]{1,80}', desktop) or int(pid) <= 0:
|
||||
raise ValueError('invalid private CAD desktop or PID')
|
||||
if not re.fullmatch(r'[0-9a-f]{32}', token):
|
||||
raise ValueError('invalid private CAD session token')
|
||||
return cls(desktop, int(pid), guid, token)
|
||||
|
||||
def environment(self):
|
||||
return dict(os.environ, DXPART_DESKTOP=self.desktop, DXPART_CAD_PID=str(self.pid),
|
||||
DXPART_CAD_GUID=self.guid, DXPART_SESSION_TOKEN=self.token)
|
||||
|
||||
|
||||
def verify_application(app, binding=None, *, require_view=True):
|
||||
binding = CadBinding.from_environment() if binding is None else binding
|
||||
if binding is None:
|
||||
return None
|
||||
if current_desktop_name() != binding.desktop:
|
||||
raise ValueError('CAD helper is not running on its owned desktop')
|
||||
if str(app.Guid()).casefold() != binding.guid.casefold():
|
||||
raise ValueError('CAD COM GUID does not match its owned session')
|
||||
if require_view:
|
||||
handle = int(app.ActiveViewHandle)
|
||||
if not handle or window_pid(handle) != binding.pid:
|
||||
raise ValueError('CAD view PID does not match its owned session')
|
||||
return binding
|
||||
|
||||
|
||||
def project_file_path(app):
|
||||
"""COM type libraries expose this getter as either a property or a method."""
|
||||
value = app.GetProjectData().GetProjectFilePath
|
||||
value = value() if callable(value) else value
|
||||
if not isinstance(value, str):
|
||||
raise ValueError('CAD returned an invalid project path')
|
||||
return value
|
||||
@@ -0,0 +1,332 @@
|
||||
"""Interactive entry point and recoverable batch lifecycle."""
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
import importlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import signal
|
||||
import sys
|
||||
from threading import Event
|
||||
import traceback
|
||||
import uuid
|
||||
|
||||
from . import __version__
|
||||
from .core import discover_projects, load_json, project_folder_name, read_project, sha256, write_json
|
||||
from .environment import discover_installations, preflight_installation
|
||||
from .report import LABELS, write_report
|
||||
|
||||
APP_ROOT = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def validate_output(output, scan_root, projects):
|
||||
output, scan_root = Path(output).resolve(), Path(scan_root).resolve()
|
||||
if output == scan_root or output in scan_root.parents:
|
||||
raise ValueError("结果目录不能是扫描目录本身或其父目录")
|
||||
for project in projects:
|
||||
protected = []
|
||||
if project.get("database"):
|
||||
protected.append(Path(project["database"]).resolve())
|
||||
if project.get("library"):
|
||||
protected.append(Path(project["library"]).resolve().parent)
|
||||
for root in protected:
|
||||
if output == root or root in output.parents:
|
||||
raise ValueError("结果目录不能写入源数据库或中央库:" + str(output))
|
||||
|
||||
|
||||
def process_identity(pid):
|
||||
import win32api
|
||||
import win32process
|
||||
try:
|
||||
handle = win32api.OpenProcess(0x1000, False, int(pid))
|
||||
try:
|
||||
created = win32process.GetProcessTimes(handle)["CreationTime"].isoformat()
|
||||
return {"pid": int(pid), "created": created}
|
||||
finally:
|
||||
handle.Close()
|
||||
except Exception as exc:
|
||||
return {"pid": int(pid), "created": None, "query_error": str(exc)}
|
||||
|
||||
|
||||
def assert_retry_safe(record):
|
||||
if record.get("state") in ("complete", "cancelled"):
|
||||
return
|
||||
owner = record.get("owner")
|
||||
if not owner:
|
||||
raise ValueError("未完成批次缺少进程归属证据,请新建完整导出")
|
||||
current = process_identity(owner["pid"])
|
||||
if current.get("created") and current["created"] == owner.get("created"):
|
||||
raise ValueError("原批次进程仍在运行,请等待结束后重试")
|
||||
if current.get("created") is None:
|
||||
import win32api
|
||||
try:
|
||||
handle = win32api.OpenProcess(0x1000, False, owner["pid"])
|
||||
except Exception as exc:
|
||||
if getattr(exc, "winerror", None) != 87:
|
||||
raise ValueError("无法确认旧批次进程已退出,请稍后重试") from exc
|
||||
else:
|
||||
handle.Close()
|
||||
raise ValueError("旧批次进程状态无法确认,请稍后重试")
|
||||
|
||||
|
||||
def runtime_check():
|
||||
errors = []
|
||||
for name in ("win32api", "win32gui", "win32process", "win32event", "win32job", "win32service", "pythoncom"):
|
||||
try:
|
||||
importlib.import_module(name)
|
||||
except (ImportError, OSError) as exc:
|
||||
errors.append(f"{name}: {exc}")
|
||||
manifest = load_json(APP_ROOT / "package-manifest.json", {})
|
||||
for name, item in manifest.get("files", {}).items():
|
||||
path = APP_ROOT / name
|
||||
if not path.is_file():
|
||||
errors.append("缺失文件:" + name)
|
||||
elif sha256(path) != item["sha256"]:
|
||||
errors.append("文件摘要不匹配:" + name)
|
||||
return errors
|
||||
|
||||
|
||||
def previous_results(record):
|
||||
values = {}
|
||||
for row in record.get("projects", []):
|
||||
result = load_json(Path(row["folder"]) / "export.json", row.get("result"))
|
||||
if result:
|
||||
values[row["project"]["path"]] = result
|
||||
return values
|
||||
|
||||
|
||||
def run_batch(projects, problems, installation, output_root, scan_root, *, previous=None,
|
||||
stop_event=None, timeout=60, exporter=None, progress=print):
|
||||
if exporter is None:
|
||||
from .exports import export_project
|
||||
exporter = export_project
|
||||
stop_event = stop_event or Event()
|
||||
output_root = Path(output_root).resolve()
|
||||
validate_output(output_root, scan_root, projects)
|
||||
output_root.mkdir(parents=True, exist_ok=True)
|
||||
(output_root / ".mentor-export-output").write_text("MentorExport", encoding="ascii")
|
||||
batch = output_root / (datetime.now().strftime("批次_%Y%m%d_%H%M%S_") + uuid.uuid4().hex[:6])
|
||||
batch.mkdir()
|
||||
(batch / ".mentor-export-batch").write_text("MentorExport", encoding="ascii")
|
||||
shared = batch / "_共享库数据"
|
||||
shared.mkdir()
|
||||
record = {"schema": 1, "version": __version__, "state": "running",
|
||||
"scan_root": str(Path(scan_root).resolve()), "output_root": str(output_root),
|
||||
"created": datetime.now().astimezone().isoformat(), "installation": installation,
|
||||
"owner": process_identity(os.getpid()),
|
||||
"problems": problems, "projects": []}
|
||||
write_json(batch / "环境信息.json", installation)
|
||||
prior = previous_results(previous or {})
|
||||
for project in projects:
|
||||
record["projects"].append({"project": project, "folder": str(batch / project_folder_name(project, scan_root)), "status": "pending"})
|
||||
write_json(batch / "batch.json", record)
|
||||
write_report(batch, record)
|
||||
cleanup_uncertain = False
|
||||
for index, row in enumerate(record["projects"], 1):
|
||||
if stop_event.is_set():
|
||||
row["status"] = "cancelled"
|
||||
row["result"] = {"status": "cancelled", "reason": "用户停止,尚未执行", "items": []}
|
||||
write_json(Path(row["folder"]) / "export.json", row["result"])
|
||||
continue
|
||||
project = row["project"]
|
||||
row["status"] = "running"
|
||||
write_json(batch / "batch.json", record)
|
||||
progress(f"\n[{index}/{len(projects)}] {project['name']}")
|
||||
try:
|
||||
result = exporter(project, installation, Path(row["folder"]), shared,
|
||||
previous=prior.get(project["path"]), stop_event=stop_event,
|
||||
timeout=timeout, progress=progress)
|
||||
row["result"] = result
|
||||
row["status"] = result["status"]
|
||||
if result.get("cleanup_uncertain"):
|
||||
cleanup_uncertain = True
|
||||
stop_event.set()
|
||||
except KeyboardInterrupt:
|
||||
stop_event.set()
|
||||
row["status"] = "cancelled"
|
||||
row["result"] = load_json(Path(row["folder"]) / "export.json", {"items": []})
|
||||
row["result"].update(status="cancelled", reason="用户停止")
|
||||
except Exception as exc:
|
||||
row["status"] = "failed"
|
||||
row["result"] = load_json(Path(row["folder"]) / "export.json", {"items": []})
|
||||
row["result"].update(status="failed", reason=str(exc))
|
||||
folder = Path(row["folder"]) / "90_运行日志"
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
(folder / "exception.txt").write_text(traceback.format_exc(), encoding="utf-8")
|
||||
if getattr(exc, "cleanup_uncertain", False):
|
||||
cleanup_uncertain = True
|
||||
stop_event.set()
|
||||
row["result"]["cleanup_uncertain"] = True
|
||||
Path(row["folder"]).mkdir(parents=True, exist_ok=True)
|
||||
write_json(Path(row["folder"]) / "export.json", row["result"])
|
||||
write_json(batch / "batch.json", record)
|
||||
write_report(batch, record)
|
||||
progress("结果:" + LABELS.get(row["status"], row["status"]))
|
||||
cancelled = stop_event.is_set() or any(p["status"] == "cancelled" for p in record["projects"])
|
||||
record["state"] = "attention_required" if cleanup_uncertain else ("cancelled" if cancelled else "complete")
|
||||
record["finished"] = datetime.now().astimezone().isoformat()
|
||||
code = 1 if cleanup_uncertain else (130 if cancelled else (1 if problems or not projects or any(p["status"] != "success" for p in record["projects"]) else 0))
|
||||
record["exit_code"] = code
|
||||
write_json(batch / "batch.json", record)
|
||||
write_report(batch, record)
|
||||
return batch, code
|
||||
|
||||
|
||||
def _settings_paths():
|
||||
return [APP_ROOT / "settings.json",
|
||||
Path(os.environ.get("LOCALAPPDATA", str(Path.home()))) / "MentorExport/settings.json"]
|
||||
|
||||
|
||||
def _save_settings(value):
|
||||
for path in _settings_paths():
|
||||
try:
|
||||
write_json(path, value)
|
||||
return
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
|
||||
def _ask(prompt, default=None):
|
||||
suffix = f" [{default}]" if default else ""
|
||||
try:
|
||||
value = input(prompt + suffix + ":").strip().strip('"')
|
||||
except EOFError:
|
||||
raise ValueError("输入结束;批处理调用请提供 --input、--output 和 --yes")
|
||||
return value or default
|
||||
|
||||
|
||||
def _choose_installation(args, settings):
|
||||
installs = discover_installations(args.mentor_home, settings.get("mentor_home"))
|
||||
if not installs:
|
||||
if args.yes or args.check:
|
||||
raise ValueError("未找到 Mentor,请使用 --mentor-home 指定 SDD_HOME")
|
||||
installs = discover_installations(explicit=_ask("未找到 Mentor,请输入 SDD_HOME 或安装目录"))
|
||||
if len(installs) > 1 and not args.mentor_home:
|
||||
for i, install in enumerate(installs, 1):
|
||||
print(f" {i}. {install['version']} {install['root']}")
|
||||
if args.yes:
|
||||
raise ValueError("发现多套 Mentor,请使用 --mentor-home 明确指定")
|
||||
selected = int(_ask("选择 Mentor 安装编号", "1")) - 1
|
||||
if not 0 <= selected < len(installs):
|
||||
raise ValueError("安装编号超出范围")
|
||||
return installs[selected]
|
||||
return installs[0]
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(description="便携 Mentor 原理图批量导出;无参数进入中文问答。")
|
||||
parser.add_argument("--input", "-i", help="递归扫描文件夹")
|
||||
parser.add_argument("--output", "-o", help="结果根目录,每次新建批次")
|
||||
parser.add_argument("--mentor-home", help="指定 Mentor SDD_HOME")
|
||||
parser.add_argument("--retry", help="重试已有批次,指定批次目录或 batch.json")
|
||||
parser.add_argument("--timeout", type=float, default=60, help="单工具超时秒数,默认 60")
|
||||
parser.add_argument("--yes", "-y", action="store_true", help="采用已给定参数运行,不显示交互菜单")
|
||||
parser.add_argument("--check", action="store_true", help="只检查预置环境与 Mentor")
|
||||
parser.add_argument("--json", action="store_true", help="环境检查以 JSON 输出")
|
||||
parser.add_argument("--version", action="version", version=__version__)
|
||||
args = parser.parse_args(argv)
|
||||
try:
|
||||
if args.timeout <= 0:
|
||||
raise ValueError("超时必须大于 0")
|
||||
errors = runtime_check()
|
||||
if errors:
|
||||
raise ValueError("预置运行环境不完整,请重新解压发行包:\n" + "\n".join(errors))
|
||||
settings = next((v for p in _settings_paths() if (v := load_json(p)) is not None), {})
|
||||
installation = _choose_installation(args, settings)
|
||||
diagnostic_root = Path(os.environ.get("LOCALAPPDATA", str(Path.home()))) / "MentorExport/diagnostics" / uuid.uuid4().hex
|
||||
installation["preflight"] = preflight_installation(installation, diagnostic_root / "mentor-help")
|
||||
if installation["preflight"]["status"] != "success":
|
||||
raise ValueError(installation["preflight"]["reason"] + "\n" + str(diagnostic_root))
|
||||
if args.check:
|
||||
import json
|
||||
print(json.dumps({"version": __version__, "python": sys.executable, "runtime_ok": True,
|
||||
"installation": installation}, ensure_ascii=False, indent=2) if args.json else
|
||||
f"运行环境正常。Mentor:{installation['version']} {installation['root']}")
|
||||
return 0
|
||||
print(f"\nMentorExport {__version__} · 原理图详细导出")
|
||||
print(f"Mentor:{installation['version']} {installation['root']}")
|
||||
print("读取已保存的原理图。请先保存目标工程;工具仅在副本上运行。")
|
||||
if not args.retry and settings.get("output"):
|
||||
candidates = sorted(Path(settings["output"]).glob("批次_*/batch.json"), reverse=True)[:30]
|
||||
unfinished = [path for path in candidates if (load_json(path, {}).get("state") in ("running", "attention_required"))]
|
||||
if unfinished:
|
||||
print("发现未完成批次:" + str(unfinished[0].parent))
|
||||
if not args.yes and _ask("输入 1 重试该批次;回车开始新批次") == "1":
|
||||
return main(["--retry", str(unfinished[0]), "--mentor-home", installation["root"],
|
||||
"--timeout", str(args.timeout)])
|
||||
previous = None
|
||||
if args.retry:
|
||||
previous_path = Path(args.retry)
|
||||
if previous_path.is_dir():
|
||||
previous_path /= "batch.json"
|
||||
previous = load_json(previous_path)
|
||||
if not previous or previous.get("schema") != 1:
|
||||
raise ValueError("无效批次记录:" + str(previous_path))
|
||||
assert_retry_safe(previous)
|
||||
scan_root = Path(previous["scan_root"]).resolve()
|
||||
output_root = Path(args.output or previous["output_root"]).resolve()
|
||||
projects, problems = [], []
|
||||
candidates = [r["project"]["path"] for r in previous["projects"]]
|
||||
candidates += [p["path"] for p in previous.get("problems", []) if p.get("path", "").lower().endswith(".prj")]
|
||||
for path in dict.fromkeys(candidates):
|
||||
try:
|
||||
projects.append(read_project(path))
|
||||
except (OSError, ValueError) as exc:
|
||||
problems.append({"path": path, "reason": str(exc)})
|
||||
print("重试模式:核对输入与成功产物,匹配项复用;输入已变化的工程完整重导。")
|
||||
else:
|
||||
chosen = args.input or (None if args.yes else _ask("输入扫描文件夹", settings.get("input")))
|
||||
if not chosen:
|
||||
raise ValueError("请提供 --input 或输入扫描路径")
|
||||
scan_root = Path(chosen).resolve(strict=True)
|
||||
default_output = settings.get("output") or str(scan_root.parent / "Mentor导出结果")
|
||||
chosen_output = args.output or (default_output if args.yes else _ask("输入结果根目录", default_output))
|
||||
output_root = Path(chosen_output).resolve()
|
||||
if output_root == scan_root or output_root in scan_root.parents:
|
||||
raise ValueError("结果目录必须与扫描目录独立,或位于其专用子目录,不能使用扫描目录本身/其父目录")
|
||||
print("正在扫描工程……")
|
||||
projects, problems = discover_projects(scan_root, excluded=(output_root,))
|
||||
print(f"可导出工程:{len(projects)};扫描问题:{len(problems)}")
|
||||
validate_output(output_root, scan_root, projects)
|
||||
print("输出目录:" + str(output_root))
|
||||
for project in projects:
|
||||
print(f" {project['name']} · {project['root_block']} · {project['snapshot']}")
|
||||
for problem in problems:
|
||||
print(f" [扫描问题] {problem['path']}:{problem['reason']}")
|
||||
if not args.yes and _ask("开始导出?输入 Y 确认", "Y").upper() not in ("Y", "YES"):
|
||||
return 130
|
||||
_save_settings({"input": str(scan_root), "output": str(output_root), "mentor_home": installation["root"]})
|
||||
stop = Event()
|
||||
old_handler = signal.getsignal(signal.SIGINT)
|
||||
def cancel(signum, frame):
|
||||
if not stop.is_set():
|
||||
print("\n已请求停止,正在收尾自有导出进程,请稍候……", flush=True)
|
||||
stop.set()
|
||||
signal.signal(signal.SIGINT, cancel)
|
||||
try:
|
||||
batch, code = run_batch(projects, problems, installation, output_root, scan_root,
|
||||
previous=previous, stop_event=stop, timeout=args.timeout)
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, old_handler)
|
||||
record = load_json(batch / "batch.json")
|
||||
counts = {}
|
||||
for row in record["projects"]:
|
||||
counts[row["status"]] = counts.get(row["status"], 0) + 1
|
||||
print("\n本批结束:" + " ".join(f"{LABELS.get(k, k)} {v}" for k, v in counts.items()))
|
||||
print("汇总报告:" + str(batch / "导出汇总.html"))
|
||||
print("下次重试:导出.cmd --retry \"" + str(batch) + "\"")
|
||||
if not args.yes:
|
||||
while True:
|
||||
action = _ask("1 打开结果目录;2 打开汇总;3 重试未完成项;回车退出")
|
||||
if not action:
|
||||
break
|
||||
if action in ("1", "2"):
|
||||
os.startfile(str(batch if action == "1" else batch / "导出汇总.html"))
|
||||
elif action == "3":
|
||||
return main(["--retry", str(batch), "--mentor-home", installation["root"], "--timeout", str(args.timeout)])
|
||||
return code
|
||||
except KeyboardInterrupt:
|
||||
print("\n已取消。")
|
||||
return 130
|
||||
except (ValueError, OSError) as exc:
|
||||
print("\n无法开始:" + str(exc), file=sys.stderr)
|
||||
return 2
|
||||
@@ -0,0 +1,133 @@
|
||||
"""Project identity, filesystem discovery and atomic state utilities."""
|
||||
from pathlib import Path
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
|
||||
|
||||
def sha256(path):
|
||||
with Path(path).open("rb") as stream:
|
||||
return hashlib.file_digest(stream, "sha256").hexdigest()
|
||||
|
||||
|
||||
def read_text(path):
|
||||
data = Path(path).read_bytes()
|
||||
try:
|
||||
return data.decode("utf-8-sig")
|
||||
except UnicodeDecodeError:
|
||||
return data.decode("mbcs" if os.name == "nt" else "cp1252")
|
||||
|
||||
|
||||
def write_json(path, value):
|
||||
path = Path(path)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
# Windows app-container redirection can otherwise make rename look cross-volume.
|
||||
path = path.parent.resolve() / path.name
|
||||
temporary = path.with_name(path.name + "." + uuid.uuid4().hex + ".tmp")
|
||||
try:
|
||||
temporary.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
os.replace(temporary, path)
|
||||
finally:
|
||||
temporary.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def load_json(path, default=None):
|
||||
try:
|
||||
return json.loads(Path(path).read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError):
|
||||
return default
|
||||
|
||||
|
||||
def _resolve_path(value, folder):
|
||||
path = Path(os.path.expandvars(value.replace("\\", os.sep)))
|
||||
return (path if path.is_absolute() else folder / path).resolve()
|
||||
|
||||
|
||||
def read_project(path):
|
||||
path = Path(path).resolve(strict=True)
|
||||
if path.suffix.lower() != ".prj":
|
||||
raise ValueError("需要 Mentor .prj 工程文件")
|
||||
sections = {}
|
||||
section = ""
|
||||
for raw in read_text(path).splitlines():
|
||||
line = raw.strip()
|
||||
if line.startswith("SECTION "):
|
||||
section = line[8:].strip()
|
||||
sections.setdefault(section, {})
|
||||
elif line == "ENDSECTION":
|
||||
section = ""
|
||||
elif line.startswith("KEY "):
|
||||
match = re.fullmatch(r'KEY\s+(\S+)\s+(?:"([^"]*)"|(.+))', line)
|
||||
if match:
|
||||
sections.setdefault(section, {})[match[1]] = match[2] if match[2] is not None else match[3]
|
||||
roots = [(name, values["RootBlock"]) for name, values in sections.items() if values.get("RootBlock")]
|
||||
if not roots:
|
||||
raise ValueError("工程没有可识别的 RootBlock,无法确定原理图")
|
||||
if len(roots) != 1:
|
||||
raise ValueError("工程包含多个根配置,请拆分/明确原理图配置后导出:" + "、".join(name for name, _ in roots))
|
||||
config, block = roots[0]
|
||||
info = sections.get("DesignInfo", {})
|
||||
database = _resolve_path(sections.get("iCDB", {}).get("iCDBDir", "database"), path.parent)
|
||||
if database.name.lower() == "icdb.dat":
|
||||
database = database.parent
|
||||
if not (database / "icdb.dat").is_file():
|
||||
raise ValueError("找不到已保存数据库:" + str(database / "icdb.dat"))
|
||||
snapshot = info.get("FrontEndSnapshot", "DxD")
|
||||
if not snapshot or not block:
|
||||
raise ValueError("原理图根块或快照为空")
|
||||
library = info.get("CentralLibrary")
|
||||
return {"path": str(path), "name": path.stem, "database": str(database),
|
||||
"root_block": block, "snapshot": snapshot, "configuration": config,
|
||||
"library": str(_resolve_path(library, path.parent)) if library else None}
|
||||
|
||||
|
||||
def discover_projects(root, excluded=()):
|
||||
root = Path(root).resolve(strict=True)
|
||||
if not root.is_dir():
|
||||
raise ValueError("扫描路径必须是文件夹")
|
||||
excluded = [Path(path).resolve() for path in excluded]
|
||||
projects, problems = [], []
|
||||
def error(exc):
|
||||
problems.append({"path": str(exc.filename or root), "reason": str(exc)})
|
||||
for directory, folders, files in os.walk(root, topdown=True, followlinks=False, onerror=error):
|
||||
folder = Path(directory)
|
||||
keep = []
|
||||
for name in folders:
|
||||
member = folder / name
|
||||
try:
|
||||
if name in (".git", "__pycache__") or member.is_symlink() or member.is_junction():
|
||||
continue
|
||||
if any(member.resolve() == e or e in member.resolve().parents for e in excluded):
|
||||
continue
|
||||
if (member / ".mentor-export-batch").exists() or (member / ".mentor-export-output").exists():
|
||||
continue
|
||||
keep.append(name)
|
||||
except OSError as exc:
|
||||
error(exc)
|
||||
folders[:] = sorted(keep, key=str.casefold)
|
||||
present = {name.casefold() for name in files}
|
||||
for name in files:
|
||||
if name.lower().endswith(".dxp") and (Path(name).stem + ".prj").casefold() not in present:
|
||||
problems.append({"path": str((folder / name).resolve()),
|
||||
"reason": "只有 .dxp,未找到同名 .prj;请提供可读取的工程,工具不会启动 Designer 转换"})
|
||||
for name in sorted(files, key=str.casefold):
|
||||
if not name.lower().endswith(".prj"):
|
||||
continue
|
||||
path = folder / name
|
||||
if path.is_symlink():
|
||||
problems.append({"path": str(path), "reason": "跳过符号链接工程,避免重复/越界访问"})
|
||||
continue
|
||||
try:
|
||||
projects.append(read_project(path))
|
||||
except (OSError, ValueError) as exc:
|
||||
problems.append({"path": str(path.resolve()), "reason": str(exc)})
|
||||
return projects, problems
|
||||
|
||||
|
||||
def project_folder_name(project, root=None):
|
||||
path = Path(project["path"]).resolve()
|
||||
digest = hashlib.sha256(os.path.normcase(str(path)).encode("utf-8")).hexdigest()[:8]
|
||||
name = re.sub(r'[<>:"/\\|?*\x00-\x1f]', "_", project["name"]).rstrip(" .")[:64] or "工程"
|
||||
return f"{name}__{digest}"
|
||||
@@ -0,0 +1,178 @@
|
||||
"""Discover locally installed Mentor tools without starting Designer."""
|
||||
from pathlib import Path
|
||||
import os
|
||||
import re
|
||||
from .core import sha256
|
||||
|
||||
TOOL_PATHS = {
|
||||
"ascii": "iCDB/win32/bin/icdb2ascii.exe",
|
||||
"csv": "iCDB/win32/bin/icdb2csv.exe",
|
||||
"keyin": "wv/win32/bin/KeyinNetlist.exe",
|
||||
"rinf": "wv/win32/bin/RinfNetlist.exe",
|
||||
"edif": "wv/win32/bin/edifExporter.exe",
|
||||
"wir": "wv/win32/bin/icdb2wir.exe",
|
||||
"pxr": "wv/win32/bin/icdb2pxr.exe",
|
||||
"constraints_xml": "ces/win32/bin/cons2xml.exe",
|
||||
}
|
||||
VALIDATED_DLLS = {
|
||||
"PartsDB40.dll": "f24d44eaa13f28550317711df6a6241e227ae56805baa73c8f3020bde513ad0d",
|
||||
"CellDB40.dll": "e3fb93a0b5e5d2d027a82f71d1ee13d497598f709ced54c9145a781af6a54d26",
|
||||
}
|
||||
|
||||
|
||||
def _registry_module(progid):
|
||||
if os.name != "nt":
|
||||
return None
|
||||
import winreg
|
||||
try:
|
||||
view = winreg.KEY_READ | winreg.KEY_WOW64_32KEY
|
||||
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, progid + r"\CLSID", 0, view) as key:
|
||||
clsid = winreg.QueryValue(key, None)
|
||||
for server in ("InprocServer32", "LocalServer32"):
|
||||
try:
|
||||
with winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, "CLSID\\" + clsid + "\\" + server, 0, view) as key:
|
||||
command = os.path.expandvars(winreg.QueryValue(key, None)).strip()
|
||||
match = re.match(r'^"([^"]+)"', command) or re.match(r"^(.+?\.(?:dll|exe|ocx))(?:\s|$)", command, re.I)
|
||||
return str(Path(match[1] if match else command).resolve())
|
||||
except OSError:
|
||||
continue
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def _root_from_path(path):
|
||||
path = Path(os.path.expandvars(str(path))).expanduser().resolve()
|
||||
for candidate in (path, *path.parents):
|
||||
if (candidate / TOOL_PATHS["ascii"]).is_file():
|
||||
return candidate
|
||||
if (path / "SDD_HOME").is_dir():
|
||||
return path / "SDD_HOME"
|
||||
return path
|
||||
|
||||
|
||||
def inspect_installation(path):
|
||||
root = _root_from_path(path)
|
||||
tools = {name: str(root / relative) for name, relative in TOOL_PATHS.items() if (root / relative).is_file()}
|
||||
version = root.parent.name
|
||||
file_version = None
|
||||
if os.name == "nt":
|
||||
try:
|
||||
import win32api
|
||||
info = win32api.GetFileVersionInfo(str(root / "wv/win32/bin/viewdraw.exe"), "\\")
|
||||
ms, ls = info["FileVersionMS"], info["FileVersionLS"]
|
||||
file_version = f"{ms >> 16}.{ms & 65535}.{ls >> 16}.{ls & 65535}"
|
||||
except Exception:
|
||||
pass
|
||||
hashes = {}
|
||||
for name in VALIDATED_DLLS:
|
||||
file = root / "common/win32/lib" / name
|
||||
if file.is_file():
|
||||
hashes[name] = sha256(file)
|
||||
com = {}
|
||||
for name, progid in (("cms", "CMS.CeObjectDefsICDB"), ("central", "CentralLibSvr.LibPart.1")):
|
||||
registered = _registry_module(progid)
|
||||
matches = bool(registered and Path(registered).is_relative_to(root))
|
||||
com[name] = {"registered_path": registered, "matches": matches}
|
||||
return {"root": str(root), "version": version, "file_version": file_version, "tools": tools,
|
||||
"native_compatible": hashes == VALIDATED_DLLS, "dll_hashes": hashes, "com": com}
|
||||
|
||||
|
||||
def _registry_candidates():
|
||||
if os.name != "nt":
|
||||
return []
|
||||
import winreg
|
||||
paths = []
|
||||
for progid in ("ViewDraw.Application", "ViewDraw.Application.1", "CMS.CeObjectDefsICDB", "CentralLibSvr.LibPart.1"):
|
||||
value = _registry_module(progid)
|
||||
if value:
|
||||
paths.append(value)
|
||||
for hive in (winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER):
|
||||
for view in (winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY):
|
||||
try:
|
||||
with winreg.OpenKey(hive, r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", 0, winreg.KEY_READ | view) as parent:
|
||||
for index in range(winreg.QueryInfoKey(parent)[0]):
|
||||
try:
|
||||
with winreg.OpenKey(parent, winreg.EnumKey(parent, index)) as key:
|
||||
display = str(winreg.QueryValueEx(key, "DisplayName")[0])
|
||||
if re.search(r"mentor|xpedition|expedition|dxdesigner", display, re.I):
|
||||
value = str(winreg.QueryValueEx(key, "InstallLocation")[0])
|
||||
if value:
|
||||
paths.append(value)
|
||||
except OSError:
|
||||
continue
|
||||
except OSError:
|
||||
continue
|
||||
return paths
|
||||
|
||||
|
||||
def _conventional_candidates():
|
||||
candidates = []
|
||||
for drive in "CDEFG":
|
||||
for name in ("Mentor", "MentorGraphics", "Siemens", "Program Files/MentorGraphics", "Program Files/Siemens", "Program Files (x86)/MentorGraphics"):
|
||||
root = Path(f"{drive}:/{name}")
|
||||
if not root.is_dir():
|
||||
continue
|
||||
seen = 0
|
||||
for folder, names, files in os.walk(root, followlinks=False):
|
||||
path = Path(folder)
|
||||
seen += 1
|
||||
if path.name.casefold() == "sdd_home":
|
||||
candidates.append(path)
|
||||
names[:] = []
|
||||
elif len(path.relative_to(root).parts) >= 3 or seen >= 500:
|
||||
names[:] = []
|
||||
else:
|
||||
names[:] = [name for name in names if not (path / name).is_junction() and not (path / name).is_symlink()]
|
||||
return candidates
|
||||
|
||||
|
||||
def discover_installations(explicit=None, cached=None):
|
||||
if explicit:
|
||||
candidate = inspect_installation(explicit)
|
||||
if "ascii" not in candidate["tools"]:
|
||||
raise ValueError("该目录没有可用的 icdb2ascii.exe,请选择完整 Mentor 的 SDD_HOME:" + str(explicit))
|
||||
return [candidate]
|
||||
values = [path for path in (cached, os.environ.get("SDD_HOME")) if path]
|
||||
values.extend(_registry_candidates())
|
||||
values.extend(_conventional_candidates())
|
||||
results, seen = [], set()
|
||||
for value in values:
|
||||
try:
|
||||
root = _root_from_path(value)
|
||||
identity = os.path.normcase(str(root))
|
||||
if identity in seen:
|
||||
continue
|
||||
seen.add(identity)
|
||||
if not (root / TOOL_PATHS["ascii"]).is_file():
|
||||
continue
|
||||
results.append(inspect_installation(root))
|
||||
except (OSError, ValueError):
|
||||
continue
|
||||
return results
|
||||
|
||||
|
||||
def preflight_installation(installation, log_dir, runner=None):
|
||||
"""Execute a harmless help request with the same private process boundary."""
|
||||
from .exports import _environment, _run
|
||||
from .core import read_text
|
||||
runner = runner or _run
|
||||
log_dir = Path(log_dir)
|
||||
log_dir.parent.mkdir(parents=True, exist_ok=True)
|
||||
log_dir = log_dir.parent.resolve() / log_dir.name
|
||||
tool = installation.get("tools", {}).get("ascii")
|
||||
if not tool:
|
||||
return {"status": "failed", "reason": "没有 icdb2ascii.exe"}
|
||||
result = runner([tool, "-help"], log_dir.parent, _environment(installation), log_dir,
|
||||
stop_event=None, timeout=10)
|
||||
text = ""
|
||||
for name in ("stdout.txt", "stderr.txt"):
|
||||
path = log_dir / name
|
||||
if path.is_file():
|
||||
text += read_text(path)
|
||||
valid = (result.get("exit_code") == 0 and not result.get("timed_out")
|
||||
and not result.get("error") and not result.get("cleanup_uncertain")
|
||||
and not result.get("cleanup", {}).get("errors")
|
||||
and "iCDB2Ascii" in text and "-snap=" in text and "-offline" in text)
|
||||
return {"status": "success" if valid else "failed", "run": result, "log_dir": str(log_dir),
|
||||
"reason": "" if valid else "Mentor 工具无法正常显示预期帮助;检查安装/DLL,详见预检日志"}
|
||||
@@ -0,0 +1,656 @@
|
||||
"""Saved-project exports through short lived, owned, isolated tool processes."""
|
||||
from copy import deepcopy
|
||||
from contextlib import contextmanager
|
||||
import ctypes
|
||||
from datetime import datetime, timezone
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from .core import read_text, sha256, write_json
|
||||
from ._desktop import PrivateDesktop
|
||||
|
||||
HELPERS = Path(__file__).resolve().parent.parent / "helpers"
|
||||
ITEMS = (
|
||||
("ascii_flat", "ascii", "02_器件与引脚属性/icdb2ascii_平铺有效值"),
|
||||
("ascii_hier", "ascii", "02_器件与引脚属性/icdb2ascii_分层值"),
|
||||
("csv", "csv", "02_器件与引脚属性/icdb2csv"),
|
||||
("wir_oat", "wir", "02_器件与引脚属性/WIR_OAT"),
|
||||
("keyin", "keyin", "01_连接网表/KeyinNetlist"),
|
||||
("rinf", "rinf", "01_连接网表/RinfNetlist"),
|
||||
("pxr", "pxr", "01_连接网表/PXR"),
|
||||
("edif_netlist", "edif", "01_连接网表/EDIF_Netlist"),
|
||||
("edif_schematic", "edif", "03_位置与显示/EDIF_Schematic"),
|
||||
("constraints", "cms", "04_电气约束/CMS"),
|
||||
("library", "native", "05_关联库件与封装"),
|
||||
)
|
||||
VALIDATED_DLLS = {
|
||||
"PartsDB40.dll": "f24d44eaa13f28550317711df6a6241e227ae56805baa73c8f3020bde513ad0d",
|
||||
"CellDB40.dll": "e3fb93a0b5e5d2d027a82f71d1ee13d497598f709ced54c9145a781af6a54d26",
|
||||
}
|
||||
_UNCERTAIN_DESKTOPS = []
|
||||
_LIBRARY_CACHE = {}
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _workspace(result):
|
||||
path = Path(tempfile.mkdtemp(prefix="mexp-"))
|
||||
try:
|
||||
yield path
|
||||
finally:
|
||||
if result.get("cleanup_uncertain"):
|
||||
result.setdefault("workspace_preserved", str(path))
|
||||
else:
|
||||
shutil.rmtree(path)
|
||||
|
||||
|
||||
def _digest(value):
|
||||
return hashlib.sha256(json.dumps(value, sort_keys=True, ensure_ascii=False).encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _short(path):
|
||||
path = str(Path(path).resolve())
|
||||
if os.name == "nt":
|
||||
function = ctypes.windll.kernel32.GetShortPathNameW
|
||||
buffer = ctypes.create_unicode_buffer(32768)
|
||||
if function(path, buffer, len(buffer)):
|
||||
return Path(buffer.value)
|
||||
return Path(path)
|
||||
|
||||
|
||||
def _environment(installation):
|
||||
root = Path(installation["root"])
|
||||
env = dict(os.environ, SDD_HOME=str(root), SDD_PLATFORM="win32", PYTHONIOENCODING="utf-8")
|
||||
locations = ("common/win32/lib", "common/win32/bin", "wv/win32/bin", "iCDB/win32/bin", "ces/win32/bin")
|
||||
# Drop alternate Mentor entries instead of allowing a second installation's DLLs.
|
||||
inherited = [p for p in env.get("PATH", "").split(os.pathsep) if "sdd_home" not in p.lower()]
|
||||
env["PATH"] = os.pathsep.join([str(root / p) for p in locations] + inherited)
|
||||
return env
|
||||
|
||||
|
||||
def _run(command, cwd, environment, log_dir, *, stop_event, timeout):
|
||||
"""The single process boundary; only its own desktop and Job are closed."""
|
||||
log_dir = Path(log_dir)
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
desktop = PrivateDesktop("mexp-" + uuid.uuid4().hex, enabled=True)
|
||||
result = {"command": list(map(str, command)), "cwd": str(cwd), "exit_code": None,
|
||||
"timed_out": False, "cancelled": False}
|
||||
started = time.monotonic()
|
||||
try:
|
||||
with desktop:
|
||||
with (log_dir / "stdout.txt").open("wb") as stdout, (log_dir / "stderr.txt").open("wb") as stderr:
|
||||
process = desktop.start_process(tuple(map(str, command)), cwd=Path(cwd), env=environment,
|
||||
stdout=stdout, stderr=stderr)
|
||||
result["pid"] = process.pid
|
||||
while process.poll() is None:
|
||||
if stop_event is not None and stop_event.is_set():
|
||||
result["cancelled"] = True
|
||||
break
|
||||
if time.monotonic() - started >= timeout:
|
||||
result["timed_out"] = True
|
||||
try:
|
||||
result["dialogs"] = desktop.inspect_dialogs(process.pid)
|
||||
except Exception as error:
|
||||
result["dialog_inspection_error"] = str(error)
|
||||
break
|
||||
time.sleep(0.05)
|
||||
result["exit_code"] = process.poll()
|
||||
except Exception as error:
|
||||
result["error"] = str(error)
|
||||
finally:
|
||||
result["seconds"] = round(time.monotonic() - started, 3)
|
||||
result["cleanup"] = desktop.cleanup_record
|
||||
cleanup = result["cleanup"] or {}
|
||||
if cleanup.get("errors") or any(p.get("job_active_processes", 0) for p in cleanup.get("processes", [])):
|
||||
result["cleanup_uncertain"] = True
|
||||
# Retain owned handles for further cleanup instead of abandoning live resources.
|
||||
_UNCERTAIN_DESKTOPS.append(desktop)
|
||||
write_json(log_dir / "run.json", result)
|
||||
return result
|
||||
|
||||
|
||||
def _inputs(project):
|
||||
path, database = Path(project["path"]), Path(project["database"])
|
||||
files = {"input.prj": path, "database/icdb.dat": database / "icdb.dat"}
|
||||
# Inputs used by CSV/CMS, copied without server addresses or live server state.
|
||||
for relative in ("Integration/AugmentedPins.txt", "dxnetlister.ini", "speccomp.ini"):
|
||||
source = path.parent / relative
|
||||
if source.is_file():
|
||||
files[relative] = source
|
||||
return files
|
||||
|
||||
|
||||
def _hashes(files):
|
||||
return {str(path.resolve()): sha256(path) for path in files.values()}
|
||||
|
||||
|
||||
def _library_sources(project):
|
||||
if not project.get("library"):
|
||||
return {}
|
||||
library = Path(project["library"])
|
||||
root = library.parent if library.is_file() else library
|
||||
if not root.is_dir():
|
||||
return {}
|
||||
sources = {}
|
||||
for folder, pattern in (("PartsDBLibs", "*.pdb"), ("CellDBLibs", "*.cel")):
|
||||
for path in sorted((root / folder).glob(pattern)):
|
||||
sources[str(path.relative_to(root))] = path
|
||||
for path in root.glob("*.lmc"):
|
||||
sources[path.name] = path
|
||||
return sources
|
||||
|
||||
|
||||
def _capture(project, destination):
|
||||
files = _inputs(project)
|
||||
before = _hashes(files)
|
||||
for relative, source in files.items():
|
||||
target = destination / relative
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(source, target)
|
||||
if sha256(target) != before[str(source.resolve())]:
|
||||
raise ValueError("Source changed during capture; save the project and retry")
|
||||
if before != _hashes(_inputs(project)):
|
||||
raise ValueError("Source changed during capture; save the project and retry")
|
||||
text = read_text(destination / "input.prj")
|
||||
for name, value in (("iCDBDir", "database"), ("DedicatedServerName", ""), ("CentralLibrary", "")):
|
||||
text = re.sub(r"(?m)^(\s*KEY\s+" + name + r"\s+).*$", lambda m: m[1] + '"' + value + '"', text)
|
||||
(destination / "input.prj").write_text(text, encoding="utf-8")
|
||||
return before
|
||||
|
||||
|
||||
def _file_info(path):
|
||||
path = Path(path).resolve()
|
||||
return {"path": str(path), "size": path.stat().st_size, "sha256": sha256(path)}
|
||||
|
||||
|
||||
def _tool_identity(key, tool, installation):
|
||||
if tool == "cms":
|
||||
info = installation.get("com", {}).get("cms", {})
|
||||
path = info.get("registered_path")
|
||||
return {"path": path, "sha256": sha256(path) if path and Path(path).is_file() else None,
|
||||
"matches": info.get("matches", False), "helper_sha256": sha256(HELPERS / "constraints_read.vbs"),
|
||||
"definitions_sha256": sha256(Path(installation["root"]) / "standard/ce_ee.cns")
|
||||
if (Path(installation["root"]) / "standard/ce_ee.cns").is_file() else None}
|
||||
if tool == "native":
|
||||
path = HELPERS / "native_read.exe"
|
||||
root = Path(installation["root"]) / "common/win32/lib"
|
||||
return {"path": str(path), "sha256": sha256(path) if path.is_file() else None,
|
||||
"dlls": {name: sha256(root / name) for name in VALIDATED_DLLS if (root / name).is_file()}}
|
||||
path = installation.get("tools", {}).get(tool)
|
||||
return {"path": path, "sha256": sha256(path) if path and Path(path).is_file() else None}
|
||||
|
||||
|
||||
def _command(key, executable, project, work, output, installation):
|
||||
prj, db = work / "input.prj", work / "database"
|
||||
snapshot, top = project["snapshot"], project["root_block"]
|
||||
if key.startswith("ascii_"):
|
||||
return [executable, f"-project={prj}", f"-icdb={db}", f"-snap={snapshot}",
|
||||
"-f" if key == "ascii_flat" else "-h", "-t", "-c", "-offline", f"-file={output / 'connections.txt'}"]
|
||||
if key == "csv":
|
||||
return [executable, f"-project={prj}", f"-icdb={db}", f"-snap={snapshot}",
|
||||
"-properties", "-offline", f"-output={output}", "-file=design.csv"]
|
||||
if key == "wir_oat":
|
||||
for name in ("wir", "oat"):
|
||||
(output / name).mkdir()
|
||||
return [executable, f"-prj={prj}", f"-icdb={db}", f"-snap={snapshot}", f"-top={top}",
|
||||
f"-wir={output / 'wir'}", f"-oat={output / 'oat'}", "-preserve"]
|
||||
if key in ("keyin", "rinf"):
|
||||
name = "design.kyn" if key == "keyin" else "design.frs"
|
||||
return [executable, f"-block={top}", f"-icdbdir={db}", f"-icdbsnapshot={snapshot}",
|
||||
"-flowtype=DX", f"-out={output / name}"]
|
||||
if key == "pxr":
|
||||
return [executable, "--icdb", str(db), "--snap", snapshot, "--top", top, "--flow", "DX", "--out", str(output / "design.pxr")]
|
||||
if key.startswith("edif_"):
|
||||
return [executable] + (["-schematic"] if key == "edif_schematic" else []) + [
|
||||
"-icdb", str(db), "-isnap", snapshot, "-top", top, "-o", str(output / "design.edf")]
|
||||
if key == "constraints":
|
||||
return [str(Path(os.environ.get("SystemRoot", "C:/Windows")) / "SysWOW64/cscript.exe"), "//nologo",
|
||||
str(HELPERS / "constraints_read.vbs"), str(prj), str(output / "constraints.jsonl"),
|
||||
str(Path(installation["root"]) / "standard/ce_ee.cns"), snapshot, top]
|
||||
raise ValueError(key)
|
||||
|
||||
|
||||
def _validate(key, output):
|
||||
files = sorted(p for p in output.rglob("*") if p.is_file())
|
||||
if not files:
|
||||
raise ValueError("No output files were produced")
|
||||
if key == "csv":
|
||||
manifest = output / "work/design.csv"
|
||||
if not manifest.is_file():
|
||||
raise ValueError("CSV output manifest missing")
|
||||
names = [line[5:].strip() for line in read_text(manifest).splitlines() if line.startswith("file:")]
|
||||
if not names or any(Path(n).name != n for n in names):
|
||||
raise ValueError("Invalid CSV output manifest")
|
||||
tables = {Path(n).suffix[1:] for n in names}
|
||||
if not {"sym", "spn", "spr", "ppr", "prm", "ppn", "prt", "pth", "sht", "inf", "npr", "thn"}.issubset(tables):
|
||||
raise ValueError("CSV output manifest omits required relational tables")
|
||||
for name in names:
|
||||
table = manifest.parent / name
|
||||
if not table.is_file() or not table.stat().st_size:
|
||||
raise ValueError("CSV output table missing/empty: " + name)
|
||||
if table.suffix != ".esf" and "\t" not in read_text(table).splitlines()[0]:
|
||||
raise ValueError("CSV output table header is not tab separated: " + name)
|
||||
return files
|
||||
if key == "wir_oat":
|
||||
wire = [p for p in files if p.parent.name == "wir"]
|
||||
oat = [p for p in files if p.parent.name == "oat"]
|
||||
if (not wire or not oat or any("Wirelist" not in read_text(p)[:200] for p in wire)
|
||||
or any(not re.match(r"V\s+\d", read_text(p)) for p in oat)):
|
||||
raise ValueError("WIR/OAT output incomplete or unrecognized")
|
||||
return files
|
||||
path = output / {"ascii_flat": "connections.txt", "ascii_hier": "connections.txt", "keyin": "design.kyn",
|
||||
"rinf": "design.frs", "pxr": "design.pxr", "edif_netlist": "design.edf",
|
||||
"edif_schematic": "design.edf", "constraints": "constraints.jsonl"}[key]
|
||||
if not path.is_file() or not path.stat().st_size:
|
||||
raise ValueError("Expected output file missing or empty: " + path.name)
|
||||
text = path.read_text(encoding="utf-16") if key == "constraints" else read_text(path)
|
||||
markers = {"ascii_flat": (r"iCDB Ascii Report", r"(?m)^Config\s"),
|
||||
"ascii_hier": (r"iCDB Ascii Report", r"(?m)^Block\s"),
|
||||
"keyin": (r"%net", r"%page="), "rinf": (r"\.HEA", r"\.APP"), "pxr": (r"Net_Cross_Reference",),
|
||||
"edif_netlist": (r"\(edif\s", r"\(net\s"), "edif_schematic": (r"\(edif\s", r"\(page\s")}
|
||||
if key in markers and not all(re.search(pattern, text, re.I) for pattern in markers[key]):
|
||||
raise ValueError("Output format validation failed: " + path.name)
|
||||
if key == "constraints":
|
||||
rows = [json.loads(line) for line in text.splitlines() if line.strip()]
|
||||
if not rows or rows[0].get("kind") != "initialization" or rows[-1].get("kind") != "complete":
|
||||
raise ValueError("CMS output has no completion marker")
|
||||
if int(rows[-1]["count"]) != sum(r.get("kind") == "constraint" for r in rows):
|
||||
raise ValueError("CMS output count mismatch")
|
||||
if any(r.get("error") for r in rows):
|
||||
raise ValueError("CMS output contains failed field getters")
|
||||
return [path]
|
||||
|
||||
|
||||
def _reusable(old, fingerprint, identity):
|
||||
if not old or old.get("status") != "success" or old.get("provenance", {}).get("input_fingerprint") != fingerprint:
|
||||
return False
|
||||
provenance = old["provenance"]
|
||||
if provenance.get("tool") != identity or not old.get("outputs"):
|
||||
return False
|
||||
infos = provenance.get("output_files", [])
|
||||
try:
|
||||
return len(infos) == len(old["outputs"]) and all(_file_info(i["path"]) == i for i in infos)
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
def _status(items):
|
||||
states = [i["status"] for i in items]
|
||||
if "cancelled" in states:
|
||||
return "cancelled"
|
||||
if states and all(s == "success" for s in states):
|
||||
return "success"
|
||||
return "partial" if "success" in states else "failed"
|
||||
|
||||
|
||||
def export_project(project, installation, project_dir, shared_dir, *, previous=None, stop_event=None, timeout=45, progress=print):
|
||||
project_dir = Path(project_dir).resolve()
|
||||
project_dir.mkdir(parents=True, exist_ok=True)
|
||||
shared_dir = Path(shared_dir).resolve()
|
||||
result = {"status": "running", "project": deepcopy(project), "captured_at": datetime.now(timezone.utc).isoformat(),
|
||||
"source_fingerprint": None, "source_hashes": {}, "items": [], "source_unchanged": None,
|
||||
"coverage_limits": ["Exports preserve each tool/mode separately; no ERC or manufacturing certification.",
|
||||
"CMS requests read mode but may report ReadOnly=False; only isolated copies are opened.",
|
||||
"Cell coordinate units and rotation remain native integers; multi-slot/swappable mappings unverified."]}
|
||||
state = project_dir / "export.json"
|
||||
write_json(state, result)
|
||||
if _UNCERTAIN_DESKTOPS:
|
||||
result.update(status="failed", cleanup_uncertain=True,
|
||||
reason="A previous owned process has unconfirmed cleanup; restart only after resolving its recorded Job/processes")
|
||||
write_json(state, result)
|
||||
return result
|
||||
with _workspace(result) as temporary:
|
||||
temp = _short(temporary)
|
||||
captured = temp / "saved"
|
||||
captured.mkdir()
|
||||
try:
|
||||
result["source_hashes"] = _capture(project, captured)
|
||||
result["library_source_hashes"] = _hashes(_library_sources(project))
|
||||
fingerprint = _digest({"source": result["source_hashes"], "snapshot": project["snapshot"],
|
||||
"root_block": project["root_block"], "configuration": project.get("configuration"),
|
||||
"library": result["library_source_hashes"]})
|
||||
result["source_fingerprint"] = fingerprint
|
||||
except Exception as error:
|
||||
result.update(status="failed", reason=str(error), source_unchanged=False)
|
||||
write_json(state, result)
|
||||
return result
|
||||
environment = _environment(installation)
|
||||
old_items = {i["key"]: i for i in (previous or {}).get("items", [])}
|
||||
discovery = {"status": "failed", "names": []}
|
||||
result["snapshot_discovery"] = discovery
|
||||
try:
|
||||
snapshot_work = temp / "snapshot-discovery"
|
||||
shutil.copytree(captured, snapshot_work)
|
||||
discovery.update(_snapshot_metadata(project, installation, snapshot_work,
|
||||
project_dir / "90_运行日志" / "snapshots", stop_event=stop_event,
|
||||
timeout=min(timeout, 20)))
|
||||
if discovery.get("cleanup_uncertain"):
|
||||
result.update(cleanup_uncertain=True, workspace_preserved=str(temp))
|
||||
except Exception as error:
|
||||
discovery.update(status="failed", reason=str(error))
|
||||
if discovery["status"] != "success":
|
||||
result["coverage_limits"].append("Other snapshot names were not obtained: " + discovery.get("reason", ""))
|
||||
write_json(state, result)
|
||||
for key, tool, folder in ITEMS:
|
||||
output = None
|
||||
item = {"key": key, "status": "failed", "reason": "", "outputs": [], "provenance": {}}
|
||||
result["items"].append(item)
|
||||
try:
|
||||
progress(project["name"] + ": " + key)
|
||||
identity = _tool_identity(key, tool, installation)
|
||||
item["provenance"] = {"tool": identity, "snapshot": project["snapshot"], "root_block": project["root_block"],
|
||||
"installation": installation["root"], "version": installation.get("version"),
|
||||
"input_fingerprint": fingerprint, "reused": False}
|
||||
if result.get("cleanup_uncertain"):
|
||||
item.update(status="cancelled", reason="Not launched: previous owned process cleanup is uncertain")
|
||||
continue
|
||||
if stop_event is not None and stop_event.is_set():
|
||||
item.update(status="cancelled", reason="Cancelled before launch")
|
||||
continue
|
||||
if _reusable(old_items.get(key), fingerprint, identity):
|
||||
item.update(deepcopy(old_items[key]))
|
||||
item["provenance"]["reused"] = True
|
||||
item["reason"] = "Verified previous successful output reused"
|
||||
continue
|
||||
if key == "library":
|
||||
_export_library(item, project, installation, result, project_dir / folder, shared_dir,
|
||||
temp, environment, stop_event, timeout)
|
||||
continue
|
||||
if not identity.get("sha256") or (tool == "cms" and not identity.get("matches")):
|
||||
item.update(status="unavailable", reason="Tool missing or COM registration does not match selected installation")
|
||||
continue
|
||||
work = temp / key
|
||||
shutil.copytree(captured, work)
|
||||
output = work / "output"
|
||||
output.mkdir()
|
||||
command = _command(key, identity.get("path"), project, work, output, installation)
|
||||
log_dir = project_dir / "90_运行日志" / key
|
||||
run = _run(command, work, environment, log_dir, stop_event=stop_event, timeout=timeout)
|
||||
item["provenance"].update(run)
|
||||
item["provenance"]["log_dir"] = str(log_dir)
|
||||
if run.get("cleanup_uncertain"):
|
||||
result.update(cleanup_uncertain=True, workspace_preserved=str(temp))
|
||||
item["reason"] = "Owned process cleanup is uncertain; workspace preserved and later tools stopped"
|
||||
elif run.get("cancelled"):
|
||||
item.update(status="cancelled", reason="Cancelled while tool was running")
|
||||
elif run.get("timed_out"):
|
||||
item["reason"] = "Tool timed out; owned Job cleaned up"
|
||||
elif run.get("error") or run.get("exit_code") != 0 or run.get("cleanup", {}).get("errors"):
|
||||
item["reason"] = run.get("error") or "Tool exited unsuccessfully or process cleanup failed"
|
||||
else:
|
||||
files = _validate(key, output)
|
||||
destination = project_dir / folder
|
||||
published = []
|
||||
for file in files:
|
||||
target = destination / file.relative_to(output)
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(file, target)
|
||||
published.append(target)
|
||||
item.update(status="success", outputs=[str(p) for p in published])
|
||||
item["provenance"]["output_files"] = [_file_info(p) for p in published]
|
||||
except Exception as error:
|
||||
item.update(status="failed", reason=str(error))
|
||||
finally:
|
||||
if item["status"] in ("failed", "cancelled") and output is not None:
|
||||
try:
|
||||
evidence = project_dir / "90_运行日志" / key / "failed-output"
|
||||
saved = []
|
||||
for source in output.rglob("*"):
|
||||
if source.is_file():
|
||||
target = evidence / source.relative_to(output)
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(source, target)
|
||||
saved.append(_file_info(target))
|
||||
if saved:
|
||||
item["provenance"]["failed_output_files"] = saved
|
||||
except OSError as error:
|
||||
item["provenance"]["failure_evidence_error"] = str(error)
|
||||
result["status"] = "running"
|
||||
write_json(state, result)
|
||||
try:
|
||||
after = _hashes(_inputs(project))
|
||||
result["source_hashes_after"] = after
|
||||
result["library_source_hashes_after"] = _hashes(_library_sources(project))
|
||||
result["source_unchanged"] = (after == result["source_hashes"] and
|
||||
result["library_source_hashes_after"] == result["library_source_hashes"])
|
||||
except OSError as error:
|
||||
result["source_unchanged"] = False
|
||||
result["source_check_error"] = str(error)
|
||||
if not result["source_unchanged"]:
|
||||
for item in result["items"]:
|
||||
if item["status"] == "success":
|
||||
item.update(status="failed", reason="Source changed during export; save and run a full new attempt")
|
||||
result["status"] = "failed" if result.get("cleanup_uncertain") else _status(result["items"])
|
||||
write_json(state, result)
|
||||
return result
|
||||
|
||||
|
||||
def _snapshot_metadata(project, installation, work, log, *, stop_event, timeout):
|
||||
metadata = {"status": "unavailable", "names": [], "log_dir": str(log)}
|
||||
executable = installation.get("tools", {}).get("constraints_xml")
|
||||
if not executable:
|
||||
metadata["reason"] = "Snapshot listing tool unavailable"
|
||||
return metadata
|
||||
if stop_event is not None and stop_event.is_set():
|
||||
metadata.update(status="cancelled", reason="Cancelled before snapshot discovery")
|
||||
return metadata
|
||||
run = _run([executable, "-list", "-icdb", str(work / "database")], work, _environment(installation),
|
||||
log, stop_event=stop_event, timeout=timeout)
|
||||
metadata.update(status="failed", run=run)
|
||||
if run.get("cleanup_uncertain"):
|
||||
metadata.update(cleanup_uncertain=True, workspace_preserved=str(work),
|
||||
reason="Snapshot process cleanup uncertain; workspace preserved")
|
||||
elif run.get("cancelled"):
|
||||
metadata.update(status="cancelled", reason="Snapshot discovery cancelled")
|
||||
elif run.get("exit_code") != 0 or run.get("error") or run.get("timed_out"):
|
||||
metadata["reason"] = "Snapshot listing failed or timed out; see run log"
|
||||
else:
|
||||
metadata["names"] = list(dict.fromkeys(re.findall(r"(?m)^\s*->\s*(\S.*?)\s*$", read_text(log / "stdout.txt"))))
|
||||
if project["snapshot"] in metadata["names"]:
|
||||
metadata.update(status="success", reason="Names only; configured schematic snapshot remains selected")
|
||||
else:
|
||||
metadata["reason"] = "Configured snapshot was not found in listing"
|
||||
return metadata
|
||||
|
||||
|
||||
def list_snapshots(project, installation, work_dir):
|
||||
"""List snapshot names using a disposable copy; never changes default selection."""
|
||||
executable = installation.get("tools", {}).get("constraints_xml")
|
||||
if not executable:
|
||||
return []
|
||||
if _UNCERTAIN_DESKTOPS:
|
||||
raise RuntimeError("A prior owned process has unconfirmed cleanup; snapshot discovery stopped")
|
||||
workspace_state = {}
|
||||
with _workspace(workspace_state) as temporary:
|
||||
work = _short(temporary)
|
||||
_capture(project, work)
|
||||
metadata = _snapshot_metadata(project, installation, work, Path(work_dir) / "snapshots",
|
||||
stop_event=None, timeout=20)
|
||||
if metadata.get("cleanup_uncertain"):
|
||||
workspace_state.update(cleanup_uncertain=True, workspace_preserved=str(work))
|
||||
raise RuntimeError("Snapshot process cleanup uncertain; workspace preserved: " + str(work))
|
||||
return metadata["names"]
|
||||
|
||||
|
||||
def _export_library(item, project, installation, result, destination, shared_dir, temp, environment, stop_event, timeout):
|
||||
identity = item["provenance"]["tool"]
|
||||
if identity.get("dlls") != VALIDATED_DLLS:
|
||||
item.update(status="unavailable", reason="Native DLL ABI is unverified; exact PartsDB40/CellDB40 SHA256 required")
|
||||
return
|
||||
if not identity.get("sha256"):
|
||||
item.update(status="unavailable", reason="Native library reader executable missing")
|
||||
return
|
||||
sources = _library_sources(project)
|
||||
if not sources:
|
||||
item.update(status="unavailable", reason="Central library missing or contains no local PDB/CEL partitions")
|
||||
return
|
||||
references = {"parts": set(), "cell": set()}
|
||||
flat = next((i for i in result["items"] if i["key"] == "ascii_flat" and i["status"] == "success"), None)
|
||||
if not flat:
|
||||
item.update(status="unavailable", reason="Effective ASCII properties required to identify actually used parts/cells")
|
||||
return
|
||||
for line in read_text(flat["outputs"][0]).splitlines():
|
||||
match = re.match(r"\s*Prop\s+(Part Number|Cell Name)\s+(.+?)\s*$", line)
|
||||
if match:
|
||||
references["parts" if match[1] == "Part Number" else "cell"].add(match[2])
|
||||
if not any(references.values()):
|
||||
item.update(status="unavailable", reason="No used Part Number / Cell Name properties found")
|
||||
return
|
||||
source_hashes = result["library_source_hashes"]
|
||||
fingerprint = _digest({"sources": source_hashes, "tool": identity})
|
||||
request = {mode: sorted(names) for mode, names in references.items()}
|
||||
shared = shared_dir / fingerprint[:24]
|
||||
shared.mkdir(parents=True, exist_ok=True)
|
||||
data_path = shared / ("used-" + _digest(request)[:20] + ".json")
|
||||
data = None
|
||||
if data_path.is_file():
|
||||
try:
|
||||
cached = json.loads(data_path.read_text(encoding="utf-8"))
|
||||
if (cached.get("complete") and cached.get("fingerprint") == fingerprint and cached.get("request") == request
|
||||
and cached.get("records_sha256") == _digest(cached.get("records"))):
|
||||
data = cached
|
||||
item["provenance"]["reused"] = True
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
if data is None:
|
||||
data = {"fingerprint": fingerprint, "source_hashes": source_hashes, "request": request,
|
||||
"complete": False, "records": {"parts": [], "cell": []}, "runs": [], "errors": [],
|
||||
"coverage_limits": ["Only actually referenced effective part numbers/cells are retained.",
|
||||
"Current library mapping is not proof of the historical library version.",
|
||||
"Symbol pin ordinals and cell pad names are separate data; multi-slot/swappable mapping unverified.",
|
||||
"Cell geometry retains native unit codes; copper/drill/mask shapes are not decoded."]}
|
||||
def persist():
|
||||
data["records_sha256"] = _digest(data["records"])
|
||||
write_json(data_path, data)
|
||||
item["outputs"] = [str(data_path)]
|
||||
item["provenance"].update(library_fingerprint=fingerprint, library_source_hashes=source_hashes,
|
||||
output_files=[_file_info(data_path)])
|
||||
persist()
|
||||
dll_dir = Path(installation["root"]) / "common/win32/lib"
|
||||
partitions = _LIBRARY_CACHE.setdefault(fingerprint, {})
|
||||
for index, (relative, source) in enumerate(sources.items()):
|
||||
if source.suffix.lower() not in (".pdb", ".cel"):
|
||||
continue
|
||||
if stop_event is not None and stop_event.is_set():
|
||||
item.update(status="cancelled", reason="Library reading cancelled")
|
||||
return
|
||||
mode = "parts" if source.suffix.lower() == ".pdb" else "cell"
|
||||
if not references[mode]:
|
||||
continue
|
||||
known = partitions.get(relative)
|
||||
if known is not None:
|
||||
needed = references[mode] & known["names"]
|
||||
available = {row["name"] for row in known["selected"]}
|
||||
if needed <= available:
|
||||
data["records"][mode].extend(deepcopy(row) for row in known["selected"] if row["name"] in needed)
|
||||
data["runs"].append({"partition": relative, "reused_partition": True,
|
||||
"original_run": deepcopy(known["run"])})
|
||||
persist()
|
||||
continue
|
||||
work = temp / ("library-" + str(index))
|
||||
work.mkdir()
|
||||
copied = work / ("input" + source.suffix)
|
||||
shutil.copyfile(source, copied)
|
||||
if sha256(copied) != source_hashes[str(source.resolve())]:
|
||||
raise ValueError("Library changed during capture; run a complete new attempt")
|
||||
log = destination.parent / "90_运行日志" / "library" / f"{index:03d}"
|
||||
command = [str(HELPERS / "native_read.exe"), mode, str(dll_dir), str(copied)]
|
||||
run = _run(command, work, environment, log, stop_event=stop_event, timeout=timeout)
|
||||
run["partition"] = relative
|
||||
run["log_dir"] = str(log)
|
||||
run["stdout_retained"] = True
|
||||
write_json(log / "run.json", run)
|
||||
data["runs"].append(run)
|
||||
persist()
|
||||
if run.get("cleanup_uncertain"):
|
||||
result.update(cleanup_uncertain=True, workspace_preserved=str(temp))
|
||||
item.update(status="failed", reason="Library reader cleanup uncertain; workspace preserved and batch must stop")
|
||||
item["provenance"]["uncertain_run"] = run
|
||||
return
|
||||
if run.get("cancelled"):
|
||||
item.update(status="cancelled", reason="Library reading cancelled")
|
||||
return
|
||||
if run.get("exit_code") != 0 or run.get("error") or run.get("timed_out") or run.get("cleanup", {}).get("errors"):
|
||||
data["errors"].append({"partition": relative, "reason": run.get("error") or "Native read failed/timeout/cleanup error"})
|
||||
persist()
|
||||
continue
|
||||
try:
|
||||
native = json.loads((log / "stdout.txt").read_text(encoding="utf-8"))
|
||||
_validate_native(native, mode)
|
||||
selected = []
|
||||
for row in native["records"]:
|
||||
if row.get("name") in references[mode]:
|
||||
row = dict(row, source_partition=relative, source_sha256=source_hashes[str(source.resolve())])
|
||||
if "base_unit_code" in native:
|
||||
row["base_unit_code"] = native["base_unit_code"]
|
||||
data["records"][mode].append(row)
|
||||
selected.append(deepcopy(row))
|
||||
# Keep only name membership and already requested rows, never all cell pads.
|
||||
if known:
|
||||
old_names = {row["name"] for row in selected}
|
||||
selected.extend(row for row in known["selected"] if row["name"] not in old_names)
|
||||
partitions[relative] = {"names": {row["name"] for row in native["records"]},
|
||||
"selected": selected, "run": deepcopy(run)}
|
||||
# Remove unrelated full-library stdout only after selected records are durable.
|
||||
persist()
|
||||
(log / "stdout.txt").unlink()
|
||||
run["stdout_retained"] = False
|
||||
write_json(log / "run.json", run)
|
||||
partitions[relative]["run"] = deepcopy(run)
|
||||
persist()
|
||||
except (OSError, ValueError) as error:
|
||||
data["errors"].append({"partition": relative, "reason": str(error)})
|
||||
persist()
|
||||
if source_hashes != _hashes(_library_sources(project)):
|
||||
raise ValueError("Library changed during export; run a complete new attempt")
|
||||
data["complete"] = not data["errors"]
|
||||
data["records_sha256"] = _digest(data["records"])
|
||||
write_json(data_path, data)
|
||||
missing, ambiguous = {}, {}
|
||||
for mode in references:
|
||||
counts = {name: sum(row["name"] == name for row in data["records"][mode]) for name in references[mode]}
|
||||
missing[mode] = sorted(name for name, count in counts.items() if count == 0)
|
||||
ambiguous[mode] = sorted(name for name, count in counts.items() if count > 1)
|
||||
index = {"library": project["library"], "fingerprint": fingerprint, "shared_data": str(data_path),
|
||||
"request": request, "missing": missing, "ambiguous": ambiguous, "errors": data["errors"],
|
||||
"coverage_limits": data["coverage_limits"], "shared_data_sha256": sha256(data_path)}
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
index_path = destination / "工程引用与数据索引.json"
|
||||
write_json(index_path, index)
|
||||
item["outputs"] = [str(index_path), str(data_path)]
|
||||
item["provenance"].update(library_fingerprint=fingerprint, library_source_hashes=source_hashes,
|
||||
output_files=[_file_info(p) for p in item["outputs"]])
|
||||
if data["errors"] or any(missing.values()) or any(ambiguous.values()):
|
||||
item.update(status="failed", reason="Partial library coverage: missing/ambiguous names or failed partitions; see index")
|
||||
else:
|
||||
item.update(status="success", reason="Used part/cell records read from fingerprinted shared library")
|
||||
|
||||
|
||||
def _validate_native(native, mode):
|
||||
if native.get("mode") != mode or native.get("count") != len(native.get("records", [])):
|
||||
raise ValueError("Native enumeration count/mode mismatch")
|
||||
if any(not isinstance(row.get("name"), str) or not row["name"] for row in native["records"]):
|
||||
raise ValueError("Native getter returned invalid object name")
|
||||
def visit(value):
|
||||
if isinstance(value, dict):
|
||||
if type(value.get("location_hr")) is int and 0 <= value["location_hr"] < 0x80000000:
|
||||
point = value.get("xy_raw")
|
||||
if not isinstance(point, list) or len(point) != 2 or any(type(c) is not int for c in point):
|
||||
raise ValueError("Native getter returned invalid coordinate pointer/data")
|
||||
for key, child in value.items():
|
||||
if key == "hr" or key.endswith("_hr"):
|
||||
if type(child) is not int or child < 0 or child >= 0x80000000:
|
||||
raise ValueError("Native getter failed: " + key)
|
||||
if key == "value" and child is None:
|
||||
raise ValueError("Native getter returned null value")
|
||||
visit(child)
|
||||
elif isinstance(value, list):
|
||||
for child in value:
|
||||
visit(child)
|
||||
visit(native)
|
||||
@@ -0,0 +1,72 @@
|
||||
"""Offline HTML summaries that link to unchanged, separately sourced exports."""
|
||||
from collections import Counter
|
||||
from html import escape
|
||||
from pathlib import Path
|
||||
import os
|
||||
from urllib.parse import quote
|
||||
|
||||
LABELS = {"success": "成功", "partial": "部分成功", "failed": "失败",
|
||||
"unavailable": "不可用", "cancelled": "已取消", "running": "执行中", "pending": "待执行"}
|
||||
STYLE = """body{font:15px/1.65 'Segoe UI','Microsoft YaHei',sans-serif;background:#f4f6f9;color:#243246;margin:32px auto;max-width:1150px;padding:0 24px}
|
||||
table{border-collapse:collapse;width:100%;background:white;margin:16px 0}th,td{padding:10px 14px;border:1px solid #dde3ec;text-align:left;vertical-align:top}th{background:#eaf0f8}
|
||||
a{color:#1756a9;overflow-wrap:anywhere}.success{color:#137146}.failed{color:#b42318}.partial,.unavailable{color:#925600}code{background:#e9edf4;padding:2px 5px}small{color:#5c6a7d}.card{background:white;border:1px solid #dde3ec;border-radius:10px;padding:18px;margin:16px 0}"""
|
||||
|
||||
|
||||
def link(path, base, label=None):
|
||||
path, base = Path(path).resolve(), Path(base).resolve()
|
||||
try:
|
||||
href = quote(os.path.relpath(path, base).replace("\\", "/"), safe="/")
|
||||
except ValueError:
|
||||
href = path.as_uri()
|
||||
return f'<a href="{escape(href, quote=True)}">{escape(str(label or path.name))}</a>'
|
||||
|
||||
|
||||
def page(title, content):
|
||||
return f'<!doctype html><html lang="zh-CN"><meta charset="utf-8"><title>{escape(title)}</title><style>{STYLE}</style><h1>{escape(title)}</h1>{content}</html>'
|
||||
|
||||
|
||||
def write_report(batch, record):
|
||||
batch = Path(batch)
|
||||
batch.mkdir(parents=True, exist_ok=True)
|
||||
rows = []
|
||||
counts = Counter(row["status"] for row in record.get("projects", []))
|
||||
summary = " ".join(f"{LABELS.get(status, status)} {count}" for status, count in counts.items())
|
||||
for row in record.get("projects", []):
|
||||
project = row["project"]
|
||||
folder = Path(row["folder"])
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
result = row.get("result", {})
|
||||
items = []
|
||||
for item in result.get("items", []):
|
||||
outputs = "<br>".join(link(path, folder) for path in item.get("outputs", []))
|
||||
origin = item.get("provenance", {})
|
||||
tool = origin.get("tool") or origin.get("executable") or origin.get("backend") or item["key"]
|
||||
if isinstance(tool, dict):
|
||||
tool = Path(tool.get("path") or "").name or item["key"]
|
||||
items.append(f'<tr><td>{escape(item["key"])}</td><td>{escape(str(tool))}</td>'
|
||||
f'<td class="{escape(item["status"])}">{LABELS.get(item["status"], escape(item["status"]))}</td>'
|
||||
f'<td>{escape(str(item.get("reason", "")))}</td><td>{outputs}</td></tr>')
|
||||
limits = "".join("<li>" + escape(str(value)) + "</li>" for value in result.get("coverage_limits", []))
|
||||
discovery = result.get("snapshot_discovery", {})
|
||||
snapshot_names = "、".join(discovery.get("names", [])) or "未取得"
|
||||
details = (f'<div class="card">状态:{LABELS.get(row["status"], row["status"])}<br>'
|
||||
f'来源:{escape(str(project["path"]))}<br>'
|
||||
f'根块:{escape(str(project.get("root_block", "")))} 快照:{escape(str(project.get("snapshot", "")))}<br>'
|
||||
f'数据库快照名称(仅记录):{escape(snapshot_names)}<br>'
|
||||
f'{escape(str(result.get("reason", "")))}</div>'
|
||||
'<p>各工具和模式的输出分别保留;同名字段可能具有不同来源。导出成功不等于工程电气正确性校验通过。</p>'
|
||||
'<table><tr><th>导出项</th><th>来源</th><th>状态</th><th>说明</th><th>产物</th></tr>' + "".join(items) + '</table>'
|
||||
+ ('<h2>覆盖限制</h2><ul>' + limits + '</ul>' if limits else '')
|
||||
+ link(folder / "export.json", folder, "工程原始记录 JSON")
|
||||
+ " " + link(folder / "90_运行日志", folder, "运行日志"))
|
||||
(folder / "工程说明.html").write_text(page(project["name"], details), encoding="utf-8")
|
||||
rows.append(f'<tr><td>{link(folder / "工程说明.html", batch, project["name"])}</td>'
|
||||
f'<td class="{escape(row["status"])}">{LABELS.get(row["status"], escape(row["status"]))}</td>'
|
||||
f'<td>{escape(str(result.get("reason", "")))}</td><td>{escape(str(project["path"]))}</td></tr>')
|
||||
issues = "".join(f'<li>{escape(str(p.get("path", "")))}:{escape(str(p.get("reason", "")))}</li>' for p in record.get("problems", []))
|
||||
content = (f'<div class="card">{escape(summary or "没有可执行工程")}<br>批次状态:{escape(record.get("state", ""))}</div>'
|
||||
'<p>默认读取已保存的原理图;不同工具的字段和覆盖范围分别记录。点击工程名查看逐项结果。</p>'
|
||||
'<table><tr><th>工程</th><th>结果</th><th>说明</th><th>源文件</th></tr>' + "".join(rows) + '</table>'
|
||||
+ ('<h2>扫描问题</h2><ul>' + issues + '</ul>' if issues else '')
|
||||
+ link(batch / "batch.json", batch, "批次记录 JSON"))
|
||||
(batch / "导出汇总.html").write_text(page("Mentor 原理图批量导出", content), encoding="utf-8")
|
||||
@@ -0,0 +1,94 @@
|
||||
"""Exercise a relocated release with no system Python environment or PATH."""
|
||||
from pathlib import Path
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
ACCEPTANCE = ROOT / ("build/release-acceptance-" + time.strftime("%Y%m%d-%H%M%S"))
|
||||
|
||||
|
||||
def digest(path):
|
||||
return hashlib.file_digest(path.open("rb"), "sha256").hexdigest()
|
||||
|
||||
|
||||
def main(package):
|
||||
package = Path(package).resolve()
|
||||
destination = ACCEPTANCE / "中文 空格 便携工具" / package.name
|
||||
if destination.exists():
|
||||
raise ValueError("Acceptance destination already exists; preserve previous evidence")
|
||||
shutil.copytree(package, destination)
|
||||
inputs = ACCEPTANCE / "input"
|
||||
fixture_list = json.loads((ROOT / "build/acceptance/inputs.json").read_text(encoding="utf-8"))
|
||||
first = inputs / "普通工程"
|
||||
second = inputs
|
||||
for index in range(6):
|
||||
second /= f"长路径目录{index}_" + "中文路径" * 8
|
||||
for fixture, target in zip(fixture_list, (first, second)):
|
||||
shutil.copytree(Path(fixture["path"]).parent, target)
|
||||
prjs = list(inputs.rglob("*.prj"))
|
||||
assert len(prjs) == 2 and max(len(str(path)) for path in prjs) > 260
|
||||
before = {str(path): digest(path) for path in inputs.rglob("*") if path.is_file()}
|
||||
environment = dict(os.environ, PYTHONHOME=r"Z:\invalid-python", PYTHONPATH=r"Z:\invalid-packages",
|
||||
PYTHONIOENCODING="utf-8", PATH=os.environ.get("SystemRoot", r"C:\Windows") + r"\System32")
|
||||
evidence = {"package": str(package), "relocated": str(destination), "source_path_lengths": [len(str(p)) for p in prjs],
|
||||
"environment": {key: environment[key] for key in ("PYTHONHOME", "PYTHONPATH", "PATH")}, "runs": []}
|
||||
cmd = str(Path(os.environ.get("SystemRoot", r"C:\Windows")) / "System32/cmd.exe")
|
||||
|
||||
def run(key, arguments, stdin=None):
|
||||
started = time.monotonic()
|
||||
command = '""' + str(destination / "导出.cmd") + '" ' + subprocess.list2cmdline(arguments) + '"'
|
||||
# cmd parses quotes itself; list2cmdline escaping the /c body breaks its outer quotes.
|
||||
process = subprocess.run(subprocess.list2cmdline([cmd]) + " /d /s /c " + command, input=stdin, env=environment,
|
||||
cwd=Path(os.environ.get("SystemRoot", r"C:\Windows")) / "Temp",
|
||||
capture_output=True, encoding="utf-8", errors="replace", timeout=1200,
|
||||
creationflags=subprocess.CREATE_NO_WINDOW)
|
||||
(ACCEPTANCE / (key + ".stdout.txt")).write_text(process.stdout, encoding="utf-8")
|
||||
(ACCEPTANCE / (key + ".stderr.txt")).write_text(process.stderr, encoding="utf-8")
|
||||
evidence["runs"].append({"key": key, "arguments": arguments, "exit_code": process.returncode,
|
||||
"seconds": round(time.monotonic() - started, 3)})
|
||||
(ACCEPTANCE / "acceptance.json").write_text(json.dumps(evidence, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
print(key, process.returncode, flush=True)
|
||||
if process.returncode:
|
||||
print(process.stdout, process.stderr, flush=True)
|
||||
raise RuntimeError("Acceptance command failed: " + key)
|
||||
return process.stdout
|
||||
|
||||
diagnostic = json.loads(run("check", ["--check", "--json"]))
|
||||
assert Path(diagnostic["python"]) == destination / "runtime/python.exe"
|
||||
# Drive the actual no-argument wizard: input, output, confirmation, exit menu.
|
||||
output = ACCEPTANCE / "导出结果"
|
||||
run("wizard-export", [], f"{inputs}\n{output}\nY\n\n")
|
||||
batches = sorted(output.glob("批次_*/batch.json"))
|
||||
assert len(batches) == 1
|
||||
record = json.loads(batches[0].read_text(encoding="utf-8"))
|
||||
assert record["exit_code"] == 0 and len(record["projects"]) == 2
|
||||
for row in record["projects"]:
|
||||
result = row["result"]
|
||||
assert row["status"] == "success" and len(result["items"]) == 11
|
||||
assert all(item["status"] == "success" for item in result["items"])
|
||||
assert result["source_unchanged"] is True
|
||||
assert result["snapshot_discovery"]["status"] == "success"
|
||||
for item in result["items"]:
|
||||
assert item["provenance"]["snapshot"] == row["project"]["snapshot"]
|
||||
assert all(Path(path).is_file() for path in item["outputs"])
|
||||
run("retry", ["--retry", str(batches[0].parent), "--yes"])
|
||||
retry_path = sorted(output.glob("批次_*/batch.json"))[-1]
|
||||
retry = json.loads(retry_path.read_text(encoding="utf-8"))
|
||||
assert all(item["provenance"]["reused"] for row in retry["projects"] for item in row["result"]["items"])
|
||||
assert before == {str(path): digest(path) for path in inputs.rglob("*") if path.is_file()}
|
||||
manifest = json.loads((destination / "package-manifest.json").read_text(encoding="utf-8"))
|
||||
assert all(digest(destination / name) == info["sha256"] for name, info in manifest["files"].items())
|
||||
evidence.update(status="passed", first_batch=str(batches[0].parent), retry_batch=str(retry_path.parent),
|
||||
verified_package_files=len(manifest["files"]), source_files_unchanged=len(before),
|
||||
projects=2, successful_export_items=22, reused_items=22)
|
||||
(ACCEPTANCE / "acceptance.json").write_text(json.dumps(evidence, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
print(json.dumps(evidence, ensure_ascii=False, indent=2), flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1])
|
||||
@@ -0,0 +1,126 @@
|
||||
"""Build an offline, relocatable folder with official embedded Python and pywin32."""
|
||||
from pathlib import Path
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import urllib.request
|
||||
import zipfile
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
sys.path.insert(0, str(ROOT))
|
||||
from mentorexport.core import sha256, write_json
|
||||
|
||||
PYTHON_VERSION = "3.12.10"
|
||||
PYWIN32_VERSION = "312"
|
||||
|
||||
|
||||
def download(url, target, expected=None):
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
if not target.exists() or (expected and sha256(target) != expected):
|
||||
temporary = target.with_suffix(target.suffix + ".download")
|
||||
with urllib.request.urlopen(url, timeout=60) as source, temporary.open("wb") as stream:
|
||||
shutil.copyfileobj(source, stream)
|
||||
os.replace(temporary, target)
|
||||
if expected and sha256(target) != expected:
|
||||
raise ValueError("Downloaded package checksum mismatch: " + str(target))
|
||||
return target
|
||||
|
||||
|
||||
def extract(archive, destination):
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
with zipfile.ZipFile(archive) as package:
|
||||
if package.testzip() is not None:
|
||||
raise ValueError("Damaged archive: " + str(archive))
|
||||
for entry in package.infolist():
|
||||
if not (destination / entry.filename).resolve().is_relative_to(destination.resolve()):
|
||||
raise ValueError("Archive member escapes target")
|
||||
package.extractall(destination)
|
||||
|
||||
|
||||
def prepare_runtime():
|
||||
cache = ROOT / "build/downloads"
|
||||
python_zip = download(
|
||||
f"https://www.python.org/ftp/python/{PYTHON_VERSION}/python-{PYTHON_VERSION}-embed-amd64.zip",
|
||||
cache / f"python-{PYTHON_VERSION}-embed-amd64.zip")
|
||||
with urllib.request.urlopen(f"https://pypi.org/pypi/pywin32/{PYWIN32_VERSION}/json", timeout=30) as response:
|
||||
metadata = json.load(response)
|
||||
wheel_name = f"pywin32-{PYWIN32_VERSION}-cp312-cp312-win_amd64.whl"
|
||||
wheel_info = next(row for row in metadata["urls"] if row["filename"] == wheel_name)
|
||||
wheel = download(wheel_info["url"], cache / wheel_name, wheel_info["digests"]["sha256"])
|
||||
runtime = ROOT / "build/runtime"
|
||||
if not (runtime / "python.exe").is_file():
|
||||
extract(python_zip, runtime)
|
||||
else:
|
||||
with zipfile.ZipFile(python_zip) as package:
|
||||
expected_python = hashlib.sha256(package.read("python.exe")).hexdigest()
|
||||
if sha256(runtime / "python.exe") != expected_python:
|
||||
raise ValueError("Runtime build cache is damaged; preserve it and use a fresh build directory")
|
||||
if not (runtime / f"Lib/site-packages/pywin32-{PYWIN32_VERSION}.dist-info/METADATA").is_file():
|
||||
extract(wheel, runtime / "Lib/site-packages")
|
||||
pth = "python312.zip\n.\nLib/site-packages\nLib/site-packages/win32\nLib/site-packages/win32/lib\nimport site\n"
|
||||
pth_file = runtime / "python312._pth"
|
||||
if not pth_file.is_file() or pth_file.read_text(encoding="utf-8") != pth:
|
||||
pth_file.write_text(pth, encoding="utf-8")
|
||||
return runtime, {
|
||||
"python": {"version": PYTHON_VERSION, "architecture": "amd64", "archive_sha256": sha256(python_zip),
|
||||
"source": f"https://www.python.org/ftp/python/{PYTHON_VERSION}/"},
|
||||
"pywin32": {"version": PYWIN32_VERSION, "wheel_sha256": sha256(wheel), "filename": wheel.name},
|
||||
"user_installation_required": False,
|
||||
}
|
||||
|
||||
|
||||
def build(destination):
|
||||
destination = Path(destination).resolve()
|
||||
if destination.exists():
|
||||
raise ValueError("Choose a new output folder; existing package is preserved: " + str(destination))
|
||||
runtime, dependencies = prepare_runtime()
|
||||
if not (ROOT / "helpers/native_read.exe").is_file():
|
||||
raise ValueError("Build the x86 library helper before packaging")
|
||||
destination.mkdir(parents=True)
|
||||
shutil.copytree(runtime, destination / "runtime", ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
||||
shutil.copytree(ROOT / "mentorexport", destination / "mentorexport", ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
||||
shutil.copytree(ROOT / "helpers", destination / "helpers", ignore=shutil.ignore_patterns("*.obj", "*.pdb", "__pycache__"))
|
||||
for name in ("export.py", "导出.cmd", "README.md"):
|
||||
shutil.copy2(ROOT / name, destination / name)
|
||||
# CMD's UTF-8 parser can truncate LF-only lines after chcp; ship native CRLF.
|
||||
launcher = destination / "导出.cmd"
|
||||
launcher.write_bytes(launcher.read_bytes().replace(b"\r\n", b"\n").replace(b"\n", b"\r\n"))
|
||||
(destination / "licenses").mkdir()
|
||||
(destination / "licenses/THIRD_PARTY.md").write_text(
|
||||
"# Runtime notices\n\nPython " + PYTHON_VERSION +
|
||||
": see runtime/LICENSE.txt (PSF and included component notices).\n\n"
|
||||
"pywin32 " + PYWIN32_VERSION +
|
||||
": see runtime/Lib/site-packages/pywin32-" + PYWIN32_VERSION +
|
||||
".dist-info/ for upstream license and metadata.\n\n"
|
||||
"Mentor/Siemens vendor programs are not included. They are located on the target machine.\n",
|
||||
encoding="utf-8")
|
||||
write_json(destination / "runtime-dependencies.json", dependencies)
|
||||
files = {p.relative_to(destination).as_posix(): {"sha256": sha256(p), "bytes": p.stat().st_size}
|
||||
for p in sorted(destination.rglob("*")) if p.is_file()}
|
||||
write_json(destination / "package-manifest.json", {"schema": 1, "files": files})
|
||||
archive = destination.parent / (destination.name + ".zip")
|
||||
with zipfile.ZipFile(archive, "w", compression=zipfile.ZIP_DEFLATED, compresslevel=6) as package:
|
||||
for path in sorted(destination.rglob("*")):
|
||||
if path.is_file():
|
||||
package.write(path, arcname=(Path("MentorExport") / path.relative_to(destination)).as_posix())
|
||||
with zipfile.ZipFile(archive) as package:
|
||||
assert package.testzip() is None
|
||||
print(json.dumps({"folder": str(destination), "zip": str(archive), "bytes": archive.stat().st_size,
|
||||
"files": len(files)}, ensure_ascii=False, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--output", type=Path)
|
||||
parser.add_argument("--runtime-only", action="store_true")
|
||||
args = parser.parse_args()
|
||||
if args.runtime_only:
|
||||
print(json.dumps(prepare_runtime()[1], ensure_ascii=False, indent=2))
|
||||
elif args.output:
|
||||
build(args.output)
|
||||
else:
|
||||
parser.error("Specify --output or --runtime-only")
|
||||
@@ -0,0 +1,71 @@
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
import json
|
||||
|
||||
|
||||
def test_batch_does_not_stop_after_one_project_failure(tmp_path):
|
||||
from mentorexport.cli import run_batch
|
||||
def exporter(project, installation, folder, shared, **options):
|
||||
if project["name"] == "坏工程":
|
||||
raise ValueError("无法读取数据库")
|
||||
return {"status": "success", "items": [{"key": "ascii", "status": "success", "outputs": []}]}
|
||||
projects = [{"name": name, "path": str(tmp_path / (name + ".prj"))} for name in ("坏工程", "好工程")]
|
||||
batch, code = run_batch(projects, [], {}, tmp_path / "out", tmp_path, exporter=exporter)
|
||||
state = json.loads((batch / "batch.json").read_text(encoding="utf-8"))
|
||||
assert code == 1 and state["state"] == "complete"
|
||||
assert [p["status"] for p in state["projects"]] == ["failed", "success"]
|
||||
assert (batch / "导出汇总.html").is_file()
|
||||
|
||||
|
||||
def test_pre_cancelled_batch_does_not_start_exporter(tmp_path):
|
||||
from mentorexport.cli import run_batch
|
||||
stop = Event()
|
||||
stop.set()
|
||||
def exporter(*args, **kwargs):
|
||||
raise AssertionError("cancelled batch started native work")
|
||||
batch, code = run_batch([{"name": "工程", "path": str(tmp_path / "a.prj")}], [], {},
|
||||
tmp_path / "out", tmp_path, stop_event=stop, exporter=exporter)
|
||||
state = json.loads((batch / "batch.json").read_text(encoding="utf-8"))
|
||||
assert code == 130 and state["projects"][0]["status"] == "cancelled"
|
||||
assert (Path(state["projects"][0]["folder"]) / "export.json").is_file()
|
||||
|
||||
|
||||
def test_report_escapes_project_names_and_failure_details(tmp_path):
|
||||
from mentorexport.report import write_report
|
||||
record = {"state": "complete", "projects": [
|
||||
{"project": {"name": "<script>alert(1)</script>", "path": "a.prj"},
|
||||
"folder": str(tmp_path / "a"), "status": "failed",
|
||||
"result": {"status": "failed", "reason": "<img src=x>", "items": []}}], "problems": []}
|
||||
write_report(tmp_path, record)
|
||||
html = (tmp_path / "导出汇总.html").read_text(encoding="utf-8")
|
||||
assert "<script>" not in html and "<img src=x>" not in html
|
||||
assert "<script>" in html and "<img src=x>" in html
|
||||
|
||||
|
||||
def test_retry_loads_export_receipt_written_before_batch_crash(tmp_path):
|
||||
from mentorexport.cli import previous_results
|
||||
attempt = tmp_path / "工程"
|
||||
attempt.mkdir()
|
||||
(attempt / "export.json").write_text(json.dumps({"status": "partial", "items": [{"key": "csv", "status": "success"}]}))
|
||||
record = {"projects": [{"project": {"path": "a.prj"}, "folder": str(attempt), "status": "running"}]}
|
||||
values = previous_results(record)
|
||||
assert values["a.prj"]["items"][0]["key"] == "csv"
|
||||
|
||||
|
||||
def test_output_cannot_write_inside_source_database_or_library(tmp_path):
|
||||
from mentorexport.cli import validate_output
|
||||
import pytest
|
||||
project = {"path": str(tmp_path / "source/a.prj"), "database": str(tmp_path / "source/database"),
|
||||
"library": str(tmp_path / "library/mentor.lmc")}
|
||||
for output in (tmp_path / "source/database/results", tmp_path / "library/results"):
|
||||
with pytest.raises(ValueError):
|
||||
validate_output(output, tmp_path / "source", [project])
|
||||
validate_output(tmp_path / "results", tmp_path / "source", [project])
|
||||
|
||||
|
||||
def test_retry_cannot_claim_running_owner_has_exited():
|
||||
from mentorexport.cli import assert_retry_safe, process_identity
|
||||
import os
|
||||
import pytest
|
||||
with pytest.raises(ValueError, match="运行"):
|
||||
assert_retry_safe({"state": "running", "owner": process_identity(os.getpid())})
|
||||
@@ -0,0 +1,74 @@
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
|
||||
def make_project(root, name="Audio", block="Audio-Top", snapshot="DxD"):
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
(root / "database").mkdir(exist_ok=True)
|
||||
(root / "database/icdb.dat").write_bytes(b"saved database")
|
||||
path = root / (name + ".prj")
|
||||
path.write_text(
|
||||
f'SECTION Board1\nKEY RootBlock "{block}"\nENDSECTION\n'
|
||||
f'SECTION DesignInfo\nKEY FrontEndSnapshot "{snapshot}"\n'
|
||||
'KEY CentralLibrary "../库/mentor_lib.lmc"\nENDSECTION\n'
|
||||
'SECTION iCDB\nKEY iCDBDir "database"\nENDSECTION\n',
|
||||
encoding="utf-8")
|
||||
return path
|
||||
|
||||
|
||||
def test_project_uses_configured_snapshot_and_relative_database(tmp_path):
|
||||
from mentorexport.core import read_project
|
||||
path = make_project(tmp_path / "工程 空格", snapshot="SchematicSaved")
|
||||
result = read_project(path)
|
||||
assert result["root_block"] == "Audio-Top"
|
||||
assert result["snapshot"] == "SchematicSaved"
|
||||
assert Path(result["database"]) == path.parent / "database"
|
||||
assert Path(result["library"]) == (path.parent / "../库/mentor_lib.lmc").resolve()
|
||||
|
||||
|
||||
def test_multiple_roots_cannot_silently_export_first(tmp_path):
|
||||
from mentorexport.core import read_project
|
||||
path = make_project(tmp_path)
|
||||
with path.open("a", encoding="utf-8") as stream:
|
||||
stream.write('SECTION Board2\nKEY RootBlock "Other"\nENDSECTION\n')
|
||||
with pytest.raises(ValueError, match="多个"):
|
||||
read_project(path)
|
||||
|
||||
|
||||
def test_scan_keeps_same_names_and_excludes_own_output(tmp_path):
|
||||
from mentorexport.core import discover_projects, project_folder_name
|
||||
make_project(tmp_path / "甲")
|
||||
make_project(tmp_path / "乙")
|
||||
output = tmp_path / "exports"
|
||||
make_project(output / "copied")
|
||||
projects, problems = discover_projects(tmp_path, excluded=(output,))
|
||||
assert len(projects) == 2 and not problems
|
||||
assert len({project_folder_name(p, tmp_path) for p in projects}) == 2
|
||||
|
||||
|
||||
def test_missing_database_is_visible_problem(tmp_path):
|
||||
from mentorexport.core import discover_projects
|
||||
path = make_project(tmp_path)
|
||||
(path.parent / "database/icdb.dat").unlink()
|
||||
projects, problems = discover_projects(tmp_path)
|
||||
assert not projects and len(problems) == 1
|
||||
assert problems[0]["path"] == str(path.resolve())
|
||||
assert "数据库" in problems[0]["reason"]
|
||||
|
||||
|
||||
def test_atomic_json_preserves_unicode_and_can_be_updated(tmp_path):
|
||||
from mentorexport.core import write_json
|
||||
import json
|
||||
p = tmp_path / "nested/state.json"
|
||||
write_json(p, {"工程": "甲"})
|
||||
write_json(p, {"工程": "乙"})
|
||||
assert json.loads(p.read_text(encoding="utf-8")) == {"工程": "乙"}
|
||||
assert list(p.parent.iterdir()) == [p]
|
||||
|
||||
|
||||
def test_dxp_without_prj_is_reported_instead_of_silently_ignored(tmp_path):
|
||||
from mentorexport.core import discover_projects
|
||||
(tmp_path / "Legacy.dxp").write_text("legacy")
|
||||
projects, problems = discover_projects(tmp_path)
|
||||
assert not projects and len(problems) == 1
|
||||
assert ".prj" in problems[0]["reason"]
|
||||
@@ -0,0 +1,29 @@
|
||||
def test_inspection_does_not_mix_tools_from_other_installation(tmp_path):
|
||||
from mentorexport.environment import inspect_installation
|
||||
install = tmp_path / "SDD_HOME"
|
||||
(install / "iCDB/win32/bin").mkdir(parents=True)
|
||||
(install / "iCDB/win32/bin/icdb2ascii.exe").write_bytes(b"fixture")
|
||||
result = inspect_installation(install)
|
||||
assert result["tools"]["ascii"] == str(install / "iCDB/win32/bin/icdb2ascii.exe")
|
||||
assert "keyin" not in result["tools"]
|
||||
assert result["native_compatible"] is False
|
||||
|
||||
|
||||
def test_explicit_nonexistent_install_is_rejected(tmp_path):
|
||||
from mentorexport.environment import discover_installations
|
||||
import pytest
|
||||
with pytest.raises(ValueError):
|
||||
discover_installations(explicit=tmp_path / "missing")
|
||||
|
||||
|
||||
def test_preflight_rejects_zero_exit_without_expected_help(tmp_path):
|
||||
from mentorexport.environment import preflight_installation
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
from pathlib import Path
|
||||
Path(log_dir).mkdir(parents=True)
|
||||
(Path(log_dir) / "stdout.txt").write_text("")
|
||||
(Path(log_dir) / "stderr.txt").write_text("")
|
||||
return {"exit_code": 0, "cleanup": {"errors": []}}
|
||||
result = preflight_installation({"root": str(tmp_path), "tools": {"ascii": "ascii.exe"}},
|
||||
tmp_path / "logs", runner=runner)
|
||||
assert result["status"] == "failed"
|
||||
@@ -0,0 +1,401 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample(tmp_path):
|
||||
source = tmp_path / "source"
|
||||
(source / "database").mkdir(parents=True)
|
||||
(source / "database/icdb.dat").write_bytes(b"saved input")
|
||||
(source / "database/cdbsvr.addr").write_text("remote server")
|
||||
prj = source / "design.prj"
|
||||
prj.write_text('KEY iCDBDir "database"\nKEY DedicatedServerName "remote"\n')
|
||||
exe = tmp_path / "ascii.exe"
|
||||
exe.write_bytes(b"tool version one")
|
||||
project = dict(path=str(prj), database=str(source / "database"), root_block="Top", snapshot="Saved", name="design", library=None)
|
||||
install = dict(root=str(tmp_path), version="test", tools={"ascii": str(exe)}, com={})
|
||||
return project, install, tmp_path
|
||||
|
||||
|
||||
def backend():
|
||||
from mentorexport import exports
|
||||
return exports
|
||||
|
||||
|
||||
def ascii_runner(calls):
|
||||
def run(command, cwd, environment, log_dir, *, stop_event, timeout):
|
||||
calls.append(command)
|
||||
db = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-icdb=")))
|
||||
assert (db / "icdb.dat").read_bytes() == b"saved input"
|
||||
assert not (db / "cdbsvr.addr").exists()
|
||||
assert 'DedicatedServerName ""' in (db.parent / "input.prj").read_text()
|
||||
(db / "icdb.dat").write_bytes(b"tool mutation")
|
||||
output = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file=")))
|
||||
output.write_text("iCDB Ascii Report\n" + ("Block Top\n" if "-h" in command else "Config default\n") + " Symbol instance\n Net GND\n")
|
||||
return dict(exit_code=0, cleanup={"errors": []}, seconds=0.01)
|
||||
return run
|
||||
|
||||
|
||||
def test_exit_zero_without_output_is_failed_and_durable(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
monkeypatch.setattr(exports, "_run", lambda *a, **k: dict(exit_code=0, cleanup={"errors": []}))
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
flat = next(i for i in result["items"] if i["key"] == "ascii_flat")
|
||||
assert flat["status"] == "failed"
|
||||
assert "output" in flat["reason"].lower()
|
||||
assert json.loads((root / "attempt/export.json").read_text(encoding="utf-8"))["items"] == result["items"]
|
||||
|
||||
|
||||
def test_fresh_clones_provenance_and_unchanged_source(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
calls = []
|
||||
monkeypatch.setattr(exports, "_run", ascii_runner(calls))
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
good = [i for i in result["items"] if i["status"] == "success"]
|
||||
assert [i["key"] for i in good] == ["ascii_flat", "ascii_hier"]
|
||||
assert len(calls) == 2
|
||||
assert result["source_unchanged"] is True
|
||||
assert Path(project["database"], "icdb.dat").read_bytes() == b"saved input"
|
||||
assert good[0]["provenance"]["tool"]["sha256"] == exports.sha256(install["tools"]["ascii"])
|
||||
assert good[0]["provenance"]["output_files"][0]["sha256"]
|
||||
assert good[0]["provenance"]["snapshot"] == "Saved"
|
||||
|
||||
|
||||
def test_cancelled_before_start_does_not_launch(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
event = Event()
|
||||
event.set()
|
||||
def forbidden(*a, **k):
|
||||
raise AssertionError("should not launch")
|
||||
monkeypatch.setattr(exports, "_run", forbidden)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", stop_event=event, progress=lambda _: None)
|
||||
assert result["status"] == "cancelled"
|
||||
assert all(i["status"] == "cancelled" for i in result["items"])
|
||||
|
||||
|
||||
def test_retry_reuses_only_verified_matching_success(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
calls = []
|
||||
monkeypatch.setattr(exports, "_run", ascii_runner(calls))
|
||||
first = exports.export_project(project, install, root / "one", root / "shared", progress=lambda _: None)
|
||||
second = exports.export_project(project, install, root / "two", root / "shared", previous=first, progress=lambda _: None)
|
||||
assert len(calls) == 2
|
||||
assert next(i for i in second["items"] if i["key"] == "ascii_flat")["provenance"]["reused"] is True
|
||||
Path(project["database"], "icdb.dat").write_bytes(b"new saved input")
|
||||
monkeypatch.setattr(exports, "_run", lambda *a, **k: dict(exit_code=0, cleanup={"errors": []}))
|
||||
third = exports.export_project(project, install, root / "three", root / "shared", previous=second, progress=lambda _: None)
|
||||
assert third["source_fingerprint"] != second["source_fingerprint"]
|
||||
assert next(i for i in third["items"] if i["key"] == "ascii_flat")["status"] == "failed"
|
||||
|
||||
|
||||
def test_modified_output_and_changed_tool_are_not_reused(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
calls = []
|
||||
monkeypatch.setattr(exports, "_run", ascii_runner(calls))
|
||||
first = exports.export_project(project, install, root / "one", root / "shared", progress=lambda _: None)
|
||||
flat = next(i for i in first["items"] if i["key"] == "ascii_flat")
|
||||
Path(flat["outputs"][0]).write_text("tampered")
|
||||
exports.export_project(project, install, root / "two", root / "new-shared", previous=first, progress=lambda _: None)
|
||||
assert len(calls) == 3
|
||||
Path(install["tools"]["ascii"]).write_bytes(b"new tool")
|
||||
exports.export_project(project, install, root / "three", root / "shared", previous=first, progress=lambda _: None)
|
||||
assert len(calls) == 5
|
||||
|
||||
|
||||
def test_used_library_only_is_shared_and_source_fingerprinted(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
library = root / "library"
|
||||
(library / "PartsDBLibs").mkdir(parents=True)
|
||||
(library / "CellDBLibs").mkdir()
|
||||
(library / "PartsDBLibs/parts.pdb").write_bytes(b"parts")
|
||||
(library / "CellDBLibs/cells.cel").write_bytes(b"cells")
|
||||
(library / "lib.lmc").write_bytes(b"config")
|
||||
project["library"] = str(library / "lib.lmc")
|
||||
dll_dir = root / "common/win32/lib"
|
||||
dll_dir.mkdir(parents=True)
|
||||
for name in exports.VALIDATED_DLLS:
|
||||
(dll_dir / name).write_bytes(name.encode())
|
||||
monkeypatch.setattr(exports, "VALIDATED_DLLS", {name: exports.sha256(dll_dir / name) for name in exports.VALIDATED_DLLS})
|
||||
native_calls = []
|
||||
active_parts = ["USED"]
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
if "-f" in command or "-h" in command:
|
||||
out = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file=")))
|
||||
out.write_text("iCDB Ascii Report\n" + ("Block Top\n" if "-h" in command else "Config default\n") +
|
||||
"".join(" Symbol id\n\tProp Part Number " + part + "\n\tProp Cell Name FOOTPRINT\n" for part in active_parts))
|
||||
else:
|
||||
native_calls.append(command)
|
||||
mode = command[1]
|
||||
rows = [{"name": "USED"}, {"name": "NEW"}, {"name": "UNUSED"}] if mode == "parts" else [{"name": "FOOTPRINT"}, {"name": "UNUSED"}]
|
||||
data = {"mode": mode, "count": len(rows), "records": rows}
|
||||
Path(log_dir).mkdir(parents=True, exist_ok=True)
|
||||
(Path(log_dir) / "stdout.txt").write_text(json.dumps(data), encoding="utf-8")
|
||||
return dict(exit_code=0, cleanup={"errors": []})
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
first = exports.export_project(project, install, root / "one", root / "shared", progress=lambda _: None)
|
||||
item = next(i for i in first["items"] if i["key"] == "library")
|
||||
assert item["status"] == "success", item
|
||||
assert len(native_calls) == 2
|
||||
assert all("UNUSED" not in p.read_text(encoding="utf-8") for p in (root / "shared").rglob("*.json"))
|
||||
exports.export_project(project, install, root / "two", root / "new-shared", previous=first, progress=lambda _: None)
|
||||
assert len(native_calls) == 2
|
||||
(library / "PartsDBLibs/parts.pdb").write_bytes(b"new parts")
|
||||
third = exports.export_project(project, install, root / "three", root / "shared", previous=first, progress=lambda _: None)
|
||||
assert first["source_fingerprint"] != third["source_fingerprint"]
|
||||
assert len(native_calls) == 4
|
||||
active_parts.append("NEW")
|
||||
fourth = exports.export_project(project, install, root / "four", root / "shared", progress=lambda _: None)
|
||||
assert next(i for i in fourth["items"] if i["key"] == "library")["status"] == "success"
|
||||
assert len(native_calls) == 5, "only partition containing previously unread NEW should rerun"
|
||||
|
||||
|
||||
def test_native_abi_mismatch_never_launches_helper(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
library = root / "library"
|
||||
(library / "PartsDBLibs").mkdir(parents=True)
|
||||
(library / "PartsDBLibs/part.pdb").write_bytes(b"parts")
|
||||
project["library"] = str(library)
|
||||
monkeypatch.setattr(exports, "_run", ascii_runner([]))
|
||||
result = exports.export_project(project, install, root / "one", root / "shared", progress=lambda _: None)
|
||||
item = next(i for i in result["items"] if i["key"] == "library")
|
||||
assert item["status"] == "unavailable"
|
||||
assert "ABI" in item["reason"]
|
||||
|
||||
|
||||
def test_snapshot_listing_parses_vendor_arrows_without_selecting_root(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
install["tools"]["constraints_xml"] = install["tools"]["ascii"]
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
Path(log_dir).mkdir(parents=True)
|
||||
(Path(log_dir) / "stdout.txt").write_text("Snapshots and top blocks:\n -> Saved\n + Top\n -> Board1\n + AnotherRoot\n")
|
||||
return {"exit_code": 0}
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
assert exports.list_snapshots(project, install, root / "logs") == ["Saved", "Board1"]
|
||||
assert project["snapshot"] == "Saved"
|
||||
|
||||
|
||||
def test_batch_records_snapshot_names_from_captured_input(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
install["tools"]["constraints_xml"] = install["tools"]["ascii"]
|
||||
ascii_run = ascii_runner([])
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
if "-list" not in command:
|
||||
return ascii_run(command, cwd, environment, log_dir, **kwargs)
|
||||
assert (Path(cwd) / "database/icdb.dat").read_bytes() == b"saved input"
|
||||
Path(log_dir).mkdir(parents=True)
|
||||
(Path(log_dir) / "stdout.txt").write_text(" -> Saved\n -> Board1\n")
|
||||
return dict(exit_code=0, cleanup={"errors": []})
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
assert result["snapshot_discovery"]["names"] == ["Saved", "Board1"]
|
||||
assert result["snapshot_discovery"]["status"] == "success"
|
||||
assert result["project"]["snapshot"] == "Saved"
|
||||
|
||||
|
||||
def test_uncertain_snapshot_cleanup_stops_batch_and_preserves_copy(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
install["tools"]["constraints_xml"] = install["tools"]["ascii"]
|
||||
calls = []
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
calls.append(command)
|
||||
return dict(exit_code=0, cleanup_uncertain=True, cleanup={"errors": ["exit unconfirmed"]})
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
assert len(calls) == 1 and "-list" in calls[0]
|
||||
assert result["cleanup_uncertain"] is True
|
||||
assert result["status"] == "failed"
|
||||
assert all(i["status"] == "cancelled" for i in result["items"])
|
||||
preserved = Path(result["snapshot_discovery"]["workspace_preserved"])
|
||||
assert (preserved / "database/icdb.dat").is_file()
|
||||
exports.shutil.rmtree(preserved)
|
||||
exports.shutil.rmtree(Path(result["workspace_preserved"]))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key,name,text", [
|
||||
("ascii_hier", "connections.txt", "iCDB Ascii Report Generation Date: x\nMODE: HIER BLOCKS\nBlock Top\n\tNet Ground\n"),
|
||||
("edif_netlist", "design.edf", "(edif Saved (edifVersion 2 0 0) (viewType NETLIST) (net\n (rename GND Ground) (joined)))"),
|
||||
])
|
||||
def test_validated_vendor_formats_accept_hierarchy_block_and_net_whitespace(tmp_path, key, name, text):
|
||||
exports = backend()
|
||||
(tmp_path / name).write_text(text)
|
||||
assert exports._validate(key, tmp_path) == [tmp_path / name]
|
||||
|
||||
|
||||
def test_csv_manifest_cannot_make_malformed_tables_successful(tmp_path):
|
||||
exports = backend()
|
||||
folder = tmp_path / "work"
|
||||
folder.mkdir()
|
||||
tables = "sym spn spr ppr prm ppn prt pth sht inf npr thn".split()
|
||||
(folder / "design.csv").write_text("\n".join("file:design.csv." + t for t in tables))
|
||||
for table in tables:
|
||||
(folder / ("design.csv." + table)).write_text("not a tab separated table")
|
||||
with pytest.raises(ValueError, match="table"):
|
||||
exports._validate("csv", tmp_path)
|
||||
|
||||
|
||||
def test_empty_oat_is_not_a_successful_pair(tmp_path):
|
||||
exports = backend()
|
||||
for folder in ("wir", "oat"):
|
||||
(tmp_path / folder).mkdir()
|
||||
(tmp_path / "wir/top.1").write_text("| Wirelist created using version 6.0\nV 6.0\nDW Top\n")
|
||||
(tmp_path / "oat/top.oat").write_bytes(b"")
|
||||
with pytest.raises(ValueError, match="WIR/OAT"):
|
||||
exports._validate("wir_oat", tmp_path)
|
||||
|
||||
|
||||
def test_failed_format_output_is_retained_as_failure_evidence(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
def malformed(command, cwd, environment, log_dir, **kwargs):
|
||||
output = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file=")))
|
||||
output.write_text("partial or malformed vendor output")
|
||||
return dict(exit_code=0, cleanup={"errors": []})
|
||||
monkeypatch.setattr(exports, "_run", malformed)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
item = result["items"][0]
|
||||
assert item["status"] == "failed"
|
||||
assert item["provenance"]["failed_output_files"]
|
||||
assert Path(item["provenance"]["failed_output_files"][0]["path"]).read_text() == "partial or malformed vendor output"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("field", [{"hr": -2147467259, "value": None}, {"hr": 0, "value": None}])
|
||||
def test_native_getter_failure_or_null_is_rejected(field):
|
||||
exports = backend()
|
||||
native = {"mode": "parts", "count": 1, "records": [{"name": "USED", "part_name": field}]}
|
||||
with pytest.raises(ValueError, match="getter"):
|
||||
exports._validate_native(native, "parts")
|
||||
|
||||
|
||||
def test_native_success_hr_with_null_coordinate_pointer_is_rejected():
|
||||
exports = backend()
|
||||
native = {"mode": "cell", "count": 1, "records": [{"name": "FOOTPRINT", "pins": [
|
||||
{"location_hr": 0, "xy_raw": None}]}]}
|
||||
with pytest.raises(ValueError, match="coordinate"):
|
||||
exports._validate_native(native, "cell")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("outcome", ["crash", "invalid_json", "cancel"])
|
||||
def test_native_partition_diagnostics_and_accumulated_data_survive_failure(sample, monkeypatch, outcome):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
library = root / "library/PartsDBLibs"
|
||||
library.mkdir(parents=True)
|
||||
(library / "a.pdb").write_bytes(b"a")
|
||||
(library / "b.pdb").write_bytes(b"b")
|
||||
project["library"] = str(library.parent)
|
||||
dll_dir = root / "common/win32/lib"
|
||||
dll_dir.mkdir(parents=True)
|
||||
for name in exports.VALIDATED_DLLS:
|
||||
(dll_dir / name).write_bytes(name.encode())
|
||||
monkeypatch.setattr(exports, "VALIDATED_DLLS", {name: exports.sha256(dll_dir / name) for name in exports.VALIDATED_DLLS})
|
||||
native_count = 0
|
||||
def runner(command, cwd, environment, log_dir, **kwargs):
|
||||
nonlocal native_count
|
||||
if "-f" in command or "-h" in command:
|
||||
output = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file=")))
|
||||
output.write_text("iCDB Ascii Report\n" + ("Block Top\n" if "-h" in command else "Config default\n") + "\tProp Part Number USED\n")
|
||||
return dict(exit_code=0, cleanup={"errors": []})
|
||||
native_count += 1
|
||||
log_dir = Path(log_dir)
|
||||
log_dir.mkdir(parents=True, exist_ok=True)
|
||||
run = dict(exit_code=0, cleanup={"errors": []})
|
||||
payload = json.dumps({"mode": "parts", "count": 1, "records": [{"name": "USED"}]})
|
||||
if native_count == 2:
|
||||
payload = "incomplete raw output"
|
||||
run.update(exit_code=9 if outcome == "crash" else 0, cancelled=outcome == "cancel")
|
||||
(log_dir / "stdout.txt").write_text(payload, encoding="utf-8")
|
||||
(log_dir / "stderr.txt").write_text("native diagnostic evidence", encoding="utf-8")
|
||||
(log_dir / "run.json").write_text(json.dumps(run), encoding="utf-8")
|
||||
return run
|
||||
monkeypatch.setattr(exports, "_run", runner)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
item = next(i for i in result["items"] if i["key"] == "library")
|
||||
assert item["status"] == ("cancelled" if outcome == "cancel" else "failed")
|
||||
logs = root / "attempt/90_运行日志/library"
|
||||
assert len(list(logs.rglob("stderr.txt"))) == 2
|
||||
assert len(list(logs.rglob("run.json"))) == 2
|
||||
stdout = list(logs.rglob("stdout.txt"))
|
||||
assert len(stdout) == 1 and stdout[0].read_text() == "incomplete raw output"
|
||||
data_files = list((root / "shared").rglob("used-*.json"))
|
||||
assert len(data_files) == 1
|
||||
data = json.loads(data_files[0].read_text(encoding="utf-8"))
|
||||
assert data["complete"] is False
|
||||
assert len(data["runs"]) == 2
|
||||
assert data["records"]["parts"][0]["name"] == "USED"
|
||||
assert str(data_files[0]) in item["outputs"]
|
||||
|
||||
|
||||
def test_uncertain_cleanup_preserves_workspace_and_stops_remaining_tools(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
calls = []
|
||||
def uncertain(command, cwd, environment, log_dir, **kwargs):
|
||||
calls.append(Path(cwd))
|
||||
return dict(exit_code=0, cleanup_uncertain=True, cleanup={"errors": ["owned process exit unconfirmed"]})
|
||||
monkeypatch.setattr(exports, "_run", uncertain)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
assert len(calls) == 1
|
||||
assert result["cleanup_uncertain"] is True
|
||||
preserved = Path(result["workspace_preserved"])
|
||||
assert preserved.is_dir()
|
||||
assert calls[0].is_dir()
|
||||
assert result["status"] == "failed"
|
||||
assert all(i["status"] == "cancelled" for i in result["items"][1:])
|
||||
# No real process is running in this synthetic boundary failure.
|
||||
exports.shutil.rmtree(preserved)
|
||||
|
||||
|
||||
def test_capture_rejects_a_source_that_changes_while_copying(sample, monkeypatch):
|
||||
exports = backend()
|
||||
project, install, root = sample
|
||||
real_copy = exports.shutil.copyfile
|
||||
def changing(source, target, *args, **kwargs):
|
||||
value = real_copy(source, target, *args, **kwargs)
|
||||
if Path(source).name == "icdb.dat":
|
||||
Path(source).write_bytes(b"new saved state")
|
||||
return value
|
||||
monkeypatch.setattr(exports.shutil, "copyfile", changing)
|
||||
result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None)
|
||||
assert result["status"] == "failed"
|
||||
assert "changed" in result["reason"].lower()
|
||||
assert not result["items"]
|
||||
|
||||
|
||||
@pytest.mark.skipif(__import__("os").name != "nt", reason="Windows private desktop")
|
||||
@pytest.mark.parametrize("cancel", [False, True])
|
||||
def test_owned_process_timeout_and_active_cancel_leave_no_job_processes(tmp_path, cancel):
|
||||
pytest.importorskip("win32job")
|
||||
import os
|
||||
import sys
|
||||
from threading import Timer
|
||||
exports = backend()
|
||||
event = Event()
|
||||
timer = Timer(0.3, event.set) if cancel else None
|
||||
if timer:
|
||||
timer.start()
|
||||
try:
|
||||
result = exports._run([sys.executable, "-c", "import time;time.sleep(30)"], tmp_path,
|
||||
dict(os.environ), tmp_path / "logs", stop_event=event,
|
||||
timeout=5 if cancel else 0.3)
|
||||
finally:
|
||||
if timer:
|
||||
timer.cancel()
|
||||
assert result["cancelled"] is cancel
|
||||
assert result["timed_out"] is (not cancel)
|
||||
assert "error" not in result
|
||||
assert not result["cleanup"]["errors"]
|
||||
assert result["cleanup"]["desktop_handle_closed"]
|
||||
assert all(p["job_active_processes"] == 0 for p in result["cleanup"]["processes"])
|
||||
@@ -0,0 +1,16 @@
|
||||
@echo off
|
||||
setlocal
|
||||
chcp 65001 >nul
|
||||
set "PYTHONHOME="
|
||||
set "PYTHONPATH="
|
||||
set "PYTHONNOUSERSITE=1"
|
||||
set "PYTHONDONTWRITEBYTECODE=1"
|
||||
if not exist "%~dp0runtime\python.exe" (
|
||||
echo 预置 Python 不完整,请重新解压整个 MentorExport 文件夹。
|
||||
if "%~1"=="" pause
|
||||
exit /b 2
|
||||
)
|
||||
"%~dp0runtime\python.exe" -I -B -X utf8 "%~dp0export.py" %*
|
||||
set "export_result=%errorlevel%"
|
||||
if %export_result% equ 2 if "%~1"=="" pause
|
||||
exit /b %export_result%
|
||||
Reference in new issue
Block a user