Fix device selection and adb command safety

This commit is contained in:
Codex committed 2026-05-20 18:43:58 +08:00
1 parent 89128deb0f
commit e7a7d3f888
9 files changed
+454 -223

No files matched your search

+30 -11
View File
@@ -66,9 +66,26 @@ def list_adb_devices(adb_exe: str) -> list[str]:
)
except FileNotFoundError:
return []
text = (proc.stdout or "") + "\n" + (proc.stderr or "")
pattern = r"(\b(?:[0-9]{1,3}(?:\.[0-9]{1,3}){3}(?::[0-9]+)?|[A-Za-z0-9]{8,})\b)\s*device\b"
return re.findall(pattern, text)
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 设备")
def read_config() -> dict:
@@ -994,11 +1011,11 @@ class ProcRunner:
self._append_log("[错误] 未找到 adb.exe")
return False
if not device_id:
devices = list_adb_devices(adb)
if not devices:
self._append_log("[错误] 未找到可用设备")
try:
device_id = resolve_adb_device(adb)
except Exception as e:
self._append_log(f"[错误] 设备选择失败: {e}")
return False
device_id = devices[0]
proc = subprocess.run(
[adb, "-s", device_id, "shell", "input", "keyevent", "4"],
capture_output=True,
@@ -1268,8 +1285,8 @@ class ProcRunner:
"--test-yanghao-flow-seconds",
str(seconds),
]
if device_id:
cmd += ["--device-id", device_id]
device_id = resolve_adb_device(find_adb_executable(), device_id)
cmd += ["--device-id", device_id]
self._start_process(cmd)
@@ -1292,8 +1309,8 @@ class ProcRunner:
]
if text:
cmd += ["--test-comment-text", text]
if device_id:
cmd += ["--device-id", device_id]
device_id = resolve_adb_device(find_adb_executable(), device_id)
cmd += ["--device-id", device_id]
self._start_process(cmd)
@@ -1321,6 +1338,8 @@ class ProcRunner:
return default
device_id = str(_pick("device_id", "") or "").strip()
adb = find_adb_executable()
device_id = resolve_adb_device(adb, device_id)
y_pianyi = int(_pick("y_pianyi", 0) or 0)
needswitch = bool(_pick("needswitch", False))
reset_after_swipes_enabled = bool(_pick("reset_after_swipes_enabled", False))