forked from platypush/platypush
Added inotify files and paths monitoring backend
This commit is contained in:
parent
c4401b25be
commit
53f8f88bac
2 changed files with 90 additions and 0 deletions
60
platypush/backend/inotify/__init__.py
Normal file
60
platypush/backend/inotify/__init__.py
Normal file
|
@ -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:
|
||||||
|
|
30
platypush/message/event/path/__init__.py
Normal file
30
platypush/message/event/path/__init__.py
Normal file
|
@ -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:
|
||||||
|
|
Loading…
Reference in a new issue