调整逻辑,修复bug。
This commit is contained in:
@@ -36,7 +36,7 @@ class Deviceinfo(object):
|
||||
# 监听设备连接(死循环,内部捕获异常)
|
||||
# ----------------------------
|
||||
def startDeviceListener(self):
|
||||
LogManager.info("Device Listener started", "listener")
|
||||
# LogManager.info("Device Listener started", "listener")
|
||||
while True:
|
||||
try:
|
||||
lists = Usbmux().device_list()
|
||||
@@ -58,7 +58,6 @@ class Deviceinfo(object):
|
||||
|
||||
# 拔出设备处理
|
||||
self._removeDisconnected(lists)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
# ----------------------------
|
||||
@@ -140,9 +139,8 @@ class Deviceinfo(object):
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
# ----------------------------
|
||||
# 路径:打包/源码都能找到根目录、iproxy 目录和 iproxy 可执行文件
|
||||
# 根目录与 iproxy 可执行文件定位
|
||||
# ----------------------------
|
||||
def _base_dir(self) -> Path:
|
||||
"""
|
||||
@@ -151,55 +149,82 @@ class Deviceinfo(object):
|
||||
"""
|
||||
if getattr(sys, "frozen", False):
|
||||
return Path(sys.executable).resolve().parent
|
||||
return Path(__file__).resolve().parents[1] # iOSAI/ 作为根
|
||||
|
||||
def _iproxy_dir(self) -> Path:
|
||||
"""返回打包后的 iproxy 目录(你现在放在 Module/iproxy/)"""
|
||||
return self._base_dir() / "resources" / "iproxy"
|
||||
return Path(__file__).resolve().parents[1] # iOSAI/ 作为根
|
||||
|
||||
def _iproxy_path(self) -> Path:
|
||||
"""返回 iproxy 可执行文件的完整路径"""
|
||||
exe = "iproxy.exe" if os.name == "nt" else "iproxy"
|
||||
base = self._base_dir()
|
||||
# 常见放置位置(按优先级)
|
||||
candidates = [
|
||||
base / "resources" / "iproxy", # 新推荐:resources/iproxy/
|
||||
base / "resources" / "iproxy" / exe # 推荐:打包资源目录
|
||||
]
|
||||
for p in candidates:
|
||||
if p.exists():
|
||||
print("iproxy 路径为 ", p)
|
||||
return p
|
||||
|
||||
# 打一条清晰的错误便于排查
|
||||
tried = [str(c) for c in candidates]
|
||||
raise FileNotFoundError(f"iproxy not found, tried: {tried}")
|
||||
|
||||
# ----------------------------
|
||||
# 端口映射:启动 iproxy(设置 cwd 和 PATH,隐藏窗口)
|
||||
# 端口映射:启动 iproxy
|
||||
# ----------------------------
|
||||
def relayDeviceScreenPort(self, udid: str) -> Optional[subprocess.Popen]:
|
||||
try:
|
||||
iproxy = self._iproxy_path()
|
||||
iproxy_dir = self._iproxy_dir()
|
||||
iproxy = self._iproxy_path() # 例如 .../resources/iproxy/iproxy.exe
|
||||
iproxy_dir = iproxy.parent
|
||||
|
||||
if not iproxy.exists():
|
||||
raise FileNotFoundError(f"iproxy not found: {iproxy}")
|
||||
|
||||
# 继承环境并把 iproxy 目录加入 PATH,方便 DLL 解析
|
||||
# 继承环境并把 iproxy 目录加入 PATH(放最前)
|
||||
env = os.environ.copy()
|
||||
env["PATH"] = str(iproxy_dir) + os.pathsep + env.get("PATH", "")
|
||||
|
||||
# 可选:帮助本进程解析该目录下 DLL(py3.8+)
|
||||
try:
|
||||
os.add_dll_directory(str(iproxy_dir))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Windows 隐藏子进程窗口
|
||||
CREATE_NO_WINDOW = 0x08000000 if os.name == "nt" else 0
|
||||
creationflags = 0x08000000 if os.name == "nt" else 0
|
||||
|
||||
# 绝对路径 + shell=False,避免 PATH/别名干扰
|
||||
args = [str(iproxy), "-u", udid, str(self.screenProxy), "9100"]
|
||||
|
||||
|
||||
# (可选)把子进程输出写到你的日志里,排查更方便
|
||||
def _pipe_to_log(name: str, stream):
|
||||
try:
|
||||
for line in iter(stream.readline, ''):
|
||||
line = line.rstrip('\r\n')
|
||||
if line:
|
||||
LogManager.info(f"[iproxy {name}] {line}", udid)
|
||||
except Exception:
|
||||
print("遇到错误了")
|
||||
pass
|
||||
|
||||
p = subprocess.Popen(
|
||||
["iproxy", "-u", udid, str(self.screenProxy), "9100"],
|
||||
args,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
creationflags=CREATE_NO_WINDOW,
|
||||
cwd=str(iproxy_dir), # 关键:工作目录设为 iproxy 所在目录
|
||||
env=env, # 关键:把 iproxy_dir 注入 PATH
|
||||
text=True, # 你后面如果要读 stdout/stderr 的话更方便
|
||||
cwd=str(iproxy_dir),
|
||||
env=env,
|
||||
shell=False,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
bufsize=1
|
||||
bufsize=1,
|
||||
creationflags=creationflags,
|
||||
)
|
||||
|
||||
# 启动异步日志转发(不要阻塞主线程;需要时可删除)
|
||||
try:
|
||||
import threading
|
||||
threading.Thread(target=_pipe_to_log, args=("STDOUT", p.stdout), daemon=True).start()
|
||||
threading.Thread(target=_pipe_to_log, args=("STDERR", p.stderr), daemon=True).start()
|
||||
except Exception as e:
|
||||
print("这里有错误")
|
||||
print(e)
|
||||
pass
|
||||
|
||||
LogManager.info(f"启动 iproxy 成功,本地 {self.screenProxy} -> 设备 9100", udid)
|
||||
return p
|
||||
|
||||
|
||||
Reference in New Issue
Block a user