From 0415ecc73cc2f4d1483aa62cbb306ffd6c59cc51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E7=99=BD=E5=8F=94?= <11794527+absfan@user.noreply.gitee.com> Date: Fri, 16 Oct 2026 23:29:41 +0800 Subject: [PATCH] feat: prefer isolated native CLI for component inventory --- .../2026-10-16-native-cli-integration.md | 79 +++++++ .../2026-10-16-native-parts-lister-cli.md | 2 + docs/user/v1.1.0-project-tasks.md | 2 + .../trial_task_center/discovery_cache.py | 3 +- .../trial_task_center/native_inventory.py | 194 ++++++++++++++++++ .../native_inventory_protocol.py | 96 +++++++++ .../trial_task_center/quick_run.py | 7 + .../trial_task_center/quick_run_protocol.py | 1 + tests/v1_1/test_native_ascii_inventory.py | 87 ++++++++ tests/v1_1/test_native_inventory_reader.py | 159 ++++++++++++++ 10 files changed, 629 insertions(+), 1 deletion(-) create mode 100644 docs/research/2026-10-16-native-cli-integration.md create mode 100644 src/dx_productivity/trial_task_center/native_inventory.py create mode 100644 src/dx_productivity/trial_task_center/native_inventory_protocol.py create mode 100644 tests/v1_1/test_native_ascii_inventory.py create mode 100644 tests/v1_1/test_native_inventory_reader.py diff --git a/docs/research/2026-10-16-native-cli-integration.md b/docs/research/2026-10-16-native-cli-integration.md new file mode 100644 index 0000000..6c7fe91 --- /dev/null +++ b/docs/research/2026-10-16-native-cli-integration.md @@ -0,0 +1,79 @@ +# 原生器件采集接入与替换接口续查 + +## 接入结果(开发版,未打包) + +位号采集和 BOM 库存校验优先使用本机官方 `icdb2ascii.exe`,完整身份沿用现有工程、原理图、图页、UID、位号、旧料号和原 Value 模型。不构造假 UID,不修改模板或替换策略。 + +本机安装路径:`D:/Mentor/7.9.5EE/SDD_HOME/iCDB/win32/bin/icdb2ascii.exe`。程序从已安装 Designer 路径定位同套 iCDB 工具,不随应用分发厂商二进制。 + +```text +icdb2ascii.exe -project=<临时 input.prj> -icdb=<临时 database> + -snap=DxD -h -t -offline -file=<临时 output/scope.txt> +``` + +不传 `-blks`,读取报告内的全部原理图块,包括不连通的块。验证报告是未筛选的 FREEFORM / HIER BLOCKS,且包含文本属性。逐器件读取真实 UID、图页及属性;引脚属性不能覆盖器件属性。筛选请求的旧料号后交给原计划校验、BOM 错误报告和缓存。 + +器件报告不枚举空图页,因此返回 `complete_sheet_manifest=False`。它可以缓存组件库存,但不能作为完整图页查询的缓存命中;完整图页枚举仍使用原 CAD 接口。 + +## 工程隔离、取消及回退 + +官方导出工具可能更新运行数据库。本实现先将 `.prj` 与 `database/icdb.dat` 复制到短路径临时目录,改写临时工程的数据库目录并清除专用服务器名称,不复制运行服务器地址文件。CLI 只访问副本,源工程与源数据库在调用前后核对 SHA256。 + +每次调用使用自有私有桌面及 Job,工具等待上限 30 秒,等待期间检查取消;确认进程和桌面释放后才解析结果或回退。工具缺失、不支持的工程/报告、超时或非零退出码在安全收尾后使用原 COM 采集。取消、源工程变化和清理不确定均阻断,不继续回退。 + +诊断保存在 `data/quick-run-history/native-inventory-*/native-inventory.json` 和对应 `output`,含状态、退出码、分段耗时、清理记录及原生输出。正常收尾删除临时数据库;清理不确定时保留副本和诊断。 + +运行前保存并关闭本次目标工程,避免采集到未保存编辑以外的磁盘状态。此后端不执行替换;替换前的目标核验、备份、Library Wins、属性恢复、保存及重开回读继续使用已有执行链。 + +## 两工程 BOM 实测 + +在已有测试工程的另一份副本中完成:创建两任务 → 导出默认同批模板 → 填入有效替换和一个不存在的位号 → 导入报错 → 修正后重导入 → 保存计划。 + +| 项目 | 结果 | +| --- | --- | +| 首次导入 | 5.281 秒;通过 2 行、失败 1 行;未保存预览 | +| 修正后重导入 | 1.093 秒;通过 2 行、失败 0 行;保存成功 | +| 工程一器件采集 | 1.360 秒,4 个目标位号;CLI 本身 0.891 秒 | +| 工程二器件采集 | 0.797 秒,2 个目标位号;CLI 本身 0.734 秒 | +| 工程路径长度 | 141、296 字符 | +| 导入时启动 CAD | 无;两次均使用单个 `icdb2ascii`,未回退 COM | +| 来源工程 | `.prj` 与 `icdb.dat` 哈希未变 | + +本地证据:`build/native-cli-bom-offline-final-20261016/report.json`。这只是两个工程的数据,不能据此承诺 400 个工程或 400 条规则的吞吐量。重导入收益包含库存缓存,并非第二次完整 CLI 采集。 + +原生协议、运行监管、取消、超时、源身份变化、清理失败、图页缓存边界,以及原有 BOM/桌面隔离的针对性回归:128 项通过(26.52 秒)。只读复核另跑原生与缓存测试:56 项通过;未发现未解决的 P1/P2。 + +## 接入后实际替换回归 + +只执行上述长路径 Audio 副本:将 `RV05` 从 `4100-CA0000-T7` 替换到 `4100-CA0000-T0`,`RV11` 保持旧料号。任务完成 1 步、保存并重开核验 1 步、失败 0 步。独立 CLI 和另起 COM 会话读取的 UID、位号、料号及 Value 完全相同,来源工程哈希未变。 + +| 执行阶段 | 耗时 | +| --- | --- | +| 队列准备(包含原 COM 图页枚举与稳定性重读) | 186.393 秒 | +| 工程备份 | 0.677 秒 | +| 进度持久化 | 0.141 秒 | +| 执行接口总耗时(包含上述阶段及 CAD 替换、重开核验、释放) | 372.219 秒 | + +CLI 改进的是导入与组件采集;当前执行准备仍有多次 CAD 启动,执行耗时没有同等降低。后续可单独研究从精确位号计划建立执行页范围,不能在此直接取消原有工程稳定性与目标核验。 + +本轮关闭了 PartLabel 复制:旧 `RV05.Value=0R`,新库件的 Value 为空,最终仍为空;`RV11.Value=0R` 不变。原执行逻辑中的 `preserve` 指保留原生替换后的库件 Value,而不是保留旧器件 Value。初版探针错误地要求选中件 Value 不变,断言失败;补充独立 COM 读取及代码核对后修正测试预期,未修改生产策略,也未以 PartLabel 代填原生清单。 + +五个 CAD 私有桌面均有正常退出及释放记录,并通过另一个进程枚举桌面确认已消失。初版前台观察器因测试输出目录嵌套,未采集到自有进程句柄,因此不能宣称完成了整轮自有 PID 的独立前台跟踪;该限制保留在验证报告中。 + +本地证据:`build/native-cli-bom-offline-final-20261016/replacement-acceptance/verified-report.json`、`com-readback.json`,以及任务的 `events.jsonl`、`quick-run/private-desktop.json`。初版失败断言记录仍保留。 + +## 直接替换 CLI 续查 + +尚未找到本机 7.9.5 可验证的独立替换 CLI,不能把下面线索当作已可用的后端。 + +- `icdb2ascii`、`icdb2csv`、`icdb2bom`、`icdb2wir` 的本机帮助确认的是导出用途;`icdbPartsLister` 也只验证了清单导出。 +- `icscript.bat`、`mgcscript.bat` 是脚本启动/环境转发入口,脚本自身没有定义料号替换、位号范围或 Library Wins 参数合同。 +- 在自有桌面对 `viewdraw.exe --help` 做有界探针,未取得可用文本帮助;退出及桌面清理已确认。不能据此证明程序不存在其他命令行能力。 +- 再次离线读取本机 `viewdraw.tlb`:版本 1.0,SHA256 `09323c8940f1acf3f469d887dcfdf7bcd114164a29b01ebeb9a7f978fa1a0df7`,没有直接 `ReplacePart`。工作区版本 119.0 wrapper 的同名方法不能证明实装版本支持。 +- `viewdraw.exe` 中存在 `ccomp`、`Change Component`、`Preserve REFDES Value` 字符串;已有 TypeLib 包含 `ChangeComponentPreserveRefdes`。这些只证明内部线索,没有验证参数、目标范围、中央库、引脚网络及 Library Wins 语义。 +- 本机 CHM 中 `ccomp` 等页仍是跳转 InfoHub 的占位页。对安装样例及 `standard/ee_utils` 的脚本检索未找到可靠替换调用样例。 +- 公开搜索没有取得可复核的命令合同;Siemens 社区链接只返回动态通用页面,未获取正文,不能作为替换语义证据。 + +后续最有价值的方向仍是匹配本机版本的命令/API 手册,或在独立可丢弃工程验证 `ccomp` / `ChangeComponentPreserveRefdes` 的实际语义。在这些证据齐备前,保留当前已验证的原生对话框替换链。 + +相关证据:`build/native-replacement-cli-20261016/`、`build/native-cli-integration-20261016/current-capabilities.json`;厂商帮助内容和工程数据库不加入 Git。 diff --git a/docs/research/2026-10-16-native-parts-lister-cli.md b/docs/research/2026-10-16-native-parts-lister-cli.md index d59150b..2159c78 100644 --- a/docs/research/2026-10-16-native-parts-lister-cli.md +++ b/docs/research/2026-10-16-native-parts-lister-cli.md @@ -1,5 +1,7 @@ # 原生器件清单 CLI:首次运行验证 +后续已找到能同时导出 UID 与器件属性的 `icdb2ascii.exe`,并接入开发版正式采集入口。见 [接入与验证记录](2026-10-16-native-cli-integration.md)。下文保留 PartsLister 首次探查结论,不能代表后续后端仍受同样限制。 + 本次针对大量工程首次 BOM 校验的耗时,继续验证此前仅有静态线索的 `icdbPartsLister.exe`。没有修改正式导入/替换流程,没有打包;所有原生调用在自建私有桌面和 Job 中运行,只使用既有测试工程的另一份副本。 ## 已验证的命令入口 diff --git a/docs/user/v1.1.0-project-tasks.md b/docs/user/v1.1.0-project-tasks.md index a3abb7c..0ada835 100644 --- a/docs/user/v1.1.0-project-tasks.md +++ b/docs/user/v1.1.0-project-tasks.md @@ -31,6 +31,8 @@ V1.1.0 沿用原有任务视图和料号规则编辑,增加按工程拆分的 导入先核对所有工程归属,再按工程匹配位号;缺少清单时,按该工程 BOM 中的旧料号只读采集。不会执行替换。切换任务可查看、编辑各自预览,点击“保存本批位号计划”才一次写入所有受影响任务。保存失败全部回滚,预览期间不改已保存计划。工程、图页、UID、位号、旧料号及原 Value 用于身份校验,未知工程、重复器件或跨工程目标都会拒绝;不接受公式。布尔选项可使用 TRUE/FALSE 或 是/否。 +开发版新增原生采集(尚未打包发布):本机有兼容的 `icdb2ascii.exe` 时,BOM 校验和位号采集优先在临时数据库副本中一次导出真实 UID、位号及物料属性,不必逐条启动 CAD 查找。工具不可用或报告不受支持且已安全清理时,自动使用原 CAD 采集。源工程变化或后台清理不确定时停止并保留诊断;完整图页查询和实际替换仍沿用原执行链。运行前请保存并关闭目标工程。详情见 [原生采集接入记录](../research/2026-10-16-native-cli-integration.md)。 + BOM 首次采集或新增的位号默认“不替换”;已有计划中未列出的行保留原勾选和编辑状态,不会因后续 BOM 省略而自动停用。如果本次仅执行 BOM 列出的目标,请在预览中检查并取消其他启用行,或在 BOM 中显式填写对应的 `enabled=FALSE` 行。每个填写的位号须在当前工程清单中唯一匹配;同名位号跨图页时应填写图页或 UID。导入失败不会改变已保存计划。 “同步 NC”默认勾选,可以按行关闭;仅按原规则识别独立 `/NC` 标记。Value 复制选项和每行自定义 Value 延续原工具的处理方式。替换计划不允许旧、新料号相同,也不允许同批链式或循环替换。 diff --git a/src/dx_productivity/trial_task_center/discovery_cache.py b/src/dx_productivity/trial_task_center/discovery_cache.py index f02f462..bc8eafd 100644 --- a/src/dx_productivity/trial_task_center/discovery_cache.py +++ b/src/dx_productivity/trial_task_center/discovery_cache.py @@ -144,7 +144,8 @@ class DiscoveryCache: if entry is not None: stamp, covered, has_components, discovery = entry if (before is not None and before == stamp and requested <= covered - and (not collect_components or has_components)): + and (not collect_components or has_components) + and (collect_components or getattr(discovery, 'complete_sheet_manifest', True))): self._entries[key] = entry check_cancelled(stop_event) return deepcopy(discovery) diff --git a/src/dx_productivity/trial_task_center/native_inventory.py b/src/dx_productivity/trial_task_center/native_inventory.py new file mode 100644 index 0000000..f4092ca --- /dev/null +++ b/src/dx_productivity/trial_task_center/native_inventory.py @@ -0,0 +1,194 @@ +"""Supervise official inventory CLIs on a disposable, isolated database copy.""" +from __future__ import annotations + +import hashlib +import json +import os +from pathlib import Path +import re +import shutil +import tempfile +import time +from uuid import uuid4 + +from .errors import TaskCenterError +from .isolated_desktop import PrivateDesktop +from .native_inventory_protocol import NativeInventoryUnsupported, read_ascii_discovery, read_native_text +from .operation_control import check_cancelled, report_progress + + +def _hash(path): + with path.open('rb') as stream: + return hashlib.file_digest(stream, 'sha256').hexdigest() + + +def _verify_sources(hashes): + try: + if any(path.resolve(strict=True) != path or _hash(path) != digest for path, digest in hashes.items()): + raise OSError('source identity changed') + except OSError as error: + raise TaskCenterError('target_identity', '原生采集期间源工程或数据库已变化或无法核验,请重新导入') from error + + +def _project_options(project): + text = read_native_text(project) + def option(name, default=None): + values = set(re.findall(r'^KEY ' + name + r' "([^"\r\n]*)"\s*$', text, re.MULTILINE)) + if not values and default is not None: + return default + if len(values) != 1: + raise NativeInventoryUnsupported(f'工程 {name} 未明确指定唯一值') + return values.pop() + block = option('RootBlock') + if not re.fullmatch(r'[\w .()+-]+', block) or block in ('.', '..'): + raise NativeInventoryUnsupported('工程根块名称不适合原生清单导出') + snapshot = option('FrontEndSnapshot', 'DxD') + if snapshot.casefold() != 'dxd': + raise NativeInventoryUnsupported('当前工程不是已验证的 DxD 数据库快照') + database = (project.parent / option('iCDBDir') / 'icdb.dat').resolve(strict=True) + if not database.is_relative_to(project.parent) or not database.is_file(): + raise NativeInventoryUnsupported('数据库不在当前工程目录内') + return text, block, snapshot, database + + +class NativeInventoryReader: + def __init__(self, data_root, *, tool_directory=None, desktop_factory=None, + temporary_root=None, timeout=30): + self.data_root = Path(data_root) + self.tool_directory = tool_directory + self.desktop_factory = desktop_factory or PrivateDesktop + self.temporary_root = temporary_root + self.timeout = timeout + + def _tools(self): + directory = self.tool_directory + if directory is None: + from .isolated_cad import _designer_executable + directory = _designer_executable().parents[3] / 'iCDB/win32/bin' + directory = Path(directory).resolve(strict=True) + tools = (directory / 'icdb2ascii.exe',) + if not all(path.is_file() for path in tools): + raise NativeInventoryUnsupported('本机未安装完整的原生器件清单工具') + return tools + + def read(self, project, old_part_numbers, *, stop_event=None, on_progress=None): + check_cancelled(stop_event) + try: + project = Path(project).resolve(strict=True) + text, block, snapshot, database = _project_options(project) + tools = self._tools() + except (NativeInventoryUnsupported, OSError, ImportError, UnicodeError, TaskCenterError) as error: + report_progress(on_progress, f'使用 CAD 核验位号:{str(error)[:160]}') + return None + return self._read(project, text, block, snapshot, database, tools, old_part_numbers, + stop_event=stop_event, on_progress=on_progress) + + def _read(self, project, text, block, snapshot, database, tools, old_part_numbers, *, stop_event, on_progress): + token = uuid4().hex + evidence = self.data_root / 'quick-run-history' / ('native-inventory-' + token) + evidence.mkdir(parents=True, exist_ok=False) + base = Path(self.temporary_root or Path(os.environ.get('LOCALAPPDATA') or tempfile.gettempdir()) + / 'DxProductivity' / 'native-inventory').resolve() + base.mkdir(parents=True, exist_ok=True) + workspace = Path(tempfile.mkdtemp(prefix='dx-cli-', dir=base)).resolve() + desktop = self.desktop_factory('dx-cli-' + token, enabled=True) + record = {'backend': 'native_cli', 'project': str(project), 'state': 'reading', + 'workspace': str(workspace), 'tools': []} + started = time.monotonic() + clean = False + result = None + source_hashes = {} + try: + source_hashes = {project: _hash(project), database: _hash(database)} + if read_native_text(project) != text: + raise TaskCenterError('target_identity', '创建原生采集快照时工程配置已变化,请重新导入') + source = workspace / 'input.prj' + target_database = workspace / 'database' + target_database.mkdir() + shutil.copyfile(database, target_database / 'icdb.dat') + # No server address files or running database directory are copied. + text = re.sub(r'^KEY iCDBDir "[^"\r\n]*"\s*$', + 'KEY iCDBDir "database"', text, flags=re.MULTILINE) + text = re.sub(r'^KEY DedicatedServerName "[^"\r\n]*"\s*$', + 'KEY DedicatedServerName ""', text, flags=re.MULTILINE) + source.write_text(text, encoding='utf-8') + if len(str(workspace)) > 180 or _hash(target_database / 'icdb.dat') != source_hashes[database]: + raise NativeInventoryUnsupported('无法创建稳定的短路径数据库快照') + output = workspace / 'output' + output.mkdir() + commands = ( + (str(tools[0]), '-project=' + str(source), '-icdb=' + str(target_database), + '-snap=' + snapshot, '-h', '-t', '-offline', '-file=' + str(output / 'scope.txt')), + ) + report_progress(on_progress, '正在使用原生接口读取工程快照中的器件和位号…') + try: + desktop.__enter__() + environment = dict(os.environ, SDD_HOME=str(tools[0].parents[3])) + for command in commands: + self._run_tool(desktop, command, workspace, environment, output, + record, stop_event) + finally: + try: + desktop.close() + clean = True + except Exception as error: + record.update(state='uncertain', cleanup_error=str(error)) + try: + (evidence / 'uncertain.flag').write_text(str(error), encoding='utf-8') + except OSError as marker_error: + record['uncertain_marker_error'] = str(marker_error) + raise TaskCenterError('inventory_cleanup', + f'原生采集进程未能确认安全释放,请保留诊断记录:{evidence}') from error + _verify_sources(source_hashes) + check_cancelled(stop_event) + result = read_ascii_discovery(project, output / 'scope.txt', old_part_numbers, expected_root=block) + record.update(state='ready', components=len(result.components), source_hashes_unchanged=True) + report_progress(on_progress, f'原生采集完成,已核对 {len(result.components)} 个目标位号及 UID。') + except (NativeInventoryUnsupported, OSError, UnicodeError) as error: + if record['state'] == 'uncertain': + raise TaskCenterError('inventory_cleanup', f'原生采集清理状态不确定:{evidence}') from error + _verify_sources(source_hashes) + record.update(state='fallback', reason=str(error)) + report_progress(on_progress, f'原生清单未能完整核对,改用 CAD 采集:{str(error)[:160]}') + except TaskCenterError as error: + if record['state'] != 'uncertain': + record.update(state='cancelled' if error.code == 'operation_cancelled' else 'failed', reason=error.message) + raise + finally: + record.update(seconds=time.monotonic() - started, + cleanup=getattr(desktop, 'cleanup_record', None)) + # All diagnostics happen after process/desktop cleanup, never in + # between an exit request and the wait that confirms it. + try: + if (workspace / 'output').is_dir(): + shutil.copytree(workspace / 'output', evidence / 'output') + finally: + if clean or not record['tools'] and record['state'] != 'uncertain': + if workspace.parent == base and workspace.resolve() == workspace: + shutil.rmtree(workspace) + (evidence / 'native-inventory.json').write_text( + json.dumps(record, ensure_ascii=False, indent=2), encoding='utf-8') + return result + + def _run_tool(self, desktop, command, workspace, environment, output, record, stop_event): + check_cancelled(stop_event) + name = Path(command[0]).stem + started = time.monotonic() + metric = {'name': name, 'exit_code': None} + record['tools'].append(metric) + try: + with (output / (name + '.stdout.txt')).open('wb') as stdout, ( + output / (name + '.stderr.txt')).open('wb') as stderr: + process = desktop.start_process(command, cwd=workspace, env=environment, + stdout=stdout, stderr=stderr) + deadline = started + self.timeout + while process.poll() is None: + check_cancelled(stop_event) + if time.monotonic() >= deadline: + raise NativeInventoryUnsupported(f'{name} 原生读取超时') + time.sleep(0.05) + metric['exit_code'] = process.poll() + if metric['exit_code'] != 0: + raise NativeInventoryUnsupported(f'{name} 返回退出码 {metric["exit_code"]}') + finally: + metric['seconds'] = time.monotonic() - started diff --git a/src/dx_productivity/trial_task_center/native_inventory_protocol.py b/src/dx_productivity/trial_task_center/native_inventory_protocol.py new file mode 100644 index 0000000..f57d0aa --- /dev/null +++ b/src/dx_productivity/trial_task_center/native_inventory_protocol.py @@ -0,0 +1,96 @@ +"""Read complete, unfiltered offline iCDB reports as immutable CAD identities.""" +from __future__ import annotations + +import os +from pathlib import Path +import re + +from .quick_run_protocol import ProjectDiscovery +from .target_plan import ComponentTarget + + +class NativeInventoryUnsupported(ValueError): + """The native report cannot certify the existing inventory contract.""" + + +_UID = re.compile(r'^\$\d+I\d+$') + + +def read_native_text(path: Path) -> str: + if path.stat().st_size > 32 * 1024 * 1024: + raise NativeInventoryUnsupported('原生清单超过读取上限') + data = path.read_bytes() + try: + return data.decode('utf-8-sig') + except UnicodeDecodeError: + return data.decode('mbcs' if os.name == 'nt' else 'cp1252') + + +def _parse_scope(text, requested): + blocks, sheets, targets = set(), set(), set() + block, symbol, attributes = '', None, {} + def finish(): + if symbol is not None and attributes.get('Part Number', '').casefold() in requested: + uid, sheet = symbol + ref, part = attributes.get('Ref Designator', ''), attributes['Part Number'] + if not _UID.fullmatch(uid) or not ref: + raise NativeInventoryUnsupported('全工程报告中的请求物料缺少唯一身份') + target = block, sheet, uid, ref, part, attributes.get('Value', '') + if target in targets: + raise NativeInventoryUnsupported('全工程报告含重复器件身份') + targets.add(target) + if len(targets) > 100000: + raise NativeInventoryUnsupported('原生器件数量超过读取上限') + for line in text.splitlines(): + if line.startswith('Block '): + finish() + name = line[6:].split('\t', 1)[0].rstrip() + if not name or name in blocks: + raise NativeInventoryUnsupported('全工程报告含不明确的原理图身份') + blocks.add(name) + block, symbol, attributes = name, None, {} + elif line.startswith(' Symbol '): + finish() + match = re.fullmatch(r' Symbol (.+?)\tpage (.+)', line) + if not block or match is None: + raise NativeInventoryUnsupported('全工程报告含不明确的图页身份') + symbol, attributes = match.groups(), {} + sheets.add((block, symbol[1])) + elif symbol is not None and line.startswith('\tProp '): + # Pin properties have two tabs and must never override a component. + match = re.fullmatch(r'\tProp (Ref Designator|Part Number|Value)(?: (.*))?', line) + if match: + key, value = match.groups() + if key in attributes: + raise NativeInventoryUnsupported('全工程报告的器件属性不唯一') + attributes[key] = value or '' + finish() + if not blocks or not sheets: + raise NativeInventoryUnsupported('全工程报告缺少原理图或图页') + return blocks, sheets, targets + + +def read_ascii_discovery(project, report_path, old_part_numbers, *, expected_root=None): + text = read_native_text(report_path) + required = {'REPORT FORMAT': 'FREEFORM', 'MODE': 'HIER BLOCKS', 'TEXT LISTS': 'ON', + 'SYMBOLS ONLY': 'OFF', 'NETS ONLY': 'OFF', 'USER BLOCKS': 'NONE'} + for name, value in required.items(): + values = re.findall(r'^\s*' + re.escape(name) + r':[ \t]*([^\r\n]+)', text, re.MULTILINE) + if [item.strip() for item in values] != [value]: + raise NativeInventoryUnsupported('原生全工程报告缺字段、被筛选或格式不受支持') + requested = {part.casefold() for part in old_part_numbers} + blocks, available, targets = _parse_scope(text, requested) + if expected_root is not None and expected_root not in blocks: + raise NativeInventoryUnsupported('原生全工程报告与工程根块身份不一致') + components = tuple(ComponentTarget(Path(project), *row) for row in sorted(targets)) + sheets = tuple(sorted(available)) + matches = {sheet: set() for sheet in sheets} + seen = set() + for target in components: + if target.key in seen: + raise NativeInventoryUnsupported('原生全工程报告的 UID 身份重复') + seen.add(target.key) + matches[(target.schematic_name, target.sheet_number)].add(target.old_part_number.casefold()) + # Symbol reports certify component inventory, but cannot enumerate empty pages. + return ProjectDiscovery(sheets, {key: frozenset(value) for key, value in matches.items()}, + components, complete_sheet_manifest=False) diff --git a/src/dx_productivity/trial_task_center/quick_run.py b/src/dx_productivity/trial_task_center/quick_run.py index 595d933..b2636fd 100644 --- a/src/dx_productivity/trial_task_center/quick_run.py +++ b/src/dx_productivity/trial_task_center/quick_run.py @@ -717,6 +717,13 @@ class QuickRunPreparer: from .project_discovery import DiscoveryRuntime, discover_project from .operation_control import control_options + if collect_components and self._process_runner is _run_process: + from .native_inventory import NativeInventoryReader + native = NativeInventoryReader(self._data_root).read( + project_path, old_part_numbers, stop_event=stop_event, on_progress=on_progress) + if native is not None: + return native + process_runner = self._process_runner if stop_event is not None and process_runner is _run_process: def process_runner(command, timeout): diff --git a/src/dx_productivity/trial_task_center/quick_run_protocol.py b/src/dx_productivity/trial_task_center/quick_run_protocol.py index 61d5ed7..271d561 100644 --- a/src/dx_productivity/trial_task_center/quick_run_protocol.py +++ b/src/dx_productivity/trial_task_center/quick_run_protocol.py @@ -58,6 +58,7 @@ class ProjectDiscovery: sheets: tuple[tuple[str, str], ...] matched_old_parts: dict[tuple[str, str], frozenset[str]] components: tuple = () + complete_sheet_manifest: bool = True # Preserve callers that injected the original discovery boundary. diff --git a/tests/v1_1/test_native_ascii_inventory.py b/tests/v1_1/test_native_ascii_inventory.py new file mode 100644 index 0000000..7dc6957 --- /dev/null +++ b/tests/v1_1/test_native_ascii_inventory.py @@ -0,0 +1,87 @@ +import pytest + +from dx_productivity.trial_task_center import native_inventory_protocol as protocol + + +def report(tmp_path): + path = tmp_path / 'report.txt' + path.write_text('iCDB Ascii Report Generation Date: test\n' + ' REPORT FORMAT: FREEFORM\n MODE: HIER BLOCKS\n TEXT LISTS: ON\n' + ' SYMBOLS ONLY: OFF\n NETS ONLY: OFF\n USER BLOCKS: NONE\n' + 'Block board \tRoot\n' + ' Symbol $27I100\tpage MAIN\n\tSymbol name res.1 type Primitive Frozen ForwardPCB \n' + '\tProp Ref Designator R1\n\tProp Part Number OLD\n\tProp Value 10K/NC\n' + '\tPin 1\ttype 1\n\t\tProp Value PIN_VALUE_MUST_NOT_OVERRIDE_COMPONENT\n' + ' Symbol IN_PORT\tpage MAIN\n\tSymbol name input.1 type \n' + 'Block disconnected \tRoot\n' + ' Symbol $28I200\tpage AUX\n\tSymbol name res.1 type Primitive Frozen ForwardPCB \n' + '\tProp Value 20K\n\tProp Ref Designator R2\n\tProp Part Number OLD\n') + return path + + +def test_offline_full_project_report_includes_disconnected_schematic_and_exact_uid(tmp_path): + path = report(tmp_path) + project = tmp_path / 'source.prj' + result = protocol.read_ascii_discovery(project, path, ('old',)) + assert result.sheets == (('board', 'MAIN'), ('disconnected', 'AUX')) + assert [(t.project_path, t.schematic_name, t.sheet_number, t.uid, t.refdes, t.old_part_number, t.old_value) + for t in result.components] == [ + (project, 'board', 'MAIN', '$27I100', 'R1', 'OLD', '10K/NC'), + (project, 'disconnected', 'AUX', '$28I200', 'R2', 'OLD', '20K')] + assert result.matched_old_parts == { + ('board', 'MAIN'): frozenset({'old'}), ('disconnected', 'AUX'): frozenset({'old'})} + assert result.complete_sheet_manifest is False + + +@pytest.mark.parametrize('change', ['flat', 'filtered', 'no_properties', 'symbols_only', 'nets_only', + 'table_format', 'bad_uid', 'duplicate', 'duplicate_property', 'missing_ref']) +def test_filtered_or_ambiguous_ascii_report_requires_com_fallback(tmp_path, change): + path = report(tmp_path) + text = path.read_text() + substitutions = { + 'flat': ('MODE: HIER BLOCKS', 'MODE: FLAT'), + 'filtered': ('USER BLOCKS: NONE', 'USER BLOCKS: board'), + 'no_properties': ('TEXT LISTS: ON', 'TEXT LISTS: OFF'), + 'symbols_only': ('SYMBOLS ONLY: OFF', 'SYMBOLS ONLY: ON'), + 'nets_only': ('NETS ONLY: OFF', 'NETS ONLY: ON'), + 'table_format': ('REPORT FORMAT: FREEFORM', 'REPORT FORMAT: TABULAR'), + 'bad_uid': ('$27I100', 'unknown_uid'), + 'duplicate_property': ('\tProp Value 10K/NC', '\tProp Value 10K/NC\n\tProp Value 20K'), + 'missing_ref': ('\tProp Ref Designator R1\n', ''), + } + if change == 'duplicate': + text += ' Symbol $28I200\tpage AUX\n\tProp Ref Designator R2\n\tProp Part Number OLD\n\tProp Value 20K\n' + else: + old, new = substitutions[change] + text = text.replace(old, new) + path.write_text(text) + with pytest.raises(protocol.NativeInventoryUnsupported): + protocol.read_ascii_discovery(tmp_path / 'source.prj', path, ('OLD',)) + + +def test_absent_requested_part_returns_empty_inventory_with_available_sheets(tmp_path): + result = protocol.read_ascii_discovery(tmp_path / 'source.prj', report(tmp_path), ('OTHER',)) + assert not result.components + assert result.matched_old_parts == {('board', 'MAIN'): frozenset(), ('disconnected', 'AUX'): frozenset()} + + +def test_component_cache_cannot_substitute_for_complete_page_discovery(tmp_path): + from dx_productivity.trial_task_center.quick_run import QuickRunPreparer + from dx_productivity.trial_task_center.quick_run_protocol import ProjectDiscovery + project = tmp_path / 'project/board.prj' + project.parent.mkdir() + project.write_text('project') + preparer = QuickRunPreparer(tmp_path / 'data') + calls = [] + def discover(path, parts, *, collect_components=False, **controls): + calls.append(collect_components) + if collect_components: + return protocol.read_ascii_discovery(project, report(tmp_path), parts) + return ProjectDiscovery((('board', 'MAIN'), ('disconnected', 'AUX'), ('empty', 'BLANK')), + {('board', 'MAIN'): frozenset({'old'}), ('disconnected', 'AUX'): frozenset({'old'}), + ('empty', 'BLANK'): frozenset()}) + preparer._discover_project_sheets = discover + assert len(preparer.collect_inventory(project, ('OLD',))) == 2 + full = preparer.discover_project(project, ('OLD',)) + assert ('empty', 'BLANK') in full.sheets + assert calls == [True, False] diff --git a/tests/v1_1/test_native_inventory_reader.py b/tests/v1_1/test_native_inventory_reader.py new file mode 100644 index 0000000..851b103 --- /dev/null +++ b/tests/v1_1/test_native_inventory_reader.py @@ -0,0 +1,159 @@ +import json +from pathlib import Path +from threading import Event +from types import SimpleNamespace + +import pytest + +from dx_productivity.trial_task_center import native_inventory as runtime +from dx_productivity.trial_task_center.errors import TaskCenterError + + +def reader_fixture(tmp_path, *, fail_cleanup=False, cancel=False, corrupt=False, timeout=False): + source = tmp_path / 'source' / 'board.prj' + source.parent.mkdir() + source.write_text('SECTION DesignInfo\nKEY FrontEndSnapshot "DxD"\nENDSECTION\n' + 'SECTION Board1\nKEY RootBlock "board"\nENDSECTION\n' + 'SECTION iCDB\nKEY iCDBDir "database"\nKEY DedicatedServerName ""\nENDSECTION\n') + (source.parent / 'database').mkdir() + (source.parent / 'database/icdb.dat').write_bytes(b'disposable database') + tools = tmp_path / 'bin' + tools.mkdir() + (tools / 'icdb2ascii.exe').touch() + calls, released = [], [] + stop = Event() + class Desktop: + cleanup_record = {'desktop_confirmation': {'released': True}} + def __init__(self, name, **kwargs): + self.name = name + def __enter__(self): + return self + def start_process(self, command, **kwargs): + calls.append(command) + options = dict(arg[1:].split('=', 1) for arg in command[1:] if '=' in arg) + snapshot = Path(options.get('prj') or options['project']) + assert snapshot != source + assert (snapshot.parent / 'database/icdb.dat').read_bytes() == b'disposable database' + assert str(source.parent) not in snapshot.read_text() + Path(options['file']).write_text( + (' REPORT FORMAT: TABULAR\n' if corrupt else ' REPORT FORMAT: FREEFORM\n') + + ' MODE: HIER BLOCKS\n USER BLOCKS: NONE\n TEXT LISTS: ON\n' + ' SYMBOLS ONLY: OFF\n NETS ONLY: OFF\n' + 'Block board \tRoot\n Symbol $27I100\tpage MAIN\n' + '\tProp Ref Designator R1\n\tProp Part Number OLD\n\tProp Value 10K\n') + def poll(): + if cancel: + stop.set() + return None + return None if timeout else 0 + return SimpleNamespace(poll=poll, pid=42) + def close(self): + released.append(True) + if fail_cleanup: + raise OSError('owned desktop remained alive') + reader = runtime.NativeInventoryReader(tmp_path / 'evidence', tool_directory=tools, + desktop_factory=Desktop, temporary_root=tmp_path / 'temporary', timeout=0.02 if timeout else 30) + return source, reader, stop, calls, released + + +def test_reader_uses_a_disposable_database_and_returns_certified_native_identity(tmp_path): + project, reader, stop, calls, released = reader_fixture(tmp_path) + before = project.read_bytes(), (project.parent / 'database/icdb.dat').read_bytes() + messages = [] + result = reader.read(project, ('OLD',), stop_event=stop, on_progress=messages.append) + assert len(calls) == 1 and released == [True] + assert [(c.refdes, c.uid, c.sheet_number) for c in result.components] == [('R1', '$27I100', 'MAIN')] + assert before == (project.read_bytes(), (project.parent / 'database/icdb.dat').read_bytes()) + assert not list((tmp_path / 'temporary').iterdir()) + record = json.loads(next((tmp_path / 'evidence').rglob('native-inventory.json')).read_text()) + assert record['state'] == 'ready' and len(record['tools']) == 1 + assert messages + + +@pytest.mark.parametrize('failure', ['malformed', 'timeout']) +def test_safe_native_failure_allows_com_fallback_only_after_desktop_release(tmp_path, failure): + project, reader, stop, calls, released = reader_fixture( + tmp_path, corrupt=failure == 'malformed', timeout=failure == 'timeout') + assert reader.read(project, ('OLD',), stop_event=stop) is None + assert released == [True] + assert not list((tmp_path / 'temporary').iterdir()) + + +def test_cancellation_waits_for_owned_cleanup_and_never_falls_back(tmp_path): + project, reader, stop, calls, released = reader_fixture(tmp_path, cancel=True) + with pytest.raises(TaskCenterError) as caught: + reader.read(project, ('OLD',), stop_event=stop) + assert caught.value.code == 'operation_cancelled' + assert released == [True] and len(calls) == 1 + + +def test_native_cleanup_failure_blocks_fallback_and_keeps_recovery_evidence(tmp_path): + project, reader, stop, calls, released = reader_fixture(tmp_path, fail_cleanup=True, cancel=True) + with pytest.raises(TaskCenterError) as caught: + reader.read(project, ('OLD',), stop_event=stop) + assert caught.value.code == 'inventory_cleanup' + assert released == [True] and list((tmp_path / 'evidence').rglob('uncertain.flag')) + assert list((tmp_path / 'temporary').iterdir()) + + +def test_marker_write_failure_cannot_turn_uncertain_cleanup_into_a_fallback(tmp_path, monkeypatch): + project, reader, stop, calls, released = reader_fixture(tmp_path, fail_cleanup=True) + original = Path.write_text + def write(path, *args, **kwargs): + if path.name == 'uncertain.flag': + raise PermissionError('marker unavailable') + return original(path, *args, **kwargs) + monkeypatch.setattr(Path, 'write_text', write) + with pytest.raises(TaskCenterError) as caught: + reader.read(project, ('OLD',)) + assert caught.value.code == 'inventory_cleanup' + record = json.loads(next((tmp_path / 'evidence').rglob('native-inventory.json')).read_text()) + assert record['state'] == 'uncertain' + + +def test_source_change_during_a_failed_native_call_blocks_fallback(tmp_path, monkeypatch): + project, reader, stop, calls, released = reader_fixture(tmp_path) + def changed_source(*args): + (project.parent / 'database/icdb.dat').write_bytes(b'externally edited') + raise runtime.NativeInventoryUnsupported('native command failed') + monkeypatch.setattr(reader, '_run_tool', changed_source) + with pytest.raises(TaskCenterError) as caught: + reader.read(project, ('OLD',)) + assert caught.value.code == 'target_identity' + assert released == [True] + + +def test_unsupported_project_does_not_launch_any_native_process(tmp_path): + project, reader, stop, calls, released = reader_fixture(tmp_path) + project.write_text(project.read_text() + 'KEY RootBlock "another board"\n') + assert reader.read(project, ('OLD',)) is None + assert calls == [] and released == [] + + +@pytest.mark.parametrize('outcome', ['native', 'fallback', 'cleanup_failure']) +def test_default_inventory_prefers_native_but_never_falls_back_after_uncertain_cleanup(tmp_path, monkeypatch, outcome): + from dx_productivity.trial_task_center import quick_run, project_discovery + from dx_productivity.trial_task_center.quick_run_protocol import ProjectDiscovery + expected = ProjectDiscovery((('board', 'MAIN'),), {('board', 'MAIN'): frozenset()}) + events = [] + stop = Event() + messages = [] + monkeypatch.setattr(project_discovery, 'discover_project', lambda *args, **kwargs: events.append('cad') or expected) + preparer = quick_run.QuickRunPreparer(tmp_path / 'evidence') + progress = messages.append + def read_controlled(project, parts, **controls): + assert controls['stop_event'] is stop and controls['on_progress'] is progress + events.append('native') + if outcome == 'cleanup_failure': + raise TaskCenterError('inventory_cleanup', 'desktop is uncertain') + return expected if outcome == 'native' else None + monkeypatch.setattr(runtime, 'NativeInventoryReader', lambda root: SimpleNamespace(read=read_controlled)) + if outcome == 'cleanup_failure': + with pytest.raises(TaskCenterError) as caught: + preparer._discover_project_sheets(tmp_path / 'board.prj', ('OLD',), collect_components=True, + stop_event=stop, on_progress=progress) + assert caught.value.code == 'inventory_cleanup' + else: + assert preparer._discover_project_sheets(tmp_path / 'board.prj', ('OLD',), collect_components=True, + stop_event=stop, on_progress=progress) is expected + assert events == (['native', 'cad'] if outcome == 'fallback' else ['native'])