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

# Global variables
buildbot_home = '/var/lib/buildbot'
vm_slave_home = buildbot_home + '/slaves/vm-project-spobuild'
adept_slave_home = buildbot_home + '/slaves/adept'

# This is a sample buildmaster config file. It must be installed as
# 'master.cfg' in your buildmaster's base directory.

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

####### BUILDSLAVES

# The 'slaves' list defines the set of recognized buildslaves. Each element is
# a BuildSlave object, specifying a unique slave name and password.  The same
# slave name and password must be configured on the slave.
from buildbot.buildslave import BuildSlave
from secrets import slavepass
c['slaves'] = [BuildSlave("vm-project-spobuild", slavepass),
               BuildSlave("adept", slavepass)]

# 'slavePortnum' defines the TCP port to listen on for connections from slaves.
# This must match the value configured into the buildslaves (with their
# --master option)
c['slavePortnum'] = 9989

####### CHANGESOURCES

# the 'change_source' setting tells the buildmaster how it should find out
# about source code changes.  Here we point to the buildbot clone of pyflakes.

from buildbot.changes.pb import PBChangeSource
c['change_source'] = [PBChangeSource()]

####### SCHEDULERS

# Configure the Schedulers, which decide how to react to incoming changes.  In this
# case, just kick off a 'runtests' build

from buildbot.scheduler import Scheduler
c['schedulers'] = []
c['schedulers'].append(Scheduler(name="all",
                           branch='master',
                           treeStableTimer=None,
                           builderNames=["mcfc-output"]))

####### BUILDERS

# The 'builders' list defines the Builders, which tell Buildbot how to perform a build:
# what steps, and which slaves can execute them.  Note that any particular build will
# only take place on one slave.

from buildbot.config import BuilderConfig
from buildbot.process.factory import BuildFactory, s
from buildbot.steps.source import Git
from buildbot.steps.shell import SetProperty, ShellCommand
from buildbot.process import factory
from buildbot.process.properties import Property

#### MCFC

def gen_mcfc_env(mcfc_builder):
    bin_dir = buildbot_home + '/bin'
    mcfc_dir = vm_slave_home+'/mcfc_builder'+'/build'
    lib_dir = buildbot_home + '/lib'
    return {
        'MCFC_DIR': mcfc_dir,
        'PATH': bin_dir+':/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
        'PYTHONPATH': mcfc_dir,
        'LD_LIBRARY_PATH': lib_dir
    }

def mcfc_builder(name, repourl, env):
    fac = BuildFactory()
    # check out the source
    fac.addStep(Git(repourl=repourl, mode='update'))
    # run the tests
    fac.addStep(
        ShellCommand(command=["python", "autotest.py", "--non-interactive", "--with-objvis"],
            env=env,
            workdir="build/tests",
            description="running the autotests") )
    return BuilderConfig(name=name, slavenames=["vm-project-spobuild"], factory=fac)

# MCFC autotests (runtests)
# Builder for MCFC autotests run on the master branch

mcfc_repourl = 'git://github.com/gmarkall/manycore_form_compiler.git'
mcfc_env=gen_mcfc_env('mcfc-output')

c['builders'] = []
c['builders'].append( mcfc_builder('mcfc-output', mcfc_repourl, mcfc_env) )

#### OP2 Common sequential

def gen_op2_env():
    return {'OP2_COMPILER': 'gnu'}

def op2_common_builder(name, repourl, env):
    fac = BuildFactory()
    fac.addStep(Git(repourl=repourl, mode='update'))
    fac.addStep(
        ShellCommand(command=["make", "seq"], 
                     env=env, 
                     workdir='build/op2/c',
                     description='Building OP2-Common'))
    return BuilderConfig(name=name, slavenames=["adept"], factory=fac)

op2_common_url = 'git://github.com/carlobertolli/OP2-Common.git'

c['builders'].append( op2_common_builder('op2-common', op2_common_url, gen_op2_env()))

####### STATUS TARGETS

# 'status' is a list of Status Targets. The results of each build will be
# pushed to these targets. buildbot/status/*.py has a variety to choose from,
# including web pages, email senders, and IRC bots.

c['status'] = []

from buildbot.status import html
from buildbot.status.web import authz
authz_cfg=authz.Authz(
    # change any of these to True to enable; see the manual for more
    # options
    gracefulShutdown = False,
    forceBuild = True, # use this to test your slave once it is set up
    forceAllBuilds = False,
    pingBuilder = False,
    stopBuild = False,
    stopAllBuilds = False,
    cancelPendingBuild = False,
)
c['status'].append(html.WebStatus(http_port=8010, authz=authz_cfg, change_hook_dialects={'github': True}))

from buildbot.status import words
irc = words.IRC("irc.freenode.net", "mapdes-bot",
             channels=["mcfc"],
             allowForce=True,
             notify_events={
               'exception': 1,
               'successToFailure': 1,
               'failureToSuccess': 1,
             })
c['status'].append(irc)

####### PROJECT IDENTITY

# the 'title' string will appear at the top of this buildbot
# installation's html.WebStatus home page (linked to the
# 'titleURL') and is embedded in the title of the waterfall HTML page.

c['title'] = "MAPDES"
c['titleURL'] = "http://gow.epsrc.ac.uk/ViewGrant.aspx?GrantRef=EP/I00677X/1"

# the 'buildbotURL' string should point to the location where the buildbot's
# internal web server (usually the html.WebStatus 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://vm-project-spobuild.doc.ic.ac.uk:8010/"

####### DB URL

# This specifies what database buildbot uses to store change and scheduler
# state.  You can leave this at its default for all but the largest
# installations.
c['db_url'] = "sqlite:///state.sqlite"

