# -*- coding: utf-8 -*- r"""给管理页 head 补 favicon link,然后部署验证。 前面几次栽在"Python 三层引号套 PowerShell 引号"上, 这次全部走文件:Python 脚本 + shell 脚本,不再用 -c 内联。 """ from __future__ import annotations import ast import re import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") sys.path.insert(0, str(ROOT / "_build")) from devssh import run, upload # noqa: E402 RELAY = ROOT / "relay" / "relay.py" LINK = '\n' def patch_admin_head() -> bool: text = RELAY.read_text(encoding="utf-8") start = text.find("def _admin_page") if start < 0: print(" [失败] 找不到 _admin_page") return False head_open = text.find("", start) head_close = text.find("", head_open) if head_open < 0 or head_close < 0: print(" [失败] _admin_page 里找不到 ") return False segment = text[head_open:head_close] if "favicon" in segment: print(" 管理页已经有 favicon") return True # 插在 之后就行,不用找 charset insert_at = head_open + len("") text = text[:insert_at] + LINK + text[insert_at:] RELAY.write_text(text, encoding="utf-8", newline="\n") print(" 管理页 head 补上 favicon link") ast.parse(text) print(" relay.py 语法 OK") return True def main() -> int: if not patch_admin_head(): return 1 print() print(" 部署到设备…") upload(str(RELAY), "/root/lanremote-relay/relay.py", 0o755) upload(str(ROOT / "relay" / "favicon.ico"), "/root/lanremote-relay/favicon.ico", 0o644) run("systemctl restart lanremote-relay", timeout=90) print(" 已重启") print() print(" 验证:") _c, out, _e = run("bash /root/check_web.sh 2>&1", timeout=180) print(out) return 0 if __name__ == "__main__": sys.exit(main())