# -*- python -*-
# ex: set syntax=python:
import os

ROBOT_FRAMEWORK_REPOSITORY = 'http://robotframework.googlecode.com/svn/trunk/'
# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

####### BUILDSLAVES
from buildbot.buildslave import BuildSlave
c['slaves'] = [BuildSlave("debian-py2.4", "robotci")]
c['slavePortnum'] = 9989

####### CHANGESOURCES
from buildbot.changes.svnpoller import SVNPoller
c['change_source'] = SVNPoller(ROBOT_FRAMEWORK_REPOSITORY, pollinterval=180)

####### SCHEDULERS
from buildbot.scheduler import Scheduler
c['schedulers'] = []
c['schedulers'].append(Scheduler(name="all", branch=None, treeStableTimer=180,
                                 builderNames=["PybotTests"]))

####### BUILDERS

# the 'builders' list defines the Builders. Each one is configured with a
# dictionary, using the following keys:
#  name (required): the name used to describe this bilder
#  slavename (required): which slave to use, must appear in c['bots']
#  builddir (required): which subdirectory to run the builder in
#  factory (required): a BuildFactory to define how the build is run
#  periodicBuildTime (optional): if set, force a build every N seconds

from buildbot.process import factory
from buildbot.steps.source import SVN
from buildbot.steps.shell import ShellCommand
from buildbot.steps.master import MasterShellCommand
from buildbot.steps.transfer import FileUpload
import glob

OUTPUT_ARCHIVE = 'outputs.zip'
RESULT_DIR = 'results'

class ReportGenerator(MasterShellCommand):
    
    def __init__(self, **kwargs):
        command = ['./generate_reports.sh', RESULT_DIR]
        MasterShellCommand.__init__(self, command)
        self.addFactoryArguments(command=command)

    def finished(self, results):
        report = open(RESULT_DIR + '/report.html').read().replace('<a href="log.html', 
                                                                  '<a href="log')
        self.addHTMLLog('report', report)
        self.addHTMLLog('log', open(RESULT_DIR + '/log.html').read())
        for sublog in sorted(glob.glob(RESULT_DIR + '/log-*.html')):
            self.addHTMLLog(os.path.basename(sublog), open(sublog).read())
        return MasterShellCommand.finished(self, results)


f1 = factory.BuildFactory()
f1.addStep(SVN(svnurl=ROBOT_FRAMEWORK_REPOSITORY))
f1.addStep(ShellCommand(command=['python', './install.py', 'in'], 
				        description='Installing', 
				        descriptionDone='Install'))
f1.addStep(ShellCommand(command=['atest/run_atests.py', 'buildbot', 'python', 
				                 '--monitorcolors off', 
                                 '--exclude manual',
                                 'atest/robot/'],
                        description='Robot Tests',
				        descriptionDone='Robot Tests',
                        timeout=60*60))
f1.addStep(FileUpload(slavesrc='atest/results/' + OUTPUT_ARCHIVE, 
                      masterdest=RESULT_DIR +'/'+ OUTPUT_ARCHIVE))
f1.addStep(ReportGenerator())

b1 = {'name': "PybotTests",
      'slavename': "debian-py2.4",
      'builddir': "pybot-build",
      'factory': f1}
c['builders'] = [b1]


####### STATUS TARGETS

from buildbot.status import html
c['status'] = []
c['status'].append(html.WebStatus(http_port=8010))

from buildbot.status import mail
c['status'].append(mail.MailNotifier(fromaddr="buildbot@robot.radiaatto.ri.fi",
                                     extraRecipients=["robotframework-commit@googlegroups.com"],
                                     sendToInterestedUsers=False,
				     relayhost='10.127.0.12'))
#
# from buildbot.status import words
# c['status'].append(words.IRC(host="irc.example.com", nick="bb",
#                              channels=["#example"]))
#
# from buildbot.status import client
# c['status'].append(client.PBListener(9988))


####### DEBUGGING OPTIONS

# if you set 'debugPassword', then you can connect to the buildmaster with
# the diagnostic tool in contrib/debugclient.py . From this tool, you can
# manually force builds and inject changes, which may be useful for testing
# your buildmaster without actually commiting changes to your repository (or
# before you have a functioning 'sources' set up). The debug tool uses the
# same port number as the slaves do: 'slavePortnum'.

c['debugPassword'] = "passwd"

# if you set 'manhole', you can ssh into the buildmaster and get an
# interactive python shell, which may be useful for debugging buildbot
# internals. It is probably only useful for buildbot developers. You can also
# use an authorized_keys file, or plain telnet.
#from buildbot import manhole
#c['manhole'] = manhole.PasswordManhole("tcp:9999:interface=127.0.0.1",
#                                       "admin", "password")


####### PROJECT IDENTITY

# the 'projectName' string will be used to describe the project that this
# buildbot is working on. For example, it is used as the title of the
# waterfall HTML page. The 'projectURL' string will be used to provide a link
# from buildbot HTML pages to your project's home page.

c['projectName'] = "Robot Framework"
c['projectURL'] = "http://robotframework.org/"

# the 'buildbotURL' string should point to the location where the buildbot's
# internal web server (usually the html.Waterfall page) is visible. This
# typically uses the port number set in the Waterfall 'status' entry, but
# with an externally-visible host name which the buildbot cannot figure out
# without some help.

c['buildbotURL'] = "http://robot.radiaatto.ri.fi:8080/"
