From bc52f6ac0c56c9551b58fb5110259d87b3a97056 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Fri, 18 May 2012 10:53:35 -0400 Subject: Added inotify filemonitor driver Moved list of files to ignore into config --- src/lib/Bcfg2/Server/FileMonitor/Inotify.py | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/lib/Bcfg2/Server/FileMonitor/Inotify.py (limited to 'src/lib/Bcfg2/Server/FileMonitor/Inotify.py') diff --git a/src/lib/Bcfg2/Server/FileMonitor/Inotify.py b/src/lib/Bcfg2/Server/FileMonitor/Inotify.py new file mode 100644 index 000000000..9743d868d --- /dev/null +++ b/src/lib/Bcfg2/Server/FileMonitor/Inotify.py @@ -0,0 +1,60 @@ +""" Inotify driver for file alteration events """ + +import os +import stat +import logging +import operator +import pyinotify +from Bcfg2.Server.FileMonitor import FileMonitor, Event + +logger = logging.getLogger(__name__) + +class InotifyEvent(Event): + action_map = {pyinotify.IN_CREATE: 'created', + pyinotify.IN_DELETE: 'deleted', + pyinotify.IN_MODIFY: 'changed'} + + def __init__(self, event): + Event.__init__(self, event.wd, event.pathname, event.mask) + if event.mask in self.action_map: + self.action = self.action_map[event.mask] + + +class Inotify(FileMonitor, pyinotify.ProcessEvent): + __priority__ = 1 + mask = pyinotify.IN_CREATE | pyinotify.IN_DELETE | pyinotify.IN_MODIFY + + def __init__(self, ignore=None, debug=False): + FileMonitor.__init__(self, ignore=ignore, debug=debug) + self.wm = pyinotify.WatchManager() + self.notifier = pyinotify.ThreadedNotifier(self.wm, self) + self.notifier.start() + + def fileno(self): + return self.wm.get_fd() + + def process_default(self, event): + self.events.append(InotifyEvent(event)) + + def AddMonitor(self, path, obj): + res = self.wm.add_watch(path, self.mask, quiet=False) + if not res: + # if we didn't get a return, but we also didn't get an + # exception, we're already watching this directory, so we + # need to find the watch descriptor for it + for wd, watch in self.wm.watches.items(): + if watch.path == path: + wd = watch.wd + else: + wd = res[path] + self.handles[wd] = obj + self.events.append(Event(wd, path, "exists")) + + mode = os.stat(path)[stat.ST_MODE] + if stat.S_ISDIR(mode): + for wname in os.listdir(path): + self.events.append(Event(wd, wname, "exists")) + return wd + + def shutdown(self): + self.notifier.stop() -- cgit v1.2.3-1-g7c22