From 9a26c3f8cb8d735eeb42d4815a8f6d23561c281d Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Wed, 14 May 2014 22:57:30 +0200 Subject: [PATCH] [TASK] Improve folder watch. --- .gitignore | 1 + monitor/bot.py | 2 ++ settings.ini | 8 ------- settings.ini.example | 8 +++++++ twisted/plugins/monitorbot_plugin.py | 35 +++++++++++++++++++++++++--- 5 files changed, 43 insertions(+), 11 deletions(-) delete mode 100644 settings.ini create mode 100644 settings.ini.example diff --git a/.gitignore b/.gitignore index 7f3ba45..8c6f22a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +settings.ini *.pyc *.cache diff --git a/monitor/bot.py b/monitor/bot.py index 6653252..068f2de 100644 --- a/monitor/bot.py +++ b/monitor/bot.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + from twisted.internet import protocol from twisted.python import log from twisted.words.protocols import irc diff --git a/settings.ini b/settings.ini deleted file mode 100644 index fe8cb79..0000000 --- a/settings.ini +++ /dev/null @@ -1,8 +0,0 @@ -[irc] -endpoint = tcp:host=irc.euirc.net:port=6667 -nickName = ftpd -realName = bot: provides tracking of an ftp -channel = #Tonari. - -[fsmonitor] -path=/home/kuzuru-ftp/ diff --git a/settings.ini.example b/settings.ini.example new file mode 100644 index 0000000..7135b12 --- /dev/null +++ b/settings.ini.example @@ -0,0 +1,8 @@ +[irc] +endpoint = tcp:host=irc.euirc.net:port=6667 +nickName = FDS-kun +realName = bot: provides tracking of an ftp folder +channel = #JPT + +[fsmonitor] +path=/tmp/foobar/ diff --git a/twisted/plugins/monitorbot_plugin.py b/twisted/plugins/monitorbot_plugin.py index 72e7e3b..5b235a1 100644 --- a/twisted/plugins/monitorbot_plugin.py +++ b/twisted/plugins/monitorbot_plugin.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + from ConfigParser import ConfigParser from twisted.application.service import IServiceMaker, Service @@ -19,7 +21,7 @@ class MonitorBotService(Service): self._channel = channel self._nickname = nickname self._realname = realname - self._path = path + self._watch_path = filepath.FilePath(path) def startService(self): """Construct a client & connect to server.""" @@ -40,14 +42,41 @@ class MonitorBotService(Service): self._realname ) + def humanReadableMask(mask): + flags_to_human = [ + (inotify.IN_MODIFY, 'geändert'), + (inotify.IN_CREATE, 'erstellt'), + (inotify.IN_DELETE, 'gelöscht'), + (inotify.IN_MOVED_FROM, 'umbenannt von'), + (inotify.IN_MOVED_TO, 'umbenannt nach') + ] + + s = [] + for k, v in flags_to_human: + if k & mask: + s.append(v) + return s + def fsnotify(ignored, filepath, mask): - msg = "event %s on %s" % (', '.join(inotify.humanReadableMask(mask)), filepath) + path_segments = filepath.segmentsFrom(self._watch_path) + new_path = '/'.join(path_segments) + msg = "ftp> /%s (%s)" % (new_path, ', '.join(humanReadableMask(mask))) self._bot.msg(self._channel, msg) pass + + + watchMask = ( inotify.IN_MODIFY + | inotify.IN_CREATE + | inotify.IN_DELETE + | inotify.IN_DELETE_SELF + | inotify.IN_MOVED_FROM + | inotify.IN_MOVED_TO + | inotify.IN_MOVE_SELF ) + notifier = inotify.INotify() notifier.startReading() - notifier.watch(filepath.FilePath(self._path), autoAdd=True, recursive=True, callbacks=[fsnotify]) + notifier.watch(self._watch_path, autoAdd=True, recursive=True, callbacks=[fsnotify], mask=watchMask) """Attach defined callbacks.""" return client.connect(factory).addCallbacks(connected, failure)