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

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

####### 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, auth
from buildbot_private import web_auth

authz_cfg=authz.Authz(
    # change any of these to True to enable; see the manual for more
    # options
    auth=auth.BasicAuth([(web_auth['user'], web_auth['pass'])]),
    gracefulShutdown = False,
    forceBuild = 'auth', # use this to test your slave once it is set up
    forceAllBuilds = 'auth',
    pingBuilder = False,
    stopBuild = 'auth',
    stopAllBuilds = 'auth',
    cancelPendingBuild = 'auth',
)
c['status'].append(html.WebStatus(http_port=8010, authz=authz_cfg))

from buildbot.status import mail
from buildbot_private import email_auth
mn = mail.MailNotifier(fromaddr="buildbot@coolprop.dreamhosters.com",
                         sendToInterestedUsers=False,
                         mode=('problem'),
                         extraRecipients=["jowr@mek.dtu.dk", "ian.h.bell@gmail.com"],
                         #useTls=True,
                         relayhost="homie.mail.dreamhost.com",
                         smtpPort=587, smtpUser=email_auth['user'],
                         smtpPassword=email_auth['pass'])

c['status'].append(mn)

####### 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 buildbot_private import pass_dict
slave_commons = dict(notify_on_missing=["ian.h.bell@gmail.com", "jowr@mek.dtu.dk"], missing_timeout=600 , max_builds = 1)
c['slaves'] = [BuildSlave("linux-slave", pass_dict["linux-slave"], **slave_commons),
               BuildSlave("OSX-slave", pass_dict["OSX-slave"], **slave_commons),
               BuildSlave("windows-slave", pass_dict["windows-slave"], **slave_commons),
               BuildSlave("linux32-slave", pass_dict["linux32-slave"], **slave_commons),
               BuildSlave("linux64-slave", pass_dict["linux64-slave"]),
               BuildSlave("windows-DTU-slave", pass_dict["windows-DTU-slave"], **slave_commons),
               BuildSlave("OSX-IPU-slave", pass_dict["OSX-IPU-slave"], **slave_commons)
               ]

# '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 CoolProp source code.

from buildbot.changes.gitpoller import GitPoller
c['change_source'] = []
c['change_source'].append(GitPoller(
        'https://github.com/CoolProp/CoolProp',
        workdir='gitpoller-workdir', branch='master',
        pollinterval=300)) # Interval between triggering a build

####### 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.process.factory import BuildFactory
from buildbot.steps.source.git import Git
from buildbot.steps.shell import ShellCommand
from buildbot.steps.master import MasterShellCommand
from buildbot.steps.slave import MakeDirectory, RemoveDirectory, CopyDirectory
from buildbot.steps.transfer import DirectoryUpload, FileDownload
from buildbot.process import properties

# A custom platform and Python identifiers
platformID = {
    "windows" : 1,
    "osx"     : 2,
    "linux"   : 3
    }

bitnessID = {
    "32bit" : 1,
    "64bit" : 2
    }

pythonID = {
    "py27" : 1,
    "py32" : 2,
    "py33" : 3,
    "py34" : 4
    }

# A couple of functions to make it simpler to use the shorthand notation for the
# different platform, bitness, python combinations. These functions are candidates
# for the most inefficient code written in 2014, but they make the rest of this
# config file so much nicer and shorter.
def getIDtuple(testID):
    platformTEST = None
    bitnessTEST  = None
    pythonTEST   = None
    #
    TEST = str(testID)
    i    = len(TEST)
    if i <  1: raise ValueError("The given testID \"{0}\" is too short.".format(TEST))
    if i >= 1: platformTEST = TEST[0]
    if i >= 2: bitnessTEST  = TEST[1]
    if i >= 3: pythonTEST   = TEST[2]
    if i > 3: raise ValueError("The given testID \"{0}\" is too long.".format(TEST))
    #
    return platformTEST, bitnessTEST, pythonTEST

def getIDstr(platform=None, bitness=None, python=None):
    if platform is None and bitness is None and python is None:
        raise ValueError("All given parameters are None, this does not work.")
    #
    def getIDstr_helper(input, alt):
        if input is None: return str(0)
        if str(input) == "0": return str(0)
        if input in alt: return str(alt[input])
        return str(int(input))
    #
    platform = getIDstr_helper(platform,platformID)
    bitness  = getIDstr_helper(bitness,bitnessID)
    python   = getIDstr_helper(python,pythonID)
    #
    return platform+bitness+python

def compareID(in1, in2):
    """Takes int or str data and compares it to the other input.
    0 can be used ad a joker, which always compares to True.
    """
    if   in1 is None or   in2 is None : return True
    if int(in1) == 0 or int(in2) == 0 : return True
    return int(in1) == int(in2)

def checkID(inID, teID=None, strict=True):
    platformIN, bitnessIN, pythonIN = getIDtuple(testID=inID)
    platformTE, bitnessTE, pythonTE = getIDtuple(testID=teID)
    #
    result = compareID(platformTE, platformIN)
    if not result and strict: raise ValueError("The given input for the platform \"{0}\" did not match \"{1}\"".format(platformIN,platformTE))
    result =  compareID(bitnessTE, bitnessIN) and result
    if not result and strict: raise ValueError("The given input for the bitness \"{0}\" did not match \"{1}\"".format(bitnessIN,bitnessTE))
    result =   compareID(pythonTE, pythonIN) and result
    if not result and strict: raise ValueError("The given input for the Python version \"{0}\" did not match \"{1}\"".format(pythonIN,pythonTE))
    return result

def getFromDict(inDict, inID):
    res = inDict.get(inID)
    if res is None: # Try some more
        for key in inDict:
            if checkID(inID, key, strict=False):
                if res is not None:
                    raise ValueError("Already found a matching entry \"{0}\" for \"{1}\", please specify more parameters.".format(res,inID))
                else:
                    res = inDict[key]
    if res is None:
        raise ValueError("Could not find a matching entry for \"{0}\".".format(inID))
    else:
        return res

def getKeyFromVal(dic,val):
    for key in dic:
        if str(dic[key])==str(val):
            return key
    return None

def getJobName(inID):
    platform, bitness, python = getIDtuple(inID)
    platform = getKeyFromVal(platformID, platform)
    bitness  = getKeyFromVal( bitnessID, bitness )
    python   = getKeyFromVal(  pythonID, python  )
    res = []
    if platform is not None: res.append(platform)
    if  bitness is not None: res.append(bitness)
    if   python is not None: res.append(python)
    return "-".join(res)


class PythonSlaveConfig(object):
    def __init__(self,name):
        self.name = name
        self.pyact = {}
        self.pyenv = {}
        self.pyins = {}

    def getPyact(self, inID):
        return getFromDict(self.pyact, inID)

    def getPyenv(self, inID):
        return getFromDict(self.pyenv, inID)

    def getPyins(self, inID):
        return getFromDict(self.pyins, inID)

    def getIDs(self):
        IDs = []
        for pl in platformID:
            for bi in bitnessID:
                for py in pythonID:
                    tmpID = getIDstr(pl, bi, py)
                    try:
                        #print "Testing for {0} in act".format(tmpID)
                        self.getPyact(tmpID)
                        #print "Testing for {0} in env".format(tmpID)
                        self.getPyenv(tmpID)
                        #print "Testing for {0} in ins".format(tmpID)
                        self.getPyins(tmpID)
                        #print "Appending {0}".format(tmpID)
                        IDs.append(tmpID)
                    except:
                        pass
        return IDs


@properties.renderer
def gitModeInput(props):
    """ If we are doing a full clean, this will tell it to clobber all the files """
    if props.getProperty('fullclean', default = False) or props.getProperty('branch') == 'release':
        return 'full'
    else:
        return 'incremental'

