author ffxbld
Thu, 07 Apr 2016 19:51:06 -0400
changeset 1612 426bd32d5aa7549e518aca5e3b75465ab400d551
parent 97 c2e02e5bbfdb1c7a463cb44d75b06d45070597d3
permissions -rw-r--r--
Added FIREFOX_45_0_2esr_RELEASE FIREFOX_45_0_2esr_BUILD1 tag(s) for changeset production-0.8. DONTBUILD CLOSED TREE a=release

# GoogleCode Atom Feed Poller
# Author: Srivats P. <pstavirs>
# Based on Mozilla's HgPoller
# Description:
#   Use this ChangeSource for projects hosted on
#   This ChangeSource uses the project's commit Atom feed. Depending upon the
#   frequency of commits, you can tune the polling interval for the feed
#   (default is 1 hour)
# Parameters:
#   feedurl (MANDATORY): The Atom feed URL of the GoogleCode repo
#   pollinterval (OPTIONAL): Polling frequency for the feed (in seconds)
# Example:
# To poll the Ostinato project's commit feed every 3 hours, use -
#   from googlecode_atom import GoogleCodeAtomPoller
#   poller = GoogleCodeAtomPoller(
#            feedurl="",
#            pollinterval=10800)
#   c['change_source'] = [ poller ]

from time import strptime
from calendar import timegm
from xml.dom import minidom, Node

from twisted.python import log, failure
from twisted.internet import defer, reactor
from twisted.internet.task import LoopingCall
from twisted.web.client import getPage

from buildbot.changes import base, changes

def googleCodePollerForProject(project, vcs, pollinterval=3600):
    return GoogleCodeAtomPoller(
        '' % (project, vcs),

class GoogleCodeAtomPoller(base.ChangeSource):
    """This source will poll a GoogleCode Atom feed for changes and
    submit them to the change master. Works for both Svn and Hg repos.
    TODO: branch processing

    compare_attrs = ['feedurl', 'pollinterval']
    parent = None
    loop = None
    volatile = ['loop']
    working = False

    def __init__(self, feedurl, pollinterval=3600):
        @type   feedurl:        string
        @param  feedurl:        The Atom feed URL of the GoogleCode repo

        @type   pollinterval:   int
        @param  pollinterval:   The time (in seconds) between queries for
                                changes (default is 1 hour)

        self.feedurl = feedurl
        self.branch = None
        self.pollinterval = pollinterval
        self.lastChange = None
        self.loop = LoopingCall(self.poll)

    def startService(self):
        log.msg("GoogleCodeAtomPoller starting")
        reactor.callLater(0, self.loop.start, self.pollinterval)

    def stopService(self):
        log.msg("GoogleCodeAtomPoller stoppping")
        return base.ChangeSource.stopService(self)

    def describe(self):
        return ("Getting changes from the GoogleCode repo changes feed %s" %

    def poll(self):
        if self.working:
            log.msg("Not polling because last poll is still working")
            self.working = True
            d = self._get_changes()
            d.addCallbacks(self._finished_ok, self._finished_failure)

    def _finished_ok(self, res):
        assert self.working
        self.working = False
        log.msg("GoogleCodeAtomPoller poll success")

        return res

    def _finished_failure(self, res):
        log.msg("GoogleCodeAtomPoller poll failed: %s" % res)
        assert self.working
        self.working = False
        return None

    def _make_url(self):
        return "%s" % (self.feedurl)

    def _get_changes(self):
        url = self._make_url()
        log.msg("GoogleCodeAtomPoller polling %s" % url)

        return getPage(url, timeout=self.pollinterval)

    def _parse_changes(self, query):
        dom = minidom.parseString(query)
        entries = dom.getElementsByTagName("entry")
        changes = []
        # Entries come in reverse chronological order
        for i in entries:
            d = {}

            # revision is the last part of the 'id' url
            d["revision"] = i.getElementsByTagName(
            if d["revision"] == self.lastChange:
                break  # no more new changes

            d["when"] = timegm(strptime(
            d["author"] = i.getElementsByTagName(
            # files and commit msg are separated by 2 consecutive <br/>
            content = i.getElementsByTagName(
                "content")[0]"<br/>\n <br/>")
            # Remove the action keywords from the file list
            fl = content[0].replace(
                u' \xa0\xa0\xa0\xa0Add\xa0\xa0\xa0\xa0', '').replace(
                u' \xa0\xa0\xa0\xa0Delete\xa0\xa0\xa0\xa0', '').replace(
                u' \xa0\xa0\xa0\xa0Modify\xa0\xa0\xa0\xa0', '')
            # Get individual files and remove the 'header'
            d["files"] = fl.encode("ascii", "replace").split("<br/>")[1:]
            d["files"] = [f.strip() for f in d["files"]]
                d["comments"] = content[1].encode("ascii", "replace")
                d["comments"] = "No commit message provided"


        changes.reverse() # want them in chronological order
        return changes

    def _process_changes(self, query):
        change_list = self._parse_changes(query)

        # Skip calling addChange() if this is the first successful poll.
        if self.lastChange is not None:
            for change in change_list:
                c = changes.Change(revision = change["revision"],
                                   who = change["author"],
                                   files = change["files"],
                                   comments = change["comments"],
                                   when = change["when"],
                                   branch = self.branch)
        if change_list:
            self.lastChange = change_list[-1]["revision"]