Files
ykxmz/_tests/audit_python_names.py
T
xmz7891 bf15a2ee61 静态资源交给 Cloudflare 边缘缓存(下载提速 41 倍)
实测基线:
  ykxmz-setup-1.8.1.exe   只下到 237568/15067199 字节就停(17 KB/s)
  ykxmz-watch-1.8.1.apk   201523 字节 / 66.8 秒 = 3 KB/s
  ykxmz-server-1.8.1.zip  214900 字节 / 3.1 秒  = 67 KB/s

原因:relay 给所有响应都发 Cache-Control: no-store,
Cloudflare 完全不缓存 —— 每次下载/每张图都要穿隧道回 4G 源站。

改动:
  /download/*   no-store -> public, max-age=604800, immutable
                文件名带版本号,同一名字内容永不改变,可以长缓存
  /             -> public, max-age=60(挡住刷新重复回源)
  /favicon.ico  已是 max-age=86400,保持不变
  /shot/*       已是 max-age=86400,保持不变
  /v1/*         保持 no-store(接口必须实时)

顺带修两个自己踩的坑:

1. _send() 无条件先发 no-store,再追加 extra 里的 Cache-Control,
   结果是**两个 Cache-Control 头**,客户端只认第一个 ——
   调用方传的缓存策略被吃掉。改成 extra 里有就不再发默认值。

2. do_HEAD 和 do_GET 的缓存头不一致:HEAD 对非 /shot/ 一律报 no-store。
   下载器/代理用 HEAD 探测时会误判文件不可缓存。
   现在 _head_info 给每个路由带上和 GET 完全相同的缓存头,
   do_HEAD 也不再盲目覆盖。

结果(同一个测试口径):
  setup.exe  17 KB/s(截断) -> 695 KB/s 完整 15 MB   41 倍
  watch.apk   3 KB/s         -> 101 KB/s              33 倍
  server.zip 67 KB/s         ->  87 KB/s
  三个文件全部 cf-cache-status=HIT(不再穿隧道回源)

排查过程记一笔:我一开始用 curl -I 验证,
看到的全是 no-store —— 因为 **-I 发的是 HEAD**,
而 HEAD 走的是另一个分支。查了半天才发现是验证方法错了。
2026-10-07 15:44:48 +08:00

94 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""用 pyflakes 静态检查所有 Python 源码。
为什么必须加这一层:
我加 check_code_available() 时用了 DEFAULT_RELAY_URL,
但那个常量定义在 server.py、run_agent.py 里没导入。
结果:安装到最后一步直接弹 NameError 崩溃。
自带的回归全绿、编码审计也过 —— 因为这是**运行到那行才炸**的问题。
pyflakes 是静态分析,扫一眼就能发现"用了没定义的名字"。
本机自带 pyflakes 4.0.2。没有的话就跳过(不阻塞)。
"""
from __future__ import annotations
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
TARGETS = (
list((ROOT / "windows").glob("*.py"))
+ list((ROOT / "windows" / "lanremote").glob("*.py"))
+ list((ROOT / "relay").glob("*.py"))
+ list((ROOT / "_build").glob("*.py"))
+ list((ROOT / "_tests").glob("*.py"))
)
def main() -> int:
try:
import pyflakes # noqa: F401
except ImportError:
print(" (本机没有 pyflakes,跳过)")
return 0
files = [str(p) for p in TARGETS if p.is_file()]
print(f" 检查 {len(files)} 个 Python 文件…")
# ⚠ 不能一次把 450 多个文件全塞进命令行 ——
# Windows 的 CreateProcess 有命令行长度上限(约 32767 字符),
# 超了直接 FileNotFoundError: [WinError 206] 文件名或扩展名太长,
# 而且报错看起来像"文件找不到",很容易误判。
# 所以分批调用,每批的总长度控制住。
all_lines = []
batch, batch_len = [], 0
LIMIT = 6000 # 单批命令行里文件路径的总字符数预算(留足余量)
batches = []
for path in files:
if batch and batch_len + len(path) + 1 > LIMIT:
batches.append(batch)
batch, batch_len = [], 0
batch.append(path)
batch_len += len(path) + 1
if batch:
batches.append(batch)
for chunk in batches:
proc = subprocess.run([sys.executable, "-m", "pyflakes"] + chunk,
capture_output=True, text=True,
encoding="utf-8", errors="replace")
all_lines.extend(
line for line in (proc.stdout or "").splitlines() if line.strip())
lines = all_lines
if not lines:
print(" ✓ 没有未定义名 / 未使用导入等问题")
return 0
# 分类:undefined name 是致命的,未使用导入是无害的
fatal = [line for line in lines if "undefined name" in line]
minor = [line for line in lines if line not in fatal]
if minor:
print(f" {len(minor)} 条小问题(未使用的导入/变量,不影响运行):")
for line in minor[:8]:
print(f" {line}")
if fatal:
print()
print(f" ✗ {len(fatal)} 条**未定义名**(运行到这行会 NameError):")
for line in fatal:
print(f" {line}")
return 1
print(" ✓ 没有会导致崩溃的问题")
return 0
if __name__ == "__main__":
sys.exit(main())