@properties.renderer
def masterdestLocation(props):
    """
    If building from release branch, upload to public_html/release
    If building from master branch, upload to normal public_html/binaries folder
    If another branch, upload to public_html/unstable
    """
    if props.getProperty('branch') == 'release':
        return 'public_html/release'
    elif props.getProperty('branch') == 'master':
        return 'public_html/binaries'
    else:
        return 'public_html/unstable'

# A centralised method to provide the objects with some presets
def getBaseFactory(gitMode = 'incremental'):
    factory = BuildFactory()
    factory.addStep(Git(repourl='git://github.com/CoolProp/CoolProp', mode=gitModeInput, method = 'fresh', submodules = True, progress=True, haltOnFailure = True))
    return factory

def docActivateCmd():
    #return 'source activate CoolPropDocs &&'
    return ''

@properties.renderer
def fullBuildCommand(props):
    return ' '.join([docActivateCmd(), "python", "__init__.py", str(props.getProperty('fullBuild', default = False) or props.getProperty('branch') == 'release')])

@properties.renderer
def rsyncCommand(props):
    """
    A renderable command that creates the rsync command to be run
    """

    # Some basic preparations, make sure the machine has passwordless SSH access to the server
    #
    server_uri = 'coolprop@coolprop.dreamhosters.com'
    server_dir = '/home/coolprop/buildbot/server-master'
    server_des = "{0}:{1}".format(server_uri, server_dir)
    #
    local_build_dir = 'Web/_build/html'

    if props.getProperty('branch') == 'release':
        server_target_dir = '{0}/public_html/release/sphinx'.format(server_des)
    elif props.getProperty('branch') == 'master':
        server_target_dir = '{0}/public_html/binaries/sphinx'.format(server_des)
    else:
        server_target_dir = '{0}/public_html/unstable/sphinx'.format(server_des)

    return ' '.join([docActivateCmd(), 'rsync', '-a', '--stats', '{0}/ {1}'.format(local_build_dir,server_target_dir)])

# All what is needed to create the website, it makes sense to run the
# nightly builds on the same machine. This avoids extra data transfer.
def websiteFactory(platform, gitMode='incremental', fullBuild=False):
    if 'win' in platform.lower():
        raise ValueError("The docs cannot be build on a Windows machine, we rely on rsync...")
    #
    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    # Make a wheel - this is advantageous because it forces pip to uninstall coolprop, ensuring that all files installed are from this wheel
    factory.addStep(ShellCommand(command=' '.join([docActivateCmd(), "python", "setup.py", "bdist_wheel", '--dist-dir', 'dist']), workdir= 'build/wrappers/Python', haltOnFailure = True))
    # List the files in the dist directory
    factory.addStep(ShellCommand(command=' '.join([docActivateCmd(), "ls", 'dist/*.*']), workdir= 'build/wrappers/Python', haltOnFailure = True))
    # Install the wheel - this will uninstall the old version
    factory.addStep(ShellCommand(command=' '.join([docActivateCmd(), "pip", "install", "-vvv", "--force-reinstall", "--ignore-installed", "--upgrade", "--no-index", "--use-wheel", "`ls dist/CoolProp*.whl`"]), workdir= 'build/wrappers/Python', haltOnFailure = True))
    # Remove the generated wheel
    factory.addStep(ShellCommand(command=' '.join([docActivateCmd(), "rm", 'dist/*.whl']), workdir= 'build/wrappers/Python', haltOnFailure = True))
    # Test the compiled CoolProp
    factory.addStep(ShellCommand(command=' '.join([docActivateCmd(), "python", "-c", "\"import CoolProp; print(CoolProp.__gitrevision__)\""]), workdir= 'build/wrappers', haltOnFailure = True))
    # Test the compiled CoolProp
    factory.addStep(ShellCommand(command=' '.join([docActivateCmd(), "python", "-c", "\"import CoolProp; print(CoolProp.__file__)\""]), workdir= 'build/wrappers', haltOnFailure = True))
    # Create plots and fluid tables
    factory.addStep(ShellCommand(command=fullBuildCommand, workdir= 'build/Web/scripts', haltOnFailure = True))
    # Run doxygen build
    factory.addStep(ShellCommand(command=' '.join([docActivateCmd(), "doxygen", "--version", "&&", "doxygen", "Doxyfile"]), workdir= 'build', haltOnFailure = True))
    # Run sphinx apidoc
    factory.addStep(ShellCommand(command=' '.join([docActivateCmd(), "sphinx-apidoc","-T","-f","-e","-o","apidoc","../wrappers/Python/CoolProp"]),workdir= 'build/Web', haltOnFailure = True))
    # Run sphinx website builder
    factory.addStep(ShellCommand(command=' '.join([docActivateCmd(), "make", "html"]), workdir= 'build/Web', haltOnFailure = True))
    # Upload the generated files
    factory.addStep(ShellCommand(command=rsyncCommand, haltOnFailure = True))
    return factory


# The reworked Python builder factory. It relies on Miniconda and can handle both 32bit and 64bit builds
# Currently, it only supports Windows, but the plan is to integrate MacOS and Linux builds here as well.
# You have to have both CMake and Git available on your standard command line, pay attention when installing
# these two tools on your Windows machine.
def pythonFactory(pyID, pyCFG=PythonSlaveConfig("name"), gitMode='incremental'):
    #
    # Some basic settings for all builders
    workingFolder = "build/wrappers/Python"
    installFolder = "build/install_root"
    relinstFolder = "../../install_root" # relative path
    uploadFolder  = "install_root" # relative to build
    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    # Remove the temporary folder for installs
    factory.addStep(RemoveDirectory(dir=installFolder, haltOnFailure = True))
    #
    # Setting the appropriate virtual environment activator
    pyact = pyCFG.getPyact(pyID)
    # Setting the appropriate virtual environment
    pyenv = pyCFG.getPyenv(pyID)
    # Setting the appropriate installer options
    pyins = pyCFG.getPyins(pyID)
    # Getting the appropriate virtual environment activator
    if pyact is None:
        raise ValueError("Your selected Python installation \"{0}\" is not supported by this builder factory".format(pyID))
    # Getting the appropriate virtual environment
    if pyenv is None:
        raise ValueError("Your selected Python environment \"{0}\" is not supported by this builder factory".format(pyID))
    # Getting the appropriate installer options
    if pyins is None:
        raise ValueError("Your selected Python environment \"{0}\" does not have installer options in this builder factory".format(pyID))
    #
    activateCMD =  " ".join([pyact, pyenv]) # We always activate an environment, regardless of the host
    installCMD  =  " ".join(["python", "setup.py"] + pyins)
    combinedCMD = " && ".join([activateCMD, installCMD])
    factory.addStep(ShellCommand(command = combinedCMD, workdir = workingFolder, haltOnFailure = True))
    #
    #factory.addStep(ShellCommand(command="python -c \"import CoolProp; print(CoolProp.__file___)\"", workdir= workingFolder, haltOnFailure = True))
    #
    if not checkID(pyID, teID=300, strict=False): # Only upload non-Linux files
        factory.addStep(DirectoryUpload(slavesrc=uploadFolder, masterdest=masterdestLocation, url="binaries", compress="bz2"))
    return factory


