Files
mil_sdk/tools/verify_companions.py
T

93 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""编译完跑一遍:把 build/ 下的伴生包 .pyd 挂到 sys.modules
再用真实接口(openpyxl.load_workbook)写读一个最小 Excel,确认链路可用。
用法:
python tools/verify_companions.py
注意:本脚本直接 import mil.core.companion_loader**不**经过 mil/__init__.py
避免在无 PySide6 的开发机上因 UI 桥接而炸(生产宿主环境必然有 PySide6)。
"""
from __future__ import annotations
import argparse
import importlib.util
import sys
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
# 不走 mil/__init__.py,单独加载 companion_loader
spec = importlib.util.spec_from_file_location(
"mil_companion_loader", ROOT / "mil" / "core" / "companion_loader.py"
)
companion_loader = importlib.util.module_from_spec(spec)
spec.loader.exec_module(companion_loader)
load_companions = companion_loader.load_companions
# 伴生包清单:与 mil/__init__.py 中的 COMPANION_PACKAGES 保持一致
COMPANION_PACKAGES: list[str] = [
"openpyxl",
"et_xmlfile",
]
def main() -> int:
parser = argparse.ArgumentParser(description="验证伴生包 .pyd 加载链路")
parser.add_argument(
"--fallback",
action="store_true",
help="未发现产物时回退到 site-packages 已装版本(开发自测用)",
)
args = parser.parse_args()
loaded = load_companions(COMPANION_PACKAGES)
print(f"[verify] 加载器返回: {loaded}")
if not loaded and not args.fallback:
print(
"[verify] FAIL: 没有挂载到任何伴生包(是否忘了 python build_pyd.py?)\n"
" 调试期可加 --fallback 走 site-packages 自测加载器逻辑。",
file=sys.stderr,
)
return 1
if not loaded and args.fallback:
# 开发自测:build/mil_runtime 都没有时,从 sys.path 兜底
import importlib
for pkg in COMPANION_PACKAGES:
importlib.import_module(pkg)
loaded.append(pkg)
print(f"[verify] fallback 后挂载: {loaded}")
# 用 openpyxl 真接口验证:写一个最小 xlsx,立刻读回来
import openpyxl # noqa: WPS433 延迟导入以验证挂载生效
print(f"[verify] openpyxl 来自: {openpyxl.__file__}")
with tempfile.TemporaryDirectory() as td:
xlsx_path = Path(td) / "smoke.xlsx"
wb = openpyxl.Workbook()
ws = wb.active
ws["A1"] = "time"
ws["B1"] = "sig1"
ws["A2"] = 0.0
ws["B2"] = 1
ws["A3"] = 1.0
ws["B3"] = 2
wb.save(xlsx_path)
wb2 = openpyxl.load_workbook(xlsx_path)
ws2 = wb2.active
assert ws2["A1"].value == "time", ws2["A1"].value
assert ws2["B1"].value == "sig1", ws2["B1"].value
assert float(ws2["A3"].value) == 1.0, ws2["A3"].value
assert int(ws2["B3"].value) == 2, ws2["B3"].value
print("[verify] PASS: openpyxl 写读闭环 ✓")
return 0
if __name__ == "__main__":
sys.exit(main())