把 gui/gui_core.py 中 12 个独立顶层函数 + RunParams dataclass 拆出到 gui/gui_core_pkg/ 子包;Runner 巨型类(1164 行 / 13 实例方法 / 用 self 状态)原文件保留,仅做 import re-export。 新建子包: - adb (79) find_adb_executable / list_adb_devices / resolve_adb_device - wifi_adb (323) 7 个 WiFi-ADB 函数 - config (46) CONFIG_PATH / read_config / write_config - run_params (45) RunParams dataclass 策略:Runner 类内部用了 find_adb_executable 等顶层函数,所以原文件 ``gui/gui_core.py`` 通过 ``from gui.gui_core_pkg import *`` re-export 14 个公共符号供 Runner 内部和外部代码继续使用。 外部 import 路径全部不变: ``from gui.gui_core import find_adb_executable / RunParams / Runner`` 等 所有 14 个公共符号都保留。 - find_adb_executable 20 处外部引用 / read_config 17 处 / list_adb_devices 16 处 / 其他 1-7 处,全部不需修改 回归: - 5 个新文件 + 改写后 gui_core.py compileall 通过 - pytest 353 passed / 1 skipped - find_adb_executable() 调用正常返回 adb.EXE 绝对路径 - read_config() 读到 126 个 config keys - RunParams.is_dataclass = True,28 个字段 - Runner 类正确暴露 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
80 lines
2.3 KiB
Python
80 lines
2.3 KiB
Python
"""gui_core_pkg.adb - ADB 可执行文件查找与设备列表。
|
||
|
||
- find_adb_executable 在 PATH / 项目目录内查找 adb 可执行
|
||
- list_adb_devices 列出当前已连接设备
|
||
- resolve_adb_device 按 device_id 解析具体设备(含模糊匹配)
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import shutil
|
||
import subprocess
|
||
from typing import Optional
|
||
|
||
|
||
def find_adb_executable() -> str:
|
||
"""返回可用的 adb 可执行文件路径(优先 PATH,其次默认安装目录)。"""
|
||
adb = shutil.which("adb")
|
||
if adb:
|
||
return adb
|
||
|
||
# 默认安装目录(我们之前下载的 platform-tools)
|
||
localapp = os.environ.get("LOCALAPPDATA") or ""
|
||
candidates = []
|
||
if localapp:
|
||
candidates.append(os.path.join(localapp, "platform-tools", "adb.exe"))
|
||
|
||
# 兼容某些人手动放到用户目录的情况
|
||
userprofile = os.environ.get("USERPROFILE") or ""
|
||
if userprofile:
|
||
candidates.append(
|
||
os.path.join(userprofile, "AppData", "Local", "platform-tools", "adb.exe")
|
||
)
|
||
|
||
for p in candidates:
|
||
if os.path.exists(p):
|
||
return p
|
||
return ""
|
||
|
||
|
||
|
||
|
||
def list_adb_devices(adb_exe: str) -> list[str]:
|
||
try:
|
||
if not adb_exe:
|
||
return []
|
||
proc = subprocess.run(
|
||
[adb_exe, "devices", "-l"],
|
||
capture_output=True,
|
||
text=True,
|
||
encoding="utf-8",
|
||
errors="replace",
|
||
check=False,
|
||
)
|
||
except FileNotFoundError:
|
||
return []
|
||
devices: list[str] = []
|
||
for line in (proc.stdout or "").splitlines()[1:]:
|
||
parts = line.split()
|
||
if len(parts) >= 2 and parts[1] == "device":
|
||
devices.append(parts[0])
|
||
return devices
|
||
|
||
|
||
|
||
|
||
def resolve_adb_device(adb_exe: str, requested_device_id: str = "") -> str:
|
||
device_id = str(requested_device_id or "").strip()
|
||
devices = list_adb_devices(adb_exe)
|
||
if device_id:
|
||
if device_id in devices:
|
||
return device_id
|
||
raise RuntimeError(f"指定的 device_id '{device_id}' 当前不可用")
|
||
if len(devices) == 1:
|
||
return devices[0]
|
||
if len(devices) > 1:
|
||
raise RuntimeError(f"检测到多台 adb 设备,请先明确选择 device_id: {', '.join(devices)}")
|
||
raise RuntimeError("未检测到可用的 adb 设备")
|
||
|
||
|