# -*- python -*-

# This is a sample buildmaster config file. It must be installed as
# 'master.cfg' in your buildmaster's base directory (although the filename
# can be changed with the --basedir option to 'mktap buildbot master').

# It has one job: define a dictionary named BuildmasterConfig. This
# dictionary has a variety of keys to control different aspects of the
# buildmaster. They are documented in docs/config.xhtml .

import os.path, time
from buildbot.buildslave import BuildSlave
from buildbot.scheduler import Scheduler, Nightly, log
from buildbot.process import factory
from buildbot.status import html
from buildbot.steps.shell import ShellCommand, Test, Compile
from buildbot.steps.source import SVN
from buildbot.steps.transfer import FileUpload, DirectoryUpload
from buildbot import util
s = factory.s

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

# the 'bots' list defines the set of allowable buildslaves. Each element is a
# tuple of bot-name and bot-password. These correspond to values given to the
# buildslave's mktap invocation.
c['slaves'] = [
XXX
]


# the 'change_source' setting tells the buildmaster how it should find out
# about source code changes. Any class which implements IChangeSource can be
# put here: there are several in buildbot/changes/*.py to choose from.

from buildbot.changes.pb import PBChangeSource
c['change_source'] = PBChangeSource(prefix="python/", revlinktmpl="http://svn.python.org/view?rev=%s&view=rev")


# the 'builders' list defines the Builders. Each one is configured with a
# dictionary, using the following keys:
#  name (required): the name used to describe this builder
#  slavename (required): which slave to use, must appear in c['bots']
#  builddir (required): which subdirectory to run the builder in
#  factory (required): a BuildFactory to define how the build is run
#  periodicBuildTime (optional): if set, force a build every N seconds

# We use one scheduler per branch (as the AnyBranchScheduler is useless)
# and one builder per slave and branch

class Clean(ShellCommand):
    name = "clean"
    warnOnFailure = 1
    description = ["cleaning"]
    descriptionDone = ["clean"]
    command = ["make", "distclean"]
    alwaysRun = True

class Kill(ShellCommand):
    name = "kill.python"
    warnOnFailure = 1
    descripting = ["killing python"]
    descriptionDone = ["kill.python"]
    command = ["make", "-C", "Tools/buildbot"]

class MSIUpload(FileUpload):
    def __init__(self, build, branch, **buildstep_kwargs):
        # determine MSI filename later
        FileUpload.__init__(self, build, 'UNSET', 'UNSET', **buildstep_kwargs)
	self.branch = branch
        
    def start(self):
        # filename matches msi.py
        current_version = "%s.%s" % (self.branch, int(time.time()/3600/24))
        self.slavesrc = 'Tools/msi/python-%s.msi' % (current_version)
	self.masterdest = '/data/ftp.python.org/pub/www.python.org/dev/daily-msi/python-%s.msi' % current_version
	return FileUpload.start(self)

class UnixBuild(factory.GNUAutoconf):
    configureFlags = ["--with-pydebug", "--with-computed-gotos"]
    def __init__(self, source, parallel):
        compile = ['make', 'all']
        test = ["make", "buildbottest"]
        if parallel:
            compile = ['make', parallel, 'all']
            test = ["make", "buildbottest", "TESTOPTS="+parallel]
        factory.GNUAutoconf.__init__(self, source,
                                     configureFlags = self.configureFlags,
                                     compile=compile,
                                     test = None)
        # XXX(nnorwitz): reduced timeout, no test should take longer than 10m
        # XXX(anthony): computer says no. upped to 30m
        self.steps.append(s(Test, command=test,
                            timeout=30*60))
        self.steps.append(s(Clean))

class WideUnixBuild(UnixBuild):
    configureFlags = ["--with-pydebug", "--with-computed-gotos", "--with-wide-unicode"]

class CygwinBuild(UnixBuild):
    def __init__(self, source, parallel):
        UnixBuild.__init__(self, source)
        self.steps.insert(1, s(Kill))
        # XXX(nnorwitz): test_bsddb3 crashes the interpreter on cygwin.
        # Ignore test_bsddb3 to see if we can get some real results.
        test_step = self.steps[-2][1]
        test_step['env'] = {'EXTRATESTOPTS': '-uall -rw -x test_bsddb3 test_logging'}