def cmakeFactory(mod_name = None, gitMode = 'incremental', install = True, pre_cmd = [], cmake_args = [], build_args = [], ctest_args = [], cmake_env={}, test = True):
    """
    Parameters
    ----------
    mod_name: string
        The module to be built, one of 'Octave','python','Csharp', etc. - turns on the macro -DCOOLPROP_OCTAVE_MODULE=ON for instance if you pass octave
    gitMode: string
        What mode to use, one of 'incremental' or 'full'
    install: bool
        True for install, False for just build
    pre_cmd: list of strings
        List of strings of commands to be executed before the cmake command, gets merged with &&
    cmake_args: list of strings
        List of strings of arguments to be passed to cmake (Makefile generating) step
    cmake_env: dictionary (string->string)
        A dictionary including keys that can be set in the build environment
    build_args: list of strings
        List of strings of arguments to be passed to cmake install or build command depending on value of install
    ctest_args: list of strings
        List of strings of arguments to be passed to ctest
    """
    working_folder = "build/build"

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    #
    factory.addStep(MakeDirectory(dir=working_folder, haltOnFailure = True))
    factory.addStep(RemoveDirectory(dir="build/install_root", haltOnFailure = True))
    #
    if len(pre_cmd)>0 and not pre_cmd[-1]=="&&": pre_cmd.append("&&")
    if mod_name is not None:
        cmake_args = ["-DCOOLPROP_"+mod_name.upper()+"_MODULE=ON","-DBUILD_TESTING=ON"]+cmake_args

    cmakeCMD = pre_cmd + ["cmake", ".."]+cmake_args
    factory.addStep(ShellCommand(
      command=' '.join(cmakeCMD),
      env = cmake_env,
      workdir= working_folder,
      haltOnFailure = True))
    #
    if install: installCMD = ["--target", "install"]
    else: installCMD = []
    factory.addStep(ShellCommand(
      command=' '.join(pre_cmd+["cmake", "--build", "."]+installCMD+build_args),
      workdir = working_folder,
      haltOnFailure = True))
    #
    if test:
        factory.addStep(ShellCommand(
          command=["ctest", "--extra-verbose"] + ctest_args,
          workdir = working_folder,
          haltOnFailure = True))
    #
    if install:
        factory.addStep(DirectoryUpload(
          slavesrc="install_root",
          masterdest=masterdestLocation,
          url="binaries",
          compress="bz2"))
    return factory


def fortranFactory(platform=1,bitness=1):
    working_folder = "build/build" # Same as in cmakeFactory
    fortran_folder = "build/wrappers/Fortran/detailed_example" #
    #
    if platform==platformID["windows"]:
        if bitness==bitnessID["32bit"]:
            cmake_args = ["-DCOOLPROP_EXTERNC_STATIC_LIBRARY=ON","-G", "\"MinGW Makefiles\""]
        else:
            cmake_args = ["-DCOOLPROP_EXTERNC_STATIC_LIBRARY=ON","-G", "\"MinGW Makefiles\""]
        cp_cmd     = "copy /Y *.f90 ..\\..\\..\\build"
        build_cmd  = "gfortran -c -Wall cpinterface.f90 cool_fortran_bind.f90"
        link_cmd   = "gfortran -o main *.o libCoolProp.a -lstdc++"
        exec_cmd   = "main"
    elif platform==platformID["linux"]:
        if bitness==bitnessID["32bit"]:
            cmake_args = ["-DCOOLPROP_EXTERNC_STATIC_LIBRARY=ON"]
        else:
            cmake_args = ["-DCOOLPROP_EXTERNC_STATIC_LIBRARY=ON"]
        cp_cmd     = "cp *.f90 ../../../build/"
        build_cmd  = "gfortran -c -Wall cpinterface.f90 cool_fortran_bind.f90"
        link_cmd   = "gfortran -o main *.o libCoolProp.a -lstdc++ -ldl"
        exec_cmd   = "./main"
    elif platform==platformID["osx"]:
        if bitness==bitnessID["32bit"]:
            cmake_args = ["-DCOOLPROP_EXTERNC_STATIC_LIBRARY=ON",'-DCMAKE_C_COMPILER="/usr/local/bin/gcc-4.9"','-DCMAKE_CXX_COMPILER="/usr/local/bin/g++-4.9"', '-DCMAKE_VERBOSE_MAKEFILE=ON']
        else:
            cmake_args = ["-DCOOLPROP_EXTERNC_STATIC_LIBRARY=ON",'-DCMAKE_C_COMPILER="/usr/local/bin/gcc-4.9"','-DCMAKE_CXX_COMPILER="/usr/local/bin/g++-4.9"', '-DCMAKE_VERBOSE_MAKEFILE=ON']
        cp_cmd     = "cp *.f90 ../../../build/"
        build_cmd  = "gfortran -c -Wall cpinterface.f90 cool_fortran_bind.f90"
        link_cmd   = "g++-4.9 -o main *.o libCoolProp.a -lstdc++ -ldl -lgfortran"
        exec_cmd   = "./main"
    else:
        raise ValueError("Unknown Fortran platform: {0}".format(platform))

    # Create the factory to add the actions to
    factory = cmakeFactory(cmake_args=cmake_args,install=False,test=False)
    #
    #factory.addStep(CopyDirectory(src=fortran_folder, dest=working_folder))
    factory.addStep(ShellCommand(command=cp_cmd,  workdir = fortran_folder, haltOnFailure = True))
    for cmd in [build_cmd,link_cmd,exec_cmd]:
        factory.addStep(ShellCommand(command=cmd, workdir = working_folder, haltOnFailure = True))
    return factory


def javascript_slave(platform, cmake_args = [], cmake_env = {}, build_args = [], gitMode = 'incremental'):

    working_folder = "build/Javascript"
    from buildbot.process.properties import WithProperties

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)

    # Remove the temporary folder for installs
    factory.addStep(RemoveDirectory(dir="build/install_root", haltOnFailure = True))

    factory.addStep(ShellCommand(command="cmake .. -DCOOLPROP_JAVASCRIPT_MODULE=ON -DCMAKE_TOOLCHAIN_FILE=${EMSCRIPTEN}/cmake/Modules/Platform/Emscripten.cmake",
                                 workdir= working_folder,
                                 haltOnFailure = True))
    factory.addStep(ShellCommand(command=["cmake", "--build", ".", "--target", "install"]+build_args, workdir = working_folder, haltOnFailure = True))
    factory.addStep(DirectoryUpload(slavesrc="install_root", masterdest=masterdestLocation, url="binaries", compress="bz2"))

    return factory

def python_source_slave(key, platform, conda_env, cmake_args = [], cmake_env = {}, build_args = [], gitMode = 'incremental'):
    working_folder = "build/build"

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)

    # Remove the temporary folder for installs
    factory.addStep(RemoveDirectory(dir="build/install_root", haltOnFailure = True))
    factory.addStep(MakeDirectory(dir=working_folder, haltOnFailure = True))

    factory.addStep(ShellCommand(command = ' '.join(['source',"activate",conda_env,"&&","cmake", "..", "-DCOOLPROP_PYTHON_PYPI=ON","&&","cmake","--build",".","--target","CoolProp"]+cmake_args),
                                 env = cmake_env,
                                 workdir= working_folder,
                                 haltOnFailure = True))
    factory.addStep(DirectoryUpload(slavesrc="install_root", masterdest=masterdestLocation, url="binaries", compress="bz2"))
    return factory

def deb_slave(gitMode = 'incremental'):

    working_folder = "build/wrappers/DEB"

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)

    factory.addStep(ShellCommand(command = ' '.join(["chmod","+x","package.bsh","&&", "./package.bsh"]),
                                 workdir= working_folder,
                                 haltOnFailure = True))
    factory.addStep(MakeDirectory(dir=working_folder+'/DEB/DEB', haltOnFailure = True))
    factory.addStep(ShellCommand(command = ' '.join(["mv","*.deb","DEB/DEB"]),
                                 workdir= working_folder,
                                 haltOnFailure = True))
    factory.addStep(DirectoryUpload(slavesrc="wrappers/DEB/DEB", masterdest=masterdestLocation, url="binaries/DEB", compress="bz2"))

    return factory

