From 0af326fa11bddec0d1ea21581af45cafd78c028a Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 9 Sep 2020 02:16:13 +0200 Subject: [PATCH] Refactored inotify backend --- platypush/backend/inotify.py | 99 ++++++++++++++++++++++++ platypush/backend/inotify/__init__.py | 81 ------------------- platypush/message/event/inotify.py | 92 ++++++++++++++++++++++ platypush/message/event/path/__init__.py | 87 --------------------- 4 files changed, 191 insertions(+), 168 deletions(-) create mode 100644 platypush/backend/inotify.py delete mode 100644 platypush/backend/inotify/__init__.py create mode 100644 platypush/message/event/inotify.py delete mode 100644 platypush/message/event/path/__init__.py diff --git a/platypush/backend/inotify.py b/platypush/backend/inotify.py new file mode 100644 index 0000000000..da865d7c8a --- /dev/null +++ b/platypush/backend/inotify.py @@ -0,0 +1,99 @@ +import os + +from platypush.backend import Backend +from platypush.message.event.inotify import InotifyCreateEvent, InotifyDeleteEvent, \ + InotifyOpenEvent, InotifyModifyEvent, InotifyCloseEvent, InotifyAccessEvent, InotifyMovedEvent + + +class InotifyBackend(Backend): + """ + (Linux only) This backend will listen for events on the filesystem (whether + a file/directory on a watch list is opened, modified, created, deleted, + closed or had its permissions changed) and will trigger a relevant event. + + Triggers: + + * :class:`platypush.message.event.inotify.InotifyCreateEvent` if a resource is created + * :class:`platypush.message.event.inotify.InotifyAccessEvent` if a resource is accessed + * :class:`platypush.message.event.inotify.InotifyOpenEvent` if a resource is opened + * :class:`platypush.message.event.inotify.InotifyModifyEvent` if a resource is modified + * :class:`platypush.message.event.inotify.InotifyPermissionsChangeEvent` if the permissions of a resource are changed + * :class:`platypush.message.event.inotify.InotifyCloseEvent` if a resource is closed + * :class:`platypush.message.event.inotify.InotifyDeleteEvent` if a resource is removed + + Requires: + + * **inotify** (``pip install inotify``) + + """ + + inotify_watch = None + + def __init__(self, watch_paths=None, **kwargs): + """ + :param watch_paths: Filesystem resources to watch for events + :type watch_paths: str + """ + + super().__init__(**kwargs) + self.watch_paths = set(map( + lambda path: os.path.abspath(os.path.expanduser(path)), + watch_paths if watch_paths else [])) + + def _cleanup(self): + if not self.inotify_watch: + return + + for path in self.watch_paths: + self.inotify_watch.remove_watch(path) + + self.inotify_watch = None + + def run(self): + import inotify.adapters + super().run() + + self.inotify_watch = inotify.adapters.Inotify() + for path in self.watch_paths: + self.inotify_watch.add_watch(path) + + moved_file = None + self.logger.info('Initialized inotify file monitoring backend, monitored resources: {}' + .format(self.watch_paths)) + + try: + for inotify_event in self.inotify_watch.event_gen(): + if inotify_event is not None: + (header, inotify_types, watch_path, filename) = inotify_event + event = None + resource_type = inotify_types[1] if len(inotify_types) > 1 else None + + if moved_file: + new = filename if 'IN_MOVED_TO' in inotify_types else None + event = InotifyMovedEvent(path=watch_path, old=moved_file, new=new) + moved_file = None + + if 'IN_OPEN' in inotify_types: + event = InotifyOpenEvent(path=watch_path, resource=filename, resource_type=resource_type) + elif 'IN_ACCESS' in inotify_types: + event = InotifyAccessEvent(path=watch_path, resource=filename, resource_type=resource_type) + elif 'IN_CREATE' in inotify_types: + event = InotifyCreateEvent(path=watch_path, resource=filename, resource_type=resource_type) + elif 'IN_MOVED_FROM' in inotify_types: + moved_file = filename + elif 'IN_MOVED_TO' in inotify_types and not moved_file: + event = InotifyMovedEvent(path=watch_path, old=None, new=filename) + elif 'IN_DELETE' in inotify_types: + event = InotifyDeleteEvent(path=watch_path, resource=filename, resource_type=resource_type) + elif 'IN_MODIFY' in inotify_types: + event = InotifyModifyEvent(path=watch_path, resource=filename, resource_type=resource_type) + elif 'IN_CLOSE_WRITE' in inotify_types or 'IN_CLOSE_NOWRITE' in inotify_types: + event = InotifyCloseEvent(path=watch_path, resource=filename, resource_type=resource_type) + + if event: + self.bus.post(event) + finally: + self._cleanup() + + +# vim:sw=4:ts=4:et: diff --git a/platypush/backend/inotify/__init__.py b/platypush/backend/inotify/__init__.py deleted file mode 100644 index c90853931f..0000000000 --- a/platypush/backend/inotify/__init__.py +++ /dev/null @@ -1,81 +0,0 @@ -import os - -from platypush.backend import Backend -from platypush.message.event.path import PathCreateEvent, PathDeleteEvent, \ - PathOpenEvent, PathModifyEvent, PathPermissionsChangeEvent, PathCloseEvent - - -class InotifyBackend(Backend): - """ - (Linux only) This backend will listen for events on the filesystem (whether - a file/directory on a watch list is opened, modified, created, deleted, - closed or had its permissions changed) and will trigger a relevant event. - - Triggers: - - * :class:`platypush.message.event.path.PathCreateEvent` if a resource is created - * :class:`platypush.message.event.path.PathOpenEvent` if a resource is opened - * :class:`platypush.message.event.path.PathModifyEvent` if a resource is modified - * :class:`platypush.message.event.path.PathPermissionsChangeEvent` if the permissions of a resource are changed - * :class:`platypush.message.event.path.PathCloseEvent` if a resource is closed - * :class:`platypush.message.event.path.PathDeleteEvent` if a resource is removed - - Requires: - - * **inotify** (``pip install inotify``) - """ - - inotify_watch = None - - def __init__(self, watch_paths=[], **kwargs): - """ - :param watch_paths: Filesystem resources to watch for events - :type watch_paths: str - """ - - super().__init__(**kwargs) - self.watch_paths = set(map( - lambda path: os.path.abspath(os.path.expanduser(path)), - watch_paths)) - - def _cleanup(self): - if not self.inotify_watch: - return - - for path in self.watch_paths: - self.inotify_watch.remove_watch(path) - - self.inotify_watch = None - - def run(self): - import inotify.adapters - super().run() - - self.inotify_watch = inotify.adapters.Inotify() - for path in self.watch_paths: - self.inotify_watch.add_watch(path) - - self.logger.info('Initialized inotify file monitoring backend, monitored resources: {}' - .format(self.watch_paths)) - - try: - for inotify_event in self.inotify_watch.event_gen(): - if inotify_event is not None: - (header, inotify_types, watch_path, filename) = inotify_event - event = None - - if 'IN_OPEN' in inotify_types: - event = PathOpenEvent(path=watch_path) - elif 'IN_MODIFY' in inotify_types: - event = PathModifyEvent(path=watch_path) - elif 'IN_CLOSE_WRITE' in inotify_types or 'IN_CLOSE_NOWRITE' in inotify_types: - event = PathCloseEvent(path=watch_path) - - if event: - self.bus.post(event) - finally: - self._cleanup() - - -# vim:sw=4:ts=4:et: - diff --git a/platypush/message/event/inotify.py b/platypush/message/event/inotify.py new file mode 100644 index 0000000000..b1ac77cb2e --- /dev/null +++ b/platypush/message/event/inotify.py @@ -0,0 +1,92 @@ +import os +from typing import Optional + +from platypush.message.event import Event + + +class InotifyEvent(Event): + """ + Generic super-class for inotify events. + """ + def __init__(self, path: str, resource: Optional[str] = None, resource_type: Optional[str] = None, + *args, **kwargs): + """ + :param path: Monitored path. + :param resource: File/resource name. + :param resource_type: INotify type of the resource, if available. + """ + kwargs['full_path'] = os.path.join(path, resource) if resource else path + super().__init__(*args, path=path, resource=resource, + resource_type=self._resource_type_code_to_name(resource_type), **kwargs) + + @staticmethod + def _resource_type_code_to_name(resource_type: Optional[str] = None) -> Optional[str]: + if resource_type == 'IN_ISDIR': + return 'directory' + + return resource_type or 'file' + + +class InotifyOpenEvent(InotifyEvent): + """ + Event triggered when a monitored resource is opened. + """ + + +class InotifyCloseEvent(InotifyEvent): + """ + Event triggered when a monitored resource is closed. + """ + + +class InotifyAccessEvent(InotifyEvent): + """ + Event triggered when a monitored resource is accessed. + """ + + +class InotifyCreateEvent(InotifyEvent): + """ + Event triggered when a monitored resource is created. + """ + + +class InotifyDeleteEvent(InotifyEvent): + """ + Event triggered when a monitored resource is deleted. + """ + + +class InotifyModifyEvent(InotifyEvent): + """ + Event triggered when a monitored resource is modified. + """ + + +class InotifyMovedEvent(InotifyEvent): + """ + Event triggered when a resource in a monitored path is moved. + """ + def __init__(self, path: str, old: Optional[str] = None, new: Optional[str] = None, *args, **kwargs): + """ + :param path: Monitored path. + :param old: Old name. + :param new: New name. + """ + super().__init__(path=path, old=old, new=new, *args, **kwargs) + + +class InotifyPermissionsChangeEvent(InotifyEvent): + """ + Event triggered when the permissions on a monitored resource are changed. + """ + def __init__(self, path: str, umask: int, resource: Optional[str] = None, *args, **kwargs): + """ + :param path: Monitored path. + :param umask: New umask. + :param resource: File/resource name. + """ + super().__init__(path=path, resource=resource, umask=umask, *args, **kwargs) + + +# vim:sw=4:ts=4:et: diff --git a/platypush/message/event/path/__init__.py b/platypush/message/event/path/__init__.py deleted file mode 100644 index d5db8307f0..0000000000 --- a/platypush/message/event/path/__init__.py +++ /dev/null @@ -1,87 +0,0 @@ -from platypush.message.event import Event - - -class PathOpenEvent(Event): - """ - Event triggered when a monitored file is opened - """ - - def __init__(self, path, *args, **kwargs): - """ - :param path: File name - :type path: str - """ - - super().__init__(path=path, *args, **kwargs) - -class PathCloseEvent(Event): - """ - Event triggered when a monitored file is closed - """ - - def __init__(self, path, *args, **kwargs): - """ - :param path: File name - :type path: str - """ - - super().__init__(path=path, *args, **kwargs) - -class PathCreateEvent(Event): - """ - Event triggered when a monitored file is created - """ - - def __init__(self, path, *args, **kwargs): - """ - :param path: File name - :type path: str - """ - - super().__init__(path=path, *args, **kwargs) - -class PathDeleteEvent(Event): - """ - Event triggered when a monitored file is deleted - """ - - def __init__(self, path, *args, **kwargs): - """ - :param path: File name - :type path: str - """ - - super().__init__(path=path, *args, **kwargs) - -class PathModifyEvent(Event): - """ - Event triggered when a monitored file is modified - """ - - def __init__(self, path, *args, **kwargs): - """ - :param path: File name - :type path: str - """ - - super().__init__(path=path, *args, **kwargs) - -class PathPermissionsChangeEvent(Event): - """ - Event triggered when the permissions on a monitored file are changed - """ - - def __init__(self, path, umask, *args, **kwargs): - """ - :param path: File name - :type path: str - - :param umask: New file umask - :type umask: int - """ - - super().__init__(path=path, umask=umask, *args, **kwargs) - - -# vim:sw=4:ts=4:et: -