Rev 12490 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
###
# Copyright (c) 2007, Oleksandr Moskalenko
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
###
import svnxmlparser
import threading
import time
from supybot.commands import *
import supybot.conf as conf
import supybot.utils as utils
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.ircmsgs as ircmsgs
import supybot.registry as registry
import supybot.callbacks as callbacks
class Svnwatch(callbacks.Plugin):
"""Main class of the plugin."""
threaded = True
def __init__(self, irc):
self.__parent = super(Svnwatch, self)
self.__parent.__init__(irc)
# try:
self.xmluri = self.registryValue('xmluri')
self.wait = self.registryValue('fetchPeriod')
self.postfrequency = self.registryValue('postFrequency')
# self.batchsize = self.registryValue('batchSize')
self.channels = self.registryValue('channels')
self.lastrev = self.registryValue('lastRevision')
self.lastfetch = time.time() - self.wait - 1
self.log.debug("%s; %s, %s, %s, %s" % (self.xmluri, self.wait, self.postfrequency, self.channels, self.lastrev))
# except registry.NonExistentRegistryEntry:
# self.log.debug('%s is not a registered URI.', self.xmluri)
# irc.error('Can\'t get configuration variables.', Raise=True)
self.getCommit(irc)
def canGetNewCommit(self, irc):
"""Checks if more then fetchPeriod time lapsed since our last fetch.
Returns True is that's the case and False if we need to wait longer."""
self.now = time.time()
self.log.debug("Current time: %s; Last fetch: %s; Difference: %d sec" % (self.now, self.lastfetch, int(self.now - self.lastfetch)))
if self.now - self.lastfetch > self.wait:
self.log.debug("Enough time passed, fetching new commit...")
return True
else:
self.log.debug("Not enough time passed, standing by...")
return False
def getHead(self, xmluri):
"""Fetches the head revision number for internal processing. Returns 'HEAD'
in case of error or misconfiguration."""
self.xmluri = xmluri
self.rev = "HEAD"
self.rawxml = svnxmlparser.fetchXml(self.xmluri, self.rev)
self.commitdata = svnxmlparser.getCommit(self.rawxml)
if self.commitdata == None:
self.log.info("Cannot fetch revision %s" % (self.rev))
# raise "Cannot fetch head revision id"
return None
else:
return self.commitdata.keys()[0]
def printData(self, irc, data, channel):
irc.reply(data, to=channel, prefixNick=False, private=True)
def getCommit(self, irc):
self.channels = self.registryValue('channels')
self.log.debug("Reporting channels: %s", self.channels)
self.lastrev = self.registryValue('lastRevision')
# if self.canGetNewCommit(irc):
try:
self.log.debug('Fetching HEAD revision number from %s', self.xmluri)
self.headid = self.getHead(self.xmluri)
self.log.debug('Current HEAD revision number: %s', self.headid)
if self.headid == None:
self.log.debug('Error occured while fetching HEAD revision from %s', self.xmluri)
self.now = time.time()
self.lastfetch = self.now
self.log.debug("Failed to fetch 'HEAD' at %s" % (self.now))
elif int(self.headid) == self.lastrev:
self.log.debug("Fetched 'HEAD' - no new commmits at %s", self.xmluri)
self.now = time.time()
self.lastfetch = self.now
# self.log.debug("Fetched new 'HEAD' revision at %s" % (self.now))
else:
self.oldestnewrev = self.lastrev + 1
self.revid = "%s:HEAD" % self.oldestnewrev
self.revuri = self.xmluri + "?" + self.revid
self.log.debug("Fetching new commit(s) for %s" % self.revuri)
self.rawxml = svnxmlparser.fetchXml(self.xmluri, self.revid)
self.log.debug("Fetched xml %s" % self.rawxml)
self.resultcommitdata = svnxmlparser.getCommit(self.rawxml)
self.log.debug("Fetched data %s" % self.resultcommitdata)
self.shortdata = svnxmlparser.shortoutput(self.resultcommitdata)
self.log.debug("Data list: %s" % self.shortdata)
batch = self.shortdata[:5]
# self.lastrev = self.batch[-1]
self.log.debug("Revision #: %s" % self.lastrev)
for revisiondata in batch:
self.log.debug("Revision data: %s" % revisiondata)
self.lastrev = int(revisiondata.split(" ")[1])
self.log.debug("Processed revision #%s" % self.lastrev)
self.log.debug("Revision data: %s" % revisiondata)
for channel in self.channels:
self.printData(irc, revisiondata, channel)
# irc.replies(self.revisiondata, joiner='', to=self.channel, prefixNick=False, private=True)
# irc.reply(self.revisiondata, to=self.channel, prefixNick=False, private=True)
# if len(self.shortdata) == 1:
# self.revisiondata = " ".join(self.shortdata)
# self.log.debug("Formatted data: %s" % self.revisiondata)
# self.lastrev = int(self.revisiondata.split(" ")[1])
# self.log.debug("Revision ID: %s" % self.lastrev)
# for self.channel in self.channels:
# irc.reply(self.revisiondata, joiner='', to=self.channel, prefixNick=False, private=True)
# else:
# self.log.debug("Multiple data sets detected")
# for self.revisiondata in self.shortdata:
# self.log.debug("Revision data: %s" % self.revisiondata)
# self.lastrev = int(self.revisiondata.split(" ")[1])
# self.log.debug("Last revision ID: %s" % self.lastrev)
# for self.channel in self.channels:
# self.log.debug("Sleeping in channels for %i sec." % self.postfrequency)
# self.log.debug("Revision data: %s" % self.revisiondata)
# irc.replies(self.revisiondata, joiner='', to=self.channel, prefixNick=False, private=True)
# irc.reply(self.revisiondata, to=self.channel, prefixNick=False, private=True)
# self.log.debug("Sleeping in data cycles for %i sec." % self.postfrequency) (self.postfrequency)
# sleep(self.postfrequency)
self.now = time.time()
self.lastfetch = self.now
conf.supybot.plugins.Svnwatch.lastRevision.setValue(self.lastrev)
self.log.debug("Fetched new commit(s) up to revision %s at %s"
% (self.lastrev, self.now))
except:
raise
def __call__(self, irc, msg):
self.__parent.__call__(irc, msg)
irc = callbacks.SimpleProxy(irc, msg)
if self.canGetNewCommit(irc):
self.log.debug("Invoking __call__")
self.getCommit(irc)
Class = Svnwatch
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: