diff --git a/README.md b/README.md index afa6416..388e4a4 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,11 @@ 按 Ctrl+C 请求停止,等待工具收尾;不要直接关闭窗口。下次使用 --retry 指定批次,可恢复未完成项。成功产物只有在输入、工具和输出摘要仍匹配时才复用;源工程变化会完整重新导出该工程。 -退出码:0 全部请求项成功;1 含扫描问题/部分成功/失败;2 环境或输入配置错误;130 已取消。 +默认尽力导出并持续完成批次。单项异常、缺工具/缺库、超时、格式检查提示或进程收尾提示,都记录下来并继续后续工具和工程;报告文件被占用也不阻塞导出。只有用户主动按 Ctrl+C 才停止批次。 + +生成了非空文件便保留在正常分类目录,并显示「已导出」或「已导出(有提示)」。没有生成文件的工程显示「已跳过(无导出文件)」,单项显示「未导出」或「已跳过」。格式检查、非零退出和源文件变化保留为提示;带提示的产物不会作为已验证成功结果复用。收尾不确定时保留临时副本及自有进程句柄,继续使用独立副本运行其余任务。 + +退出码:0 批次已跑完(允许存在跳过项和提示);2 启动环境或输入配置错误,未能开始;130 用户主动取消。判断具体取得了哪些文件,应查看 HTML/JSON 中的逐项结果。 ## 输出内容 @@ -38,7 +42,7 @@ 工具、模式、快照、输入/输出摘要分别保存。同名字段不互相覆盖;分层属性、平铺有效值、网表身份表和当前中央库是不同来源。先看「导出汇总.html」,再进入工程的逐项说明。 -“成功”表示请求的导出项生成并通过产物检查,不代表换料正确、ERC 通过或全部制造几何已核验。缺工具、COM 不匹配、缺库或不支持的内容均明确列出,不使用空文件冒充成功。 +「已导出」表示文件已取得,不代表换料正确、ERC 通过或全部制造几何已核验。有检查提示时会注明具体原因;缺工具、COM 不匹配、缺库或不支持的内容均列出并跳过。 ## 兼容与边界 diff --git a/docs/releases/v0.1.1.md b/docs/releases/v0.1.1.md new file mode 100644 index 0000000..c172202 --- /dev/null +++ b/docs/releases/v0.1.1.md @@ -0,0 +1,12 @@ +# MentorExport 0.1.1 + +2026-11-09。根据工单 #1 后用户确认的新规则,默认以取得文件和跑完批次为目标。工单只有程序附件,没有导出批次日志,因此本版调整运行策略,不声称已复现现场未知故障。 + +- 格式检查不通过、工具非零退出或超时但生成了文件:保留到正常分类目录,显示「已导出(有提示)」;原始退出码、格式检查及输入摘要继续记录。 +- 只取得部分导出内容,也不把整个工程判为失败;无产物时显示「已跳过(无导出文件)」。 +- 收尾不确定、缺库、缺工具、预检帮助不匹配及单工程异常均不停止后续导出;收尾不确定仍保留临时副本与句柄证据。 +- HTML 或记录文件写入异常改为提示;单个输出复制异常不会阻止其他文件保留。 +- 正常结束批次统一退出 0,用户取消为 130,无法启动的配置错误为 2。退出 0 表示批次跑完,不代表所有内容完整。 +- 带提示的产物不参与「已验证成功」重试复用。源工程和中央库仍只读,工具运行在隔离副本上。 + +本次不修改网表解析和比对功能,也不自动改动工程或重试换料。 diff --git a/docs/superpowers/specs/2026-11-06-batch-export.md b/docs/superpowers/specs/2026-11-06-batch-export.md index 12e350e..a598a9e 100644 --- a/docs/superpowers/specs/2026-11-06-batch-export.md +++ b/docs/superpowers/specs/2026-11-06-batch-export.md @@ -2,6 +2,8 @@ 日期:2026-11-06。状态:已获用户确认并实施;最终验收见 ../../validation-2026-11-06.md。 +2026-11-09 更新:用户明确要求尽力导出、单项问题不阻塞批次。下文原始设计中的严格全成功判定和收尾不确定时停止批次,已被 0.1.1 的持续导出策略取代,现行规则见 ../../releases/v0.1.1.md。 + ## 已确认要求 - 独立、可复制的工具文件夹,以命令行启动,中文问答式交互。 diff --git a/docs/validation-2026-11-09.md b/docs/validation-2026-11-09.md new file mode 100644 index 0000000..e505fa9 --- /dev/null +++ b/docs/validation-2026-11-09.md @@ -0,0 +1,28 @@ +# 0.1.1 持续导出验收 + +2026-11-09。按用户明确要求调整:取得文件即保留,单项问题不阻塞后续工具和工程,完整跑完批次退出 0;仅主动取消为 130,无法启动为 2。 + +## 自动回归 + +`python -X utf8 -m pytest -q`:**55 passed in 69.90s**。 + +覆盖非零退出仍有文件、未识别格式、文件复制/摘要异常、伴随文件保留、日志被占用、预检返回异常及抛异常、中央库不可读、原文件导出中变化、进程收尾不确定后继续、临时副本删除失败、普通工程异常后继续、用户主动取消以及带提示的 ASCII 仍可提供库件引用。带提示产物不会作为已验证成功结果复用。 + +独立审查发现的遗漏均已修正并加入先失败后通过的回归;最后两项针对 ASCII 主文件选择与逐文件元数据错误的复查无待处理发现。 + +## 最终便携包实际批次 + +将最终 ZIP 对应工具复制到中文和空格目录,从 `C:\Windows\Temp` 使用真实 CMD 入口。PATH 仅保留 System32,PYTHONHOME/PYTHONPATH 指向无效路径;仍使用预置 Python 并自动发现本机 Mentor EE7.9.5。 + +- 真实工程 A:只在验收副本中清空中央库路径,10 项生成,库件读取跳过,工程标记「已导出(有提示)」。 +- 真实工程 B:A 之后继续执行,全部 11 项生成。 +- 第三个扫描条目缺数据库:记录为扫描问题,不阻止另外两个工程。 +- 首轮实际导出 159.828 秒,退出码 0;随后重试 11.219 秒,退出码 0。 +- 15 个验收输入文件摘要前后完全一致;两个工程的 source_unchanged 均为 true。 +- 664 个发行文件及 ZIP 内文件与完整性清单一致。 + +机器可读证据:`build/best-effort-acceptance-20261109-110803/acceptance.json`。 + +最终发布 ZIP SHA256:`01ab5b5eec7f32f43d6670d9f4bfd893c505e7c73336587a25fbe1afd911e69b`。 + +工单 #1 原附件只有程序与配置,没有实际导出批次日志,本次未复现其未知现场根因;本版验证的是用户指定的新判定和继续执行策略。也未将导出文件标记为电气正确性校验通过。 diff --git a/mentorexport/__init__.py b/mentorexport/__init__.py index 95162b5..8a2342c 100644 --- a/mentorexport/__init__.py +++ b/mentorexport/__init__.py @@ -1,2 +1,2 @@ """Portable Mentor saved-schematic exports.""" -__version__ = "0.1.0" +__version__ = "0.1.1" diff --git a/mentorexport/cli.py b/mentorexport/cli.py index 28f3f4f..d11079f 100644 --- a/mentorexport/cli.py +++ b/mentorexport/cli.py @@ -113,23 +113,45 @@ def run_batch(projects, problems, installation, output_root, scan_root, *, previ "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) + "problems": problems, "projects": [], "warnings": []} + def warn(message): + if message not in record["warnings"]: + record["warnings"].append(message) + progress("提示:" + message) + + def checkpoint(): + try: + write_report(batch, record) + except Exception as error: + warn("汇总报告暂未写入,继续导出:" + str(error)) + try: + write_json(batch / "batch.json", record) + except Exception as error: + warn("批次记录暂未写入,继续导出:" + str(error)) + + def save_result(row): + try: + write_json(Path(row["folder"]) / "export.json", row["result"]) + except Exception as error: + warn(row["project"]["name"] + ":工程记录暂未写入,继续下一工程:" + str(error)) + + try: + write_json(batch / "环境信息.json", installation) + except Exception as error: + warn("环境记录暂未写入,继续导出:" + str(error)) 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 + checkpoint() 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"]) + save_result(row) continue project = row["project"] row["status"] = "running" - write_json(batch / "batch.json", record) + checkpoint() progress(f"\n[{index}/{len(projects)}] {project['name']}") try: result = exporter(project, installation, Path(row["folder"]), shared, @@ -138,36 +160,36 @@ def run_batch(projects, problems, installation, output_root, scan_root, *, previ row["result"] = result row["status"] = result["status"] if result.get("cleanup_uncertain"): - cleanup_uncertain = True - stop_event.set() + warn(project["name"] + ":进程收尾有提示,保留临时副本并继续下一工程") 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["status"] = "skipped" row["result"] = load_json(Path(row["folder"]) / "export.json", {"items": []}) - row["result"].update(status="failed", reason=str(exc)) + if any(item.get("outputs") for item in row["result"].get("items", [])): + row["status"] = "exported" + row["result"].update(status=row["status"], 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") + try: + folder.mkdir(parents=True, exist_ok=True) + (folder / "exception.txt").write_text(traceback.format_exc(), encoding="utf-8") + except OSError as log_error: + warn(project["name"] + ":异常日志暂未写入:" + str(log_error)) + warn(project["name"] + ":" + str(exc) + ";已跳过并继续") 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) + save_result(row) + checkpoint() 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["state"] = "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)) + code = 130 if cancelled else 0 record["exit_code"] = code - write_json(batch / "batch.json", record) - write_report(batch, record) + checkpoint() return batch, code @@ -233,15 +255,18 @@ def main(argv=None): 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") + try: + installation["preflight"] = preflight_installation(installation, diagnostic_root / "mentor-help") + except Exception as error: + installation["preflight"] = {"status": "failed", "reason": str(error)} if installation["preflight"]["status"] != "success": - raise ValueError(installation["preflight"]["reason"] + "\n" + str(diagnostic_root)) + print("提示:" + 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 + return 0 if installation["preflight"]["status"] == "success" else 2 print(f"\nMentorExport {__version__} · 原理图详细导出") print(f"Mentor:{installation['version']} {installation['root']}") print("读取已保存的原理图。请先保存目标工程;工具仅在副本上运行。") @@ -307,11 +332,11 @@ def main(argv=None): previous=previous, stop_event=stop, timeout=args.timeout) finally: signal.signal(signal.SIGINT, old_handler) - record = load_json(batch / "batch.json") + record = load_json(batch / "batch.json", {"projects": []}) 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("\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: diff --git a/mentorexport/exports.py b/mentorexport/exports.py index d44d14d..2ac97ee 100644 --- a/mentorexport/exports.py +++ b/mentorexport/exports.py @@ -38,16 +38,30 @@ _UNCERTAIN_DESKTOPS = [] _LIBRARY_CACHE = {} +def _checkpoint(path, record): + try: + write_json(path, record) + except Exception as error: + message = str(path) + ": " + str(error) + if message not in record.setdefault("warnings", []): + record["warnings"].append(message) + + @contextmanager -def _workspace(result): +def _workspace(result, receipt=None): path = Path(tempfile.mkdtemp(prefix="mexp-")) try: yield path finally: - if result.get("cleanup_uncertain"): + if result.get("cleanup_uncertain") or result.get("workspace_preserved"): result.setdefault("workspace_preserved", str(path)) else: - shutil.rmtree(path) + try: + shutil.rmtree(path) + except OSError as error: + result.update(workspace_preserved=str(path), workspace_cleanup_error=str(error)) + if receipt is not None: + _checkpoint(receipt, result) def _digest(value): @@ -111,7 +125,7 @@ def _run(command, cwd, environment, log_dir, *, stop_event, timeout): 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) + _checkpoint(log_dir / "run.json", result) return result @@ -286,7 +300,7 @@ def _status(items): return "cancelled" if states and all(s == "success" for s in states): return "success" - return "partial" if "success" in states else "failed" + return "exported" if any(i.get("outputs") for i in items) else "skipped" def export_project(project, installation, project_dir, shared_dir, *, previous=None, stop_event=None, timeout=45, progress=print): @@ -299,26 +313,26 @@ def export_project(project, installation, project_dir, shared_dir, *, previous=N "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: + _checkpoint(state, result) + with _workspace(result, state) 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)) + try: + result["library_source_hashes"] = _hashes(_library_sources(project)) + except Exception as error: + result["library_source_hashes"] = {} + result["library_source_error"] = str(error) + result["coverage_limits"].append("Library fingerprint unavailable: " + str(error)) 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) + result.update(status="skipped", reason=str(error), source_unchanged=False) + _checkpoint(state, result) return result environment = _environment(installation) old_items = {i["key"]: i for i in (previous or {}).get("items", [])} @@ -336,7 +350,7 @@ def export_project(project, installation, project_dir, shared_dir, *, previous=N 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) + _checkpoint(state, result) for key, tool, folder in ITEMS: output = None item = {"key": key, "status": "failed", "reason": "", "outputs": [], "provenance": {}} @@ -347,9 +361,6 @@ def export_project(project, installation, project_dir, shared_dir, *, previous=N 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 @@ -376,7 +387,7 @@ def export_project(project, installation, project_dir, shared_dir, *, previous=N 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" + item["reason"] = "Owned process cleanup is uncertain; workspace preserved; remaining exports continue" elif run.get("cancelled"): item.update(status="cancelled", reason="Cancelled while tool was running") elif run.get("timed_out"): @@ -384,7 +395,8 @@ def export_project(project, installation, project_dir, shared_dir, *, previous=N 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) + _validate(key, output) + files = sorted(p for p in output.rglob("*") if p.is_file() and p.stat().st_size) destination = project_dir / folder published = [] for file in files: @@ -393,26 +405,46 @@ def export_project(project, installation, project_dir, shared_dir, *, previous=N shutil.copyfile(file, target) published.append(target) item.update(status="success", outputs=[str(p) for p in published]) + item["validation"] = {"status": "passed"} 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: + saved = [] + publication_errors = [] try: - evidence = project_dir / "90_运行日志" / key / "failed-output" - saved = [] + destination = project_dir / folder 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 + try: + target = destination / source.relative_to(output) + target.parent.mkdir(parents=True, exist_ok=True) + shutil.copyfile(source, target) + if target.stat().st_size: + saved.append(_file_info(target)) + except OSError as error: + publication_errors.append(str(error)) + result.setdefault("workspace_preserved", str(temp)) + try: + if source.is_file() and source.stat().st_size: + saved.append(_file_info(source)) + except OSError as metadata_error: + saved.append({"path": str(source), "size": None, "sha256": None, + "metadata_error": str(metadata_error)}) except OSError as error: - item["provenance"]["failure_evidence_error"] = str(error) + publication_errors.append(str(error)) + if saved: + item["outputs"] = [file["path"] for file in saved] + item["provenance"]["output_files"] = saved + if publication_errors: + item["provenance"]["publication_errors"] = publication_errors + if item["status"] == "failed" and item.get("outputs"): + item["status"] = "exported" + if item["status"] == "exported": + item["validation"] = {"status": "warning", "reason": item["reason"]} result["status"] = "running" - write_json(state, result) + _checkpoint(state, result) try: after = _hashes(_inputs(project)) result["source_hashes_after"] = after @@ -425,9 +457,10 @@ def export_project(project, installation, project_dir, shared_dir, *, previous=N 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) + item.update(status="exported", reason="Source changed during export; captured output retained; re-export to verify current source", + validation={"status": "warning", "reason": "Source changed during export"}) + result["status"] = _status(result["items"]) + _checkpoint(state, result) return result @@ -464,8 +497,6 @@ def list_snapshots(project, installation, work_dir): 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) @@ -491,11 +522,15 @@ def _export_library(item, project, installation, result, destination, shared_dir 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) + flat = next((i for i in result["items"] if i["key"] == "ascii_flat" and i["status"] in ("success", "exported") and i.get("outputs")), 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(): + report_path = next((Path(path) for path in flat["outputs"] if Path(path).name == "connections.txt"), None) + if report_path is None: + item.update(status="unavailable", reason="ASCII properties report was not generated") + return + for line in read_text(report_path).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]) @@ -562,17 +597,21 @@ def _export_library(item, project, installation, result, destination, shared_dir 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) + if run.get("cleanup_uncertain"): + result.update(cleanup_uncertain=True, workspace_preserved=str(temp)) run["partition"] = relative run["log_dir"] = str(log) run["stdout_retained"] = True - write_json(log / "run.json", run) + _checkpoint(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.update(status="failed", reason="Library reader cleanup uncertain; workspace preserved; remaining exports continue") item["provenance"]["uncertain_run"] = run - return + data["errors"].append({"partition": relative, "reason": item["reason"]}) + persist() + continue if run.get("cancelled"): item.update(status="cancelled", reason="Library reading cancelled") return @@ -601,7 +640,7 @@ def _export_library(item, project, installation, result, destination, shared_dir persist() (log / "stdout.txt").unlink() run["stdout_retained"] = False - write_json(log / "run.json", run) + _checkpoint(log / "run.json", run) partitions[relative]["run"] = deepcopy(run) persist() except (OSError, ValueError) as error: @@ -629,7 +668,9 @@ def _export_library(item, project, installation, result, destination, shared_dir 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") + item.update(status="success" if flat["status"] == "success" else "exported", + reason="Used part/cell records read from fingerprinted shared library" + + ("; references came from ASCII export with warnings" if flat["status"] != "success" else "")) def _validate_native(native, mode): diff --git a/mentorexport/report.py b/mentorexport/report.py index 7d55e80..7fc7448 100644 --- a/mentorexport/report.py +++ b/mentorexport/report.py @@ -5,8 +5,8 @@ from pathlib import Path import os from urllib.parse import quote -LABELS = {"success": "成功", "partial": "部分成功", "failed": "失败", - "unavailable": "不可用", "cancelled": "已取消", "running": "执行中", "pending": "待执行"} +LABELS = {"success": "已导出", "exported": "已导出(有提示)", "partial": "已导出(有提示)", "failed": "未导出", + "skipped": "已跳过(无导出文件)", "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}""" @@ -34,7 +34,6 @@ def write_report(batch, record): 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", []): @@ -53,20 +52,30 @@ def write_report(batch, record): f'来源:{escape(str(project["path"]))}
' f'根块:{escape(str(project.get("root_block", "")))} 快照:{escape(str(project.get("snapshot", "")))}
' f'数据库快照名称(仅记录):{escape(snapshot_names)}
' + + (f'暂存文件位置:{link(result["workspace_preserved"], folder)}
' if result.get("workspace_preserved") else '') + f'{escape(str(result.get("reason", "")))}' '

