#!/usr/bin/env python3 """ Watch for code changes in core/ and app/ and restart ur_gia container. """ import os import sys import time import subprocess from pathlib import Path from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler class ChangeHandler(FileSystemEventHandler): def __init__(self): self.last_restart = 0 self.restart_debounce = 2 # seconds def on_modified(self, event): self._check_and_restart(event.src_path) def on_created(self, event): self._check_and_restart(event.src_path) def on_deleted(self, event): self._check_and_restart(event.src_path) def _check_and_restart(self, path): # Ignore pycache and compiled files if '__pycache__' in path or '.pyc' in path or '.git' in path: return now = time.time() if now - self.last_restart < self.restart_debounce: return self.last_restart = now print(f'[{time.strftime("%H:%M:%S")}] Change detected: {path}', flush=True) time.sleep(1) self._restart_ur() def _restart_ur(self): print(f'[{time.strftime("%H:%M:%S")}] Restarting ur_gia...', flush=True) # Try podman first (preferred in this setup), then docker result = subprocess.run( 'podman restart ur_gia 2>/dev/null || docker restart ur_gia 2>/dev/null', shell=True, capture_output=True, ) if result.returncode == 0: print(f'[{time.strftime("%H:%M:%S")}] ur_gia restarted successfully', flush=True) else: print(f'[{time.strftime("%H:%M:%S")}] ur_gia restart failed', flush=True) time.sleep(1) def main(): handler = ChangeHandler() observer = Observer() # Watch both core and app directories watch_paths = ['/code/GIA/core', '/code/GIA/app'] for path in watch_paths: if os.path.exists(path): observer.schedule(handler, path, recursive=True) print(f'Watching: {path}', flush=True) observer.start() print(f'[{time.strftime("%H:%M:%S")}] File watcher started. Monitoring for changes...', flush=True) try: while True: time.sleep(1) except KeyboardInterrupt: observer.stop() observer.join() if __name__ == '__main__': main()