class WindowsBuild(factory.BuildFactory):
    def __init__(self, source, parallel):
        factory.BuildFactory.__init__(self, [
            source,
            s(Compile, command=[r'Tools\buildbot\build.bat']),
            s(Test, command=[r'Tools\buildbot\test.bat']),
            s(Clean, command=[r'Tools\buildbot\clean.bat']),
            ])

class WindowsAMD64PCBuild(factory.BuildFactory):
    def __init__(self, source, parallel):
        factory.BuildFactory.__init__(self, [
            source,
            s(Compile, command=[r'Tools\buildbot\build-amd64.bat']),
            s(Test, command=[r'Tools\buildbot\test-amd64.bat']),
            s(Clean, command=[r'Tools\buildbot\clean-amd64.bat']),
            ])

class WindowsMSI(factory.BuildFactory):
    def __init__(self, source, branch):
        factory.BuildFactory.__init__(self, [source,
	    s(Compile, command=[r'Tools\buildbot\buildmsi.bat']),
	    s(MSIUpload, branch=branch, mode=0644),
	    #s(FileUpload, slavesrc=r"Tools\msi\100k.dat",  masterdest="/data/ftp.python.org/pub/www.python.org/dev/daily-msi/100k.dat", mode=0644),
	    #s(FileUpload, slavesrc=r"Tools\msi\1m.dat",  masterdest="/data/ftp.python.org/pub/www.python.org/dev/daily-msi/1m.dat", mode=0644),
	    #s(FileUpload, slavesrc=r"Tools\msi\10m.dat",  masterdest="/data/ftp.python.org/pub/www.python.org/dev/daily-msi/10m.dat", mode=0644),
	    s(Clean, command=[r'Tools\buildbot\clean.bat']),
	    ])

class DMG(factory.BuildFactory):
    def __init__(self, source, branch):
        current_version = "%s.%s" % (branch, int(time.time()/3600/24))
        outfile = "python-%s.dmg" % current_version
        factory.BuildFactory.__init__(self, [source,
            s(ShellCommand, description='umask', command=['/bin/sh', '-c', 'umask']),
            s(Compile, workdir='build/Mac/BuildScript', command=['python2.5', './build-installer.py', '--build-dir', '../../dmg']),
            #s(ShellCommand, description='rename', workdir='build/dmg/diskimage', command=['/bin/sh', '-c', 'mv *.dmg '+outfile]),
            s(DirectoryUpload, workdir='build/dmg', slavesrc='diskimage', masterdest='/data/ftp.python.org/pub/www.python.org/dev/daily-dmg'),
            ])

class DailyScheduler(Scheduler):
    # Issue builds not more than once a day
    lastbuildtime = 0

    def addImportantChange(self, change):
        log.msg("%s: change is important, adding %s" % (self, change))
        self.importantChanges.append(change)
        self.nextBuildTime = max(self.nextBuildTime, 
                                 self.lastbuildtime + 3600*24,
                                 change.when + self.treeStableTimer)
        log.msg("%s: next build at %s" % (self, self.nextBuildTime))
        self.setTimer(self.nextBuildTime)

    def fireTimer(self):
        Scheduler.fireTimer(self)
        self.lastbuildtime = util.now()

baseURL="http://svn.python.org/projects/python/"
branches = [
            ("branches/release27-maint", "2.7"),
            ("branches/release31-maint", "3.1"),
            ("branches/py3k", "3.x"),
           ]

STABLE=".stable"
UNSTABLE=".unstable"

