# -*- python -*-
# ex: set syntax=python:
from buildbot.process.factory import BuildFactory
from buildbot.changes.pb import PBChangeSource
from buildbot.changes.svnpoller import SVNPoller
from buildbot.schedulers.basic import SingleBranchScheduler
from buildbot.schedulers import timed
from buildbot.changes.filter import ChangeFilter
from buildbot.config import BuilderConfig
from buildbot import locks
from buildbot.steps import source,shell
from buildbot.process import factory
from buildbot.buildslave import BuildSlave
from buildbot.status import html
from buildbot.status.web import auth, authz
from buildbot.status.mail import MailNotifier
from buildbot.schedulers.forcesched import ForceScheduler
from buildbot.changes.gitpoller import GitPoller
import locks

SLAVEIP = "slave_ip"

################################# BUILDSLAVES #################################


################################# CHANGESOURCES #################################
####################################GitPoller#################################
#cs_gitpoller = GitPoller(#project = "buildername",
#                         repourl="git_url",
#                         #branches = ["master"],
#                         branches = branches_list,
#						 pollInterval = 60,
#						 gitbin = "/usr/bin/git")
#						 #gitbin = "c:/Program Files/Git/bin/git.exe")								 


################################# SCHEDULERS #################################	
def dev_branch_fn_git(branch):
    branch_list = branches_list
    if branch in branch_list:
        return branch
			
			
def isImportantSlave_NameWindows(change):
    is_important = False
    for each_file in change.files:
        each_file = each_file.replace("\\","/")
        if each_file.startswith("/"):
            each_file = each_file[1:]
        if each_file.endswith("/"):
            each_file = each_file[:-1]
        #if each_file.startswith("DVDFab/test.txt"):
        if each_file.startswith("monitor_file_path"):
            is_important = True
            break
    return is_important
	
scheduler_buildername_build = SingleBranchScheduler(name = "buildername",
                                               change_filter = ChangeFilter(branch_fn=dev_branch_fn_git),
                                               treeStableTimer = 5,
                                               builderNames = ["buildername"],
                                               fileIsImportant = isImportantSlave_NameWindows,
                                               onlyImportant = True,
                                               properties={'owner':send_mail_list})	

			
Slave_Name_time = timed.Nightly(
					name = 'Slave_Name',
					builderNames = ["buildername"],
					branch = "trunk/goland",
					hour = [start_hour],
					minute = start_minute,
					dayOfWeek = [0, 1, 2, 3, 4])		
					
buildername_force_builder= ForceScheduler( name="all_buildername",
	            builderNames=["buildername"])										   



################################# Factory  ################################

import dvdfab_factory_buildername
f_buildername = BuildFactory()
buildername = dvdfab_factory_buildername.Factory(f_buildername)
f_buildername = buildername.f_build()

def get_lock(build_lock_dict):
    db_lock = ""
    db_lock_ip = "db_lock_" + SLAVEIP.strip().split(".")[-1]
    if db_lock_ip in build_lock_dict.keys():
        db_lock = build_lock_dict[db_lock_ip]
    return db_lock

db_lock = get_lock(locks.build_lock_dict)
################################# BUILDERS  #################################
buildername_build_dir = "build_buildername_dir"
b_buildername = {
		'name' : 'buildername',
		'slavename' : 'Slave_Name',
		'builddir' : buildername_build_dir,
                'slavebuilddir' : buildername_build_dir,
	        'locks' : [db_lock.access("exclusive")],
		'factory' : f_buildername}

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

################################# PROJECT MailNotifier #################################
buildername_mail = MailNotifier(
					fromaddr="buildbot@goland.cn",
					sendToInterestedUsers=True,
					lookup = "goland.cn",
					mode='failing',
					relayhost = '10.10.7.100',
					smtpPort = 25,
					smtpUser = 'buildbot@goland.cn',
					smtpPassword = '123456',
					builders = ['buildername'],
					extraRecipients=send_mail_list)					



def update_params_dict(c):
    c['slaves'].append(BuildSlave("Slave_Name", "123456"))
    #c['change_source'].append(cs_gitpoller)	
    c['schedulers'].append(scheduler_buildername_build)
    c['schedulers'].append(Slave_Name_time)
    c['schedulers'].append(buildername_force_builder)
    c['builders'].append(b_buildername)
    c['status'].append(buildername_mail)
    return c

