add lock support to linux filemon

This commit is contained in:
Lance Edgar 2012-09-06 13:54:36 -07:00
parent 5373e8c68b
commit 6794633205
2 changed files with 32 additions and 5 deletions

View file

@ -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):
"""

View file

@ -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")