# -*- coding: utf-8 -*-
r"""修上一步闯的祸:JS 在 f-string 里,大括号必须写两遍。
上一步把下载区的 JS 换成新版时用了单大括号 { },
但那一段是 f-string —— 单大括号会被当成占位符,
于是 relay.py 报 "f-string: invalid syntax"。
(原来那段用的是 {{ }},我漏看了。)
"""
from __future__ import annotations
import ast
import re
import sys
from pathlib import Path
RELAY = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\relay" r"\relay.py")
# f-string 里要写双大括号的版本
NEW_JS = '''// ---------------- 下载(就一个链接)----------------
(async () => {{
const body = document.getElementById('dlBody');
if (!body) return;
try {{
const r = await fetch('/v1/downloads');
const j = await r.json();
const files = (j && j.files) || [];
if (!files.length) {{
body.innerHTML = '服务器上还没有成品。';
return;
}}
// 优先挑被控端整包;没有就挑第一个 zip;再没有就第一个
const pick = files.find(f => /agent/i.test(f.name))
|| files.find(f => /\\.zip$/i.test(f.name))
|| files[0];
const mb = (pick.size / 1024 / 1024).toFixed(2);
body.innerHTML =
''
+ '⬇ 下载 ' + pick.name + ''
+ '
';
}} catch (e) {{
body.innerHTML = '读不到:' + e + '';
}}
}})();
// ---------------- 自动刷新 ----------------'''
def main() -> int:
text = RELAY.read_text(encoding="utf-8")
# 先确认现在确实是坏的
try:
ast.parse(text)
print(" 语法本来就是好的,不用修")
return 0
except SyntaxError as exc:
print(f" 当前语法错误在行 {exc.lineno}(预期之内)")
# 把"下载"注释到"自动刷新"之间整段换掉
pattern = re.compile(
r'// ---------------- 下载.*?// ---------------- 自动刷新 ----------------',
re.S)
match = pattern.search(text)
if not match:
print(" [失败] 找不到要替换的 JS 段")
return 1
text = text[:match.start()] + NEW_JS + text[match.end():]
RELAY.write_text(text, encoding="utf-8", newline="\n")
try:
ast.parse(text)
print(" ✓ relay.py 语法恢复正常(大括号改成 {{ }})")
except SyntaxError as exc:
print(f" ✗ 还有错:行 {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(f" {i + 1}: {lines[i][:100]}")
return 1
return 0
if __name__ == "__main__":
sys.exit(main())