Subversion Repositories Scribus

Rev

Rev 12490 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12490 malex 1
###
2
# Copyright (c) 2007, Oleksandr Moskalenko
3
# All rights reserved.
4
#
5
#    This program is free software: you can redistribute it and/or modify
6
#    it under the terms of the GNU General Public License as published by
7
#    the Free Software Foundation, either version 3 of the License, or
8
#    (at your option) any later version.
9
#
10
#    This program is distributed in the hope that it will be useful,
11
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
#    GNU General Public License for more details.
14
#
15
#    You should have received a copy of the GNU General Public License
16
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
###
18
import svnxmlparser
19
 
20
import threading
21
import time
22
 
23
from supybot.commands import *
24
import supybot.conf as conf
25
import supybot.utils as utils
26
import supybot.plugins as plugins
27
import supybot.ircutils as ircutils
28
import supybot.ircmsgs as ircmsgs
29
import supybot.registry as registry
30
import supybot.callbacks as callbacks
31
 
32
class Svnwatch(callbacks.Plugin):
33
    """Main class of the plugin."""
34
    threaded = True
35
    def __init__(self, irc):
36
        self.__parent = super(Svnwatch, self)
37
        self.__parent.__init__(irc)
12512 malex 38
#        try:
39
        self.xmluri = self.registryValue('xmluri')
40
        self.wait = self.registryValue('fetchPeriod')
41
        self.postfrequency = self.registryValue('postFrequency')
42
#        self.batchsize = self.registryValue('batchSize')
43
        self.channels = self.registryValue('channels')
44
        self.lastrev = self.registryValue('lastRevision')
45
        self.lastfetch = time.time() - self.wait - 1
46
        self.log.debug("%s; %s, %s, %s, %s" % (self.xmluri, self.wait, self.postfrequency, self.channels, self.lastrev))
47
#        except registry.NonExistentRegistryEntry:
48
#            self.log.debug('%s is not a registered URI.', self.xmluri)
49
#            irc.error('Can\'t get configuration variables.', Raise=True)
12490 malex 50
        self.getCommit(irc)
51
 
52
    def canGetNewCommit(self, irc):
53
        """Checks if more then fetchPeriod time lapsed since our last fetch.
54
        Returns True is that's the case and False if we need to wait longer."""
55
        self.now = time.time()
56
        self.log.debug("Current time: %s; Last fetch: %s; Difference: %d sec" % (self.now, self.lastfetch, int(self.now - self.lastfetch)))
57
        if self.now - self.lastfetch > self.wait:
58
            self.log.debug("Enough time passed, fetching new commit...")
59
            return True
60
        else:
61
            self.log.debug("Not enough time passed, standing by...")
62
            return False
63
 
64
    def getHead(self, xmluri):
65
        """Fetches the head revision number for internal processing. Returns 'HEAD'
66
        in case of error or misconfiguration."""
67
        self.xmluri = xmluri
68
        self.rev = "HEAD"
69
        self.rawxml = svnxmlparser.fetchXml(self.xmluri, self.rev)
70
        self.commitdata = svnxmlparser.getCommit(self.rawxml)
71
        if self.commitdata == None:
72
            self.log.info("Cannot fetch revision %s" % (self.rev))
73
#            raise "Cannot fetch head revision id"
74
            return None
75
        else:
76
            return self.commitdata.keys()[0]
77
 
12512 malex 78
    def printData(self, irc, data, channel):
79
        irc.reply(data, to=channel, prefixNick=False, private=True)
80
 
12490 malex 81
    def getCommit(self, irc):
82
        self.channels = self.registryValue('channels')
83
        self.log.debug("Reporting channels: %s", self.channels)
84
        self.lastrev = self.registryValue('lastRevision')
85
#        if self.canGetNewCommit(irc):
86
        try:
87
            self.log.debug('Fetching HEAD revision number from %s', self.xmluri)
88
            self.headid = self.getHead(self.xmluri)
89
            self.log.debug('Current HEAD revision number: %s', self.headid)
90
            if self.headid == None:
91
                self.log.debug('Error occured while fetching HEAD revision from %s', self.xmluri)
92
                self.now = time.time()