各工具和模式的输出分别保留;同名字段可能具有不同来源。导出成功不等于工程电气正确性校验通过。

' '' + "".join(items) + '
导出项来源状态说明产物
' + ('

覆盖限制

' if limits else '') + link(folder / "export.json", folder, "工程原始记录 JSON") + " " + link(folder / "90_运行日志", folder, "运行日志")) - (folder / "工程说明.html").write_text(page(project["name"], details), encoding="utf-8") + try: + folder.mkdir(parents=True, exist_ok=True) + (folder / "工程说明.html").write_text(page(project["name"], details), encoding="utf-8") + except OSError as error: + message = project["name"] + ":工程说明暂未写入:" + str(error) + if message not in record.setdefault("warnings", []): + record["warnings"].append(message) rows.append(f'{link(folder / "工程说明.html", batch, project["name"])}' f'{LABELS.get(row["status"], escape(row["status"]))}' f'{escape(str(result.get("reason", "")))}{escape(str(project["path"]))}') issues = "".join(f'
  • {escape(str(p.get("path", "")))}:{escape(str(p.get("reason", "")))}
  • ' for p in record.get("problems", [])) - content = (f'
    {escape(summary or "没有可执行工程")}
    批次状态:{escape(record.get("state", ""))}
    ' + warnings = "".join('
  • ' + escape(str(message)) + '
  • ' for message in record.get("warnings", [])) + batch_state = {"complete": "已完成", "running": "执行中", "cancelled": "已取消"}.get(record.get("state"), record.get("state", "")) + content = (f'
    {escape(summary or "没有可执行工程")}
    批次状态:{escape(batch_state)}
    单项问题记录提示,批次继续执行。
    ' '

    默认读取已保存的原理图;不同工具的字段和覆盖范围分别记录。点击工程名查看逐项结果。

    ' '' + "".join(rows) + '
    工程结果说明源文件
    ' + ('

    扫描问题

    ' if issues else '') + + ('

    运行提示

    ' if warnings else '') + link(batch / "batch.json", batch, "批次记录 JSON")) (batch / "导出汇总.html").write_text(page("Mentor 原理图批量导出", content), encoding="utf-8") diff --git a/tests/test_cli.py b/tests/test_cli.py index df13803..be6b63e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -12,8 +12,8 @@ def test_batch_does_not_stop_after_one_project_failure(tmp_path): 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 code == 0 and state["state"] == "complete" + assert [p["status"] for p in state["projects"]] == ["skipped", "success"] assert (batch / "导出汇总.html").is_file() @@ -69,3 +69,65 @@ def test_retry_cannot_claim_running_owner_has_exited(): import pytest with pytest.raises(ValueError, match="运行"): assert_retry_safe({"state": "running", "owner": process_identity(os.getpid())}) + + +def test_uncertain_cleanup_does_not_cancel_following_project(tmp_path): + from mentorexport.cli import run_batch + visited = [] + def exporter(project, installation, folder, shared, **options): + visited.append(project["name"]) + return {"status": "skipped", "cleanup_uncertain": True, "items": []} + projects = [{"name": name, "path": str(tmp_path / (name + ".prj"))} for name in ("工程A", "工程B")] + stop = Event() + batch, code = run_batch(projects, [], {}, tmp_path / "out", tmp_path, exporter=exporter, stop_event=stop) + state = json.loads((batch / "batch.json").read_text(encoding="utf-8")) + assert visited == ["工程A", "工程B"] + assert code == 0 and state["state"] == "complete" and not stop.is_set() + assert state["warnings"] + + +def test_report_error_does_not_block_batch(tmp_path, monkeypatch): + from mentorexport import cli + visited = [] + def broken_report(*args): + raise OSError("HTML file is locked") + def exporter(project, installation, folder, shared, **options): + visited.append(project["name"]) + return {"status": "success", "items": []} + monkeypatch.setattr(cli, "write_report", broken_report) + projects = [{"name": name, "path": str(tmp_path / (name + ".prj"))} for name in ("工程A", "工程B")] + batch, code = cli.run_batch(projects, [], {}, tmp_path / "out", tmp_path, exporter=exporter) + record = json.loads((batch / "batch.json").read_text(encoding="utf-8")) + assert visited == ["工程A", "工程B"] and code == 0 + assert any("HTML file is locked" in message for message in record["warnings"]) + + +def test_scan_problems_and_no_projects_do_not_return_failed_batch(tmp_path): + from mentorexport.cli import run_batch + batch, code = run_batch([], [{"path": "missing.prj", "reason": "missing database"}], {}, tmp_path / "out", tmp_path) + state = json.loads((batch / "batch.json").read_text(encoding="utf-8")) + assert code == 0 and state["state"] == "complete" and state["problems"] + + +def test_export_continues_despite_help_preflight_failure(tmp_path, monkeypatch): + from mentorexport import cli + monkeypatch.setattr(cli, "runtime_check", lambda: []) + monkeypatch.setattr(cli, "_choose_installation", lambda *args: {"root": str(tmp_path), "version": "test"}) + monkeypatch.setattr(cli, "preflight_installation", lambda *args: {"status": "failed", "reason": "help did not match"}) + monkeypatch.setattr(cli, "_settings_paths", lambda: []) + monkeypatch.setattr(cli, "_save_settings", lambda value: None) + result = cli.main(["--input", str(tmp_path), "--output", str(tmp_path / "out"), "--yes"]) + assert result == 0 + assert list((tmp_path / "out").glob("批次_*/batch.json")) + + +def test_preflight_exception_is_a_warning_not_a_batch_blocker(tmp_path, monkeypatch): + from mentorexport import cli + monkeypatch.setattr(cli, "runtime_check", lambda: []) + monkeypatch.setattr(cli, "_choose_installation", lambda *args: {"root": str(tmp_path), "version": "test"}) + def locked(*args): raise PermissionError("diagnostic directory locked") + monkeypatch.setattr(cli, "preflight_installation", locked) + monkeypatch.setattr(cli, "_settings_paths", lambda: []) + monkeypatch.setattr(cli, "_save_settings", lambda value: None) + assert cli.main(["--input", str(tmp_path), "--output", str(tmp_path / "out"), "--yes"]) == 0 + assert list((tmp_path / "out").glob("批次_*/batch.json")) diff --git a/tests/test_exports.py b/tests/test_exports.py index 212387f..df22e92 100644 --- a/tests/test_exports.py +++ b/tests/test_exports.py @@ -110,7 +110,8 @@ def test_modified_output_and_changed_tool_are_not_reused(sample, monkeypatch): assert len(calls) == 5 -def test_used_library_only_is_shared_and_source_fingerprinted(sample, monkeypatch): +@pytest.mark.parametrize("ascii_warning", [False, True]) +def test_used_library_only_is_shared_and_source_fingerprinted(sample, monkeypatch, ascii_warning): exports = backend() project, install, root = sample library = root / "library" @@ -132,6 +133,7 @@ def test_used_library_only_is_shared_and_source_fingerprinted(sample, monkeypatc 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)) + (out.parent / "companion.txt").write_text("additional exported data") else: native_calls.append(command) mode = command[1] @@ -139,11 +141,11 @@ def test_used_library_only_is_shared_and_source_fingerprinted(sample, monkeypatc 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": []}) + return dict(exit_code=9 if ascii_warning and ("-f" in command or "-h" in command) else 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 item["status"] == ("exported" if ascii_warning else "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) @@ -154,7 +156,7 @@ def test_used_library_only_is_shared_and_source_fingerprinted(sample, monkeypatc 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 next(i for i in fourth["items"] if i["key"] == "library")["status"] == ("exported" if ascii_warning else "success") assert len(native_calls) == 5, "only partition containing previously unread NEW should rerun" @@ -204,7 +206,7 @@ def test_batch_records_snapshot_names_from_captured_input(sample, monkeypatch): assert result["project"]["snapshot"] == "Saved" -def test_uncertain_snapshot_cleanup_stops_batch_and_preserves_copy(sample, monkeypatch): +def test_uncertain_snapshot_cleanup_continues_exports_and_preserves_copy(sample, monkeypatch): exports = backend() project, install, root = sample install["tools"]["constraints_xml"] = install["tools"]["ascii"] @@ -214,14 +216,15 @@ def test_uncertain_snapshot_cleanup_stops_batch_and_preserves_copy(sample, monke 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 len(calls) == 3 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"]) + assert result["status"] == "skipped" + assert not any(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"])) + if Path(result["workspace_preserved"]) != preserved and Path(result["workspace_preserved"]).exists(): + exports.shutil.rmtree(Path(result["workspace_preserved"])) @pytest.mark.parametrize("key,name,text", [ @@ -256,7 +259,7 @@ def test_empty_oat_is_not_a_successful_pair(tmp_path): exports._validate("wir_oat", tmp_path) -def test_failed_format_output_is_retained_as_failure_evidence(sample, monkeypatch): +def test_unrecognized_format_is_exported_with_warning_in_normal_category(sample, monkeypatch): exports = backend() project, install, root = sample def malformed(command, cwd, environment, log_dir, **kwargs): @@ -266,9 +269,14 @@ def test_failed_format_output_is_retained_as_failure_evidence(sample, monkeypatc 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" + assert item["status"] == "exported" + assert item["validation"]["status"] == "warning" + path = Path(item["outputs"][0]) + assert "02_器件与引脚属性" in str(path) and "90_运行日志" not in str(path) + assert path.read_text() == "partial or malformed vendor output" + assert result["status"] == "exported" + again = exports.export_project(project, install, root / "retry", root / "shared", previous=result, progress=lambda _: None) + assert not again["items"][0]["provenance"]["reused"] @pytest.mark.parametrize("field", [{"hr": -2147467259, "value": None}, {"hr": 0, "value": None}]) @@ -323,7 +331,7 @@ def test_native_partition_diagnostics_and_accumulated_data_survive_failure(sampl 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") + assert item["status"] == ("cancelled" if outcome == "cancel" else "exported") logs = root / "attempt/90_运行日志/library" assert len(list(logs.rglob("stderr.txt"))) == 2 assert len(list(logs.rglob("run.json"))) == 2 @@ -338,7 +346,7 @@ def test_native_partition_diagnostics_and_accumulated_data_survive_failure(sampl assert str(data_files[0]) in item["outputs"] -def test_uncertain_cleanup_preserves_workspace_and_stops_remaining_tools(sample, monkeypatch): +def test_uncertain_cleanup_preserves_workspace_and_continues_remaining_tools(sample, monkeypatch): exports = backend() project, install, root = sample calls = [] @@ -347,13 +355,13 @@ def test_uncertain_cleanup_preserves_workspace_and_stops_remaining_tools(sample, 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 len(calls) == 2 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:]) + assert result["status"] == "skipped" + assert not any(i["status"] == "cancelled" for i in result["items"]) # No real process is running in this synthetic boundary failure. exports.shutil.rmtree(preserved) @@ -369,11 +377,150 @@ def test_capture_rejects_a_source_that_changes_while_copying(sample, monkeypatch 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 result["status"] == "skipped" assert "changed" in result["reason"].lower() assert not result["items"] +def test_nonzero_tool_with_output_is_retained_and_later_tool_runs(sample, monkeypatch): + exports = backend() + project, install, root = sample + calls = [] + real_runner = ascii_runner(calls) + def runner(*args, **kwargs): + run = real_runner(*args, **kwargs) + run["exit_code"] = 7 + return run + monkeypatch.setattr(exports, "_run", runner) + result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None) + assert len(calls) == 2 + assert all(item["status"] == "exported" and Path(item["outputs"][0]).is_file() for item in result["items"][:2]) + assert result["items"][0]["provenance"]["exit_code"] == 7 + + +@pytest.mark.parametrize("lock", ["copy", "metadata"]) +def test_one_locked_output_does_not_hide_other_generated_files(sample, monkeypatch, lock): + exports = backend() + project, install, root = sample + def runner(command, cwd, environment, log_dir, **kwargs): + output = Path(next(p.split("=", 1)[1] for p in command if p.startswith("-file="))).parent + (output / "a.txt").write_text("exported A") + (output / "b.txt").write_text("exported B") + return dict(exit_code=8, cleanup={"errors": []}) + real_copy = exports.shutil.copyfile + def copy(source, target, *args, **kwargs): + if Path(source).name == "a.txt": + raise PermissionError("one output is locked") + return real_copy(source, target, *args, **kwargs) + monkeypatch.setattr(exports, "_run", runner) + if lock == "copy": + monkeypatch.setattr(exports.shutil, "copyfile", copy) + else: + info = exports._file_info + def locked_info(path): + if Path(path).name == "a.txt": + raise PermissionError("one output hash is unavailable") + return info(path) + monkeypatch.setattr(exports, "_file_info", locked_info) + result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None) + assert result["items"][0]["status"] == "exported" + assert {Path(path).read_text() for path in result["items"][0]["outputs"]} == {"exported A", "exported B"} + assert result["workspace_preserved"] + exports.shutil.rmtree(Path(result["workspace_preserved"])) + + +def test_source_changed_after_capture_keeps_outputs_as_warning(sample, monkeypatch): + exports = backend() + project, install, root = sample + real_runner = ascii_runner([]) + def runner(*args, **kwargs): + run = real_runner(*args, **kwargs) + Path(project["database"], "icdb.dat").write_bytes(b"new source saved during export") + return run + monkeypatch.setattr(exports, "_run", runner) + result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None) + assert result["source_unchanged"] is False and result["status"] == "exported" + assert all(item["status"] == "exported" and item["outputs"] for item in result["items"][:2]) + + +def test_locked_receipt_does_not_stop_following_tools(sample, monkeypatch): + exports = backend() + project, install, root = sample + calls = [] + monkeypatch.setattr(exports, "_run", ascii_runner(calls)) + write = exports.write_json + def locked(path, value): + if Path(path).name == "export.json": + raise PermissionError("receipt is locked") + return write(path, value) + monkeypatch.setattr(exports, "write_json", locked) + result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None) + assert len(calls) == 2 and result["items"][1]["outputs"] + assert result["warnings"] + + +def test_unreadable_library_fingerprint_does_not_block_schematic_tools(sample, monkeypatch): + exports = backend() + project, install, root = sample + calls = [] + hashes = exports._hashes + monkeypatch.setattr(exports, "_library_sources", lambda _: {"parts": root / "parts.pdb"}) + def unreadable(files): + if "parts" in files: + raise PermissionError("central library unreadable") + return hashes(files) + monkeypatch.setattr(exports, "_hashes", unreadable) + monkeypatch.setattr(exports, "_run", ascii_runner(calls)) + result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None) + assert len(calls) == 2 and result["items"][0]["outputs"] + + +def test_successful_export_also_retains_companion_files(sample, monkeypatch): + exports = backend() + project, install, root = sample + run = ascii_runner([]) + def runner(command, cwd, environment, log_dir, **kwargs): + result = run(command, cwd, environment, log_dir, **kwargs) + (Path(cwd) / "output/companion.txt").write_text("additional exported data") + return result + monkeypatch.setattr(exports, "_run", runner) + result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None) + assert {Path(path).name for path in result["items"][0]["outputs"]} == {"connections.txt", "companion.txt"} + + +def test_workspace_cleanup_error_is_recorded_and_receipt_updated(sample, monkeypatch): + exports = backend() + project, install, root = sample + monkeypatch.setattr(exports, "_run", ascii_runner([])) + remove = exports.shutil.rmtree + def locked(path): + raise PermissionError("temporary workspace locked") + monkeypatch.setattr(exports.shutil, "rmtree", locked) + result = exports.export_project(project, install, root / "attempt", root / "shared", progress=lambda _: None) + receipt = json.loads((root / "attempt/export.json").read_text(encoding="utf-8")) + assert receipt["workspace_preserved"] == result["workspace_preserved"] + assert "locked" in result["workspace_cleanup_error"] + remove(Path(result["workspace_preserved"])) + + +def test_run_receipt_write_error_keeps_cleanup_uncertainty(tmp_path, monkeypatch): + exports = backend() + class Desktop: + cleanup_record = {"errors": ["unconfirmed owned process"], "processes": []} + def __init__(self, *args, **kwargs): pass + def __enter__(self): return self + def __exit__(self, *args): pass + def start_process(self, *args, **kwargs): return self + def poll(self): return 0 + pid = 123 + monkeypatch.setattr(exports, "PrivateDesktop", Desktop) + monkeypatch.setattr(exports, "_UNCERTAIN_DESKTOPS", []) + def locked(*args): raise PermissionError("run receipt locked") + monkeypatch.setattr(exports, "write_json", locked) + result = exports._run(["test"], tmp_path, {}, tmp_path / "logs", stop_event=None, timeout=1) + assert result["cleanup_uncertain"] and result["warnings"] + + @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):