From e8b325fc5e82919ac10c3f93bb6e19e48026ebcd Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Sun, 17 Nov 2013 20:27:23 +0100 Subject: [PATCH] inotify monitor skeleton --- manifests/default.pp | 2 +- src/back/kite/distdaemon.py | 49 ----------------------------------- src/back/kite/filterdaemon.py | 22 ++++++++++++++++ 3 files changed, 23 insertions(+), 50 deletions(-) delete mode 100644 src/back/kite/distdaemon.py create mode 100644 src/back/kite/filterdaemon.py diff --git a/manifests/default.pp b/manifests/default.pp index 612fdca..1005cb2 100644 --- a/manifests/default.pp +++ b/manifests/default.pp @@ -6,7 +6,7 @@ exec {"apt-get update": path => ["/usr/bin/", "/usr/sbin"], } -$packages = ["python", "bpython", "sl", "mutt"] +$packages = ["python", "bpython", "mutt", "python-pyinotify"] $kite_gid = "5000" $kite_uid = "500" diff --git a/src/back/kite/distdaemon.py b/src/back/kite/distdaemon.py deleted file mode 100644 index 3053952..0000000 --- a/src/back/kite/distdaemon.py +++ /dev/null @@ -1,49 +0,0 @@ -# distdaemon.py -# Postfix forwards all the emails sent and received by the machine -# to one maildir. -# -# This background process distributes the emails to each user. -# -# It also handles email threads and filters. - -import time -import fcntl -import os -import signal -import utils -import time - -# FIXME: swap this out someday with inotify, or python-watchdog -watched_dirs = {} -last_check_time = 0 - -def handler(signum, frame): - for dirname in watched_dirs: - dir = watched_dirs[dirname] - mtime = os.path.getmtime(dirname) - if mtime > last_check_time: - print "found modified folder" - last_check_time = int(time.time()) - -signal.signal(signal.SIGIO, handler) - -def watch_dir(dirname): - if dirname in watched_dirs: - return - - fd = os.open(dirname, os.O_RDONLY) - watched_dirs[dirname] = {fd: fd} - watched_dirs[dirname]["mtime"] = os.path.getmtime(dirname) - - fcntl.fcntl(fd, fcntl.F_SETSIG, 0) - fcntl.fcntl(fd, fcntl.F_NOTIFY, - fcntl.DN_MODIFY | fcntl.DN_CREATE | fcntl.DN_MULTISHOT) - -def find_modified_files(folder): - for file in os.listdir(folder): - if os.path.getmtime(os.path.join(folder, file)) >= dir_mtime: - print "file %s modified" % file - -watch_dir("/tmp/t") -while True: - time.sleep(10000) diff --git a/src/back/kite/filterdaemon.py b/src/back/kite/filterdaemon.py new file mode 100644 index 0000000..b0686f2 --- /dev/null +++ b/src/back/kite/filterdaemon.py @@ -0,0 +1,22 @@ +# filterdaemon.py +# This background process creates metadata files for threads and filters. +# +import sys +import pyinotify + +wm = pyinotify.WatchManager() # Watch Manager +mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE # watched events + +class EventHandler(pyinotify.ProcessEvent): + def process_IN_CREATE(self, event): + print "Creating:", event.pathname + + def process_IN_DELETE(self, event): + print "Removing:", event.pathname + +handler = EventHandler() +notifier = pyinotify.Notifier(wm, handler) +wdd = wm.add_watch(sys.argv[1], mask, rec=True) +print "Watching %s" % sys.argv[1] + +notifier.loop() -- 2.43.0