# -*- coding: utf-8 -*- r'''把产品展示区块接进官网,并把截图传上去。''' from __future__ import annotations import ast import sys from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") LANDING = ROOT / "relay" / "landing.py" RELAY = ROOT / "relay" / "relay.py" sys.path.insert(0, str(ROOT / "_build")) from devssh import run, upload # noqa: E402 def wire_landing() -> None: text = LANDING.read_text(encoding="utf-8") if "showcase" in text: print(" landing.py 已经接过 showcase") return # 1) 引入 text = text.replace( "from urllib.parse import quote", "from urllib.parse import quote\n\n" "import showcase", 1) # 2) CSS 拼进去 marker = '""" % {"BG": BG, "SURFACE": SURFACE, "BORDER": BORDER, "TEXT": TEXT,' if marker in text: text = text.replace( marker, '""" + showcase.SHOWCASE_CSS % {"BG": BG, "SURFACE": SURFACE,\n' ' "BORDER": BORDER, "TEXT": TEXT,', 1) print(" CSS 已拼接") else: print(" [警告] CSS 锚点没找到,跳过") # 3) HTML 区块插到"下载"之前 anchor = '
' if anchor in text: text = text.replace( anchor, '%(showcase)s\n\n' + anchor, 1) # 4) 渲染参数里加上 text = text.replace( ' "dls": _download_cards(files),', ' "showcase": showcase.render_showcase(),\n' ' "dls": _download_cards(files),', 1) print(" HTML 区块已插入") else: print(" [警告] download 区块锚点没找到") LANDING.write_text(text, encoding="utf-8", newline="\n") try: ast.parse(text) print(" landing.py 语法 OK") except SyntaxError as exc: print(" [失败] 语法错误 行 %s: %s" % (exc.lineno, exc.msg)) lines = text.splitlines() for i in range(max(0, (exc.lineno or 1) - 3), min(len(lines), (exc.lineno or 1) + 2)): print(" %d: %s" % (i + 1, lines[i][:92])) def wire_relay_route() -> None: """加一条 /shot/ 路由,把截图发出去(白名单后缀)。""" text = RELAY.read_text(encoding="utf-8") if "/shot/" in text: print(" relay.py 已经有 /shot/ 路由") return # 找下载路由附近插入 anchor = 'if path.startswith("/download/"):' if anchor not in text: print(" [警告] 找不到 /download/ 路由锚点,手工看下") return block = ''' # 官网用的产品截图。 # 单独一条路由(不放进下载区),只允许 png/jpg/webp。 if path.startswith("/shot/"): name = path[len("/shot/"):] if (not name or ".." in name or "/" in name or "\\\\" in name or not name.lower().endswith((".png", ".jpg", ".jpeg", ".webp"))): self._json(404, {"ok": False, "error": "没有这张图"}) return target = Path(__file__).resolve().parent / "site_shots" / name if not target.is_file(): self._json(404, {"ok": False, "error": "没有这张图"}) return body = target.read_bytes() ctype = ("image/png" if name.lower().endswith(".png") else "image/jpeg") self.send_response(200) self.send_header("Content-Type", ctype) self.send_header("Content-Length", str(len(body))) self.send_header("Cache-Control", "public, max-age=86400") self.end_headers() self.wfile.write(body) return ''' text = text.replace(" " + anchor, block + " " + anchor, 1) RELAY.write_text(text, encoding="utf-8", newline="\n") try: ast.parse(text) print(" relay.py 语法 OK") except SyntaxError as exc: print(" [失败] relay.py 语法错误 行 %s: %s" % (exc.lineno, exc.msg)) def deploy() -> None: print() print(" 部署") run("mkdir -p /root/lanremote-relay/site_shots", timeout=60) upload(str(ROOT / "_build" / "site_shots" / "pc-main.png"), "/root/lanremote-relay/site_shots/pc-main.png", 0o644) print(" 截图已上传") upload(str(LANDING), "/root/lanremote-relay/landing.py", 0o644) upload(str(ROOT / "relay" / "showcase.py"), "/root/lanremote-relay/showcase.py", 0o644) upload(str(RELAY), "/root/lanremote-relay/relay.py", 0o755) print(" 代码已上传") run("systemctl restart lanremote-relay", timeout=90) out = run("sleep 6; curl -s -o /dev/null -w 'HTTP %{http_code} %{size_download}\\n' " "http://127.0.0.1:8080/; " "curl -s -o /dev/null -w '截图 HTTP %{http_code} %{size_download}\\n' " "http://127.0.0.1:8080/shot/pc-main.png", timeout=120)[1] print(" " + out.replace("\n", "\n ")) def main() -> int: wire_landing() wire_relay_route() deploy() return 0 if __name__ == "__main__": sys.exit(main())