Work on fixing bugs and reformat

This commit is contained in:
2026-02-16 16:01:17 +00:00
parent 8ca1695fab
commit 3f82c27ab9
32 changed files with 1100 additions and 442 deletions

View File

@@ -15,9 +15,9 @@ Touching a file under the watched path will trigger a restart of the target
container; e.g. `touch /code/core/__restart__` will cause the watcher to act.
"""
import os
import subprocess
import sys
import time
import subprocess
def get_mtime(path):
@@ -25,9 +25,9 @@ def get_mtime(path):
max_mtime = 0
for root, dirs, files in os.walk(path):
# Skip pycache and hidden dirs
dirs[:] = [d for d in dirs if not d.startswith('.') and d != '__pycache__']
dirs[:] = [d for d in dirs if not d.startswith(".") and d != "__pycache__"]
for file in files:
if file.endswith(('.pyc', '.pyo')):
if file.endswith((".pyc", ".pyo")):
continue
try:
mtime = os.path.getmtime(os.path.join(root, file))
@@ -39,9 +39,9 @@ def get_mtime(path):
def restart_ur():
"""Restart target container (defaults to `ur_gia`)."""
target = os.environ.get('TARGET_CONTAINER', 'ur_gia')
target = os.environ.get("TARGET_CONTAINER", "ur_gia")
print(f'[{time.strftime("%H:%M:%S")}] Restarting {target}...', flush=True)
cmd = f'podman restart {target} 2>/dev/null || docker restart {target} 2>/dev/null'
cmd = f"podman restart {target} 2>/dev/null || docker restart {target} 2>/dev/null"
result = subprocess.run(cmd, shell=True, capture_output=True)
if result.returncode == 0:
print(f'[{time.strftime("%H:%M:%S")}] {target} restarted', flush=True)
@@ -53,16 +53,16 @@ def main():
# In the container the repository is mounted at /code
# Allow overriding watched paths via environment variable `WATCH_PATHS`.
# Default is `/code/core,/code/app`.
paths_env = os.environ.get('WATCH_PATHS', '/code/core,/code/app')
paths = [p.strip() for p in paths_env.split(',') if p.strip()]
paths_env = os.environ.get("WATCH_PATHS", "/code/core,/code/app")
paths = [p.strip() for p in paths_env.split(",") if p.strip()]
last_mtimes = {}
for path in paths:
if os.path.exists(path):
print(f'Watching: {path}', flush=True)
print(f"Watching: {path}", flush=True)
last_mtimes[path] = get_mtime(path)
else:
print(f'Not found: {path}', flush=True)
print(f"Not found: {path}", flush=True)
print(f'[{time.strftime("%H:%M:%S")}] Watcher started', flush=True)
restart_debounce = 0
@@ -77,15 +77,17 @@ def main():
continue
current_mtime = get_mtime(path)
if current_mtime > last_mtimes.get(path, 0):
print(f'[{time.strftime("%H:%M:%S")}] Changes in {path}', flush=True)
print(
f'[{time.strftime("%H:%M:%S")}] Changes in {path}', flush=True
)
last_mtimes[path] = current_mtime
if restart_debounce <= 0:
restart_ur()
restart_debounce = 5 # Don't restart more than every 5s
except KeyboardInterrupt:
print('Watcher stopped', flush=True)
print("Watcher stopped", flush=True)
sys.exit(0)
if __name__ == '__main__':
if __name__ == "__main__":
main()