333 lines
17 KiB
Python
333 lines
17 KiB
Python
"""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
|