import sys

from polyjit.buildbot.builders import register
from polyjit.buildbot import slaves
from polyjit.buildbot.utils import (builder, define, git, mkdir,
                                    ucmd, cmd, ucompile, ip,
                                    s_nightly, s_sbranch, s_force, s_trigger,
                                    hash_upload_to_master)
from polyjit.buildbot.repos import make_cb, make_new_cb, codebases
from polyjit.buildbot.master import URL
from buildbot.plugins import util
from buildbot.changes import filter

codebase = make_cb(['polli-sb'])
force_codebase = make_new_cb(['polli-sb'])

P = util.Property
BuildFactory = util.BuildFactory

supported = {
    "debussy": slaves.infosun['debussy'],
    "ligeti": slaves.infosun['ligeti']
}
accepted_builders = slaves.get_hostlist(supported)


# yapf: disable
def configure(c):
    steps = [
        define("SUPERBUILD_ROOT", ip("%(prop:builddir)s/polli-sb")),
        define("UCHROOT_SUPERBUILD_ROOT", "/mnt/polli-sb"),
        define("POLYJIT_DEFAULT_BRANCH", "WIP-merge-next"),

        git('polli-sb', 'master', codebases, workdir=P("SUPERBUILD_ROOT"),
            mode="full", method="fresh"),
        ucmd('/opt/cmake/bin/cmake', P("UCHROOT_SUPERBUILD_ROOT"),
             '-DCMAKE_BUILD_TYPE=Release',
             '-DCMAKE_INSTALL_PREFIX=./_install',
             ip('-DPOLYJIT_BRANCH_CLANG=%(prop:POLYJIT_DEFAULT_BRANCH)s'),
             ip('-DPOLYJIT_BRANCH_LLVM=%(prop:POLYJIT_DEFAULT_BRANCH)s'),
             ip('-DPOLYJIT_BRANCH_POLLI=%(prop:POLYJIT_DEFAULT_BRANCH)s'),
             ip('-DPOLYJIT_BRANCH_POLLY=%(prop:POLYJIT_DEFAULT_BRANCH)s'),
             '-G', 'Ninja',
             env={
                 "PATH": ["/opt/cmake/bin", "/usr/local/bin", "/usr/bin", "/bin"]
             },
             mounts={
                 '%(prop:cmake_prefix)s' : "/opt/cmake"
             },
             usePTY=True,
             name="cmake",
             logEnviron=True,
             description="[uchroot] cmake: release build",
             descriptionDone="[uchroot] configured."),
        ucmd("/usr/bin/ninja",
             env={
                 "PATH": ["/opt/cmake/bin", "/usr/local/bin", "/usr/bin", "/bin"]
             },
             mounts={
                 '%(prop:cmake_prefix)s' : "/opt/cmake"
             },
             usePTY=True,
             logEnviron=True,
             haltOnFailure=True,
             name="build jit",
             description="[uchroot] building PolyJIT",
             descriptionDone="[uchroot] built PolyJIT",
             timeout=4800),
        cmd("tar", "czf", "../polyjit_sb.tar.gz", "-C", "./_install", ".")
    ]

    upload_pj = hash_upload_to_master(
        "polyjit_sb.tar.gz",
        "../polyjit_sb.tar.gz",
        "public_html/polyjit_sb.tar.gz", URL)
    steps.extend(upload_pj)

    c['builders'].append(
        builder("polyjit-superbuild", None,
                accepted_builders,
                tags=['polyjit'], factory=BuildFactory(steps)))
# yapf: enable


def schedule(c):
    c['schedulers'].extend([
        s_sbranch("branch-sched-polyjit-superbuild", codebase,
                  ["polyjit-superbuild"],
                  change_filter=filter.ChangeFilter(branch_re='master')),
        s_force("force-sched-polyjit-superbuild", force_codebase,
                ["polyjit-superbuild"]),
        s_trigger("trigger-sched-polyjit-superbuild", codebase,
                  ["polyjit-superbuild"]),
        s_nightly("nightly-sched-polyjit-superbuild", codebase,
                  ["polyjit-superbuild"],
                  hour=20, minute=0)
    ])


register(sys.modules[__name__])

@@ -1175,24 +1175,37 @@
     
-u
 cmd(
-'/opt/cmake
+ip('%25(prop:cmake_prefix)s
 /bin
@@ -1215,21 +1215,14 @@
 ake'
+)
 , P(%22
-UCHROOT_
 SUPE
@@ -1240,33 +1240,32 @@
 %22),%0A            
- 
 '-DCMAKE_BUILD_T
@@ -1278,17 +1278,16 @@
 lease',%0A
- 
         
@@ -1331,33 +1331,32 @@
 l',%0A            
- 
 ip('-DPOLYJIT_BR
@@ -1393,33 +1393,32 @@
 ULT_BRANCH)s'),%0A
- 
             ip('
@@ -1478,33 +1478,32 @@
 '),%0A            
- 
 ip('-DPOLYJIT_BR
@@ -1540,33 +1540,32 @@
 ULT_BRANCH)s'),%0A
- 
             ip('
@@ -1634,17 +1634,16 @@
         
- 
 '-G', 'N
@@ -1653,220 +1653,8 @@
 a',%0A
-             env=%7B%0A                 %22PATH%22: %5B%22/opt/cmake/bin%22, %22/usr/local/bin%22, %22/usr/bin%22, %22/bin%22%5D%0A             %7D,%0A             mounts=%7B%0A                 '%25(prop:cmake_prefix)s' : %22/opt/cmake%22%0A             %7D,%0A 
     
@@ -1666,33 +1666,32 @@
    usePTY=True,%0A
- 
             name
@@ -1700,17 +1700,16 @@
 cmake%22,%0A
- 
         
@@ -1733,33 +1733,32 @@
 ue,%0A            
- 
 description=%22%5Buc
@@ -1787,17 +1787,16 @@
 build%22,%0A
- 
         
@@ -1849,17 +1849,16 @@
         
-u
 cmd(%22/us
@@ -1875,220 +1875,8 @@
 a%22,%0A
-             env=%7B%0A                 %22PATH%22: %5B%22/opt/cmake/bin%22, %22/usr/local/bin%22, %22/usr/bin%22, %22/bin%22%5D%0A             %7D,%0A             mounts=%7B%0A                 '%25(prop:cmake_prefix)s' : %22/opt/cmake%22%0A             %7D,%0A 
     
@@ -1888,33 +1888,32 @@
    usePTY=True,%0A
- 
             logE
@@ -1937,17 +1937,16 @@
         
- 
 haltOnFa
@@ -1953,25 +1953,24 @@
 ilure=True,%0A
- 
             
@@ -1991,33 +1991,32 @@
 t%22,%0A            
- 
 description=%22%5Buc
@@ -2033,33 +2033,32 @@
 lding PolyJIT%22,%0A
- 
             desc
@@ -2088,33 +2088,32 @@
 built PolyJIT%22,%0A
- 
             time

no uchroot