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

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

import re, time
import itertools

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

#SP epw        http://qeforge.qe-forge.org/svn/epw/branches/sponce/epw
#SP qe         http://qeforge.qe-forge.org/svn/q-e/trunk/espresso
#source_root = 'http://qeforge.qe-forge.org/svn/'
#source_user = 'qe-anonymous'
qe_project = 'https://gitlab.com/QEF/q-e.git'
projects = ['https://gitlab.com/QEF/q-e.git']

#all_repos = {
#    'quantum_espresso': {
#        'repository': 'https://github.com/QEF/q-e.git',
#        'branch': 'master',
#    },
#    'sternheimer_gw': {
#        'repository': 'mml_farm@maroon.materials.ox.ac.uk:~/SGW/',
#        'branch': 'develop',
#    },
#    'wannier90': {
#        'repository': 'https://github.com/wannier-developers/wannier90.git',
#        'branch': 'develop',
#    },
#}

passwd = {
    'source': '',
   'farmer-slave1': 'XXXXXXX',
}

from buildbot.locks import MasterLock
source_lock = MasterLock('source')

#slave_builders = { "farmer-slave1": ["farmer_gcc730_openmpi1107","farmer_pgi18_mvapich23b",\
#                                     "farmer_intel18_impi","farmer_intel18_mvapich23", "farmer_gcc650_serial"]}
slave_builders = { "farmer-slave1": ["farmer_gcc640_serial","farmer_gcc730_openmpi1107","farmer_intel18_openmpi313",\
                                     "farmer_pgi18_mvapich23b","farmer_intel17_openmpi313","farmer_intel17_impi",\
                                     "farmer_intel18_openmpi313_openmp"]}

slaves = slave_builders.keys()
builders = list( itertools.chain.from_iterable( slave_builders.values() ) )
print builders

def getslavebybuilders(b):
        for s in slaves:
                for i in range(len(slave_builders[s])):
                        if b == slave_builders[s][i]:
                                return s
        return None

builder_epw = []
#builder_sgw = []
builder_wan = []
#builder_test = []
for v in builders:
  builder_epw.extend(["%s-%s" % ('QE', v)])
#  if v != "farmer_gcc640_serial":
#    builder_sgw.extend(["%s-%s" % ('SGW', v)])
  builder_wan.extend(["%s-%s" % ('WAN', v)])
#  builder_test.extend(["%s-%s" % ('test', v)])

#builder_qe = builder_epw + builder_sgw
#builder_all = builder_epw + builder_sgw + builder_wan #+ builder_test
builder_qe = builder_epw 
builder_all = builder_epw + builder_wan #+ builder_test

####### 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
c['slaves'] = [
    BuildSlave('farmer-slave1', passwd['farmer-slave1'],max_builds=1, keepalive_interval=120),
    #BuildSlave('farmer-slave1', passwd['farmer-slave1'],max_builds=2, keepalive_interval=120),
]

# '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.gitpoller import GitPoller

c['change_source'] = []
c['change_source'].extend([
        GitPoller(
            project='quantum_espresso',
#            repourl='https://github.com/QEF/q-e.git',
            repourl='https://gitlab.com/QEF/q-e.git',
            branches=['develop'],
            workdir='gitpoller_QE',
            category='qe_update',
        )])# for i, name in enumerate(projects))
#c['change_source'].extend([
#        GitPoller(
#            project='sternheimer_gw',
#            repourl='https://github.com/mmdg-oxford/SternheimerGW.git',
#            branches=['develop'],
#            workdir='gitpoller_SGW',
#            category='sgw_update',
#        )])
c['change_source'].extend([
        GitPoller(
            project='wannier90',
            repourl='https://github.com/wannier-developers/wannier90.git',
            branches=['develop'],
            workdir='gitpoller_WAN',
            category='wan_update',
        )])


####### SCHEDULERS

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

