view autobot/template.py @ 41:42ef457708de

stub out adding factories to our specialty CLI class
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 Jan 2011 11:22:10 -0800
parents b41f50162908
children 6981dcad3b2c
line wrap: on
line source

#!/usr/bin/env python

"""
templates for the A*Team's buildbot
"""

import os
import sys
from makeitso.cli import MakeItSoCLI
from makeitso.template import assemble
from makeitso.template import MakeItSoTemplate
from makeitso.template import Variable
from projects import factories
from StringIO import StringIO

try:
    from subprocess import check_call as call
except ImportError:
    from subprocess import call

class AutobotMasterTemplate(MakeItSoTemplate):
    name = 'autobot-master'
    description = 'template for the autotools buildbot master'
    templates = [os.path.join('template', 'master')]
    vars = [Variable('slave', 'buildslave name', 'slave'),
            Variable('passwd', 'buildslave password', default='passwd'),
            Variable('slaveport', 'port to talk to slaves on', default=9010, cast=int),
            Variable('htmlport', 'port for waterfall display', default=8010, cast=int)]

    def factory_descriptions(self):
        buffer = StringIO()
        print >> buffer, 'Factories:\n'
        for key in sorted(factories.keys()):
            print >> buffer, '%s:' % key
            print >> buffer, getattr(factories[key], '__doc__', '').strip()
            print >> buffer
        return buffer.getvalue()

    def pre(self, variables):
        factory = variables.get('factory')
        if factory:
            assert factory in factories, 'Factory must be one of: ' % ', '.join(factories.keys())
        elif self.interactive:
            print self.factory_descriptions()
            sys.stdout.write('Enter factory: ')
            factory = raw_input()
            assert factory in factories, 'Factory must be one of: ' % ', '.join(factories.keys())
            variables['factory'] = factory
        else:
            raise AssertionError("No factory provided")

    def create(self, output, variables):
        command = ['buildbot', 'create-master', output]
        print ' '.join(command)
        call(command)

    def post(self, variables):
        """
        called after the template is applied
        """
        self.create(self.output, variables)

class AutobotSlaveTemplate(MakeItSoTemplate):
    name = 'autobot-slave'
    description = 'template for the autotools buildbot slave'
    templates = [os.path.join('template', 'slave')]
    vars = [Variable('master', 'host of the master', default='localhost'),
            Variable('slave', 'buildslave name', 'slave'),
            Variable('passwd', 'buildslave password', default='passwd'),
            Variable('slaveport', 'port to talk to slaves on', default=9010)]

    def create(self, output, variables):
        command = ['buildslave', 'create-slave', output,
                   '%s:%d' % (variables['master'], variables['slaveport']),
                   variables['slave'],
                   variables['passwd'] ]
        print ' '.join(command)
        call(command)
        

    def post(self, variables):
        self.create(self.output, variables)


class AutobotTemplate(AutobotMasterTemplate, AutobotSlaveTemplate):
    name = 'autobot'
    description = 'template for the autotools buildbot master+slave'
    templates = ['template']
    vars = assemble(AutobotMasterTemplate, AutobotSlaveTemplate)
    vars.append(Variable('debug', default=True, cast='eval'))

    def post(self, variables):
        AutobotMasterTemplate.create(self, os.path.join(self.output, 'master'), variables)
        AutobotSlaveTemplate.create(self, os.path.join(self.output, 'slave'), variables)

# CLI front end functions
# (console_script entry points)

class AutobotMasterCLI(MakeItSoCLI):
    """
    command line handler for the master
    """
    def parser(self):
        parser = MakeItSoCLI.parser(self)
        parser.add_option('-f', '--factory', dest='factory',
                          help="factory to use")
        parser.add_option('--list-factories', dest='_list_factories',
                          default=False, action='store_true',
                          help="list available factories")
        return parser

    def parse(self, parser=None, options=None, args=None):

        # parse the command line                                                    
        if not parser or not options or not args:
            parser = self.parser()
            options, args = parser.parse_args()

        # list the factories
        if options._list_factories:
            print self.template.factory_descriptions()
            parser.exit()

        # call the parent
        MakeItSoCLI.parse(self, parser, options, args)

def create_master(args=sys.argv[1:]):
    cli = AutobotMasterCLI(AutobotMasterTemplate)
    template = cli.parse()
    template.substitute()

def create_slave(args=sys.argv[1:]):
    cli = MakeItSoCLI(AutobotSlaveTemplate)
    template = cli.parse()
    template.substitute()

def create_autobot(args=sys.argv[1:]):
    cli = MakeItSoCLI(AutobotTemplate)
    template = cli.parse()
    template.substitute()

if __name__ == '__main__':
    create_master()