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

# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from buildbot.changes import svnpoller
from buildbot.scheduler import Scheduler

from master import build_utils
from master import gatekeeper
from master import chromium_step
from master import master_utils
from master import slaves_list
from master.factory import swarm_factory

import config
import master_site_config

ActiveMaster = master_site_config.ChromiumSwarm

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

# 'slavePortnum' defines the TCP port to listen on. This must match the value
# configured into the buildslaves (with their --master option)
c['slavePortnum'] = ActiveMaster.slave_port

config.DatabaseSetup(c, require_dbconfig=ActiveMaster.is_production_host)

####### CHANGESOURCES

# the 'change_source' list tells the buildmaster how it should find out about
# source code changes. Any class which implements IChangeSource can be added
# to this list: there are several in buildbot/changes/*.py to choose from.
def ChromeTreeFileSplitter(path):
  """split_file for the 'src' project in the trunk."""

  # Exclude .DEPS.git from triggering builds on chrome.
  if path == 'src/.DEPS.git':
    return None

  # List of projects we are interested in. The project names must exactly
  # match paths in the Subversion repository, relative to the 'path' URL
  # argument. build_utils.SplitPath() will use them as branch names to
  # kick off the Schedulers for different projects.
  projects = ['src', 'tools/swarm_client']
  return build_utils.SplitPath(projects, path)

# Rev link urls.
chromium_rev = 'http://src.chromium.org/viewvc/chrome?view=rev&revision=%s'

# Polls config.Master.trunk_url for changes
trunk_poller = svnpoller.SVNPoller(svnurl=config.Master.trunk_url,
                                   split_file=ChromeTreeFileSplitter,
                                   pollinterval=10,
                                   revlinktmpl=chromium_rev)

c['change_source'] = [trunk_poller]

####### SCHEDULERS

s_chromium_swarm = Scheduler(name='experimental',
                             branch='src',
                             treeStableTimer=60,
                             builderNames=['Linux Swarm Tests',
                                           'Mac Swarm Tests',
                                           'Windows Swarm Tests',
                                           'Linux Isolate Tests',
                                           'Mac Isolate Tests',
                                           'Windows Isolate Tests'])

c['schedulers'] = [s_chromium_swarm]

# ----------------------------------------------------------------------------
# FACTORIES

m_linux_swarm = swarm_factory.SwarmFactory(
    'src/out',
    target_platform='linux2',
    full_checkout=True,
    swarm_client_canary=True)
f_chromium_linux_swarm = m_linux_swarm.SwarmFactory(
    options=[
      '--build-tool=ninja',
      '--compiler=goma',
      'chromium_swarm_tests',
      '--',
      '-k', '0',
    ],
    swarm_server=config.Master.swarm_server_dev_internal_url,
    isolate_server=config.Master.swarm_hashtable_server_dev_internal,
    tests=[
      'base_unittests',
      'browser_tests',
      'interactive_ui_tests',
      'net_unittests',
      'unit_tests',
    ],
    factory_properties={
      'compile_env': {
        'ISOLATE_DEBUG': '1',
      },
      'gclient_env': {
        'GYP_DEFINES': 'fastbuild=2',
        'GYP_GENERATORS': 'ninja',
      },
    })

m_linux_isolate = swarm_factory.IsolatedFactory(
    'src/out',
    target_platform='linux2',
    full_checkout=True,
    swarm_client_canary=True)
f_chromium_linux_isolate = m_linux_isolate.IsolatedFactory(
    options=[
      '--build-tool=ninja',
      '--compiler=goma',
      'chromium_swarm_tests',
      '--',
      '-k', '0',
    ],
    tests=[
      'base_unittests',
      'browser_tests',
      'interactive_ui_tests',
      'net_unittests',
      'unit_tests',
    ],
    factory_properties={
      'compile_env': {
        'ISOLATE_DEBUG': '1',
      },
      'gclient_env': {
        'GYP_DEFINES': 'fastbuild=2',
        'GYP_GENERATORS': 'ninja',
      },
    })

m_win_swarm = swarm_factory.SwarmFactory(
    'src/out',
    target_platform='win32',
    full_checkout=True,
    swarm_client_canary=True)
f_chromium_win_swarm = m_win_swarm.SwarmFactory(
    options=[
      '--build-tool=ninja',
      '--compiler=goma',
      'chromium_swarm_tests',
      '--',
      '-k', '0',
    ],
    swarm_server=config.Master.swarm_server_dev_internal_url,
    isolate_server=config.Master.swarm_hashtable_server_dev_internal,
    tests=[
      'base_unittests',
      'browser_tests',
      'interactive_ui_tests',
      'net_unittests',
      'unit_tests',
    ],
    factory_properties={
      'compile_env': {
        'ISOLATE_DEBUG': '1',
      },
      'gclient_env': {
        'GYP_DEFINES': 'fastbuild=2',
        'GYP_GENERATORS': 'ninja',
      },
    })