# buildbot version 0.8.5
from buildbot.changes.filter import ChangeFilter
from buildbot.schedulers.basic import SingleBranchScheduler
from buildbot.schedulers.timed import Nightly
c['schedulers'] = []
c['schedulers'].extend([
        Nightly(
          name = 'QE' + '-nightly',
          change_filter=ChangeFilter(
              project = 'quantum_espresso',
              category = 'qe_update',
              branch = ['develop'],
          ),
          #branch = 'master',
          onlyIfChanged = True,
          hour = 2,
          minute = 0,
          builderNames=builder_qe,
          createAbsoluteSourceStamps = True,
        )
])
# SP: Disabled for now - do not remove 
#c['schedulers'].extend([
#        Nightly(
#          name = 'SGW' + '-nightly',
#          change_filter=ChangeFilter(
#              project = 'sternheimer_gw',
#              category = 'sgw_update',
#              branch = ['develop'],
#          ),
#          #branch = 'master',
#          onlyIfChanged = True,
#          hour = 1,
#          minute = 0,
#          builderNames=builder_sgw,
#          createAbsoluteSourceStamps = True,
#        )
#])
c['schedulers'].extend([
        Nightly(
          name = 'WAN' + '-nightly',
          change_filter=ChangeFilter(
              project = 'wannier90',
              category = 'wan_update',
              branch = 'develop',
          ),
          branch = 'develop',
          onlyIfChanged = True,
          hour = 2,
          minute = 0,
          builderNames=builder_wan,
          createAbsoluteSourceStamps = True,
        )
])

#
# buildbot version 0.8.6p1
# SP: Seems to not work with 0.9.9

from buildbot.schedulers.forcesched import ForceScheduler
from buildbot.plugins import schedulers, util

c['schedulers'].extend([
  ForceScheduler(
    name='force',
    builderNames=builder_all,
    reasonString='%(reason)s',
    codebases=[
      util.CodebaseParameter(
        "",
        name='branch',
        branch=util.ChoiceStringParameter(
            name="branch",
            choices=["develop"],
            default="develop"),
        # Remove the fields 
        revision=util.FixedParameter(name="revision", default=""),
        repository=util.FixedParameter(name="repository", default=""),
        project=util.FixedParameter(name="project", default=""),
      )
    ],
  )])

#From buildbot.schedulers.forcesched import ForceScheduler
#From buildbot.plugins import util
#C['schedulers'].extend([
#             ForceScheduler(
#                 name='force build',
#                 builderNames=builder_all,
#                 CodeBase(name='branch') #,
#                    # choices=["master","stable_release","release"], default="master"),
#                 branch=util.ChoiceStringParameter(name="branch",
#                     choices=["master","stable_release","release"], default="master"),
#             )
#             for name in projects
#])

#from buildbot.plugins import schedulers, util

#sch = schedulers.ForceScheduler(
#    name="force",
#    buttonName="pushMe!",
#    label="QEF build request form",
#    builderNames=["builder_qe"],
#
#    codebases=[
#        util.CodebaseParameter(
#            "",
#            name="Main repository",
#            # will generate a combo box
#            branch=util.ChoiceStringParameter(
#                name="branch",
#                choices=["master", "devel"],
#                default="master"),
#
#            # will generate nothing in the form, but revision, repository,
#            # and project are needed by buildbot scheduling system so we
#            # need to pass a value ("")
#            revision=util.FixedParameter(name="revision", default=""),
#            repository=util.FixedParameter(name="repository", default=""),
#            project=util.FixedParameter(name="project", default=""),
#        ),
#    ],
#    # will generate a text input
#    reason=util.StringParameter(name="reason",
#                                label="reason:",
#                                required=True, size=80),
#
#    # in case you don't require authentication this will display
#    # input for user to type his name
#    username=util.UserNameParameter(label="your name:",
#                                    size=80),
#    # A completely customized property list.  The name of the
#    # property is the name of the parameter
#    properties=[
#        util.NestedParameter(name="options", label="Build Options", layout="vertical", fields=[
#            util.StringParameter(name="pull_url",
#                                 label="optionally give a public Git pull url:",
#                                 default="", size=80),
#            util.BooleanParameter(name="force_build_clean",
#                                  label="force a make clean",
#                                  default=False)
#        ])
#    ])
#
#c['schedulers'].extend(sch)


####### 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.steps.source import Git
from buildbot.plugins import steps 
from buildbot.process.factory import BuildFactory
from buildbot.steps.shell import ShellCommand
from buildbot.steps.shell import Compile