def excel_slave(gitMode = 'incremental'):
    """
    """
    working_folder = "build/build"

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    #
    factory.addStep(MakeDirectory(dir='build/bin/MicrosoftExcel', haltOnFailure = True))
    factory.addStep(MakeDirectory(dir=working_folder+'/32bitDLL', haltOnFailure = True))
    factory.addStep(MakeDirectory(dir=working_folder+'/64bitDLL', haltOnFailure = True))
    factory.addStep(RemoveDirectory(dir="build/install_root", haltOnFailure = True))
    # *************************
    # Make 32-bit __stdcall DLL
    # *************************
    factory.addStep(ShellCommand(command=["cmake", "../..", "-DCOOLPROP_32BIT_STDCALL_SHARED_LIBRARY=ON","-G", "Visual Studio 10"],
                                 workdir= working_folder+'/32bitDLL',
                                 haltOnFailure = True))
    factory.addStep(ShellCommand(command=["cmake", "--build", ".", "--target", "install", "--config", "Release"], workdir = working_folder+'/32bitDLL', haltOnFailure = True))
    # Copy the created DLL
    factory.addStep(ShellCommand(command=' '.join(["copy", "/Y", "install_root\\shared_library\\Windows\\32bit__stdcall_calling_convention\\CoolProp.dll", "bin\\MicrosoftExcel"]), workdir = 'build', haltOnFailure = True))

    # ***************
    # Make 64-bit DLL
    # ***************
    factory.addStep(ShellCommand(command=["cmake", "../..", "-DCOOLPROP_64BIT_SHARED_LIBRARY=ON","-G", "Visual Studio 10 Win64"],
                                 workdir= working_folder+'/64bitDLL',
                                 haltOnFailure = True))
    factory.addStep(ShellCommand(command=["cmake", "--build", ".", "--target", "install", "--config", "Release"], workdir = working_folder+'/64bitDLL', haltOnFailure = True))

    factory.addStep(MakeDirectory(dir='build\\bin\\MicrosoftExcel', haltOnFailure = True))
    factory.addStep(ShellCommand(command=' '.join(["copy", "/Y", "install_root\\shared_library\\Windows\\64bit\\CoolProp.dll", "bin\\MicrosoftExcel\\CoolProp_x64.dll"]), workdir = 'build', haltOnFailure = True))

    # Copy other files
    factory.addStep(ShellCommand(command=["copy", "wrappers\Excel\CoolProp.xlam", "bin\MicrosoftExcel"], workdir = 'build', haltOnFailure = True))
    factory.addStep(ShellCommand(command=["copy", "wrappers\Excel\CoolProp.xla", "bin\MicrosoftExcel"], workdir = 'build', haltOnFailure = True))
    factory.addStep(ShellCommand(command=["copy", "wrappers\Excel\TestExcel.xlsx", "bin\MicrosoftExcel"], workdir = 'build', haltOnFailure = True))
    # Upload the files
    factory.addStep(DirectoryUpload(slavesrc="bin",masterdest=masterdestLocation,url="MicrosoftExcel",compress="bz2"))
    return factory
    
def smath_builder(gitMode = 'incremental'):
    """
    """
    working_folder = "build/build"

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    #
    factory.addStep(MakeDirectory(dir='build/bin/SMath', haltOnFailure = True))
    factory.addStep(MakeDirectory(dir=working_folder+'/32bitDLL', haltOnFailure = True))
    factory.addStep(MakeDirectory(dir=working_folder+'/64bitDLL', haltOnFailure = True))
    factory.addStep(RemoveDirectory(dir="build/install_root", haltOnFailure = True))
    factory.addStep(ShellCommand(command=["cmake", "..", "-DCOOLPROP_SMATH_MODULE=ON","-G", "Visual Studio 10"],
                                 workdir= working_folder,
                                 haltOnFailure = True))
    # Build the SMath C# class library using visual studio
    factory.addStep(ShellCommand(command=["build"], workdir= 'build/wrappers/SMath', haltOnFailure = True))
                                 
    # *************************
    # Make 32-bit __stdcall DLL
    # *************************
    factory.addStep(ShellCommand(command=["cmake", "../..", "-DCOOLPROP_32BIT_STDCALL_SHARED_LIBRARY=ON","-G", "Visual Studio 10"],
                                 workdir= working_folder+'/32bitDLL',
                                 haltOnFailure = True))
    factory.addStep(ShellCommand(command=["cmake", "--build", ".", "--target", "install", "--config", "Release"], workdir = working_folder+'/32bitDLL', haltOnFailure = True))
    # Copy the created DLL
    factory.addStep(ShellCommand(command=' '.join(["copy", "/Y", "install_root\\shared_library\\Windows\\32bit__stdcall_calling_convention\\CoolProp.dll", "wrappers\\SMath\\coolprop_wrapper\\bin\\Release\\CoolProp.x86.dll"]), workdir = 'build', haltOnFailure = True))

    # ***************
    # Make 64-bit DLL
    # ***************
    factory.addStep(ShellCommand(command=["cmake", "../..", "-DCOOLPROP_64BIT_SHARED_LIBRARY=ON","-G", "Visual Studio 10 Win64"],
                                 workdir= working_folder+'/64bitDLL',
                                 haltOnFailure = True))
    factory.addStep(ShellCommand(command=["cmake", "--build", ".", "--target", "install", "--config", "Release"], workdir = working_folder+'/64bitDLL', haltOnFailure = True))

    factory.addStep(MakeDirectory(dir='build\\bin\\MicrosoftExcel', haltOnFailure = True))
    factory.addStep(ShellCommand(command=' '.join(["copy", "/Y", "install_root\\shared_library\\Windows\\64bit\\CoolProp.dll", "wrappers\\SMath\\coolprop_wrapper\\bin\\Release\\CoolProp.x64.dll"]), workdir = 'build', haltOnFailure = True))

    # Copy other files to a temporary directory
    factory.addStep(ShellCommand(command=["build_zip"], workdir = 'build\\wrappers\\SMath\\coolprop_wrapper', haltOnFailure = True))
    factory.addStep(ShellCommand(command=["copy", "/Y", "wrappers\\SMath\\coolprop_wrapper\\coolprop_wrapper.7z", "bin\\SMath"], workdir = 'build', haltOnFailure = True))
    # Upload the files
    factory.addStep(DirectoryUpload(slavesrc="bin",masterdest=masterdestLocation,url="SMath",compress="bz2"))
    return factory

def julia_builder(gitMode = 'incremental'):
    """
    """
    working_folder = "build/build"

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    # Clean the install_root
    factory.addStep(RemoveDirectory(dir="build/install_root/Julia", haltOnFailure = True))
    factory.addStep(MakeDirectory(dir='build/install_root/Julia', haltOnFailure = True))
    factory.addStep(MakeDirectory(dir='build/install_root/Julia/0.3', haltOnFailure = True))
    # Copy other files
    factory.addStep(ShellCommand(command=["cp", "wrappers/Julia/0.3/CoolProp.jl", "install_root/Julia/0.3"], workdir = 'build', haltOnFailure = True))
    factory.addStep(ShellCommand(command=["cp", "wrappers/Julia/CoolProp.jl", "install_root/Julia/"], workdir = 'build', haltOnFailure = True))
    # Upload the files - TODO: Is this the correct directory?
    factory.addStep(DirectoryUpload(slavesrc="install_root",masterdest=masterdestLocation,url="binaries",compress="bz2"))
    return factory