m_win_isolate = swarm_factory.IsolatedFactory(
    'src/out',
    target_platform='win32',
    full_checkout=True,
    swarm_client_canary=True)
f_chromium_win_isolate = m_win_isolate.IsolatedFactory(
    options=[
      '--build-tool=ninja',
      '--compiler=goma',
      'chromium_swarm_tests',
      '--',
      '-k', '0',
    ],
    tests=[
      'base_unittests',
      'browser_tests',
      'interactive_ui_tests',
      'net_unittests',
      'unit_tests',
    ],
    factory_properties={
      'compile_env': {
        'ISOLATE_DEBUG': '1',
      },
      'gclient_env': {
        'GYP_DEFINES': 'fastbuild=2',
        'GYP_GENERATORS': 'ninja',
      },
    })

m_mac_swarm = swarm_factory.SwarmFactory(
    'src/out',
    target_platform='darwin',
    full_checkout=True,
    swarm_client_canary=True)
f_chromium_mac_swarm = m_mac_swarm.SwarmFactory(
    options=[
      '--build-tool=ninja',
      '--compiler=goma-clang',
      'chromium_swarm_tests',
      '--',
      '-k', '0',
    ],
    swarm_server=config.Master.swarm_server_dev_internal_url,
    isolate_server=config.Master.swarm_hashtable_server_dev_internal,
    tests=[
      'base_unittests',
      'browser_tests',
      'interactive_ui_tests',
      'net_unittests',
      'unit_tests',
    ],
    factory_properties={
      'compile_env': {
        'ISOLATE_DEBUG': '1',
      },
      'gclient_env': {
        'GYP_DEFINES': 'fastbuild=2',
        'GYP_GENERATORS': 'ninja',
      },
    })

m_mac_isolate = swarm_factory.IsolatedFactory(
    'src/out',
    target_platform='darwin',
    full_checkout=True,
    swarm_client_canary=True)
f_chromium_mac_isolate = m_mac_isolate.IsolatedFactory(
    options=[
      '--build-tool=ninja',
      '--compiler=goma-clang',
      'chromium_swarm_tests',
      '--',
      '-k', '0',
    ],
    tests=[
      'base_unittests',
      'browser_tests',
      'interactive_ui_tests',
      'net_unittests',
      'unit_tests',
    ],
    factory_properties={
      'compile_env': {
        'ISOLATE_DEBUG': '1',
      },
      'gclient_env': {
        'GYP_DEFINES': 'fastbuild=2',
        'GYP_GENERATORS': 'ninja',
      },
    })


# ----------------------------------------------------------------------------
# BUILDER DEFINITIONS

b_chromium_linux_swarm = {
  'name': 'Linux Swarm Tests',
  'builddir': 'chromium-linux-swarm',
  'factory': f_chromium_linux_swarm,
  'auto_reboot': False,
  'category': 'Swarm',
}

b_chromium_linux_isolate = {
  'name': 'Linux Isolate Tests',
  'builddir': 'chromium-linux-isolate',
  'factory': f_chromium_linux_isolate,
  'category': 'Isolate',
}

b_chromium_win_swarm = {
  'name': 'Windows Swarm Tests',
  'builddir': 'chromium-win-swarm',
  'factory': f_chromium_win_swarm,
  'auto_reboot': False,
  'category': 'Swarm',
}

b_chromium_win_isolate = {
  'name': 'Windows Isolate Tests',
  'builddir': 'chromium-win-isolate',
  'factory': f_chromium_win_isolate,
  'category': 'Isolate',
}

b_chromium_mac_swarm = {
  'name': 'Mac Swarm Tests',
  'builddir': 'chromium-mac-swarm',
  'factory': f_chromium_mac_swarm,
  'auto_reboot': False,
  'category': 'Swarm',
}

b_chromium_mac_isolate = {
  'name': 'Mac Isolate Tests',
  'builddir': 'chromium-mac-isolate',
  'factory': f_chromium_mac_isolate,
  'auto_reboot': False,
  'category': 'Isolate',
}

c['builders'] = [b_chromium_linux_isolate,
                 b_chromium_linux_swarm,
                 b_chromium_win_isolate,
                 b_chromium_win_swarm,
                 b_chromium_mac_isolate,
                 b_chromium_mac_swarm]

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'ChromiumSwarm')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### STATUS TARGETS

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster,
    public_html="../master.chromium.git/public_html",
    templates=['../master.chromium/templates'],
    enable_http_status_push=ActiveMaster.is_production_host)

if ActiveMaster.is_production_host:
  c['status'].append(gatekeeper.GateKeeper(
        None,
        fromaddr='isolate@chromium.org',
        relayhost=config.Master.smtp,
        status_header='%(steps)s failed on "%(builder)s"',
        subject='chromium.swarm %(result)s in %(projectName)s '
                'on %(builder)s',
        extraRecipients=['isolate@chromium.org'],
        sendToInterestedUsers=False))

####### PROJECT IDENTITY

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
c['buildbotURL'] = 'http://build.chromium.org/p/chromium.swarm/'
