# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""This PollingChangeSource polls a URL for the change number.

Each change is submited to change master which triggers build steps.

Example:
To poll a change in Chromium build snapshots, use -
from buildbot.changes import url_poller
changeurl = 'http://commondatastorage.googleapis.com/'
            'chromium-browser-snapshots/Linux/LAST_CHANGE'
poller = urlpoller.URLPoller(changeurl=changeurl, pollInterval=10800)
c['change_source'] = [poller]
"""

from twisted.python import log
from twisted.web.client import getPage

from buildbot.changes import base


class URLPoller(base.PollingChangeSource):
  """Poll a URL for change number and submit to change master."""

  compare_attrs = ['changeurl', 'pollInterval']

  def __init__(self, changeurl, pollInterval=3600, category=None):
    """Initialize URLPoller.

    Args:
    changeurl: The URL to change number.
    pollInterval: The time (in seconds) between queries for
                          changes (default is 1 hour)
    """
    self.changeurl = changeurl
    self.pollInterval = pollInterval
    self.category = category
    self.last_change = None
    base.PollingChangeSource.__init__(self, changeurl, pollInterval, category)

  def describe(self):
    return 'URLPoller watching %s' % self.changeurl

  def poll(self):
    log.msg('URLPoller polling %s' % self.changeurl)
    d = getPage(self.changeurl, timeout=self.pollInterval)
    d.addCallback(self._process_changes)
    d.addErrback(self._finished_failure)
    return d

  def _finished_failure(self, res):
    log.msg('URLPoller poll failed: %s. URL: %s' % (res, self.changeurl))

  def _process_changes(self, change):
    log.msg('URLPoller finished polling %s' % self.changeurl)
    # Skip calling addChange() if this is the first successful poll.
    if self.last_change != change:
      self.master.addChange(who='committer',
                            files=[],
                            comments='comment',
                            category=self.category)
    self.last_change = change

Add a URL poller for Chromebot.
Add a URL poller for Chromebot.

Specificly for Chromebot, it just need a simple way to trigger tests when 'http://commondatastorage.googleapis.com/chromium-browser-snapshots/Linux/LAST_CHANGE' changes.

Review URL: https://chromiumcodereview.appspot.com/12079048

git-svn-id: 239fca9b83025a0b6f823aeeca02ba5be3d9fd76@180488 0039d316-1c4b-4281-b951-d872f2087c98