def cmake_slave(mod_name, platform, gitMode = 'incremental', install = True, cmake_args = [], build_args = [], ctest_args = [], cmake_env={}, test = True):
    """
    Parameters
    ----------
    mod_name: string
        The module to be built, one of 'Octave','python','Csharp', etc. - turns on the macro -DCOOLPROP_OCTAVE_MODULE=ON for instance if you pass octave
    gitMode: string
        What mode to use, one of 'incremental' or 'full'
    install: bool
        True for install, False for just build
    cmake_args: list of strings
        List of strings of arguments to be passed to cmake (Makefile generating) step
    cmake_env: dictionary (string->string)
        A dictionary including keys that can be set in the build environment
    build_args: list of strings
        List of strings of arguments to be passed to cmake install or build command depending on value of install
    ctest_args: list of strings
        List of strings of arguments to be passed to ctest
    """
    working_folder = "build/build"

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    #
    factory.addStep(MakeDirectory(dir=working_folder, haltOnFailure = True))
    factory.addStep(RemoveDirectory(dir="build/install_root", haltOnFailure = True))
    if mod_name.lower() == 'octave' and platform.lower() == 'windows':
        pre = ['set','"PATH=%OCTAVE_ROOT%/bin;%PATH%"','&&']
    else:
        pre = []
    factory.addStep(ShellCommand(command=' '.join(pre+["cmake", "..", "-DCOOLPROP_"+mod_name.upper()+"_MODULE=ON","-DBUILD_TESTING=ON"]+cmake_args),
                                 env = cmake_env,
                                 workdir= working_folder,
                                 haltOnFailure = True))
    if install:
        factory.addStep(ShellCommand(command=' '.join(pre+["cmake", "--build", ".", "--target", "install"]+build_args), workdir = working_folder, haltOnFailure = True))
    else:
        factory.addStep(ShellCommand(command=' '.join(pre+["cmake", "--build", "."]+build_args), workdir = working_folder, haltOnFailure = True))
    if test:
        factory.addStep(ShellCommand(command=["ctest", "--extra-verbose"] + ctest_args, workdir = working_folder, haltOnFailure = True))
    if install:
        factory.addStep(DirectoryUpload(slavesrc="install_root",masterdest=masterdestLocation,url="binaries",compress="bz2"))
    return factory

def swig_matlab_builder(platform, gitMode = 'incremental', build_args = [], cmake_args = [], ctest_args = [], cmake_env = None):
    """
    Download SWIG+MATLAB version, use it to build _wrap file for MATLAB builder, upload generated file back to master.

    These generated files are needed for the other swig builders and are cross-platform
    """

    working_folder = "build/build"

    def prepend_path(cmd):
        if platform == 'windows':
            pre_path = 'set "PATH=swig-matlab-bin\\bin;C:\\Program Files\\7-Zip\\;%PATH%" && set "SWIG_LIB=swig-matlab-bin\\share\\swig\\3.0.5" && '
        else:
            pre_path = 'export PATH=swig-matlab-bin/bin:${PATH}                           && export SWIG_LIB=swig-matlab-bin/share/swig/3.0.5   && '
        return pre_path+cmd

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    #
    # Download files from nightly build for swig
    factory.addStep(FileDownload(mastersrc = 'public_html/nightly/swig+MATLAB/'+platform+'/swig_MATLAB.7z',
                                 slavedest = 'swig_MATLAB.7z', haltOnFailure = True))
    # Directory for build
    factory.addStep(MakeDirectory(dir='build/build', haltOnFailure = True))
    # Unzip
    factory.addStep(ShellCommand(command = prepend_path('7z -y -obuild x swig_MATLAB.7z'),
                                 workdir = 'build'                      , haltOnFailure = True))
    # Call cmake
    factory.addStep(ShellCommand(
      command=prepend_path('cmake .. -DCOOLPROP_MATLAB_SWIG_MODULE=ON -DSWIG_DIR=swig-matlab-bin/bin ' + ' '.join(cmake_args)),
                                 workdir= "build/build", env = cmake_env, haltOnFailure = True))
    # Build the binaries
    factory.addStep(ShellCommand(command=prepend_path('cmake --build . --target install ' + ' '.join(build_args)),
                                 workdir= "build/build"                 , haltOnFailure = True))
    # Run simple integration test, but only on platforms other than windows
    if 'win' not in platform:
        factory.addStep(ShellCommand(command=["ctest", "--extra-verbose"] + ctest_args, workdir = "build/build", haltOnFailure = True))
    # Upload the files
    factory.addStep(DirectoryUpload(slavesrc="install_root",masterdest=masterdestLocation,url="binaries",compress="bz2"))
    # Return the object
    return factory

def swig_scilab_builder(platform, gitMode = 'incremental'):
    """
    Download SWIG+scilab version on linux, use it to build _wrap file for scilab builder, upload generated file(s) back to master.

    These generated files are needed for the other swig builders and are cross-platform
    """

    working_folder = "build/build"

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    #
    # Download files from nightly build for swig
    factory.addStep(FileDownload(mastersrc = 'public_html/nightly/swig+SCILAB/'+platform+'/swig_SCILAB.7z', slavedest = 'swig_SCILAB.7z', haltOnFailure = True))
    # Directory for build
    factory.addStep(MakeDirectory(dir='build/build', haltOnFailure = True))
    # Unzip
    factory.addStep(ShellCommand(command = '7z -y -obuild x swig_SCILAB.7z', workdir = 'build', haltOnFailure = True))
    if platform == 'windows':
        command = ["set", '"PATH=swig-scilab-bin\\bin;%PATH%"', "&&", "cmake", "..", "-DCOOLPROP_SCILAB_SWIG_MODULE=ON","-DSWIG_DIR=swig-scilab-bin"]
    else:
        command = ["PATH=swig-scilab-bin/bin:${PATH}","cmake", "..", "-DCOOLPROP_SCILAB_SWIG_MODULE=ON","-DSWIG_DIR=swig-scilab-bin"]
    factory.addStep(ShellCommand(command=' '.join(command),
                                 workdir= "build/build",
                                 haltOnFailure = True))
    if platform == 'windows':
        command = ["set", '"SWIG_LIB=swig-scilab-bin\\share\\swig\\3.0.2"', "&&", "cmake", "--build", ".", "--target", "install"]
    else:
        command = ["SWIG_LIB=swig-scilab-bin/share/swig/3.0.2", "cmake", "--build", ".", "--target", "install"]
    factory.addStep(ShellCommand(command=' '.join(command),
                                 workdir= "build/build",
                                 haltOnFailure = True))
    # Upload the files
    factory.addStep(DirectoryUpload(slavesrc="install_root",masterdest=masterdestLocation,url="binaries",compress="bz2"))
    return factory

def SWIG_MATLAB_bin_builder(platform, gitMode = 'incremental', windows = False):

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    # Build SWIG with MATLAB
    args = ["python","build_swig_matlab.py"]
    if windows:
        args += ['--windows']
        platform = 'windows'
    factory.addStep(ShellCommand(command=' '.join(args),
                                 workdir= "build/dev/scripts",
                                 haltOnFailure = True))
    factory.addStep(MakeDirectory(dir='build/dev/scripts/swig-MATLAB-temp', haltOnFailure = True))
    # Zip up the directory that was generated using bzip
    factory.addStep(ShellCommand(command=' '.join(["7z","a","../swig-MATLAB-temp/swig_MATLAB.7z","swig-matlab-bin"]),
                                 workdir= "build/dev/scripts/swig-matlab",
                                 haltOnFailure = True))
    # Upload swig+MATLAB
    factory.addStep(DirectoryUpload(slavesrc="dev/scripts/swig-MATLAB-temp",
                                    masterdest="public_html/nightly/swig+MATLAB/"+platform,
                                    url="nightly/swig+MATLAB/"+platform,
                                    compress="bz2"))
    return factory

