check_site.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. """全站完整性扫描:逐文件对比本地与线上字节数,找出未真正上传的文件"""
  3. import os, subprocess, concurrent.futures, sys
  4. SITE = "/opt/data/projects/hardware-lab/site"
  5. BASE = "https://app.fmode.cn/dev/GvsWkfZb36"
  6. IDX_SIZE = os.path.getsize(os.path.join(SITE, "index.html"))
  7. files = []
  8. for root, dirs, names in os.walk(SITE):
  9. dirs[:] = [d for d in dirs if d != "node_modules"]
  10. for n in names:
  11. if n.endswith(".map"):
  12. continue
  13. p = os.path.join(root, n)
  14. rel = os.path.relpath(p, SITE)
  15. files.append((rel, os.path.getsize(p)))
  16. print(f"共 {len(files)} 个文件,index.html = {IDX_SIZE} 字节")
  17. def check(item):
  18. rel, local = item
  19. try:
  20. out = subprocess.run(
  21. ["curl", "-s", "-o", "/dev/null", "-w", "%{size_download}", "--max-time", "30",
  22. f"{BASE}/{rel}?t=1"],
  23. capture_output=True, text=True, timeout=40).stdout.strip()
  24. remote = int(out) if out.isdigit() else -1
  25. except Exception:
  26. remote = -1
  27. ok = (remote == local)
  28. fallback = (remote == IDX_SIZE and local != IDX_SIZE)
  29. return (rel, local, remote, ok, fallback)
  30. bad = []
  31. with concurrent.futures.ThreadPoolExecutor(max_workers=12) as ex:
  32. for rel, local, remote, ok, fallback in ex.map(check, files):
  33. if not ok:
  34. bad.append((rel, local, remote, "FALLBACK" if fallback else "MISSING"))
  35. print(f"\n{'='*70}")
  36. print(f"正常: {len(files) - len(bad)} / {len(files)}")
  37. print(f"异常: {len(bad)}")
  38. for rel, local, remote, kind in bad[:40]:
  39. print(f" {kind:9} {rel} 本地={local} 线上={remote}")
  40. if bad:
  41. with open("/opt/data/projects/hardware-lab/logs/missing.txt", "w") as f:
  42. for rel, _, _, _ in bad:
  43. f.write(rel + "\n")
  44. print(f"\n清单已写入 logs/missing.txt")