-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.py
More file actions
70 lines (58 loc) · 2.22 KB
/
execute.py
File metadata and controls
70 lines (58 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import sys
import tempfile
from pathlib import Path
CDN_URL = "https://cdn.dos.zone/custom/dos/ultimate-doom.jsdos"
ASSETS_DIR = Path(__file__).parent / "webui" / "assets"
BUNDLE_PATH = ASSETS_DIR / "ultimate-doom.jsdos"
def main():
try:
import requests
except ImportError:
print("ERROR: 'requests' package is required. Install with: pip install requests")
return 1
print(f"Checking CDN bundle at {CDN_URL} ...")
try:
head = requests.head(CDN_URL, timeout=15, allow_redirects=True)
head.raise_for_status()
except requests.RequestException as e:
print(f"ERROR: Could not reach CDN: {e}")
return 1
remote_size = int(head.headers.get("Content-Length", 0))
if remote_size == 0:
print("WARNING: CDN did not report Content-Length; will download anyway.")
if BUNDLE_PATH.exists() and remote_size > 0:
local_size = BUNDLE_PATH.stat().st_size
if local_size == remote_size:
print(f"Bundle already cached ({local_size:,} bytes). Skipping download.")
return 0
print(f"Local size ({local_size:,}) differs from remote ({remote_size:,}). Re-downloading.")
ASSETS_DIR.mkdir(parents=True, exist_ok=True)
print(f"Downloading ({remote_size:,} bytes) ...")
try:
resp = requests.get(CDN_URL, stream=True, timeout=60)
resp.raise_for_status()
except requests.RequestException as e:
print(f"ERROR: Download failed: {e}")
return 1
# Write to temp file then atomic replace.
fd, tmp_path = tempfile.mkstemp(dir=str(ASSETS_DIR), suffix=".tmp")
try:
written = 0
with os.fdopen(fd, "wb") as f:
for chunk in resp.iter_content(chunk_size=64 * 1024):
f.write(chunk)
written += len(chunk)
os.replace(tmp_path, str(BUNDLE_PATH))
print(f"Done. Cached {written:,} bytes to {BUNDLE_PATH.relative_to(Path(__file__).parent)}")
except Exception as e:
# Clean up temp file on failure.
try:
os.unlink(tmp_path)
except OSError:
pass
print(f"ERROR: Failed to write bundle: {e}")
return 1
return 0
if __name__ == "__main__":
sys.exit(main())