builders = [("sparc solaris10 gcc", "loewis-sun", UnixBuild,STABLE),
            ("amd64 gentoo", "norwitz-amd64", UnixBuild,STABLE),
            ("x86 gentoo", "norwitz-x86", UnixBuild,UNSTABLE),
            #("g4 osx.4", "psf-g4", UnixBuild,STABLE),
            #("alpha Tru64 5.1", "norwitz-tru64", UnixBuild,UNSTABLE),
            ("i386 Ubuntu", "klose-ubuntu-i386", UnixBuild,UNSTABLE),
            ("ia64 Ubuntu", "klose-debian-ia64", UnixBuild,STABLE),
            ("sparc Debian", "klose-debian-sparc", UnixBuild,STABLE),
            ("alpha Debian", "klose-debian-alpha", UnixBuild,UNSTABLE),
            #("MIPS Debian", "klose-debian-mips", UnixBuild,UNSTABLE),
            #("PPC64 Debian", "klose-debian-ppc64", UnixBuild,UNSTABLE),
            #("S-390 Debian", "klose-debian-s390", UnixBuild,UNSTABLE),
            #("x86 mvlgcc", "loewis-linux", UnixBuild,UNSTABLE),
            ("x86 XP-4", "bolen-windows", WindowsBuild,STABLE),
            ("x86 FreeBSD", "bolen-freebsd", UnixBuild,UNSTABLE),
            ("x86 Windows7", "bolen-windows7", WindowsBuild,UNSTABLE),
            ("x86 FreeBSD 7.2", "bolen-freebsd7", UnixBuild,UNSTABLE),
            ("x86 Ubuntu", "bolen-ubuntu", UnixBuild,UNSTABLE),
	    ("ARMv7Thumb Ubuntu", "klose-linux-arm", UnixBuild,UNSTABLE),
	    ("ARMv4 Debian", "klose-linux-armeabi", UnixBuild,UNSTABLE),
	    #("x86 FreeBSD 2", "werven-freebsd", UnixBuild,UNSTABLE),
            #("x86 Gentoo", "murray-gentoo", UnixBuild, UNSTABLE),
            #("x86 Gentoo wide", "murray-gentoo-wide", WideUnixBuild, UNSTABLE),
            ("x86 XP-5", "moore-windows", WindowsBuild, UNSTABLE),
            ("x86 Tiger", "bolen-tiger", UnixBuild, UNSTABLE),
            ("PPC Tiger", "parc-tiger-1", UnixBuild, UNSTABLE),
            ("PPC Leopard", "parc-leopard-1", UnixBuild, UNSTABLE),
            ("x86 debian parallel", "loewis-parallel", UnixBuild,UNSTABLE),
            ("AMD64 debian parallel", "loewis-parallel2", UnixBuild,UNSTABLE),
           ]

dailybuilders = ["alpha Debian", "ARMv7Thumb Ubuntu"]

c['builders'] = []
c['schedulers'] = []

parallel = {'loewis-parallel':'-j4', 'loewis-parallel2':'-j48'}
from buildbot import locks
cpulock = locks.SlaveLock("cpu", maxCountForSlave={'loewis-parallel':4, 
                                                   'loewis-parallel2':4, 
                                                   'loewis-sun':2})

def is_important_file(filename):
    # Filename appears to be trunk/XXX or 2.4/XXX
    # Ignore changes from these directories, buildbot doesn't handle them.
    for prefix in ('/Misc/', '/Doc/', '/Demo/'):
        if filename.find(prefix) >= 0:
            return False
    return True

def is_important_change(change):
    # If any file is important, the change is important.
    for filename in change.files:
        if is_important_file(filename):
            return True
    return False

for branch, branchname in branches:
    buildernames = []
    dailybuildernames = []
    for name, slave, buildfactory, stability in builders:
        buildername = name + " " + branchname
        source = s(SVN, baseURL=baseURL, mode="update")
        p = parallel.get(slave)
        if branchname in ('3.1',):
            p = None # cannot do parallel tests in these branches
        f = buildfactory(source, p)
        if name in dailybuilders:
            dailybuildernames.append(buildername)
        else:
            buildernames.append(buildername)
        c['builders'].append({
                'name' : buildername,
                'slavename' : slave,
                'builddir' : '%s.%s' % (branchname,slave),
                'factory' : f,
                'category' : branchname+stability,
                'locks' : [cpulock],
                })
    c['schedulers'].append(Scheduler(name=branchname, 
                                 branch=branch,
                                 treeStableTimer=5*60, # seconds
                                 builderNames=buildernames,
                                 fileIsImportant=is_important_change))
    c['schedulers'].append(DailyScheduler(name=branchname+"-daily",
                                 branch=branch,
                                 treeStableTimer=5*60, # seconds
                                 builderNames=dailybuildernames,
                                 fileIsImportant=is_important_change))

