# -*- coding: utf-8 -*- """改名收尾:防火墙规则名 + verify_all 路径。 1. firewall.py 的 PROGRAM_RULE_NAMES 这是"修复防火墙时要删掉哪些旧规则"的清单。 新程序叫 ykxmz.exe,所以得加进去; 旧的 lanremote-agent.exe 也**保留** —— 老用户升级上来时, 那条旧规则还躺在他机器上,不清掉会两条打架。 2. _tests/verify_all.ps1 还指向 dist_exe\\LanRemote-Agent 3. 测试里的期望值跟着改 """ from __future__ import annotations import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") FIREWALL = ROOT / "windows" / "lanremote" / "firewall.py" VERIFY = ROOT / "_tests" / "verify_all.ps1" FW_TEST = ROOT / "_tests" / "test_firewall.py" # ---------- 1. firewall.py ---------- RULE_OLD = '''PROGRAM_RULE_NAMES = ("lanremote-agent.exe", "python.exe", "pythonw.exe")''' RULE_NEW = '''# 修复防火墙时要清掉的旧规则名。 # ykxmz.exe 是现在的名字;lanremote-agent.exe 是改名前的老名字 —— # 保留它,好让老用户升级上来时把旧规则清掉,不然会两条打架。 PROGRAM_RULE_NAMES = ("ykxmz.exe", "lanremote-agent.exe", "python.exe", "pythonw.exe")''' # ---------- 2. verify_all.ps1(注意 BOM)---------- VERIFY_OLD = '''$exe = Join-Path $root "dist_exe\\LanRemote-Agent\\LanRemote-Agent.exe"''' VERIFY_NEW = '''$exe = Join-Path $root "dist_exe\\ykxmz\\ykxmz.exe"''' VERIFY_PROC_OLD = '''Get-Process LanRemote-Agent -ErrorAction SilentlyContinue |''' VERIFY_PROC_NEW = '''Get-Process ykxmz -ErrorAction SilentlyContinue |''' # ---------- 3. 测试期望值 ---------- TEST_RULES = [ ('names == {"lanremote-agent.exe"}, str(names))', 'names == {"ykxmz.exe"}, str(names))'), ('check("脚本删除了 lanremote-agent.exe 的规则",\n' ' \'delete rule name="lanremote-agent.exe" dir=in\' in script)', 'check("脚本删除了 ykxmz.exe 的规则",\n' ' \'delete rule name="ykxmz.exe" dir=in\' in script)'), ('zh.get("RuleName") == "lanremote-agent.exe", str(zh.get("RuleName")))', 'zh.get("RuleName") == "ykxmz.exe", str(zh.get("RuleName")))'), ('(zh.get("Program") or "").lower().endswith("lanremote-agent.exe"),', '(zh.get("Program") or "").lower().endswith("ykxmz.exe"),'), ('en.get("RuleName") == "lanremote-agent.exe", str(en.get("RuleName")))', 'en.get("RuleName") == "ykxmz.exe", str(en.get("RuleName")))'), ] def main() -> int: ok = True # firewall.py text = FIREWALL.read_text(encoding="utf-8") if "ykxmz.exe" in text: print(" firewall.py: 已经改过") elif RULE_OLD in text: text = text.replace(RULE_OLD, RULE_NEW, 1) FIREWALL.write_text(text, encoding="utf-8", newline="\n") print(" firewall.py: PROGRAM_RULE_NAMES 加上 ykxmz.exe") else: print(" [失败] firewall.py 锚点没找到") ok = False # verify_all.ps1(保持 BOM) raw = VERIFY.read_bytes() had_bom = raw.startswith(b"\xef\xbb\xbf") text = raw.decode("utf-8-sig") changed = 0 if VERIFY_OLD in text: text = text.replace(VERIFY_OLD, VERIFY_NEW, 1) changed += 1 if VERIFY_PROC_OLD in text: text = text.replace(VERIFY_PROC_OLD, VERIFY_PROC_NEW, 1) changed += 1 if changed: body = text.replace("\r\n", "\n").encode("utf-8") VERIFY.write_bytes((b"\xef\xbb\xbf" if had_bom else b"") + body) print(f" verify_all.ps1: 改了 {changed} 处(BOM {'保留' if had_bom else '无'})") else: print(" verify_all.ps1: 没变化") # 测试期望值 text = FW_TEST.read_text(encoding="utf-8") changed = 0 for old, new in TEST_RULES: if old in text: text = text.replace(old, new) changed += 1 if changed: FW_TEST.write_text(text, encoding="utf-8", newline="\n") print(f" 测试期望值: 改了 {changed} 处") else: print(" 测试期望值: 没变化") # 语法 import ast for label, path in (("firewall.py", FIREWALL), ("test_firewall.py", FW_TEST)): 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())