r97773 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97772‎ | r97773 | r97774 >
Date:00:14, 22 September 2011
Author:laner
Status:deferred
Tags:
Comment:
Make ircecho inotify use watch_transient_file, so that files in logrotated directories will still be read
Modified paths:
  • /trunk/debs/ircecho/ircecho (modified) (history)

Diff [purge]

Index: trunk/debs/ircecho/ircecho
@@ -8,33 +8,26 @@
99
1010 import sys
1111 import pyinotify
12 -import ircbot
13 -import irclib
1412 import threading
15 -
1613 from optparse import OptionParser
 14+sys.path.append('/usr/ircecho/lib')
 15+from ircbot import SingleServerIRCBot
1716
1817 class EchoReader(threading.Thread):
19 - def __init__(self, bot, chans, infile=''):
 18+ def __init__(self, infile=''):
2019 threading.Thread.__init__(self)
21 - self.bot = bot
22 - self.chans = chans
2320 self.infile = infile
2421
2522 def run(self):
2623 if self.infile:
2724 print "Using infile"
 25+ self.f = open(self.infile)
 26+ # Seek to the end of the file
 27+ self.f.seek(0,2)
2828 wm = pyinotify.WatchManager()
29 - mask = pyinotify.IN_MODIFY
30 - f = open(self.infile)
31 - # Seek to the end of the file
32 - f.seek(0,2)
33 - handler = EventHandler()
34 - handler.infile = f
35 - handler.bot = self.bot
36 - handler.chans = self.chans
37 - notifier = pyinotify.Notifier(wm, handler)
38 - wdd = wm.add_watch(self.infile, mask)
 29+ notifier = pyinotify.Notifier(wm)
 30+ mask = pyinotify.IN_MODIFY | pyinotify.IN_CREATE
 31+ wdd = wm.watch_transient_file(self.infile, mask, EventHandler)
3932 notifier.loop()
4033 else:
4134 while True:
@@ -48,10 +41,10 @@
4942 except Exception:
5043 pass
5144
52 -class EchoBot(ircbot.SingleServerIRCBot):
 45+class EchoBot(SingleServerIRCBot):
5346 def __init__(self, chans, nickname, server):
5447 print "*** Connecting to IRC server %s..." % server
55 - ircbot.SingleServerIRCBot.__init__(self, [(server, 6667)], nickname, "IRC echo bot")
 48+ SingleServerIRCBot.__init__(self, [(server, 6667)], nickname, "IRC echo bot")
5649 self.chans = chans
5750
5851 def on_nicknameinuse(self, c, e):
@@ -59,14 +52,22 @@
6053
6154 def on_welcome(self, c, e):
6255 print "*** Connected"
63 - for chan in self.chans:
 56+ for chan in [self.chans]:
6457 c.join(chan)
6558
6659 class EventHandler(pyinotify.ProcessEvent):
6760 def process_IN_MODIFY(self, event):
68 - s = self.infile.read()
69 - self.bot.connection.privmsg(self.chans, s)
 61+ s = reader.f.read()
 62+ bot.connection.privmsg(bot.chans, s)
7063
 64+ def process_IN_CREATE(self, event):
 65+ try:
 66+ print "Reopening file"
 67+ reader.f = open(reader.infile)
 68+ except IOError:
 69+ print "Failed to reopen file"
 70+ pass
 71+
7172 parser = OptionParser(conflict_handler="resolve")
7273 parser.set_usage("ircecho [--infile=<filename>] <channel> <nickname> <server>")
7374 parser.add_option("--infile", dest="infile", help="Read input from the specific file instead of from stdin")
@@ -74,7 +75,9 @@
7576 chans = args[0]
7677 nickname = args[1]
7778 server = args[2]
78 -bot = EchoBot([chans], nickname, server)
79 -sthr = EchoReader(bot, chans, options.infile)
80 -sthr.start()
 79+global bot
 80+bot = EchoBot(chans, nickname, server)
 81+global reader
 82+reader = EchoReader(options.infile)
 83+reader.start()
8184 bot.start()

Status & tagging log