def SWIG_scilab_bin_builder(platform, gitMode = 'incremental', windows = False):

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    # Build SWIG with scilab
    args = ["python","build_swig_scilab.py"]
    if windows:
        args += ['--windows']
        platform = 'windows'
    # Build SWIG with scilab
    factory.addStep(ShellCommand(command=' '.join(args),
                                 workdir= "build/dev/scripts",
                                 haltOnFailure = True))
    factory.addStep(MakeDirectory(dir='build/dev/scripts/swig-SCILAB-temp', haltOnFailure = True))
    # Zip up the directory that was generated using bzip
    factory.addStep(ShellCommand(command=' '.join(["7z","a","../swig-SCILAB-temp/swig_SCILAB.7z","swig-scilab-bin"]),
                                 workdir= "build/dev/scripts/swig-scilab",
                                 haltOnFailure = True))
    # Upload swig+scilab
    factory.addStep(DirectoryUpload(slavesrc="dev/scripts/swig-SCILAB-temp",
                                    masterdest="public_html/nightly/swig+SCILAB/"+platform,
                                    url="nightly/swig+SCILAB/"+platform,
                                    compress="bz2"))
    return factory

def memory_sanitizer_builder():
    """ Run clang's memory sanitizer on the Catch tests to check for memory leaks """

    # Hardcoded path to the clang compiler
    CLANG_ROOT='/Users/Ian/Downloads/clang+llvm-3.5.0-macosx-apple-darwin'

    commons = dict(workdir = 'build/asan', haltOnFailure = True, env = dict(CLANG_ROOT = CLANG_ROOT))

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    factory.addStep(MakeDirectory(dir='build/asan', haltOnFailure = True))
    factory.addStep(ShellCommand(command='cmake .. -DCOOLPROP_CLANG_ADDRESS_SANITIZER=ON -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_C_COMPILER=${CLANG_ROOT}/bin/clang -DCMAKE_CXX_COMPILER=${CLANG_ROOT}/bin/clang++', **commons))
    factory.addStep(ShellCommand(command='DYLD_LIBRARY_PATH=${CLANG_ROOT}/lib/clang/3.5.0/lib/darwin/ ASAN_SYMBOLIZER_PATH=${CLANG_ROOT}/bin/llvm-symbolizer  ASAN_OPTIONS=verbosity=1 cmake --build .', **commons))
    return factory

def vxworks_module_builder(gitMode = 'incremental', cmake_args = [], cmake_env = None):
    """
    These generated files are needed for the other swig builders and are cross-platform
    """

    working_folder = "build/build"

    # Create the factory to add the actions to
    factory = getBaseFactory(gitMode=gitMode)
    # Directory for build
    factory.addStep(MakeDirectory(dir='build/build', haltOnFailure = True))
    # Generate makefile
    factory.addStep(ShellCommand(command = 'cmake .. -DCOOLPROP_VXWORKS_LIBRARY_MODULE=ON -DCMAKE_TOOLCHAIN_FILE=../dev/cmake/Toolchains/powerpc-vxworks-crio.cmake -DCMAKE_CXX_FLAGS="-D__powerpc__"', workdir = 'build/build', haltOnFailure = True))
    # Build
    factory.addStep(ShellCommand(command = 'cmake --build . --target install', workdir = 'build/build', haltOnFailure = True))
    # Upload the files
    factory.addStep(DirectoryUpload(slavesrc="install_root", masterdest=masterdestLocation,url="binaries",compress="bz2"))
    return factory

from buildbot.config import BuilderConfig

c['builders'] = []

# c['builders'].append(
#   BuilderConfig(name="sphinx pages",
#     slavenames=["linux32-slave"],
#     factory = website_factory(platform = 'linux')
#   )
# )

c['builders'].append(
    BuilderConfig(name="Debian package",
                  slavenames=["linux-slave"],
                  factory = deb_slave()
                  )
      )

c['builders'].append(
    BuilderConfig(name="Excel",
                  slavenames=["windows-slave"],
                  factory = excel_slave() ) )
c['builders'].append(
    BuilderConfig(name="SMath",
                  slavenames=["windows-slave"],
                  factory = smath_builder() ) )


c['builders'].append(
  BuilderConfig(
    name="Sphinx docs",
    slavenames=["OSX-slave"],
    factory = websiteFactory(platform = 'osx')
  )
)

c['builders'].append(
BuilderConfig(name="Javascript-linux",
              slavenames=["linux-slave"],
              factory = javascript_slave(platform = 'linux')
              )
  )
c['builders'].append(
    BuilderConfig(name="Python-sdist",
                  slavenames=["OSX-IPU-slave"],
                  factory = python_source_slave("PYPI", platform = 'OSX', conda_env = 'CoolProp27')
                  )
      )

c['builders'].append(
    BuilderConfig(name="memory sanitizer",
                  slavenames=["OSX-slave"],
                  factory = memory_sanitizer_builder()
                  )
      )

# We centralise the Python builder configuration here
#
# Setting the appropriate installer options
relinstFolder = "../../install_root" # relative path
baseins = ['bdist_wheel', '--dist-dir', relinstFolder+'/Python']

windowsDTUslave = PythonSlaveConfig("windows-DTU-slave")
windowsDTUslave.pyact[getIDstr("windows", "32bit", 0     )] = "\"C:\\Program Files (x86)\\Miniconda32_27\\Scripts\\activate.bat\""
windowsDTUslave.pyact[getIDstr("windows", "64bit", 0     )] = "\"C:\\Program Files\\Miniconda64_27\\Scripts\\activate.bat\""
windowsDTUslave.pyenv[getIDstr("windows", 0      , "py27")] = "CoolProp27"
windowsDTUslave.pyenv[getIDstr("windows", 0      , "py33")] = "CoolProp33"
windowsDTUslave.pyenv[getIDstr("windows", 0      , "py34")] = "CoolProp34"
tmpins = baseins + ['bdist_wininst','--dist-dir', relinstFolder+'/Python']
windowsDTUslave.pyins[getIDstr("windows", "32bit", "py27")] = tmpins + ['--cmake-compiler', 'vc9' , '--cmake-bitness', '32']
windowsDTUslave.pyins[getIDstr("windows", "32bit", "py33")] = tmpins + ['--cmake-compiler', 'vc10', '--cmake-bitness', '32']
windowsDTUslave.pyins[getIDstr("windows", "32bit", "py34")] = tmpins + ['--cmake-compiler', 'vc10', '--cmake-bitness', '32']
windowsDTUslave.pyins[getIDstr("windows", "64bit", "py27")] = tmpins + ['--cmake-compiler', 'vc9' , '--cmake-bitness', '64']
windowsDTUslave.pyins[getIDstr("windows", "64bit", "py33")] = tmpins + ['--cmake-compiler', 'vc10', '--cmake-bitness', '64']
windowsDTUslave.pyins[getIDstr("windows", "64bit", "py34")] = tmpins + ['--cmake-compiler', 'vc10', '--cmake-bitness', '64']

linuxDTUslave = PythonSlaveConfig("linux32-slave")
linuxDTUslave.pyact[getIDstr(  "linux"  , "32bit", 0     )] = "source /home/jowr/miniconda/bin/activate"
linuxDTUslave.pyenv[getIDstr(  "linux"  , 0      , "py27")] = "CoolProp27"
linuxDTUslave.pyenv[getIDstr(  "linux"  , 0      , "py33")] = "CoolProp33"
linuxDTUslave.pyenv[getIDstr(  "linux"  , 0      , "py34")] = "CoolProp34"
linuxDTUslave.pyins[getIDstr(  "linux"  , "32bit", 0     )] = baseins

