From 6794633205ee149842c74d74501f8c1bb83b13c9 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Thu, 6 Sep 2012 13:54:36 -0700 Subject: [PATCH] add lock support to linux filemon --- edbob/filemon/__init__.py | 3 +++ edbob/filemon/linux.py | 34 +++++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/edbob/filemon/__init__.py b/edbob/filemon/__init__.py index 1020da8..07662e5 100644 --- a/edbob/filemon/__init__.py +++ b/edbob/filemon/__init__.py @@ -62,6 +62,9 @@ class MonitorProfile(object): func = edbob.load_spec(spec) self.actions.append((spec, func, args)) + self.locks = edbob.config.getboolean( + '%s.filemon' % appname, '%s.locks' % key, default=False) + def get_monitor_profiles(appname): """ diff --git a/edbob/filemon/linux.py b/edbob/filemon/linux.py index 3961055..23bbad1 100644 --- a/edbob/filemon/linux.py +++ b/edbob/filemon/linux.py @@ -45,16 +45,36 @@ class EventHandler(pyinotify.ProcessEvent): Event processor for file monitor daemon. """ - def my_init(self, actions=[], **kwargs): + def my_init(self, actions=[], locks=False, **kwargs): self.actions = actions + self.locks = locks + + def process_IN_ACCESS(self, event): + log.debug("EventHandler: IN_ACCESS: %s" % event.pathname) + + def process_IN_ATTRIB(self, event): + log.debug("EventHandler: IN_ATTRIB: %s" % event.pathname) + + def process_IN_CLOSE_WRITE(self, event): + log.debug("EventHandler: IN_CLOSE_WRITE: %s" % event.pathname) + if not self.locks: + self.perform_actions(event.pathname) def process_IN_CREATE(self, event): log.debug("EventHandler: IN_CREATE: %s" % event.pathname) - self.perform_actions(event.pathname) + + def process_IN_DELETE(self, event): + log.debug("EventHandler: IN_DELETE: %s" % event.pathname) + if self.locks and event.pathname.endswith('.lock'): + self.perform_actions(event.pathname[:-5]) + + def process_IN_MODIFY(self, event): + log.debug("EventHandler: IN_MODIFY: %s" % event.pathname) def process_IN_MOVED_TO(self, event): log.debug("EventHandler: IN_MOVED_TO: %s" % event.pathname) - self.perform_actions(event.pathname) + if not self.locks: + self.perform_actions(event.pathname) def perform_actions(self, path): for spec, func, args in self.actions: @@ -85,10 +105,14 @@ def start_daemon(appname, daemonize=True): monitored = get_monitor_profiles(appname) - mask = pyinotify.IN_CREATE | pyinotify.IN_MOVED_TO + mask = (pyinotify.IN_ACCESS | pyinotify.IN_ATTRIB + | pyinotify.IN_CLOSE_WRITE | pyinotify.IN_CREATE + | pyinotify.IN_DELETE | pyinotify.IN_MODIFY + | pyinotify.IN_MOVED_TO) for profile in monitored.itervalues(): for path in profile.dirs: - wm.add_watch(path, mask, proc_fun=EventHandler(actions=profile.actions)) + wm.add_watch(path, mask, proc_fun=EventHandler( + actions=profile.actions, locks=profile.locks)) if not daemonize: sys.stderr.write("Starting file monitor. (Press Ctrl+C to quit.)\n")