for version, branch, hour in (
                              ('2.7', 'branches/release27-maint', 12), 
			      ('3.1', 'branches/release31-maint', 13),
			      ('3.x', 'branches/py3k', 14),
			      ):
    dmgbuilder = DMG(s(SVN, baseURL=baseURL, defaultBranch=branch, mode="update"), version)
    name = '%s.dmg' % version
    c['builders'].append({
        'name' : name,
        'slavename' : 'bolen-dmg',
        'builddir' : name,
        'category' : 'dmg',
        'factory' : dmgbuilder,
    })
    c['schedulers'].append(Nightly(name, [name], hour=hour, minute=0))

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

c['slavePortnum'] = 9020

# 'status' is a list of Status Targets. The results of each build will be
# pushed to these targets. buildbot/status/*.py has a variety to choose from,
# including web pages, email senders, and IRC bots.

buildbot_css = "/data/buildbot/master/python.css"

c['status'] = []
allowForce=True
c['status'].append(html.Waterfall(http_port='tcp:9010:interface=127.0.0.1',allowForce=allowForce,css=buildbot_css))
c['status'].append(html.Waterfall(http_port='tcp:9011:interface=127.0.0.1',categories=['2.7.stable','2.7.unstable'],allowForce=allowForce,css=buildbot_css))
#c['status'].append(html.Waterfall(http_port='tcp:9012:interface=127.0.0.1',categories=['dmg'],allowForce=allowForce,css=buildbot_css))
c['status'].append(html.Waterfall(http_port='tcp:9014:interface=127.0.0.1',categories=['3.1.stable','3.1.unstable'],allowForce=allowForce,css=buildbot_css))
c['status'].append(html.Waterfall(http_port='tcp:9021:interface=127.0.0.1',categories=['3.x.stable','3.x.unstable'],allowForce=allowForce,css=buildbot_css))

c['status'].append(html.Waterfall(http_port='tcp:9016:interface=127.0.0.1',categories=['2.7.stable'],allowForce=allowForce,css=buildbot_css))
c['status'].append(html.Waterfall(http_port='tcp:9017:interface=127.0.0.1',categories=['3.1.stable'],allowForce=allowForce,css=buildbot_css))
c['status'].append(html.Waterfall(http_port='tcp:9022:interface=127.0.0.1',categories=['3.x.stable'],allowForce=allowForce,css=buildbot_css))
c['status'].append(html.Waterfall(http_port='tcp:9018:interface=127.0.0.1',categories=['2.7.stable', '3.1.stable', '3.x.stable'],allowForce=allowForce,css=buildbot_css))

from buildbot.status import mail
c['status'].append(mail.MailNotifier(fromaddr="buildbot@python.org", mode="problem",
relayhost="mail.python.org",
replyaddr="python-checkins@python.org",
extraRecipients=["python-checkins@python.org"],
sendToInterestedUsers=False))
from buildbot.status import words
c['status'].append(words.IRC(host="irc.freenode.org", nick="py-bb",
channels=["#python-dev"], allowForce=False))


# if you set 'debugPassword', then you can connect to the buildmaster with
# the diagnostic tool in contrib/debugclient.py . From this tool, you can
# manually force builds and inject changes, which may be useful for testing
# your buildmaster without actually commiting changes to your repository (or
# before you have a functioning 'sources' set up). The debug tool uses the
# same port number as the slaves do: 'slavePortnum'.

c['debugPassword'] = "tivertio"

# if you set 'manhole', you can telnet into the buildmaster and get an
# interactive python shell, which may be useful for debugging buildbot
# internals. It is probably only useful for buildbot developers.
# from buildbot.master import Manhole
# c['manhole'] = Manhole(9999, "admin", "oneddens")

# the 'projectName' string will be used to describe the project that this
# buildbot is working on. For example, it is used as the title of the
# waterfall HTML page. The 'projectURL' string will be used to provide a link
# from buildbot HTML pages to your project's home page.

c['projectName'] = "Python"
c['projectURL'] = "http://www.python.org/"

# the 'buildbotURL' string should point to the location where the buildbot's
# internal web server (usually the html.Waterfall page) is visible. This
# typically uses the port number set in the Waterfall 'status' entry, but
# with an externally-visible host name which the buildbot cannot figure out
# without some help.

c['buildbotURL'] = "http://www.python.org/dev/buildbot/all/"