ehatool 6.0.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ehatool/VERSION +1 -0
- ehatool/__init__.py +4 -0
- ehatool/acceptance.py +92 -0
- ehatool/brt27.py +105 -0
- ehatool/brt27_sim.py +114 -0
- ehatool/calibration.py +111 -0
- ehatool/can_sniff.py +162 -0
- ehatool/cia402.py +31 -0
- ehatool/cli.py +541 -0
- ehatool/console.py +31 -0
- ehatool/device/__init__.py +4 -0
- ehatool/device/can.py +201 -0
- ehatool/device/can_discovery.py +387 -0
- ehatool/device/can_discovery.toml +19 -0
- ehatool/device/io.py +20 -0
- ehatool/devices.py +187 -0
- ehatool/diagnostics.py +58 -0
- ehatool/diagnostics.toml +208 -0
- ehatool/discovery.py +95 -0
- ehatool/display.toml +127 -0
- ehatool/ecat.py +520 -0
- ehatool/ecat.toml +17 -0
- ehatool/ecat_probe.py +401 -0
- ehatool/ecat_probe.toml +14 -0
- ehatool/eha_od.json +5871 -0
- ehatool/experiment.py +576 -0
- ehatool/image.py +70 -0
- ehatool/info.py +121 -0
- ehatool/info.toml +41 -0
- ehatool/install_host.py +243 -0
- ehatool/install_host.toml +27 -0
- ehatool/link.py +811 -0
- ehatool/link.toml +18 -0
- ehatool/live.py +112 -0
- ehatool/motor.py +86 -0
- ehatool/od.py +200 -0
- ehatool/odrive.py +450 -0
- ehatool/output.py +208 -0
- ehatool/ram_load.py +62 -0
- ehatool/scope.py +50 -0
- ehatool/sil.py +260 -0
- ehatool/update.py +320 -0
- ehatool/usb_discovery.py +164 -0
- ehatool/usb_discovery.toml +16 -0
- ehatool/web/__init__.py +28 -0
- ehatool/web/brt_sim.py +130 -0
- ehatool/web/canbus.py +95 -0
- ehatool/web/devices.py +216 -0
- ehatool/web/experiments.py +226 -0
- ehatool/web/job_params.py +208 -0
- ehatool/web/layout.toml +61 -0
- ehatool/web/longrun.py +430 -0
- ehatool/web/polling.py +41 -0
- ehatool/web/recipes/bench.toml +18 -0
- ehatool/web/records.py +333 -0
- ehatool/web/requests.py +254 -0
- ehatool/web/server.py +1051 -0
- ehatool/web/session.py +1552 -0
- ehatool/web/static/THIRD_PARTY_LICENSES.txt +133 -0
- ehatool/web/static/assets/ehapower-logo-Cr84XeX3.png +0 -0
- ehatool/web/static/assets/ehapower-mark-Con0OjPM.png +0 -0
- ehatool/web/static/assets/index-CdLvwKa-.css +1 -0
- ehatool/web/static/assets/index-DiLUQD1i.js +134 -0
- ehatool/web/static/index.html +16 -0
- ehatool/web/streams.py +64 -0
- ehatool/web/workspaces.py +124 -0
- ehatool/workflows/__init__.py +4 -0
- ehatool/workflows/cancheck.py +394 -0
- ehatool/workflows/cancheck.toml +9 -0
- ehatool/workflows/checks.py +43 -0
- ehatool/workflows/usbcheck.py +212 -0
- ehatool-6.0.0.dev0.dist-info/METADATA +38 -0
- ehatool-6.0.0.dev0.dist-info/RECORD +77 -0
- ehatool-6.0.0.dev0.dist-info/WHEEL +5 -0
- ehatool-6.0.0.dev0.dist-info/entry_points.txt +2 -0
- ehatool-6.0.0.dev0.dist-info/licenses/LICENSE +21 -0
- ehatool-6.0.0.dev0.dist-info/top_level.txt +1 -0
ehatool/info.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# Copyright The eha-firmware Contributors
|
|
2
|
+
|
|
3
|
+
## @file
|
|
4
|
+
## @brief 采集一次 info 查询的数据,再由同一份记录生成终端显示或 JSON。
|
|
5
|
+
|
|
6
|
+
import pathlib
|
|
7
|
+
import tomllib
|
|
8
|
+
|
|
9
|
+
from .cia402 import state_name
|
|
10
|
+
from .diagnostics import decode_error_code
|
|
11
|
+
from .link import CHANNEL_NAMES, DEVELOPER_SIGNATURE, OWNER_LOST
|
|
12
|
+
from .update import SLOT_NAMES
|
|
13
|
+
|
|
14
|
+
with (pathlib.Path(__file__).with_suffix(".toml")).open("rb") as _source:
|
|
15
|
+
DISPLAY = tomllib.load(_source)
|
|
16
|
+
|
|
17
|
+
INFO_OBJECTS = (
|
|
18
|
+
"statusword", "error_code", "product_code", "revision", "dc_link_voltage_mv",
|
|
19
|
+
"firmware_slot", "firmware_boot_confirmed", "firmware_debug", "firmware_confirmed",
|
|
20
|
+
"firmware_slot_a_version", "firmware_slot_b_version", "developer_mode", "control_owner",
|
|
21
|
+
"params_source", "params_loaded", "control_load_pct", "core1_load_pct", "drive_cycle_max_us",
|
|
22
|
+
"core1_loop_max_us", "chip_temperature_c", "reset_cause", "can_error_state",
|
|
23
|
+
"sync0_missed_count", "ipc_read_failures", "core1_init_error", "core1_watchdog_reset",
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## @brief 按顺序各读一次设备型号可见的对象;这些独立对象读取不构成设备的原子状态快照。
|
|
28
|
+
## 型号没有的对象(如分体型号的 Core 1、SYNC0 诊断)不读,记录中也没有这一项。
|
|
29
|
+
def read_info(dev):
|
|
30
|
+
values = {name: dev.read(name) for name in INFO_OBJECTS if name in dev.od.by_name}
|
|
31
|
+
owner = values["control_owner"]
|
|
32
|
+
return {
|
|
33
|
+
"kind": "device_info",
|
|
34
|
+
"objects": values,
|
|
35
|
+
"state": state_name(values["statusword"]),
|
|
36
|
+
"diagnostic": decode_error_code(dev.od, values["error_code"]),
|
|
37
|
+
"developer_enabled": values["developer_mode"] == DEVELOPER_SIGNATURE,
|
|
38
|
+
"control_owner": {"channel": owner & 0x7F, "lost": bool(owner & OWNER_LOST)},
|
|
39
|
+
# 位名取自对象字典(bits),工具不另存一份。
|
|
40
|
+
"bits": {name: dev.od.by_name[name].get("bits", []) for name in ("reset_cause", "can_error_state")
|
|
41
|
+
if name in dev.od.by_name},
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
## @brief 型号没有的对象在终端显示中占位为"—",不冒充数值。
|
|
46
|
+
class _Absent:
|
|
47
|
+
def __format__(self, spec):
|
|
48
|
+
return "—"
|
|
49
|
+
|
|
50
|
+
def __bool__(self):
|
|
51
|
+
return False
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## @brief 置位的位名(names 为对象字典的 bits 列表),没有名称的位保留编号。
|
|
55
|
+
def bit_names(value, names):
|
|
56
|
+
return [names[bit] if bit < len(names) and names[bit] else DISPLAY["text"]["unknown_bit"].format(bit=bit)
|
|
57
|
+
for bit in range(32) if value & (1 << bit)]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def version_text(value):
|
|
61
|
+
if not value:
|
|
62
|
+
return DISPLAY["text"]["unbootable"]
|
|
63
|
+
return f"{value >> 16}.{(value >> 8) & 0xFF}.{value & 0xFF}"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
## @brief 只呈现已采集记录,不访问设备;Rich 负责窄终端换行与中文对齐。
|
|
67
|
+
def print_info(info, out):
|
|
68
|
+
from rich.console import Console
|
|
69
|
+
from rich.table import Table
|
|
70
|
+
from rich.text import Text
|
|
71
|
+
|
|
72
|
+
from .output import render_diagnostic
|
|
73
|
+
|
|
74
|
+
values = {name: _Absent() for name in INFO_OBJECTS} | info["objects"]
|
|
75
|
+
labels, text = DISPLAY["labels"], DISPLAY["text"]
|
|
76
|
+
console = Console(file=out, markup=False, highlight=False)
|
|
77
|
+
console.print(Text(text["summary"].format(
|
|
78
|
+
**values, state=info["state"], dc_link_voltage_v=values["dc_link_voltage_mv"] / 1000)))
|
|
79
|
+
if values["error_code"]:
|
|
80
|
+
console.print(Text(render_diagnostic(info["diagnostic"])))
|
|
81
|
+
|
|
82
|
+
owner = info["control_owner"]
|
|
83
|
+
channel = CHANNEL_NAMES.get(owner["channel"], str(owner["channel"]))
|
|
84
|
+
owner_text = channel
|
|
85
|
+
if owner["channel"]:
|
|
86
|
+
owner_text = text["owner"].format(channel=channel, status=text["lost"] if owner["lost"] else text["online"])
|
|
87
|
+
if owner["lost"]:
|
|
88
|
+
owner_text += text["takeover"]
|
|
89
|
+
slot = SLOT_NAMES.get(values["firmware_slot"], values["firmware_slot"])
|
|
90
|
+
boot_slot = values["firmware_boot_confirmed"]
|
|
91
|
+
boot_confirmed = text["no_confirmed_slot"] if boot_slot == 255 else SLOT_NAMES.get(boot_slot, boot_slot)
|
|
92
|
+
source = DISPLAY["params"].get(str(values["params_source"]), str(values["params_source"]))
|
|
93
|
+
bits = info.get("bits", {})
|
|
94
|
+
flags = (bit_names(values["can_error_state"] & 0xFFFF0000, bits.get("can_error_state", []))
|
|
95
|
+
if isinstance(values["can_error_state"], int) else [])
|
|
96
|
+
reset = text["reset"].format(
|
|
97
|
+
**values, causes=(text["separator"].join(bit_names(values["reset_cause"], bits.get("reset_cause", []))) or "—")
|
|
98
|
+
if isinstance(values["reset_cause"], int) else "—",
|
|
99
|
+
can_tx_errors=values["can_error_state"] & 0xFF,
|
|
100
|
+
can_rx_errors=(values["can_error_state"] >> 8) & 0xFF,
|
|
101
|
+
can_flags=text["can_flags"].format(flags=text["separator"].join(flags)) if flags else "")
|
|
102
|
+
table = Table.grid(padding=(0, 2))
|
|
103
|
+
table.add_column(no_wrap=True, overflow="fold")
|
|
104
|
+
table.add_column(overflow="fold")
|
|
105
|
+
rows = (
|
|
106
|
+
("firmware", text["firmware"].format(
|
|
107
|
+
build=text["debug"] if values["firmware_debug"] else text["release"], slot=slot,
|
|
108
|
+
confirmation=text["confirmed"] if values["firmware_confirmed"] else text["trial"],
|
|
109
|
+
boot_confirmed=boot_confirmed, slot_a_version=version_text(values["firmware_slot_a_version"]),
|
|
110
|
+
slot_b_version=version_text(values["firmware_slot_b_version"]))),
|
|
111
|
+
("developer", text["developer_on"] if info["developer_enabled"] else text["developer_off"]),
|
|
112
|
+
("owner", owner_text),
|
|
113
|
+
("params", source.format(**values)),
|
|
114
|
+
("diagnostics", text["diagnostics"].format(**values)),
|
|
115
|
+
("reset", reset),
|
|
116
|
+
)
|
|
117
|
+
for name, value in rows:
|
|
118
|
+
table.add_row(Text(labels[name]), Text(value))
|
|
119
|
+
if values["core1_init_error"]:
|
|
120
|
+
table.add_row(Text(labels["core1_error"]), Text(text["core1_error"].format(**values)))
|
|
121
|
+
console.print(table)
|
ehatool/info.toml
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# ehatool info 的显示文案;设备对象与故障说明继续来自 eha_od.toml。
|
|
2
|
+
|
|
3
|
+
[labels]
|
|
4
|
+
firmware = "固件"
|
|
5
|
+
developer = "开发者模式"
|
|
6
|
+
owner = "控制属主"
|
|
7
|
+
params = "参数"
|
|
8
|
+
diagnostics = "诊断"
|
|
9
|
+
reset = "复位与通信"
|
|
10
|
+
core1_error = "Core 1 初始化失败"
|
|
11
|
+
|
|
12
|
+
[text]
|
|
13
|
+
summary = "产品 0x{product_code:08X} 修订 0x{revision:08X} 状态 {state}(0x{statusword:04X}) 故障 0x{error_code:04X} 母线 {dc_link_voltage_v:.1f} V"
|
|
14
|
+
firmware = "{build}版本,运行槽 {slot}({confirmation}),已确认槽 {boot_confirmed};槽 A {slot_a_version},槽 B {slot_b_version}"
|
|
15
|
+
developer_on = "开启(复位后回到用户模式)"
|
|
16
|
+
developer_off = "关闭(用户模式;ehatool dev on 进入)"
|
|
17
|
+
debug = "调试"
|
|
18
|
+
release = "发布"
|
|
19
|
+
confirmed = "已确认"
|
|
20
|
+
trial = "试运行中,正常运行 10 s 后确认"
|
|
21
|
+
no_confirmed_slot = "无(槽尾没有已确认的镜像)"
|
|
22
|
+
unbootable = "不可启动"
|
|
23
|
+
owner = "{channel}({status})"
|
|
24
|
+
online = "在线"
|
|
25
|
+
lost = "已失联"
|
|
26
|
+
takeover = ",其他通道可以接管"
|
|
27
|
+
diagnostics = "负载 EHA 控制 {control_load_pct:.1f}%、Core 1 {core1_load_pct:.1f}%;单周期最长执行 驱动任务 {drive_cycle_max_us} µs、Core 1 {core1_loop_max_us} µs(周期 1000 µs);芯片温度 {chip_temperature_c:.1f} °C"
|
|
28
|
+
reset = "{causes}(0x{reset_cause:08X});SYNC0 超时 {sync0_missed_count} 次,共享块读取失败 {ipc_read_failures} 次;对外 CAN 错误计数 发送 {can_tx_errors}、接收 {can_rx_errors}{can_flags}"
|
|
29
|
+
can_flags = ",{flags}"
|
|
30
|
+
unknown_bit = "位 {bit}"
|
|
31
|
+
separator = "、"
|
|
32
|
+
core1_error = "0x{core1_init_error:X}(位定义见对象字典 0x2045),电机不能使能"
|
|
33
|
+
|
|
34
|
+
[params]
|
|
35
|
+
0 = "出厂默认(没有已保存的参数)"
|
|
36
|
+
1 = "已保存参数(加载 {params_loaded} 项)"
|
|
37
|
+
2 = "参数区不可用,按出厂默认运行"
|
|
38
|
+
3 = "已恢复出厂,复位后生效"
|
|
39
|
+
4 = "已保存参数与本固件格式不符(对象含义已变),按出厂默认运行,须重新配置并保存"
|
|
40
|
+
5 = "已保存参数,但部分对象类型不符或超出当前范围,这些对象取出厂默认(加载 {params_loaded} 项),须核对后重新保存"
|
|
41
|
+
|
ehatool/install_host.py
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# Copyright The eha-firmware Contributors
|
|
2
|
+
|
|
3
|
+
## @file
|
|
4
|
+
## @brief 一次准备宿主虚拟环境与平台通信依赖,可直接用 Python 3.11+ 执行。
|
|
5
|
+
|
|
6
|
+
import argparse
|
|
7
|
+
import contextlib
|
|
8
|
+
import ctypes
|
|
9
|
+
import ctypes.util
|
|
10
|
+
from email.parser import BytesParser
|
|
11
|
+
import hashlib
|
|
12
|
+
import io
|
|
13
|
+
import os
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
import shutil
|
|
16
|
+
import subprocess
|
|
17
|
+
import sys
|
|
18
|
+
import tarfile
|
|
19
|
+
import tempfile
|
|
20
|
+
import tomllib
|
|
21
|
+
import urllib.request
|
|
22
|
+
import venv
|
|
23
|
+
import zipfile
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
with Path(__file__).with_suffix(".toml").open("rb") as source:
|
|
27
|
+
SETTINGS = tomllib.load(source)
|
|
28
|
+
MESSAGES = SETTINGS["messages"]
|
|
29
|
+
PCBUSB = SETTINGS["pcbusb"]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@contextlib.contextmanager
|
|
33
|
+
def installation_lock(path):
|
|
34
|
+
"""同一环境或同一原生库安装目录同时只运行一个安装器。"""
|
|
35
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
36
|
+
with path.open("a+b") as handle:
|
|
37
|
+
try:
|
|
38
|
+
if os.name == "nt":
|
|
39
|
+
import msvcrt
|
|
40
|
+
|
|
41
|
+
handle.write(b"\0")
|
|
42
|
+
handle.flush()
|
|
43
|
+
handle.seek(0)
|
|
44
|
+
msvcrt.locking(handle.fileno(), msvcrt.LK_NBLCK, 1)
|
|
45
|
+
else:
|
|
46
|
+
import fcntl
|
|
47
|
+
|
|
48
|
+
fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
49
|
+
except OSError as error:
|
|
50
|
+
raise RuntimeError(MESSAGES["busy"].format(path=path)) from error
|
|
51
|
+
try:
|
|
52
|
+
yield
|
|
53
|
+
finally:
|
|
54
|
+
if os.name == "nt":
|
|
55
|
+
handle.seek(0)
|
|
56
|
+
msvcrt.locking(handle.fileno(), msvcrt.LK_UNLCK, 1)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _pcbusb_available():
|
|
60
|
+
path = ctypes.util.find_library("PCBUSB")
|
|
61
|
+
if path:
|
|
62
|
+
try:
|
|
63
|
+
library = ctypes.CDLL(path)
|
|
64
|
+
library.CAN_Initialize
|
|
65
|
+
library.CAN_GetValue
|
|
66
|
+
return True
|
|
67
|
+
except (AttributeError, OSError):
|
|
68
|
+
pass
|
|
69
|
+
return False
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _download_release():
|
|
73
|
+
request = urllib.request.Request(PCBUSB["url"], headers={"User-Agent": "ehatool-host-installer"})
|
|
74
|
+
with urllib.request.urlopen(request, timeout=30) as response:
|
|
75
|
+
data = response.read(2_000_001)
|
|
76
|
+
if len(data) > 2_000_000 or hashlib.sha256(data).hexdigest() != PCBUSB["sha256"]:
|
|
77
|
+
raise RuntimeError(MESSAGES["integrity"])
|
|
78
|
+
return data
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _extract_release(data, destination):
|
|
82
|
+
"""固定发布包仅含普通文件和目录;拒绝越出发布根目录的条目。"""
|
|
83
|
+
with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as archive:
|
|
84
|
+
for member in archive:
|
|
85
|
+
parts = Path(member.name).parts
|
|
86
|
+
if not parts or parts[0] != "PCBUSB" or ".." in parts or not (member.isfile() or member.isdir()):
|
|
87
|
+
raise RuntimeError(MESSAGES["archive"])
|
|
88
|
+
path = destination / member.name
|
|
89
|
+
if member.isdir():
|
|
90
|
+
path.mkdir(parents=True, exist_ok=True)
|
|
91
|
+
else:
|
|
92
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
93
|
+
with archive.extractfile(member) as incoming, path.open("wb") as outgoing:
|
|
94
|
+
shutil.copyfileobj(incoming, outgoing)
|
|
95
|
+
for required in (PCBUSB["library"], "LICENSE", "COPYRIGHT", "README", "install.txt"):
|
|
96
|
+
if not (destination / "PCBUSB" / required).is_file():
|
|
97
|
+
raise RuntimeError(MESSAGES["archive"])
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _managed_release(release):
|
|
101
|
+
"""只替换带原始固定发行包且没有额外文件的本工具安装目录。"""
|
|
102
|
+
archive_path = release / PCBUSB["archive"]
|
|
103
|
+
if release.is_symlink() or not archive_path.is_file():
|
|
104
|
+
return False
|
|
105
|
+
data = archive_path.read_bytes()
|
|
106
|
+
if hashlib.sha256(data).hexdigest() != PCBUSB["sha256"]:
|
|
107
|
+
return False
|
|
108
|
+
with tarfile.open(fileobj=io.BytesIO(data), mode="r:gz") as archive:
|
|
109
|
+
expected = {Path(member.name) for member in archive}
|
|
110
|
+
expected.add(Path(PCBUSB["archive"]))
|
|
111
|
+
expected.update(parent for path in tuple(expected) for parent in path.parents)
|
|
112
|
+
return all(not path.is_symlink() and path.relative_to(release) in expected
|
|
113
|
+
for path in release.rglob("*"))
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def ensure_pcbusb(home=None):
|
|
117
|
+
"""保留完整上游发行件,在用户库搜索路径注册 PCBUSB。"""
|
|
118
|
+
if _pcbusb_available():
|
|
119
|
+
return
|
|
120
|
+
home = Path.home() if home is None else Path(home)
|
|
121
|
+
root = home / "Library" / "Application Support" / "ehatool" / "drivers"
|
|
122
|
+
release = root / ("PCBUSB-" + PCBUSB["version"])
|
|
123
|
+
library = release / "PCBUSB" / PCBUSB["library"]
|
|
124
|
+
link = home / "lib" / "libPCBUSB.dylib"
|
|
125
|
+
with installation_lock(root / ".install.lock"):
|
|
126
|
+
if _pcbusb_available():
|
|
127
|
+
return
|
|
128
|
+
if os.path.lexists(link) and not (link.is_symlink() and link.resolve() == library):
|
|
129
|
+
raise RuntimeError(MESSAGES["link_exists"].format(path=link))
|
|
130
|
+
if release.exists() and not _managed_release(release):
|
|
131
|
+
raise RuntimeError(MESSAGES["release_exists"].format(path=release))
|
|
132
|
+
data = _download_release()
|
|
133
|
+
with tempfile.TemporaryDirectory(prefix="pcbusb-", dir=root) as temporary:
|
|
134
|
+
staging = Path(temporary) / "release"
|
|
135
|
+
staging.mkdir()
|
|
136
|
+
_extract_release(data, staging)
|
|
137
|
+
(staging / PCBUSB["archive"]).write_bytes(data)
|
|
138
|
+
# 检查架构和动态链接依赖,失败时不替换已有安装。
|
|
139
|
+
loaded = ctypes.CDLL(str(staging / "PCBUSB" / PCBUSB["library"]))
|
|
140
|
+
loaded.CAN_Initialize
|
|
141
|
+
loaded.CAN_GetValue
|
|
142
|
+
backup = Path(temporary) / "previous"
|
|
143
|
+
if release.exists():
|
|
144
|
+
release.rename(backup)
|
|
145
|
+
try:
|
|
146
|
+
staging.rename(release)
|
|
147
|
+
except BaseException:
|
|
148
|
+
if backup.exists():
|
|
149
|
+
backup.rename(release)
|
|
150
|
+
raise
|
|
151
|
+
link.parent.mkdir(parents=True, exist_ok=True)
|
|
152
|
+
if not os.path.lexists(link):
|
|
153
|
+
link.symlink_to(library)
|
|
154
|
+
if not _pcbusb_available():
|
|
155
|
+
raise RuntimeError(MESSAGES["load_failed"].format(path=library))
|
|
156
|
+
print(MESSAGES["pcbusb_ready"].format(path=release))
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _source_is_ehatool(source):
|
|
160
|
+
try:
|
|
161
|
+
if source.is_dir():
|
|
162
|
+
with (source / "pyproject.toml").open("rb") as handle:
|
|
163
|
+
return tomllib.load(handle).get("project", {}).get("name") == "ehatool"
|
|
164
|
+
if source.is_file() and source.suffix == ".whl":
|
|
165
|
+
with zipfile.ZipFile(source) as archive:
|
|
166
|
+
metadata = [name for name in archive.namelist() if name.endswith(".dist-info/METADATA")]
|
|
167
|
+
return len(metadata) == 1 and BytesParser().parsebytes(archive.read(metadata[0]))["Name"] == "ehatool"
|
|
168
|
+
except (OSError, UnicodeError, zipfile.BadZipFile, tomllib.TOMLDecodeError):
|
|
169
|
+
return False
|
|
170
|
+
return False
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _environment_status(environment, python):
|
|
174
|
+
configured = (environment / "pyvenv.cfg").is_file() and python.is_file()
|
|
175
|
+
marker = environment / ".ehatool-installing"
|
|
176
|
+
resumable = marker.is_file() and not marker.is_symlink() and marker.read_bytes() == b"ehatool-host-installer\n"
|
|
177
|
+
if not configured and not resumable and any(path.name != ".ehatool-install.lock" for path in environment.iterdir()):
|
|
178
|
+
raise RuntimeError(MESSAGES["venv_invalid"].format(path=environment))
|
|
179
|
+
return configured, resumable
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def main(argv=None):
|
|
183
|
+
checkout = Path(__file__).resolve().parents[1]
|
|
184
|
+
parser = argparse.ArgumentParser(description=MESSAGES["description"])
|
|
185
|
+
parser.add_argument("--source", type=Path, default=checkout, help=MESSAGES["source_help"])
|
|
186
|
+
parser.add_argument("--venv", type=Path, help=MESSAGES["venv_help"])
|
|
187
|
+
args = parser.parse_args(argv)
|
|
188
|
+
source = args.source.expanduser().resolve()
|
|
189
|
+
wheel = source.is_file() and source.suffix == ".whl"
|
|
190
|
+
if not _source_is_ehatool(source):
|
|
191
|
+
parser.error(MESSAGES["source_missing"])
|
|
192
|
+
environment = (args.venv or source.parent / ".venv").expanduser().resolve()
|
|
193
|
+
python = environment / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
|
|
194
|
+
try:
|
|
195
|
+
if environment.exists():
|
|
196
|
+
_environment_status(environment, python)
|
|
197
|
+
environment.mkdir(parents=True, exist_ok=True)
|
|
198
|
+
lock_path = environment / ".ehatool-install.lock"
|
|
199
|
+
with installation_lock(lock_path):
|
|
200
|
+
configured, resumable = _environment_status(environment, python)
|
|
201
|
+
marker = environment / ".ehatool-installing"
|
|
202
|
+
if not configured or resumable:
|
|
203
|
+
if not resumable:
|
|
204
|
+
marker.write_text("ehatool-host-installer\n")
|
|
205
|
+
venv.EnvBuilder(with_pip=True).create(environment)
|
|
206
|
+
marker.unlink()
|
|
207
|
+
if not (environment / "pyvenv.cfg").is_file() or not python.is_file():
|
|
208
|
+
raise RuntimeError(MESSAGES["venv_invalid"].format(path=environment))
|
|
209
|
+
install = [str(python), "-m", "pip", "install", "--upgrade"]
|
|
210
|
+
if not wheel:
|
|
211
|
+
install.append("--editable")
|
|
212
|
+
subprocess.run([*install, str(source)], check=True)
|
|
213
|
+
if sys.platform == "darwin":
|
|
214
|
+
subprocess.run([str(python), "-c", "from ehatool.install_host import ensure_pcbusb; ensure_pcbusb()"], check=True)
|
|
215
|
+
print(MESSAGES["macos_kvaser"])
|
|
216
|
+
elif sys.platform == "win32":
|
|
217
|
+
print(MESSAGES["windows_drivers"])
|
|
218
|
+
else:
|
|
219
|
+
if shutil.which("ip") is None:
|
|
220
|
+
raise RuntimeError(MESSAGES["iproute2_missing"])
|
|
221
|
+
print(MESSAGES["socketcan"])
|
|
222
|
+
subprocess.run([str(python), "-m", "pip", "check"], check=True)
|
|
223
|
+
check = """import can, serial, rich, pysoem, matplotlib, sys
|
|
224
|
+
if sys.platform in ('darwin', 'win32'):
|
|
225
|
+
import gs_usb.gs_usb
|
|
226
|
+
from ehatool.device.can import initialize_gs_usb_backend
|
|
227
|
+
initialize_gs_usb_backend()
|
|
228
|
+
if sys.platform == 'darwin':
|
|
229
|
+
from can.interfaces.pcan.basic import PCANBasic
|
|
230
|
+
PCANBasic()
|
|
231
|
+
"""
|
|
232
|
+
subprocess.run([str(python), "-c", check], check=True)
|
|
233
|
+
command = python.parent / ("ehatool.exe" if os.name == "nt" else "ehatool")
|
|
234
|
+
subprocess.run([str(command), "--help"], check=True, stdout=subprocess.DEVNULL)
|
|
235
|
+
except (AttributeError, OSError, RuntimeError, subprocess.CalledProcessError, tarfile.TarError) as error:
|
|
236
|
+
print(MESSAGES["failed"].format(reason=error), file=sys.stderr)
|
|
237
|
+
return 1
|
|
238
|
+
print(MESSAGES["ready"].format(command=python.parent / ("ehatool.exe" if os.name == "nt" else "ehatool")))
|
|
239
|
+
return 0
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
if __name__ == "__main__":
|
|
243
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# 宿主安装的外部发行件和诊断,与设备固件安装分开。
|
|
2
|
+
[pcbusb]
|
|
3
|
+
version = "0.13"
|
|
4
|
+
url = "https://github.com/mac-can/PCBUSB-Library/releases/download/v0.13/macOS_Library_for_PCANUSB_v0.13.tar.gz"
|
|
5
|
+
archive = "macOS_Library_for_PCANUSB_v0.13.tar.gz"
|
|
6
|
+
library = "libPCBUSB.0.13.dylib"
|
|
7
|
+
sha256 = "e06e5d650868b11ecb4c27f03749e61ab43825580c644ec593dd3e7ac7be0dd5"
|
|
8
|
+
|
|
9
|
+
[messages]
|
|
10
|
+
description = "一次安装 EHA 宿主工具的全部 Python 功能依赖,并准备当前平台的通信库"
|
|
11
|
+
source_help = "含 pyproject.toml 的源码包目录或 wheel 文件;默认使用本脚本所在 checkout 的 ehatool 目录"
|
|
12
|
+
venv_help = "安装虚拟环境;默认使用源码包上级的 .venv"
|
|
13
|
+
source_missing = "未找到源码包或 wheel,请通过 --source 指定 checkout 中的 ehatool 目录或 .whl 文件"
|
|
14
|
+
venv_invalid = "目录 {path} 已存在,但没有可运行的虚拟环境 Python"
|
|
15
|
+
busy = "另一个安装器正在使用 {path},请在该安装完成后重试"
|
|
16
|
+
integrity = "PCBUSB 发布包大小或 SHA-256 与固定版本不符"
|
|
17
|
+
archive = "PCBUSB 发布包缺少所需文件,或包含不支持的归档路径与文件类型"
|
|
18
|
+
link_exists = "已有用户库 {path} 无法加载,请检查现有安装后重试"
|
|
19
|
+
release_exists = "目录 {path} 包含已有文件且无法确认为本工具的完整安装,请检查该目录后重试"
|
|
20
|
+
load_failed = "PCBUSB 已保存到 {path},当前 Python 仍无法通过用户库搜索路径加载它"
|
|
21
|
+
pcbusb_ready = "PCBUSB 已就绪,完整发行件及许可保存在 {path}"
|
|
22
|
+
macos_kvaser = "macOS 的 Kvaser 原生库与当前 Python 后端接口尚未接通;该后端需要另行实现"
|
|
23
|
+
windows_drivers = "Windows 的 PCAN、Kvaser 原生驱动及 gs_usb 的 WinUSB 绑定须由对应厂商安装器准备"
|
|
24
|
+
socketcan = "Linux CAN 使用内核 SocketCAN;当前系统需提供对应设备的内核驱动和接口权限"
|
|
25
|
+
iproute2_missing = "未找到 Linux 的 ip 命令,请先通过系统包管理器安装 iproute2,再运行宿主安装器"
|
|
26
|
+
failed = "宿主安装未完成({reason})"
|
|
27
|
+
ready = "Python 功能依赖安装完成,可执行 {command} devices"
|