From e251f094837691bd7ec4ad5618f42b068bfaaac1 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 29 Aug 2012 16:25:08 -0700 Subject: [PATCH] fix filemon support in linux --- edbob/commands.py | 9 ++++++++- edbob/filemon/linux.py | 32 ++++++++++++++------------------ setup.py | 16 +++++++++++++++- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/edbob/commands.py b/edbob/commands.py index 0dc4e9b..6c47598 100644 --- a/edbob/commands.py +++ b/edbob/commands.py @@ -412,6 +412,8 @@ class FileMonitorCommand(Subcommand): name = 'filemon' description = "Manage the file monitor service" + appname = 'edbob' + def add_parser_args(self, parser): subparsers = parser.add_subparsers(title='subcommands') @@ -430,6 +432,11 @@ class FileMonitorCommand(Subcommand): uninstall = subparsers.add_parser('uninstall', help="Uninstall (remove) service") uninstall.set_defaults(subcommand='remove') + else: + parser.add_argument('-D', '--dont-daemonize', + action='store_false', dest='daemonize', + help="Don't daemonize when starting") + def get_win32_module(self): from edbob.filemon import win32 return win32 @@ -447,7 +454,7 @@ class FileMonitorCommand(Subcommand): from edbob.filemon import linux as filemon if args.subcommand == 'start': - filemon.start_daemon() + filemon.start_daemon(self.appname, daemonize=args.daemonize) elif args.subcommand == 'stop': filemon.stop_daemon() diff --git a/edbob/filemon/linux.py b/edbob/filemon/linux.py index 4715a58..3961055 100644 --- a/edbob/filemon/linux.py +++ b/edbob/filemon/linux.py @@ -30,10 +30,14 @@ import sys import os import os.path import signal +import logging import pyinotify import edbob -from edbob.filemon import MonitorProfile +from edbob.filemon import get_monitor_profiles + + +log = logging.getLogger(__name__) class EventHandler(pyinotify.ProcessEvent): @@ -45,20 +49,15 @@ class EventHandler(pyinotify.ProcessEvent): self.actions = actions def process_IN_CREATE(self, event): + log.debug("EventHandler: IN_CREATE: %s" % event.pathname) self.perform_actions(event.pathname) def process_IN_MOVED_TO(self, event): + log.debug("EventHandler: IN_MOVED_TO: %s" % event.pathname) self.perform_actions(event.pathname) def perform_actions(self, path): - for action in self.actions: - if isinstance(action, tuple): - func = action[0] - args = action[1:] - else: - func = action - args = [] - func = edbob.load_spec(func) + for spec, func, args in self.actions: func(path, *args) @@ -71,7 +70,7 @@ def get_pid_path(): return '/tmp/%s_filemon.pid' % basename -def start_daemon(): +def start_daemon(appname, daemonize=True): """ Starts the file monitor daemon. """ @@ -84,19 +83,16 @@ def start_daemon(): wm = pyinotify.WatchManager() notifier = pyinotify.Notifier(wm) - monitored = {} - keys = edbob.config.require('edbob.filemon', 'monitored') - keys = keys.split(',') - for key in keys: - key = key.strip() - monitored[key] = MonitorProfile(key) + monitored = get_monitor_profiles(appname) mask = pyinotify.IN_CREATE | 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)) - notifier.loop(daemonize=True, pid_file=pid_path) + if not daemonize: + sys.stderr.write("Starting file monitor. (Press Ctrl+C to quit.)\n") + notifier.loop(daemonize=daemonize, pid_file=pid_path) def stop_daemon(): @@ -113,7 +109,7 @@ def stop_daemon(): pid = f.read().strip() f.close() if not pid.isdigit(): - print "Hm, found bogus PID:", pid + log.warning("stop_daemon: Found bogus PID (%s) in file: %s" % (pid, pid_path)) return os.kill(int(pid), signal.SIGKILL) diff --git a/setup.py b/setup.py index 6eb94e4..4fe2b01 100644 --- a/setup.py +++ b/setup.py @@ -104,6 +104,13 @@ extras = { 'Sphinx', # 1.1.3 ], + 'filemon': [ + # + # package # low high + + # This is just a placeholder on Windows; Linux requires this extra. + ], + 'pyramid': [ # # package # low high @@ -147,7 +154,7 @@ if sys.platform == 'win32': 'py-bcrypt-w32', # 0.2.2 ] -else: +elif sys.platform == 'linux2': extras['db'] += [ # @@ -156,6 +163,13 @@ else: 'py-bcrypt', # 0.2 ] + extras['filemon'] += [ + # + # package # low high + + 'pyinotify', # 0.9.3 + ] + extras['pyramid'] += [ # # package # low high