c['builders'] = []

for name in projects:
  for BuilderID in builders:
        with open( '%s.cfg' % BuilderID ) as fn:
                exec fn
        c['builders'].append(
                BuilderConfig(
                        name= '%s-%s' % ('QE',BuilderID), 
                        slavename=getslavebybuilders(BuilderID),
                        factory=f,
                )
        )
#  for BuilderID in builders:
#        if BuilderID != "farmer_gcc640_serial":
#            with open( '%s.cfg' % BuilderID ) as fn:
#                    exec fn
#            c['builders'].append(
#                    BuilderConfig(
#                            name= '%s-%s' % ('SGW',BuilderID), 
#                            slavename=getslavebybuilders(BuilderID),
#                            factory=f_SGW,
#                    )
#            )
  for BuilderID in builders:
        with open( '%s.cfg' % BuilderID ) as fn:
                exec fn
        c['builders'].append(
                BuilderConfig(
                        name= '%s-%s' % ('WAN',BuilderID), 
                        slavename=getslavebybuilders(BuilderID),
                        factory=f_WAN,
                )
        )





####### 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.

# Old version

#c['status'] = []
#from buildbot.status import html
#from buildbot.status.web import authz, auth

#authz_cfg=authz.Authz(
#    # change any of these to True to enable; see the manual for more
#    # options
#    auth=auth.BasicAuth([("admin","mml21br")]),
#    gracefulShutdown = 'auth',
#    forceBuild = 'auth', # use this to test your slave once it is set up
#    forceAllBuilds = 'auth',
#    pingBuilder = 'auth',
#    stopBuild = 'auth',
#    stopAllBuilds = 'auth',
#    cancelPendingBuild = 'auth',
#)

#c['status'].append(html.WebStatus(http_port=8010, authz=authz_cfg))

# New version

from buildbot.plugins import util

c['www'] = dict(port=8010,
                plugins=dict(waterfall_view={},
                console_view={}),
                auth=util.UserPasswordAuth({"admin": "qeffarm"}) )

###### SEND EMAIL if FAIL ####
#    server = SMTP(SMTP_HOST)
#
##    server.ehlo()
##    print(server.ehlo())
#
#    server.starttls()
#
#    print(server.ehlo(LOCAL_HOST))
#
#    user = raw_input('user: ')
#    password = getpass('password: ')
#
#    print(server.login(user, password))
#
#    fromaddr = 'testfarmqef@gmail.com'
#    toaddrs  = 'samuel.pon@gmail.com'
#    msg = "\r\n".join([
#      "From: testfarmqef@gmail.com",
#      "To: samuel.pon@gmail.com",
#      "Subject: Buildbot",
#      "",
#      "Why, oh why"
#      ])
#
#
#    server.sendmail(fromaddr, toaddrs, msg)
#    server.close()
#

email = ['samuel.pon@gmail.com','martin.schlipf@gmail.com','p.giannozzi@gmail.com',\
         'degironc@sissa.it','baroni@sissa.it','feliciano.giustino@materials.ox.ac.uk',\
         'pdelugas@sissa.it','p.bonfa@cineca.it','andrea.ferretti@nano.cnr.it',\
         'dalcorso@sissa.it','brunato@sissa.it','icarnimeo@sissa.it',\
         'lercole@sissa.it','fgrassel@sissa.it',\
         'c.cavazzoni@cineca.it','akaithal@sissa.it' ]


from buildbot.plugins import reporters
c['services'] = []
mn = reporters.MailNotifier(fromaddr="testfarmqef@gmail.com",
                            sendToInterestedUsers=True,
                            extraRecipients=email,
                            relayhost="smtp.gmail.com",
                            smtpUser="testfarmqef@gmail.com",
                            mode=('failing'),
                            useSmtps=True,
                            smtpPort=465, 
                            builders=builder_epw, 
                            #useTls=True, 
                            subject='Test-farm failing',
                            smtpPassword="QEFtestfarm")
c['services'].append(mn)

####### 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'] = "QEF_FARM"
c['titleURL'] = "http://foundation.quantum-espresso.org/"

# 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://130.186.13.169:8010/"

####### DB URL

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