fix: Manually map inotify watch descriptors to paths to fix AttributeError
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
18
main.py
18
main.py
@@ -29,13 +29,14 @@ 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):
|
def add_watch_recursive(inotify, path, watch_flags, wd_to_path):
|
||||||
"""Walk a directory path and add watches recursively."""
|
"""Walk a directory path and add watches recursively."""
|
||||||
try:
|
try:
|
||||||
for root, _, _ in os.walk(path):
|
for root, _, _ in os.walk(path):
|
||||||
try:
|
try:
|
||||||
logging.info(f"Watching {root}")
|
logging.info(f"Watching {root}")
|
||||||
inotify.add_watch(root, watch_flags)
|
wd = inotify.add_watch(root, watch_flags)
|
||||||
|
wd_to_path[wd] = root
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
logging.error(f"Error adding watch for {root}: {e}")
|
logging.error(f"Error adding watch for {root}: {e}")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@@ -45,11 +46,12 @@ def add_watch_recursive(inotify, path, watch_flags):
|
|||||||
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
|
||||||
|
wd_to_path = {}
|
||||||
|
|
||||||
with INotify() as inotify:
|
with INotify() as inotify:
|
||||||
for path in secrets.WATCH_PATHS:
|
for path in secrets.WATCH_PATHS:
|
||||||
logging.info(f"Watching {path} and its subdirectories recursively.")
|
logging.info(f"Watching {path} and its subdirectories recursively.")
|
||||||
add_watch_recursive(inotify, path, watch_flags)
|
add_watch_recursive(inotify, path, watch_flags, wd_to_path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
@@ -58,11 +60,13 @@ def main():
|
|||||||
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):
|
if event.mask & flags.ISDIR and (event.mask & flags.CREATE or event.mask & flags.MOVED_TO):
|
||||||
parent_dir_path_bytes = inotify.paths[event.wd]
|
parent_dir_path = wd_to_path.get(event.wd)
|
||||||
new_dir_path_bytes = os.path.join(parent_dir_path_bytes, event.name)
|
if not parent_dir_path:
|
||||||
new_dir_path = new_dir_path_bytes.decode('utf-8')
|
logging.warning(f"Could not find path for watch descriptor {event.wd}")
|
||||||
|
continue
|
||||||
|
new_dir_path = os.path.join(parent_dir_path, event.name.decode('utf-8'))
|
||||||
logging.info(f"New directory {new_dir_path} detected, adding watches.")
|
logging.info(f"New directory {new_dir_path} detected, adding watches.")
|
||||||
add_watch_recursive(inotify, new_dir_path, watch_flags)
|
add_watch_recursive(inotify, new_dir_path, watch_flags, wd_to_path)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if timer:
|
if timer:
|
||||||
|
|||||||
Reference in New Issue
Block a user