#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# Copyright (c) 2007 Tarek Ziadé
#
# Authors:
#   Tarek Ziadé <tarek@ziade.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
import datetime, time, os.path
from buildbot.scheduler import Scheduler, Nightly
from buildbot.process import step, factory
from buildbot.status import html
from buildbot.status import mail
from buildbot.changes.pb import PBChangeSource
from twisted.python import log

# configuration
SVN_USER = 'buildbot'
SVN_PASSWORD = 'bb426hy544'
SVN_SERVER = 'localhost/repo'
TEST_BUNDLE = '/private/ecs/bundles/emencia'
PATH_BUNDLE = '/private/ecs/build/ecs'
TARGET_MAIL = 'checkins@lists.emencia.net'
SOURCE_MAIL = 'buildbot@lists.emencia.net'
SVN_FRONT_NAME = 'svn.emencia.net'

svnserver = 'http://%s:%s@/%s' % (SVN_USER, SVN_PASSWORD, SVN_SERVER)
port = 9989
wport = 8010

class RepositoryScheduler(Scheduler):
        def __init__(self, name, branch, treeStableTimer, builderNames,
                     repository, fileIsImportant=None):
                Scheduler.__init__(self, name, branch, treeStableTimer,
                                   builderNames, fileIsImportant)
                self.repository = repository

        def addChange(self, change):
                # check to make sure the repository matches!
                cs = change.comments.split(' ')
                if len(cs) > 0:
                        repo = cs[0]
                        log.msg('checking %s vs %s' %
                                 (repo, self.repository))
                        if repo != self.repository:
                                log.msg("%s ignoring repository %s" %
                                        (self, repo))
                                return
                # call our parent since this on the correct repository
                Scheduler.addChange(self, change)

s = factory.s
c = BuildmasterConfig = {}
c['bots'] = [("bot", SVN_PASSWORD)]
c['sources'] = [PBChangeSource()]
c['schedulers'] = []
c['builders'] = []
c['status'] = []

repos = (('ecs', TEST_BUNDLE
          'http://%s:%s@%s/%s' % (SVN_USER, SVN_PASSWORD, SVN_SERVER,
                                  TEST_PATH),)

for name, path, svnurl in repos:
    quick = Scheduler(name, None, 60, [name])
    c['schedulers'].append(quick)
    test_steps = [s(step.SVN,  mode="update", baseURL= svnserver + path,
                    svnurl=svnurl),
                  s(step.Test, command=["make", "test"])]
    test_f = factory.BuildFactory(test_steps)
    pauline = {'name': name, 'slavename': 'bot',
                'builddir': name, 'factory': test_f}
    c['builders'].append(pauline)

names = [name for name, path, svnurl in repos]

c['status'].append(mail.MailNotifier(builders=names,
                   fromaddr=SOURCE_MAIL,
                   extraRecipients=[TARGET_MAIL],
                   mode='failing',
                   sendToInterestedUsers=True))


c['status'].append(html.Waterfall(http_port=wport))
c['slavePortnum'] = port
c['projectName'] = "ecs"
c['projectURL'] = ""
c['buildbotURL'] = "http://%s:%d/" % (SVN_FRONT_NAME, wport)

