-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch_build.py
More file actions
54 lines (45 loc) · 1.57 KB
/
Copy pathwatch_build.py
File metadata and controls
54 lines (45 loc) · 1.57 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
#!/usr/bin/env python3
"""Watch campus_auto_login.py and auto-rebuild exe on change."""
import subprocess
import sys
import time
from pathlib import Path
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
SCRIPT_DIR = Path(__file__).resolve().parent
TARGET = SCRIPT_DIR / "campus_auto_login.py"
SPEC = SCRIPT_DIR / "campus_auto_login.spec"
DEBOUNCE_SEC = 2
class RebuildHandler(FileSystemEventHandler):
def __init__(self):
self._last_trigger = 0.0
def on_modified(self, event):
if Path(event.src_path).resolve() != TARGET:
return
now = time.time()
if now - self._last_trigger < DEBOUNCE_SEC:
return
self._last_trigger = now
print("\n[watch_build] Change detected, rebuilding exe ...", flush=True)
ret = subprocess.run(
[sys.executable, "-m", "PyInstaller", "--clean", str(SPEC)],
cwd=str(SCRIPT_DIR),
)
if ret.returncode == 0:
print(f"[watch_build] Build succeeded: {SCRIPT_DIR / 'dist' / 'campus_auto_login.exe'}", flush=True)
else:
print(f"[watch_build] Build FAILED (exit {ret.returncode})", flush=True)
def main():
handler = RebuildHandler()
observer = Observer()
observer.schedule(handler, str(SCRIPT_DIR), recursive=False)
observer.start()
print(f"[watch_build] Watching {TARGET} for changes ...", flush=True)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
main()