This commit is contained in:
1 parent
60332db82b
commit
16019ee649
4 files changed
+120
-10
No files matched your search
+70
-4
@@ -13,6 +13,10 @@ from typing import Optional
|
||||
|
||||
from douyin.core import path_config
|
||||
|
||||
DEFAULT_ADB_SERVER_PORT = "5037"
|
||||
FALLBACK_ADB_SERVER_PORTS = ("5038", "5039", "5040")
|
||||
_ADB_SERVER_PROBED_FOR: set[str] = set()
|
||||
|
||||
|
||||
def _normalize_adb_path(adb: str | None) -> str:
|
||||
value = str(adb or "").strip()
|
||||
@@ -20,19 +24,80 @@ def _normalize_adb_path(adb: str | None) -> str:
|
||||
value = value[1:-1]
|
||||
return value
|
||||
|
||||
|
||||
def _adb_env(port: str | None = None) -> dict[str, str]:
|
||||
env = os.environ.copy()
|
||||
if port:
|
||||
env["ANDROID_ADB_SERVER_PORT"] = str(port)
|
||||
# Windows 上 adb 的 Bonjour backend 经常没有 daemon;openscreen 更稳定。
|
||||
env.setdefault("ADB_MDNS_OPENSCREEN", "1")
|
||||
return env
|
||||
|
||||
|
||||
def _adb_server_responds(adb_exe: str, port: str, timeout: float = 4.0) -> bool:
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
[adb_exe, "devices"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
check=False,
|
||||
timeout=timeout,
|
||||
env=_adb_env(port),
|
||||
)
|
||||
except Exception:
|
||||
return False
|
||||
out = ((proc.stdout or "") + "\n" + (proc.stderr or "")).lower()
|
||||
if proc.returncode != 0:
|
||||
return False
|
||||
return "failed to start daemon" not in out and "cannot connect to daemon" not in out
|
||||
|
||||
|
||||
def ensure_adb_server_port(adb_exe: str) -> None:
|
||||
"""Ensure later adb subprocesses inherit a working server port.
|
||||
|
||||
On some Windows machines the default 5037 daemon fails to start while a
|
||||
fallback port works. The GUI gets the adb path once and every later
|
||||
operation inherits this process environment, so doing the probe here keeps
|
||||
dashboard / legacy GUI / web GUI consistent.
|
||||
"""
|
||||
adb_path = os.path.abspath(str(adb_exe or ""))
|
||||
if not adb_path or adb_path in _ADB_SERVER_PROBED_FOR:
|
||||
return
|
||||
_ADB_SERVER_PROBED_FOR.add(adb_path)
|
||||
|
||||
os.environ.setdefault("ADB_MDNS_OPENSCREEN", "1")
|
||||
if os.environ.get("ANDROID_ADB_SERVER_PORT"):
|
||||
return
|
||||
|
||||
if _adb_server_responds(adb_path, DEFAULT_ADB_SERVER_PORT):
|
||||
return
|
||||
for port in FALLBACK_ADB_SERVER_PORTS:
|
||||
if _adb_server_responds(adb_path, port):
|
||||
os.environ["ANDROID_ADB_SERVER_PORT"] = port
|
||||
return
|
||||
|
||||
|
||||
def _prepare_adb_path(adb: str) -> str:
|
||||
path = os.path.abspath(adb)
|
||||
ensure_adb_server_port(path)
|
||||
return path
|
||||
|
||||
|
||||
def find_adb_executable() -> str:
|
||||
"""返回可用的 adb 可执行文件路径。"""
|
||||
env_adb = _normalize_adb_path(os.environ.get("ADB_EXECUTABLE"))
|
||||
if env_adb and os.path.exists(env_adb):
|
||||
return os.path.abspath(env_adb)
|
||||
return _prepare_adb_path(env_adb)
|
||||
|
||||
bundled_adb = path_config.find_bundled_adb()
|
||||
if bundled_adb:
|
||||
return bundled_adb
|
||||
return _prepare_adb_path(bundled_adb)
|
||||
|
||||
adb = shutil.which("adb")
|
||||
if adb:
|
||||
return adb
|
||||
return _prepare_adb_path(adb)
|
||||
|
||||
# 默认安装目录(本机开发/打包机常用 platform-tools 位置)
|
||||
localapp = os.environ.get("LOCALAPPDATA") or ""
|
||||
@@ -49,7 +114,7 @@ def find_adb_executable() -> str:
|
||||
|
||||
for p in candidates:
|
||||
if os.path.exists(p):
|
||||
return os.path.abspath(p)
|
||||
return _prepare_adb_path(p)
|
||||
return ""
|
||||
|
||||
|
||||
@@ -66,6 +131,7 @@ def list_adb_devices(adb_exe: str) -> list[str]:
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
check=False,
|
||||
env=_adb_env(),
|
||||
)
|
||||
except FileNotFoundError:
|
||||
return []
|
||||
|
||||
@@ -16,7 +16,7 @@ import subprocess
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
from .adb import find_adb_executable, list_adb_devices
|
||||
from .adb import _adb_env, find_adb_executable, list_adb_devices
|
||||
|
||||
|
||||
def _discover_mdns_connect_targets(adb_exe: str) -> list[tuple[str, int]]:
|
||||
@@ -38,6 +38,7 @@ def _connect_wifi_adb_once(adb_exe: str, target: str) -> tuple[bool, str, str]:
|
||||
errors="replace",
|
||||
check=False,
|
||||
timeout=10,
|
||||
env=_adb_env(),
|
||||
)
|
||||
|
||||
output = ((proc.stdout or "") + "\n" + (proc.stderr or "")).strip()
|
||||
@@ -142,6 +143,7 @@ def enable_wifi_adb(adb_exe: str, device_id: str) -> tuple[bool, str]:
|
||||
errors="replace",
|
||||
check=False,
|
||||
timeout=10,
|
||||
env=_adb_env(),
|
||||
)
|
||||
|
||||
if proc.returncode == 0:
|
||||
@@ -241,6 +243,7 @@ def disconnect_wifi_adb(
|
||||
errors="replace",
|
||||
check=False,
|
||||
timeout=10,
|
||||
env=_adb_env(),
|
||||
)
|
||||
|
||||
output = (proc.stdout or "") + "\n" + (proc.stderr or "")
|
||||
@@ -281,6 +284,7 @@ def get_device_ip(adb_exe: str, device_id: str) -> tuple[bool, str]:
|
||||
errors="replace",
|
||||
check=False,
|
||||
timeout=10,
|
||||
env=_adb_env(),
|
||||
)
|
||||
except subprocess.TimeoutExpired:
|
||||
return False, "获取设备IP超时"
|
||||
@@ -315,6 +319,7 @@ def get_device_ip(adb_exe: str, device_id: str) -> tuple[bool, str]:
|
||||
errors="replace",
|
||||
check=False,
|
||||
timeout=10,
|
||||
env=_adb_env(),
|
||||
)
|
||||
except Exception:
|
||||
proc2 = None
|
||||
@@ -352,6 +357,7 @@ def get_device_ip(adb_exe: str, device_id: str) -> tuple[bool, str]:
|
||||
errors="replace",
|
||||
check=False,
|
||||
timeout=10,
|
||||
env=_adb_env(),
|
||||
)
|
||||
if proc3.returncode == 0:
|
||||
m = re.search(r"inet\s+addr:?\s*(\d+\.\d+\.\d+\.\d+)", proc3.stdout or "")
|
||||
|
||||
@@ -17,6 +17,8 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from dataclasses import dataclass
|
||||
from typing import Callable, Iterable, Optional
|
||||
|
||||
from .adb import _adb_env
|
||||
|
||||
|
||||
DEFAULT_ADB_PORT = 5555
|
||||
HI_PORT_MIN = 37000
|
||||
@@ -665,10 +667,6 @@ def scan_open_ports(
|
||||
|
||||
def _run_adb(adb_exe: str, args: list[str], timeout: float = 5.0) -> tuple[int, str]:
|
||||
"""运行 adb 命令,返回 (returncode, stdout+stderr)。"""
|
||||
env = os.environ.copy()
|
||||
# Windows 常见:Bonjour backend 无 daemon,mDNS 发现始终为空。
|
||||
# 默认强制 openscreen,除非用户显式覆盖。
|
||||
env.setdefault("ADB_MDNS_OPENSCREEN", "1")
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
[adb_exe, *args],
|
||||
@@ -678,7 +676,7 @@ def _run_adb(adb_exe: str, args: list[str], timeout: float = 5.0) -> tuple[int,
|
||||
errors="replace",
|
||||
check=False,
|
||||
timeout=timeout,
|
||||
env=env,
|
||||
env=_adb_env(),
|
||||
)
|
||||
except subprocess.TimeoutExpired:
|
||||
return -1, "timeout"
|
||||
|
||||
Reference in new issue
Block a user