dryymatt commited on
Commit
cc19b50
Β·
verified Β·
1 Parent(s): 9758373

πŸ§™β€β™‚οΈ First Commit: hot_reload.py

Browse files
Files changed (1) hide show
  1. hot_reload.py +71 -0
hot_reload.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Wizard-Vibe Core β€” Hot Reload Watcher
3
+ ======================================
4
+ Watches file changes and restarts the server automatically.
5
+ Uses inotify/file polling for cross-platform support.
6
+ """
7
+
8
+ import os
9
+ import sys
10
+ import time
11
+ import subprocess
12
+ from pathlib import Path
13
+
14
+
15
+ WATCH_DIRS = ["."]
16
+ WATCH_EXTS = {".py", ".html", ".css", ".js"}
17
+ IGNORE_DIRS = {"__pycache__", ".git", "generated", "venv", ".venv"}
18
+
19
+
20
+ def get_mtimes(root: Path) -> dict:
21
+ """Get mtimes for all watched files."""
22
+ mtimes = {}
23
+ for d in WATCH_DIRS:
24
+ for path in Path(d).rglob("*"):
25
+ if any(p in IGNORE_DIRS for p in path.parts):
26
+ continue
27
+ if path.is_file() and path.suffix in WATCH_EXTS:
28
+ mtimes[str(path)] = path.stat().st_mtime
29
+ return mtimes
30
+
31
+
32
+ def main():
33
+ """Run the server with hot-reload."""
34
+ print("πŸ§™β€β™‚οΈ Wizard-Vibe Core β€” Hot Reload Watcher")
35
+ print(" Watching: *.py, *.html, *.css, *.js")
36
+
37
+ # Set hot-reload flag
38
+ env = os.environ.copy()
39
+ env["WIZARD_HOT_RELOAD"] = "1"
40
+
41
+ proc = subprocess.Popen([sys.executable, "core.py"], env=env)
42
+ last_mtimes = get_mtimes(Path("."))
43
+
44
+ try:
45
+ while True:
46
+ time.sleep(1)
47
+ current = get_mtimes(Path("."))
48
+
49
+ if current != last_mtimes:
50
+ changed = [k for k in current if current[k] != last_mtimes.get(k)]
51
+ print(f"\nπŸ”„ File change detected: {changed[0] if changed else 'unknown'}")
52
+ print(" Restarting server...")
53
+
54
+ proc.terminate()
55
+ try:
56
+ proc.wait(timeout=5)
57
+ except subprocess.TimeoutExpired:
58
+ proc.kill()
59
+
60
+ proc = subprocess.Popen([sys.executable, "core.py"], env=env)
61
+ last_mtimes = current
62
+ print(f" βœ… Server restarted (PID: {proc.pid})")
63
+
64
+ except KeyboardInterrupt:
65
+ print("\nπŸ›‘ Shutting down...")
66
+ proc.terminate()
67
+ proc.wait()
68
+
69
+
70
+ if __name__ == "__main__":
71
+ main()