Repository navigation
Expand file tree
/
Copy pathdapflash.py
More file actions
66 lines (55 loc) · 2.18 KB
/
Copy pathdapflash.py
File metadata and controls
66 lines (55 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""PyInstaller 打包入口。
用法:
python dapflash.py # 启动 GUI(等同 python -m app.main)
DAPFlash.exe # 打包后的 exe,双击启动 GUI
DAPFlash.exe --selftest # 自检:扫描探针+加载芯片,结果写入 selftest_result.txt
# (窗口版 exe 无控制台,故写文件便于诊断打包是否成功)
"""
from __future__ import annotations
import os
import sys
def _selftest() -> int:
"""不弹 GUI,验证冻结环境下 pyOCD 与芯片配置是否可用,结果写文件。"""
lines: list[str] = []
lines.append(f"frozen={getattr(sys, 'frozen', False)}")
lines.append(f"executable={sys.executable}")
try:
from app.core.target_manager import TargetManager
from app.paths import default_packs_root, default_targets_dir
lines.append(f"targets_dir={default_targets_dir()}")
lines.append(f"packs_root={default_packs_root()}")
tm = TargetManager()
lines.append(f"targets_loaded={len(tm)}")
lines.append("target_ids=" + ",".join(m.id for m in tm.all_models()))
from app.backends.pyocd_backend import PyOCDBackend
be = PyOCDBackend()
probes = be.scan_probes()
lines.append("probes=" + repr([(p.name, p.unique_id) for p in probes]))
lines.append("RESULT=OK")
rc = 0
except Exception as exc: # noqa: BLE001
import traceback
lines.append("RESULT=ERROR " + repr(exc))
lines.append(traceback.format_exc())
rc = 1
base = (os.path.dirname(sys.executable)
if getattr(sys, "frozen", False) else os.getcwd())
out = os.path.join(base, "selftest_result.txt")
try:
with open(out, "w", encoding="utf-8") as fh:
fh.write("\n".join(lines) + "\n")
except OSError:
pass
# 同时尝试打印(源码/控制台版可见)
try:
print("\n".join(lines))
except Exception:
pass
return rc
def main() -> int:
if "--selftest" in sys.argv:
return _selftest()
from app.main import main as gui_main
return gui_main()
if __name__ == "__main__":
raise SystemExit(main())