Compare commits
3 Commits
cbab9b3949
...
04bb79111b
| Author | SHA1 | Date | |
|---|---|---|---|
| 04bb79111b | |||
| 079deae811 | |||
| 8dd622f9cf |
27
main.py
27
main.py
@@ -29,14 +29,27 @@ def trigger_scan():
|
|||||||
logging.error(f"Failed to trigger scan: {e}")
|
logging.error(f"Failed to trigger scan: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def add_watch_recursive(inotify, path, watch_flags):
|
||||||
|
"""Walk a directory path and add watches recursively."""
|
||||||
|
try:
|
||||||
|
for root, _, _ in os.walk(path):
|
||||||
|
try:
|
||||||
|
logging.info(f"Watching {root}")
|
||||||
|
inotify.add_watch(root, watch_flags)
|
||||||
|
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.")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global timer
|
global timer
|
||||||
watch_flags = flags.MODIFY | flags.CREATE | flags.DELETE | flags.MOVED_FROM | flags.MOVED_TO
|
watch_flags = flags.MODIFY | flags.CREATE | flags.DELETE | flags.MOVED_FROM | flags.MOVED_TO
|
||||||
|
|
||||||
with INotify() as inotify:
|
with INotify() as inotify:
|
||||||
for path in WATCH_PATHS:
|
for path in secrets.WATCH_PATHS:
|
||||||
logging.info(f"Watching {path}")
|
logging.info(f"Watching {path} and its subdirectories recursively.")
|
||||||
inotify.add_watch(path, watch_flags)
|
add_watch_recursive(inotify, path, watch_flags)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@@ -44,6 +57,14 @@ def main():
|
|||||||
if events:
|
if events:
|
||||||
for event in events:
|
for event in events:
|
||||||
logging.debug(f"Event: {event!r}")
|
logging.debug(f"Event: {event!r}")
|
||||||
|
if event.mask & flags.ISDIR and (event.mask & flags.CREATE or event.mask & flags.MOVED_TO):
|
||||||
|
parent_dir_path_bytes = inotify.paths[event.wd]
|
||||||
|
new_dir_path_bytes = os.path.join(parent_dir_path_bytes, event.name)
|
||||||
|
new_dir_path = new_dir_path_bytes.decode('utf-8')
|
||||||
|
logging.info(f"New directory {new_dir_path} detected, adding watches.")
|
||||||
|
add_watch_recursive(inotify, new_dir_path, watch_flags)
|
||||||
|
continue
|
||||||
|
|
||||||
if timer:
|
if timer:
|
||||||
timer.cancel()
|
timer.cancel()
|
||||||
logging.info('Debounce cancelled timer.')
|
logging.info('Debounce cancelled timer.')
|
||||||
|
|||||||
Reference in New Issue
Block a user