linuxJorritSlave = PythonSlaveConfig("linux64-slave")
linuxJorritSlave.pyact[getIDstr(  "linux"  , "64bit", 0     )] = "source /home/jorrit/miniconda/bin/activate"
linuxJorritSlave.pyenv[getIDstr(  "linux"  , 0      , "py27")] = "CoolProp27"
linuxJorritSlave.pyenv[getIDstr(  "linux"  , 0      , "py33")] = "CoolProp33"
linuxJorritSlave.pyenv[getIDstr(  "linux"  , 0      , "py34")] = "CoolProp34"
linuxJorritSlave.pyins[getIDstr(  "linux"  , "64bit", 0     )] = baseins

osxIPUslave = PythonSlaveConfig("OSX-IPU-slave")
osxIPUslave.pyact[getIDstr(  "osx"  , "64bit", 0     )] = "source /Users/jowr/miniconda/bin/activate"
osxIPUslave.pyenv[getIDstr(  "osx"  , "64bit", "py27")] = "CoolProp27"
osxIPUslave.pyenv[getIDstr(  "osx"  , "64bit", "py33")] = "CoolProp33"
osxIPUslave.pyenv[getIDstr(  "osx"  , "64bit", "py34")] = "CoolProp34"
osxIPUslave.pyins[getIDstr(  "osx"  , 0      , 0     )] = baseins

pythonSlaves = [windowsDTUslave, linuxJorritSlave, osxIPUslave]

for slave in pythonSlaves:
    for pyID in slave.getIDs():
        c['builders'].append(
          BuilderConfig(
            name="-".join(["Python", "binaries", getJobName(pyID)]),
            slavenames=[slave.name],
            factory = pythonFactory(pyID, pyCFG=slave, gitMode = 'incremental')
          )
        )

# # Enable multiple slaves for one build for load
# # balancing and backup purposes.
# # Note that the configuration has to be the same.
# pythonSlaves = [windowsDTUslave, windowsIANslave, linuxDTUslave]
# enabledIDs = {}
# for slave in pythonSlaves:
#     for pyID in slave.getIDs():
#         if pyID in enabledIDs:
#             enabledIDs[pyID].append(slave.name)
#         else:
#             enabledIDs[pyID] = [slave.name]
#
# for pyID in enabledIDs:
#     c['builders'].append(
#       BuilderConfig(
#         name="-".join(["Python", "binaries", getJobName(pyID)]),
#         slavenames=enabledIDs[pyID],
#         factory = pythonFactory(pyID, pyCFG=slave, gitMode = 'incremental')
#       )
#     )

windowsDTUslave.pyact[getIDstr("windows", "32bit", 0     )] = "\"C:\\Program Files (x86)\\Miniconda32_27\\Scripts\\activate.bat\""
windowsDTUslave.pyact[getIDstr("windows", "64bit", 0     )] = "\"C:\\Program Files\\Miniconda64_27\\Scripts\\activate.bat\""
windowsDTUslave.pyenv[getIDstr("windows", 0      , "py27")] = "CoolProp27"

#c['builders'].append(
#BuilderConfig(name="StaticLibrary-binaries-windows-32bit-MinGW-ExternC",
#			  slavenames=["windows-DTU-slave"],
#			  factory = cmakeFactory(cmake_args=[
#			  "-DCOOLPROP_EXTERNC_STATIC_LIBRARY=ON",
#			  "-G", "\"MinGW Makefiles\""],
#             install = False)
#			  )
#  )
c['builders'].append(
  BuilderConfig(
    name="Fortran-executable-windows-32bit-MinGW-ExternC",
	slavenames=["windows-DTU-slave"],
	factory = fortranFactory(platform=platformID["windows"],bitness=bitnessID["32bit"])
	)
)
c['builders'].append(
  BuilderConfig(
    name="Fortran-executable-linux-32bit-GCC-ExternC",
	slavenames=["linux32-slave"],
	factory = fortranFactory(platform=platformID["linux"],bitness=bitnessID["32bit"])
	)
)
c['builders'].append(
  BuilderConfig(
    name="Fortran-executable-linux-64bit-GCC-ExternC",
    slavenames=["linux64-slave"],
    factory = fortranFactory(platform=platformID["linux"],bitness=bitnessID["64bit"])
    )
)
#c['builders'].append(
#  BuilderConfig(
#    name="Fortran-executable-osx-32bit-GCC-ExternC",
#    slavenames=["linux32-slave"],
#    factory = fortranFactory(platform=platformID["linux"],bitness=bitnessID["32bit"])
#    )
#)
c['builders'].append(
  BuilderConfig(
    name="Fortran-executable-osx-64bit-GCC-ExternC",
    slavenames=["OSX-IPU-slave"],
    factory = fortranFactory(platform=platformID["osx"],bitness=bitnessID["64bit"])
    )
)

#Common boring 64-bit modules for windows, linux and OSX
### OSX
for platform in ['OSX', 'linux', 'windows']:
    for wrapper in ['Java','Csharp','Octave','PHP','64BIT_SHARED_LIBRARY','STATIC_LIBRARY','MATHEMATICA','VBDOTNET']:
        if wrapper == 'PHP' and platform != 'linux': continue # only build PHP on linux
        if wrapper == 'VBDOTNET' and not platform.startswith('windows'): continue # only build VB.net on windows
        ctest_args, cmake_args, build_args = [], ['-DCMAKE_BUILD_TYPE=Release','-DCMAKE_VERBOSE_MAKEFILE=ON'], ['--config','Release']
        if platform.startswith('windows'):
            ctest_args = ['-C', 'Release']
            if wrapper == 'Octave':
                cmake_args = ['-G', '"MinGW Makefiles"']
            elif wrapper == 'VBDOTNET':
                cmake_args = ['-G', '"Visual Studio 11 2012 Win64"']
            else:
                cmake_args = ['-G', '"Visual Studio 10 2010 Win64"']
        platmod = ''
        if platform == 'OSX' and wrapper in ['64BIT_SHARED_LIBRARY','STATIC_LIBRARY']:
            platmod = '-IPU'
        c['builders'].append(
            BuilderConfig(name=wrapper + "-" + platform,
                          slavenames=[platform + platmod +"-slave"],
                          factory = cmake_slave(wrapper,
                                                platform = platform,
                                                ctest_args = ctest_args,
                                                cmake_args = cmake_args,
                                                build_args = build_args)
                          )
              )
    cmake_args, build_args = [], []
    if platform.startswith('windows'):
        cmake_args = ['-G', '"Visual Studio 10 2010 Win64"']
        build_args = ['--config','Release']
    platmod = ''
    if platform == 'OSX':
        platmod = '-IPU' # Give this builder to OSX-IPU-slave slave
    c['builders'].append(
    BuilderConfig(name="Catch-"+platform,
                  slavenames=[platform + platmod + "-slave"],

                  factory = cmake_slave('Catch',
                                        platform = platform,
                                        install=False,
                                        cmake_args = cmake_args,
                                        build_args = build_args)
                  )
      )

c['builders'].append(
  BuilderConfig(name="32bitDLL-OSX",slavenames=["OSX-IPU-slave"],
    factory = cmake_slave(
      '32BIT_SHARED_LIBRARY_LINUX',
      test = False,
      platform = 'OSX',
      cmake_args=['-DFORCE_BITNESS_32=ON'])
  )
)
c['builders'].append(
  BuilderConfig(name="Julia-OSX",
   slavenames=["OSX-IPU-slave"],
    factory = julia_builder()
  )
)

c['builders'].append(
  BuilderConfig(name="32bitDLL-Linux",slavenames=["linux32-slave"],
    factory = cmake_slave(
      '32BIT_SHARED_LIBRARY_LINUX',
      test = False,
      platform = 'linux')
  )
)

c['builders'].append(
  BuilderConfig(name="VxWorks-Linux",
                slavenames=["linux-slave"],
                factory = vxworks_module_builder()
  )
)