93
                self.lastfetch = self.now
94
                self.log.debug("Failed to fetch 'HEAD' at %s" % (self.now))
95
            elif int(self.headid) == self.lastrev:
96
                self.log.debug("Fetched 'HEAD' - no new commmits at %s", self.xmluri)
97
                self.now = time.time()
98
                self.lastfetch = self.now
12512 malex 99
#                self.log.debug("Fetched new 'HEAD' revision at %s" % (self.now))
12490 malex 100
            else:
101
                self.oldestnewrev = self.lastrev + 1
102
                self.revid = "%s:HEAD" % self.oldestnewrev
103
                self.revuri = self.xmluri + "?" + self.revid
104
                self.log.debug("Fetching new commit(s) for %s" % self.revuri)
105
                self.rawxml = svnxmlparser.fetchXml(self.xmluri, self.revid)
106
                self.log.debug("Fetched xml %s" % self.rawxml)
107
                self.resultcommitdata = svnxmlparser.getCommit(self.rawxml)
108
                self.log.debug("Fetched data %s" % self.resultcommitdata)
109
                self.shortdata = svnxmlparser.shortoutput(self.resultcommitdata)
12512 malex 110
                self.log.debug("Data list: %s" % self.shortdata)
111
                batch = self.shortdata[:5]
112
#                self.lastrev = self.batch[-1]
113
                self.log.debug("Revision #: %s" % self.lastrev)
114
                for revisiondata in batch:
115
                    self.log.debug("Revision data: %s" % revisiondata)
116
                    self.lastrev = int(revisiondata.split(" ")[1])
117
                    self.log.debug("Processed revision #%s" % self.lastrev)
118
                    self.log.debug("Revision data: %s" % revisiondata)
119
                    for channel in self.channels:
120
                        self.printData(irc, revisiondata, channel)
121
#                        irc.replies(self.revisiondata, joiner='', to=self.channel, prefixNick=False, private=True)
122
#                        irc.reply(self.revisiondata, to=self.channel, prefixNick=False, private=True)
123
#                if len(self.shortdata) == 1:
124
#                    self.revisiondata = " ".join(self.shortdata)
125
#                    self.log.debug("Formatted data: %s" % self.revisiondata)
126
#                    self.lastrev = int(self.revisiondata.split(" ")[1])
127
#                    self.log.debug("Revision ID: %s" % self.lastrev)
128
#                    for self.channel in self.channels:
129
#                        irc.reply(self.revisiondata, joiner='', to=self.channel, prefixNick=False, private=True)
130
#                else:
131
#                    self.log.debug("Multiple data sets detected")
132
#                    for self.revisiondata in self.shortdata:
133
#                        self.log.debug("Revision data: %s" % self.revisiondata)
134
#                        self.lastrev = int(self.revisiondata.split(" ")[1])
135
#                        self.log.debug("Last revision ID: %s" % self.lastrev)
136
#                        for self.channel in self.channels:
137
#                            self.log.debug("Sleeping in channels for %i sec." % self.postfrequency)
138
#                            self.log.debug("Revision data: %s" % self.revisiondata)
139
#                            irc.replies(self.revisiondata, joiner='', to=self.channel, prefixNick=False, private=True)
140
#                            irc.reply(self.revisiondata, to=self.channel, prefixNick=False, private=True)
141
#                        self.log.debug("Sleeping in data cycles for %i sec." % self.postfrequency) (self.postfrequency)
142
#                sleep(self.postfrequency)
12490 malex 143
                self.now = time.time()
144
                self.lastfetch = self.now
145
                conf.supybot.plugins.Svnwatch.lastRevision.setValue(self.lastrev)
146
                self.log.debug("Fetched new commit(s) up to revision %s at %s"
147
                        % (self.lastrev, self.now))
148
        except:
149
            raise
150
 
151
    def __call__(self, irc, msg):
152
        self.__parent.__call__(irc, msg)
153
        irc = callbacks.SimpleProxy(irc, msg)
154
        if self.canGetNewCommit(irc):
155
            self.log.debug("Invoking __call__")
156
            self.getCommit(irc)
157
 
158
Class = Svnwatch
159
 
160
 
161
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: