bep40 commited on
Commit
e838482
·
verified ·
1 Parent(s): 970bc2d

Self-heal _run.py: restore main.py từ 5b6000c nếu bị hỏng PLACEHOLDER

Browse files
Files changed (1) hide show
  1. _run.py +45 -0
_run.py CHANGED
@@ -1 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from app_v2_entry import app # v5-stable inline bongda proxy
 
1
+ """VNEWS boot entry — self-heal before import.
2
+
3
+ If main.py is the PLACEHOLDER stub (11 bytes, left by a bad overwrite),
4
+ restore the known-good version from commit 5b6000c via huggingface_hub,
5
+ then proceed with the normal boot chain (_run -> app_v2_entry -> main).
6
+ If main.py is already healthy, this does nothing.
7
+ """
8
+ import os
9
+
10
+ _APP_DIR = os.path.dirname(os.path.abspath(__file__))
11
+ _MAIN = os.path.join(_APP_DIR, "main.py")
12
+ _GOOD_REV = "5b6000c58f89723b3d228afb06c891ce909ac488"
13
+
14
+
15
+ def _main_is_broken(path):
16
+ try:
17
+ if os.path.getsize(path) < 50000:
18
+ return True
19
+ with open(path, encoding="utf-8", errors="replace") as f:
20
+ head = f.read(2048)
21
+ return "PLACEHOLDER" in head or "app = FastAPI()" not in head
22
+ except Exception:
23
+ return True
24
+
25
+
26
+ if _main_is_broken(_MAIN):
27
+ print(f"[self-heal] main.py damaged -> restoring from {_GOOD_REV}", flush=True)
28
+ try:
29
+ from huggingface_hub import hf_hub_download
30
+
31
+ good = hf_hub_download(
32
+ repo_id="bep40/VNEWS",
33
+ filename="main.py",
34
+ revision=_GOOD_REV,
35
+ repo_type="space",
36
+ )
37
+ with open(good, encoding="utf-8") as f:
38
+ data = f.read()
39
+ assert "app = FastAPI()" in data and "proxy_m3u_hls" in data
40
+ with open(_MAIN, "w", encoding="utf-8") as f:
41
+ f.write(data)
42
+ print(f"[self-heal] main.py restored ({len(data)} bytes)", flush=True)
43
+ except Exception as e:
44
+ print(f"[self-heal] FAILED to restore main.py: {e}", flush=True)
45
+
46
  from app_v2_entry import app # v5-stable inline bongda proxy