# -*- coding: utf-8 -*- """把产品名改成 ykxmz,并加一个正经的卸载。 用户要求:"改成 ykxmz" 做这些: 1. 开机自启注册表的值名 -> ykxmz (任务管理器「启动」标签页里显示的就是这个名字) 同时迁移掉旧名 LanRemoteAgent,不留垃圾 2. 版本信息里的产品名/文件名 -> ykxmz 3. 打包出来的 exe 叫 ykxmz.exe 4. 加 --uninstall:一条命令清干净 (停进程、删自启、删数据目录) 刻意保留的东西: * **启动项保持可见**。改名字是品牌化,藏起来是另一回事。 * 被控端被连接时的屏幕提示也保留。 """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") WINDOWS = ROOT / "windows" AUTOSTART = WINDOWS / "lanremote" / "autostart.py" VERSION = WINDOWS / "version_info.txt" BUILD = WINDOWS / "build_exe.ps1" RUN = WINDOWS / "run_agent.py" UNINSTALL = WINDOWS / "lanremote" / "uninstall.py" NEW_NAME = "ykxmz" # ==================== 1. autostart.py ==================== AUTO_HEAD_OLD = '''RUN_KEY = r"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run" VALUE_NAME = "LanRemoteAgent"''' AUTO_HEAD_NEW = '''RUN_KEY = r"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run" # 任务管理器「启动」标签页里显示的就是这个名字。 # 用 ykxmz 而不是冒充 "Windows xxx" —— 用的人应该看得出这是什么。 VALUE_NAME = "ykxmz" # 早期版本用过的名字。开着自启的老用户升级后要把它清掉,别留两条。 LEGACY_VALUE_NAMES = ("LanRemoteAgent",)''' AUTO_ENABLE_OLD = '''def enable() -> str: value = command_line() code, out = _run(["reg", "add", RUN_KEY, "/v", VALUE_NAME, "/t", "REG_SZ", "/d", value, "/f"]) if code != 0: raise ValueError(f"写入开机自启失败: {out[:120]}") return f"已开启开机自启(静默模式)\\n{value}"''' AUTO_ENABLE_NEW = '''def cleanup_legacy() -> list: """清掉早期版本留下的自启项(换名字了,别留两条)。""" removed = [] for name in LEGACY_VALUE_NAMES: code, _out = _run(["reg", "delete", RUN_KEY, "/v", name, "/f"]) if code == 0: removed.append(name) return removed def enable() -> str: value = command_line() code, out = _run(["reg", "add", RUN_KEY, "/v", VALUE_NAME, "/t", "REG_SZ", "/d", value, "/f"]) if code != 0: raise ValueError(f"写入开机自启失败: {out[:120]}") gone = cleanup_legacy() extra = f"(顺手清掉旧自启项 {', '.join(gone)})" if gone else "" return f"已开启开机自启(静默模式){extra}\\n{value}"''' # ==================== 2. version_info.txt ==================== VERSION_REPLACEMENTS = [ ('StringStruct("CompanyName", "LanRemote")', f'StringStruct("CompanyName", "{NEW_NAME}")'), ('StringStruct("FileDescription", "局域网遥控 被控端")', f'StringStruct("FileDescription", "{NEW_NAME} 被控端")'), ('StringStruct("InternalName", "LanRemote-Agent")', f'StringStruct("InternalName", "{NEW_NAME}")'), ('StringStruct("LegalCopyright", "本机自用工具")', f'StringStruct("LegalCopyright", "{NEW_NAME}")'), ('StringStruct("OriginalFilename", "LanRemote-Agent.exe")', f'StringStruct("OriginalFilename", "{NEW_NAME}.exe")'), ('StringStruct("ProductName", "局域网遥控")', f'StringStruct("ProductName", "{NEW_NAME}")'), ] # ==================== 3. build_exe.ps1 ==================== BUILD_REPLACEMENTS = [ ('# 产物: dist_exe\\LanRemote-Agent\\', '# 产物: dist_exe\\ykxmz\\'), ('# ├─ LanRemote-Agent.exe <- 双击运行', '# ├─ ykxmz.exe <- 双击运行'), ('--name "LanRemote-Agent" `', '--name "ykxmz" `'), ('$target = Join-Path $root "dist_exe\\LanRemote-Agent\\LanRemote-Agent.exe"', '$target = Join-Path $root "dist_exe\\ykxmz\\ykxmz.exe"'), ] # ==================== 4. uninstall.py(新文件)==================== UNINSTALL_SRC = '''# -*- coding: utf-8 -*- """卸载:一条命令把程序从这台电脑上清干净。 ykxmz.exe --uninstall 清这些: 1. 停掉正在跑的进程 2. 删掉开机自启项 3. 删掉数据目录(配对码、已配对手表……) **故意做这个**:一个能开机自启、又能远程控制的软件, 必须能干净地卸载掉 —— 否则它就成了"赖着不走"的东西。 电脑的主人应该随时能把它彻底清掉。 """ from __future__ import annotations import os import shutil import subprocess import sys import time from pathlib import Path from .autostart import VALUE_NAME, RUN_KEY, LEGACY_VALUE_NAMES, _run from .config import app_dir, data_dir _NO_WINDOW = getattr(subprocess, "CREATE_NO_WINDOW", 0) def _kill_running() -> str: """把正在跑的进程停掉(除了自己)。""" if not getattr(sys, "frozen", False): return "(源码模式,跳过进程清理)" exe = Path(sys.executable).name code, out = _run(["taskkill", "/F", "/IM", exe]) if code == 0: return f"已停止进程 {exe}" if "not found" in out.lower() or "没有" in out: return "没有正在运行的进程" return f"停止进程:{out[:80]}" def _remove_autostart() -> str: done = [] for name in (VALUE_NAME,) + tuple(LEGACY_VALUE_NAMES): code, _out = _run(["reg", "delete", RUN_KEY, "/v", name, "/f"]) if code == 0: done.append(name) if done: return f"已删除开机自启项 {', '.join(done)}" return "没有开机自启项" def _remove_data() -> str: """删数据目录。删不掉就留着,不影响卸载。""" target = Path(data_dir()) if not target.is_dir(): return "没有数据目录" try: shutil.rmtree(target, ignore_errors=True) if not target.is_dir(): return f"已删除数据目录 {target}" return f"数据目录没删干净(可能被占用):{target}" except Exception as exc: # noqa: BLE001 return f"删数据目录失败:{exc}" def _schedule_self_delete() -> str: """把自己这个 exe 也删掉。 进程还在跑的时候删不掉自己,所以要起一个延迟的小命令, 等我们退出后再删目录。 """ if not getattr(sys, "frozen", False): return "(源码模式,不删自己)" folder = Path(sys.executable).resolve().parent cmd = (f'timeout /t 2 /nobreak >nul & ' f'rmdir /s /q "{folder}"') try: subprocess.Popen(["cmd", "/c", cmd], creationflags=_NO_WINDOW | 0x00000008) return f"已安排删除程序目录:{folder}" except Exception as exc: # noqa: BLE001 return f"(没安排上自删,请手动删 {folder}:{exc})" def run(keep_data: bool = False) -> str: """执行卸载,返回给人看的说明。""" lines = ["开始卸载 " + VALUE_NAME] lines.append(" " + _remove_autostart()) if keep_data: lines.append(" 保留数据目录(你要求保留)") else: lines.append(" " + _remove_data()) lines.append(" " + _schedule_self_delete()) lines.append("") lines.append("卸载完成。程序目录会在几秒后自动删除。") return "\\n".join(lines) ''' def main() -> int: ok = True # ---- autostart.py ---- text = AUTOSTART.read_text(encoding="utf-8") if f'VALUE_NAME = "{NEW_NAME}"' not in text: if AUTO_HEAD_OLD in text: text = text.replace(AUTO_HEAD_OLD, AUTO_HEAD_NEW, 1) print(" autostart.py: 改名 + 旧名清理常量") else: print(" [失败] autostart.py 头部锚点没找到") ok = False if AUTO_ENABLE_OLD in text: text = text.replace(AUTO_ENABLE_OLD, AUTO_ENABLE_NEW, 1) print(" autostart.py: enable 顺手清旧项") else: print(" [跳过] autostart.py enable 锚点没找到") AUTOSTART.write_text(text, encoding="utf-8", newline="\n") else: print(" autostart.py: 已经改过") # ---- version_info.txt ---- text = VERSION.read_text(encoding="utf-8") changed = 0 for old, new in VERSION_REPLACEMENTS: if old in text: text = text.replace(old, new, 1) changed += 1 if changed: VERSION.write_text(text, encoding="utf-8", newline="\n") print(f" version_info.txt: 改了 {changed} 个字段") else: print(" version_info.txt: 没变化(可能已改过)") # ---- build_exe.ps1 ---- text = BUILD.read_text(encoding="utf-8") changed = 0 for old, new in BUILD_REPLACEMENTS: if old in text: text = text.replace(old, new, 1) changed += 1 if changed: # 保持 BOM(PowerShell 5.1 没 BOM 会按 GBK 读) BUILD.write_bytes(b"\xef\xbb\xbf" + text.replace("\r\n", "\n").encode("utf-8")) print(f" build_exe.ps1: 改了 {changed} 处(BOM 已保留)") else: print(" build_exe.ps1: 没变化") # ---- uninstall.py ---- if UNINSTALL.exists(): print(" uninstall.py: 已存在") else: UNINSTALL.write_text(UNINSTALL_SRC, encoding="utf-8", newline="\n") print(" uninstall.py: 新建") # ---- 语法检查 ---- import ast for label, path in (("autostart.py", AUTOSTART), ("uninstall.py", UNINSTALL)): try: ast.parse(path.read_text(encoding="utf-8")) print(f" {label} 语法 OK") except SyntaxError as exc: print(f" {label} 语法错误: {exc}") ok = False return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())