c['builders'].append(
    BuilderConfig(name="32bitCdeclDLL-windows",
      slavenames=["windows-slave"],
      factory = cmake_slave('32BIT_CDECL_SHARED_LIBRARY',
                            test = False,
                            platform = 'windows',
                            cmake_args=['-G','"Visual Studio 10 2010"'],
                            build_args = ['--config','Release']
      )
      )
      )
c['builders'].append(
    BuilderConfig(name="32bitStdcallDLL-windows",
      slavenames=["windows-slave"],
      factory = cmake_slave('32BIT_STDCALL_SHARED_LIBRARY',
                            test = False,
                            platform = 'windows',
                            cmake_args=['-G','"Visual Studio 10 2010"'],
                            build_args = ['--config','Release']
      )
      )
      )
c['builders'].append(
    BuilderConfig(name="Csharp-windows32",
      slavenames=["windows-slave"],
      factory = cmake_slave('Csharp',
                            test = True,
                            platform = 'windows',
                            cmake_args=['-G','"Visual Studio 10 2010"'],
                            build_args = ['--config','Release'],
                            ctest_args = ['-C','Release']
      )
      )
      )
c['builders'].append(
    BuilderConfig(name="EES-windows",
      slavenames=["windows-slave"],
      factory = cmake_slave('EES',
                            platform = 'windows',
                            test = False,
                            cmake_args = ['-G','"Visual Studio 10 2010"'],
                            build_args = ['--config','Release'])
      )
      )

#for platform in ['windows', 'linux', 'OSX']:
for platform in ['linux', 'OSX']:
    cmake_args = ['-DBUILD_TESTING=ON']
    ctest_args = []
    build_args = []
    if platform == 'windows':
        build_args = ['--config','Release']
        cmake_args += ['-G', '"Visual Studio 10 2010 Win64"']
        ctest_args = ['-C','Release']
    c['builders'].append(BuilderConfig(name="MATLAB-"+platform, slavenames=[platform+"-slave"], factory = swig_matlab_builder(platform, build_args = build_args, cmake_args = cmake_args, ctest_args = ctest_args)))
    if platform == 'linux':
        c['builders'].append(BuilderConfig(name="Scilab-"+platform, slavenames=[platform+"-slave"], factory = swig_scilab_builder(platform)))
c['builders'].append(BuilderConfig(name="MATLAB-windows32",
                                   slavenames=["windows-slave"],
                                   factory = swig_matlab_builder('windows',
                                                                 cmake_env={'MATLAB_ROOT':'c:\Program Files (x86)\MATLAB\R2014a'},
                                                                 cmake_args = ['-G', '"Visual Studio 10 2010"'],
                                                                 build_args = ['--config','Release'],
                                                                 ctest_args = ['-C','Release'])))

c['builders'].append(BuilderConfig(
  name="Matlab-binaries-windows-64bit-VS10",
  slavenames = ["windows-DTU-slave"],
  factory    = swig_matlab_builder("windows",
    cmake_env  = {'MATLAB_ROOT':'c:\Program Files\MatLab R2014a'},
    cmake_args = ['-G', '"Visual Studio 10 2010 Win64"'],
    build_args = ['--config','Release'],
    ctest_args = ['-C','Release'])))

# c['builders'].append(BuilderConfig(
#   name="Matlab-binaries-windows-32bit-VS10",
#   slavenames = ["windows-DTU-slave"],
#   factory    = swig_matlab_builder("windows",
#     cmake_env  = {'MATLAB_ROOT':'c:\Program Files (x86)\MATLAB\R2014b'},
#     cmake_args = ['-G', '"Visual Studio 10 2010"'],
#     build_args = ['--config','Release'],
#     ctest_args = ['-C','Release'])))

# Sort the list in place and store the names
c['builders'].sort(key=lambda x: x.name)
all_builder_names = [builder.name for builder in c['builders']]

#######  NIGHTLY build ###############
swig_builders = []
for platform in ['windows', 'linux', 'OSX']:
    platname = platform
    if platform == 'windows':
        platform = 'linux'
        windows = True
    else:
        windows = False

    c['builders'].append(
        BuilderConfig(name="nightlySWIG+MATLAB-" + platname,
          slavenames=[platform + "-slave"],
          factory = SWIG_MATLAB_bin_builder(platform, windows = windows)
          )
          )
    swig_builders.append(c['builders'][-1].name)
    c['builders'].append(
        BuilderConfig(name="nightlySWIG+scilab-" + platname,
          slavenames=[platform + "-slave"],
          factory = SWIG_scilab_bin_builder(platform, windows = windows)
          )
          )
    swig_builders.append(c['builders'][-1].name)




############# RELEASE BUILDER #######################

@properties.renderer
def releaseCommand(props):
    if props.getProperty('dryrun', default = False):
        release = 'dryrun'
    else:
        release = 'release'

    version = props.getProperty('version')

    return ' '.join(['${HOME}/scripts/release.bsh',version, release])

def release_builder():
    f = BuildFactory()
    f.addStep(MasterShellCommand(command = releaseCommand))
    return f

c['builders'].append(
        BuilderConfig(name="release version",
          slavenames=["OSX-slave"], # Slave is not used, all commands run on master
          factory = release_builder()
          )
          )

####### SCHEDULERS

from buildbot.schedulers.basic import SingleBranchScheduler
from buildbot.schedulers.timed import Nightly
from buildbot.schedulers.forcesched import *
from buildbot.changes import filter

c['schedulers'] = []
c['schedulers'].append(SingleBranchScheduler(
                            name="all",
                            change_filter=filter.ChangeFilter(branch='master'),
                            treeStableTimer=None,
                            builderNames=all_builder_names))
c['schedulers'].append(ForceScheduler(
                            name="force",
                            builderNames=all_builder_names,
                            properties=[
                                BooleanParameter(name="fullclean", label="Do a full clean", default=False),
                                BooleanParameter(name="fullBuild", label="Do a full build of all the expensive docs", default=False)
                                ]
                            ))
c['schedulers'].append(ForceScheduler(
                            name="release_scheduler",
                            builderNames=['release version'],
                            properties=[
                                StringParameter(name="version", label="Version number", default="0.0.0"),
                                BooleanParameter(name="dryrun", label="Do a dry-run of release", default=True)]
                                ))
c['schedulers'].append(ForceScheduler(
                            name="force_swig_builders",
                            builderNames=swig_builders,
                            properties=[
                                BooleanParameter(name="fullclean",
                                    label="Do a full clean", default=False)]))
c['schedulers'].append(ForceScheduler(
                            name="force_sphinx_expensive",
                            builderNames=['Sphinx docs'],
                            properties=[
                                BooleanParameter(name="fullBuild",
                                    label="Do a full build of all the expensive docs", default=False)]))
c['schedulers'].append(Nightly(name='nightly',
                               branch='master',
                               builderNames=['Sphinx docs'],
                               hour=3, # 3am in Boulder = 12pm in Copenhagen
                               minute=7,
                               onlyIfChanged=True))

#~ from buildbot.schedulers import basic

#~ swig_matlab_pre_scheduler = SingleBranchScheduler(
                            #~ name="swig-matlab-scheduler",
                            #~ change_filter=filter.ChangeFilter(branch='master'),
                            #~ treeStableTimer=None,
                            #~ builderNames=['SWIG-MATLAB-pre'])
#~ c['schedulers'].append(swig_matlab_pre_scheduler)

#~ c['schedulers'].append(basic.Dependent(name="swig-matlab-dependency",
                                       #~ upstream=swig_matlab_pre_scheduler, # <- no quotes!
                                       #~ builderNames=["MATLAB32-windows"])
                        #~ )




####### 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'] = "CoolProp"
c['titleURL'] = "https://www.coolprop.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://www.coolprop.dreamhosters.com: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",
}
