import sys

from polyjit.buildbot.builders import register
from polyjit.buildbot import slaves
from polyjit.buildbot.utils import (builder, define, git, cmd, ucmd, ucompile,
                                    upload_file, trigger, ip, mkdir,
                                    rmdir, s_sbranch, s_force, s_trigger,
                                    hash_download_from_master,
                                    property_is_false, clean_unpack)
from polyjit.buildbot.repos import make_cb, codebases
from polyjit.buildbot.master import URL
from buildbot.plugins import util
from buildbot.changes import filter

codebase = make_cb(['benchbuild'])

P = util.Property
BuildFactory = util.BuildFactory

def has_munged(host):
    if "has_munged" in host["properties"]:
        return host["properties"]["has_munged"]
    return False

accepted_builders = slaves.get_hostlist(slaves.infosun, predicate = has_munged)


# yapf: disable
def configure(c):
    llvm_dl = hash_download_from_master("public_html/llvm.tar.gz",
                                        "llvm.tar.gz", "llvm")
    polyjit_dl = hash_download_from_master("public_html/polyjit.tar.gz",
                                        "polyjit.tar.gz", "polyjit")
    steps = [
#        trigger(schedulerNames=['trigger-build-llvm', 'trigger-build-jit']),
        define("scratch", ip("/scratch/pjtest/%(prop:buildnumber)s"))
    ]
    steps.extend(llvm_dl)
    steps.extend(clean_unpack("llvm.tar.gz", "llvm"))
    steps.extend(polyjit_dl)
    steps.extend(clean_unpack("polyjit.tar.gz", "polyjit"))
    steps.extend([
        define("BENCHBUILD_ROOT", ip("%(prop:builddir)s/build/benchbuild/")),
        git('benchbuild', 'develop', codebases, workdir=P("BENCHBUILD_ROOT")),

    ])

    steps.extend([
        ucmd('virtualenv', '-ppython3', 'env/'),
        ucmd('/mnt/build/env/bin/pip3', 'install', '--upgrade', '.',
            workdir='build/benchbuild'),
        ucmd('/mnt/build/env/bin/benchbuild', 'bootstrap', env={
            'PATH': '/opt/cmake/bin:/usr/sbin:/sbin:/usr/bin:/bin',
            'BB_ENV_COMPILER_PATH':
                ip('%(prop:scratch)s/llvm/bin:'
                   '/mnt/build/llvm/bin:'
                   '/mnt/build/polyjit/bin'),
            'BB_ENV_COMPILER_LD_LIBRARY_PATH':
                ip('%(prop:scratch)s/llvm/lib:'
                   '/mnt/build/llvm/lib:'
                   '/mnt/build/polyjit/lib'),
            'BB_ENV_LOOKUP_PATH':
                ip('%(prop:scratch)s/llvm/lib:'
                   '/mnt/build/llvm/lib:'
                   '/mnt/build/polyjit/bin'),
            'BB_ENV_LOOKUP_LD_LIBRARY_PATH':
                ip('%(prop:scratch)s/polyjit/lib:'
                   '/mnt/build/llvm/lib:'
                   '/mnt/build/polyjit/lib'),
            'BB_LLVM_DIR': '/mnt/build/llvm',
            'BB_LIKWID_PREFIX': '/usr/local',
            'BB_PAPI_INCLUDE': '/usr/include',
            'BB_PAPI_LIBRARY': '/usr/lib',
            'BB_SRC_DIR': '/mnt/build/benchbuild',
            'BB_UNIONFS_ENABLE': 'false'
        },
        workdir='build/benchbuild'),
        mkdir(P("scratch")),
        cmd("cp", "-var", "build/*", P("scratch"))
    ])

    c['builders'].append(builder("build-slurm-set", None, accepted_builders,
        factory=BuildFactory(steps)))
# yapf: enable

def schedule(c):
    c['schedulers'].extend([
        s_sbranch("build-slurm-set-sched", codebase, ["build-slurm-set"],
                  change_filter=filter.ChangeFilter(branch_re='next|develop'),
                  treeStableTimer=2 * 60),
        s_force("force-build-slurm-set", codebase, ["build-slurm-set"]),
        s_trigger("trigger-slurm-set", codebase, ['build-slurm-set'])
    ])


register(sys.modules[__name__])

@@ -1910,32 +1910,33 @@
 nchbuild'),%0A    
+#
     ucmd('/mnt/b
@@ -1980,24 +1980,25 @@
 , env=%7B%0A    
+#
         'PAT
@@ -2049,24 +2049,25 @@
 :/bin',%0A    
+#
         'BB_
@@ -2082,32 +2082,33 @@
 ILER_PATH':%0A    
+#
             ip('
@@ -2131,32 +2131,33 @@
 /llvm/bin:'%0A    
+#
                '
@@ -2176,22 +2176,27 @@
 lvm/bin:
+    
 '%0A    
+#
         
@@ -2225,32 +2225,33 @@
 yjit/bin'),%0A    
+#
         'BB_ENV_
@@ -2273,32 +2273,33 @@
 RARY_PATH':%0A    
+#
             ip('
@@ -2322,32 +2322,33 @@
 /llvm/lib:'%0A    
+#
                '
@@ -2365,32 +2365,33 @@
 /llvm/lib:'%0A    
+#
                '
@@ -2412,32 +2412,33 @@
 yjit/lib'),%0A    
+#
         'BB_ENV_
@@ -2447,32 +2447,33 @@
 OKUP_PATH':%0A    
+#
             ip('
@@ -2496,32 +2496,33 @@
 /llvm/lib:'%0A    
+#
                '
@@ -2539,32 +2539,33 @@
 /llvm/lib:'%0A    
+#
                '
@@ -2586,32 +2586,33 @@
 yjit/bin'),%0A    
+#
         'BB_ENV_
@@ -2636,24 +2636,25 @@
 _PATH':%0A    
+#
             
@@ -2684,32 +2684,33 @@
 lyjit/lib:'%0A    
+#
                '
@@ -2731,24 +2731,25 @@
 m/lib:'%0A    
+#
             
@@ -2774,32 +2774,33 @@
 yjit/lib'),%0A    
+#
         'BB_LLVM
@@ -2825,24 +2825,25 @@
 /llvm',%0A    
+#
         'BB_
@@ -2872,24 +2872,25 @@
 local',%0A    
+#
         'BB_
@@ -2920,24 +2920,25 @@
 clude',%0A    
+#
         'BB_
@@ -2964,24 +2964,25 @@
 r/lib',%0A    
+#
         'BB_
@@ -3016,24 +3016,25 @@
 build',%0A    
+#
         'BB_
@@ -3058,24 +3058,25 @@
 'false'%0A    
+#
     %7D,%0A     
@@ -3070,24 +3070,25 @@
 #    %7D,%0A    
+#
     workdir=

disable bootstrap