Compare commits
3 Commits
d811483e94
...
79bf32b35c
| Author | SHA1 | Date | |
|---|---|---|---|
| 79bf32b35c | |||
| 2eb104f4c3 | |||
| 941fb535f6 |
36
main.py
36
main.py
@@ -31,30 +31,38 @@ def trigger_scan():
|
||||
|
||||
def add_watch_recursive(inotify, path, watch_flags, wd_to_path):
|
||||
"""Walk a directory path and add watches recursively."""
|
||||
try:
|
||||
for root, _, _ in os.walk(path):
|
||||
try:
|
||||
logging.info(f"Watching {root}")
|
||||
wd = inotify.add_watch(root, watch_flags)
|
||||
wd_to_path[wd] = root
|
||||
except OSError as e:
|
||||
logging.error(f"Error adding watch for {root}: {e}")
|
||||
except FileNotFoundError:
|
||||
logging.warning(f"Could not scan {path} as it was not found.")
|
||||
logging.info(f"Watching {path} and its subdirectories recursively.")
|
||||
for root, _, _ in os.walk(path):
|
||||
try:
|
||||
logging.info(f"Watching {root}")
|
||||
wd = inotify.add_watch(root, watch_flags)
|
||||
wd_to_path[wd] = root
|
||||
except OSError as e:
|
||||
logging.error(f"Error adding watch for {root}: {e}")
|
||||
|
||||
|
||||
def main():
|
||||
global timer
|
||||
watch_flags = flags.MODIFY | flags.CREATE | flags.DELETE | flags.MOVED_FROM | flags.MOVED_TO
|
||||
wd_to_path = {}
|
||||
missing_paths = set()
|
||||
|
||||
with INotify() as inotify:
|
||||
for path in secrets.WATCH_PATHS:
|
||||
logging.info(f"Watching {path} and its subdirectories recursively.")
|
||||
add_watch_recursive(inotify, path, watch_flags, wd_to_path)
|
||||
|
||||
try:
|
||||
while True:
|
||||
watched_dirs = set(wd_to_path.values())
|
||||
for path in secrets.WATCH_PATHS:
|
||||
if path not in watched_dirs:
|
||||
if os.path.exists(path):
|
||||
if path in missing_paths:
|
||||
logging.info(f"Watch path {path} is now available.")
|
||||
missing_paths.remove(path)
|
||||
add_watch_recursive(inotify, path, watch_flags, wd_to_path)
|
||||
else:
|
||||
if path not in missing_paths:
|
||||
logging.warning(f"Watch path {path} does not exist. Re-checking every second...")
|
||||
missing_paths.add(path)
|
||||
|
||||
events = inotify.read(timeout=1000)
|
||||
if events:
|
||||
scan_needed = False
|
||||
|
||||
Reference in New Issue
Block a user