# -*- coding: utf-8 -*- """给 --netcheck 加个保险:结果同时写一份文件。 为什么:EXE 是 --windowed 打包的,没有控制台。 我用 AllocConsole 自己开了一个,但这类做法在某些情况下 (控制台被系统回收、被杀软拦、权限受限)输出会丢, 用户就只看到一个一闪而过的黑框、什么也没看到。 所以自检结果**同时**写到「网络自检结果.txt」, wlzj.bat 跑完自动用记事本打开它 —— 不管控制台有没有显示出来, 用户总能拿到结论。 """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") RUN = ROOT / "windows" / "run_agent.py" TOOLS = ROOT / "tools" CLI_OLD = '''def cli_netcheck(seconds: int) -> int: """网络自检:监听端口,看手表的包到底有没有到这台电脑。""" attach_console() try: from lanremote import netcheck return netcheck.run_check(int(seconds or 90)) except Exception as exc: # noqa: BLE001 print(f"自检运行失败: {exc}") return 1 finally: wait_key()''' CLI_NEW = '''class _Tee: """把输出同时送到控制台和文件。""" def __init__(self, *streams): self.streams = streams def write(self, text): for stream in self.streams: try: stream.write(text) stream.flush() except Exception: # noqa: BLE001 pass def flush(self): for stream in self.streams: try: stream.flush() except Exception: # noqa: BLE001 pass def cli_netcheck(seconds: int) -> int: """网络自检:监听端口,看手表的包到底有没有到这台电脑。 EXE 是 --windowed 打包的,本来没有控制台。这里自己开一个, 同时把结果写一份到「网络自检结果.txt」—— 控制台万一没显示出来(被杀软拦、权限受限等), 用户还能从文件里看到结论。wlzj.bat 跑完会自动打开这个文件。 """ attach_console() original = sys.stdout path = app_dir() / "网络自检结果.txt" handle = None try: handle = open(path, "w", encoding="utf-8") except OSError: handle = None if handle is not None: sys.stdout = _Tee(original, handle) code = 1 try: from lanremote import netcheck code = netcheck.run_check(int(seconds or 90)) except Exception as exc: # noqa: BLE001 print(f"自检运行失败: {exc}") finally: if handle is not None: try: sys.stdout = original handle.close() except Exception: # noqa: BLE001 pass try: print() print(f"结果已保存到:{path}") except Exception: # noqa: BLE001 pass wait_key() return code''' WLZJ = r"""@echo off chcp 936 >nul title LAN Remote - Network Self Check cd /d "%~dp0" rem 找被控端 exe(脚本可能在 exe 同目录,也可能在子目录) set "EXE=%~dp0ykxmz.exe" if not exist "%EXE%" set "EXE=%~dp0..\ykxmz.exe" if not exist "%EXE%" set "EXE=%~dp0LanRemote-Agent\ykxmz.exe" if not exist "%EXE%" ( echo. echo 找不到 ykxmz.exe echo 请把这个脚本放在被控端目录里运行。 echo. pause exit /b 1 ) rem 自检逻辑已经打进 EXE,不需要系统装 Python。 rem /wait:EXE 是图形程序,不加这个 cmd 不会等它跑完。 start "" /wait "%EXE%" --netcheck 90 rem 不管控制台有没有正常显示,结果文件一定有,直接打开给用户看。 if exist "%~dp0网络自检结果.txt" ( start "" notepad "%~dp0网络自检结果.txt" ) else ( echo. echo 自检跑完了,但没找到「网络自检结果.txt」。 echo 请确认这个脚本和被控端在同一个目录。 echo. pause ) exit /b 0 """ def main() -> int: text = RUN.read_text(encoding="utf-8") if "_Tee" in text: print(" run_agent.py 已经改过") elif CLI_OLD not in text: print(" [失败] cli_netcheck 锚点没找到") return 1 else: text = text.replace(CLI_OLD, CLI_NEW, 1) RUN.write_text(text, encoding="utf-8") print(" cli_netcheck: 现在同时写结果文件") (TOOLS / "wlzj.bat").write_bytes( WLZJ.replace("\n", "\r\n").encode("gbk", errors="replace")) print(" wlzj.bat: 已更新(跑完自动用记事本打开结果)") import ast try: ast.parse(RUN.read_text(encoding="utf-8")) print(" run_agent.py: 语法 OK") except SyntaxError as exc: print(f" run_agent.py: 语法错误 {exc}") return 1 return 0 if __name__ == "__main__": sys.exit(main())