From 53f8f88bac824cd76029aa9daeddc3abcc1e4d48 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 23 Apr 2018 16:32:35 +0200 Subject: [PATCH] Added inotify files and paths monitoring backend --- platypush/backend/inotify/__init__.py | 60 ++++++++++++++++++++++++ platypush/message/event/path/__init__.py | 30 ++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 platypush/backend/inotify/__init__.py create mode 100644 platypush/message/event/path/__init__.py diff --git a/platypush/backend/inotify/__init__.py b/platypush/backend/inotify/__init__.py new file mode 100644 index 00000000..26dad5ec --- /dev/null +++ b/platypush/backend/inotify/__init__.py @@ -0,0 +1,60 @@ +import os +import logging +import inotify.adapters + +from platypush.backend import Backend +from platypush.message.event.path import PathCreateEvent, PathDeleteEvent, \ + PathOpenEvent, PathModifyEvent, PathPermissionsChangeEvent, PathCloseEvent + +class InotifyBackend(Backend): + inotify_watch = None + + def __init__(self, watch_paths=[], **kwargs): + super().__init__(**kwargs) + self.watch_paths = set(map( + lambda path: os.path.abspath(os.path.expanduser(path)), + watch_paths)) + + def send_message(self, msg): + pass + + 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): + super().run() + + self.inotify_watch = inotify.adapters.Inotify() + for path in self.watch_paths: + self.inotify_watch.add_watch(path) + + logging.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/path/__init__.py b/platypush/message/event/path/__init__.py new file mode 100644 index 00000000..8cc2ac1d --- /dev/null +++ b/platypush/message/event/path/__init__.py @@ -0,0 +1,30 @@ +from platypush.message.event import Event + + +class PathOpenEvent(Event): + def __init__(self, path, *args, **kwargs): + super().__init__(path=path, *args, **kwargs) + +class PathCloseEvent(Event): + def __init__(self, path, *args, **kwargs): + super().__init__(path=path, *args, **kwargs) + +class PathCreateEvent(Event): + def __init__(self, path, *args, **kwargs): + super().__init__(path=path, *args, **kwargs) + +class PathDeleteEvent(Event): + def __init__(self, path, *args, **kwargs): + super().__init__(path=path, *args, **kwargs) + +class PathModifyEvent(Event): + def __init__(self, path, *args, **kwargs): + super().__init__(path=path, *args, **kwargs) + +class PathPermissionsChangeEvent(Event): + def __init__(self, path, umask, *args, **kwargs): + super().__init__(path=path, umask=umask, *args, **kwargs) + + +# vim:sw=4:ts=4:et: +