#!/usr/bin/python
# Copyright (c) 2011 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.

import os
import signal
import sys
import time
import urllib2

from buildbot.scripts import runner

PID_PATH = os.path.realpath(__file__) + '.pid'

# Trigger the buildbot when LKGR is updated.
def TriggerBot(revision):
  sys.argv = ['buildbot', 'sendchange',
              '--master', 'localhost:8118',
              '--revision', revision,
              '--branch', 'src',
              '--username', 'lkgr',
              '--category', 'lkgr',
              'no file information']
  runner.run()

def main():
  # Make sure the master can kill us by writing a pid file.
  if os.path.exists(PID_PATH):
    print "Poller already running or you have a stale %s file" % PID_PATH
    sys.exit(1)

  pid_file = open(PID_PATH, 'w')
  pid_file.write(str(os.getpid()))
  pid_file.close()

  # Poll LKGR every 90 seconds. Trigger the buildbot when it changes.  
  revision = None
  while True:
    try:
      lkgr_file = urllib2.urlopen('http://chromium-status.appspot.com/lkgr')
      lkgr = lkgr_file.read()
      lkgr_file.close()
      if revision is None:
        # We don't know if we already triggered this build.
        # Skipping this revision.
        print "Set initial LKGR revision to " + lkgr
        revision = lkgr
      
      if lkgr != revision:
        # LKGR got updated. Triggering the bots.
        revision = lkgr
        TriggerBot(revision)

    except urllib2.HTTPError, e:
      print "Failed to read LKGR - HTTP error " + e.code

    except urllib2.URLError, e:
      print "Failed to read LKGR - URL error " + e.reason
  
    time.sleep(90)

# We are going away. Delete the PID file.
def clean(*args):
  if os.path.exists(PID_PATH):
    os.remove(PID_PATH)
  sys.exit(0)

if '__main__' == __name__:
  for sig in (signal.SIGINT, signal.SIGTERM):
    signal.signal(sig, clean)
    
  main()

@@ -1680,14 +1680,19 @@
 %22 + 
+str(
 e.code
+)
 %0D%0A%0D%0A

Fix a double exception when we fail to fetch lkgr from the server. Review URL: http://codereview.chromium.org/6578032