view talosnames/ @ 69:25812a846d24

update to new location;
author Jeff Hammel <>
date Thu, 13 Dec 2012 11:06:13 -0800
parents 78e72729582e
line wrap: on
line source

import os
import re
import require
import subprocess
import sqlite3
import sys
import talos
import talos.run_tests
import tempfile
import urllib2
import yaml

    call = subprocess.check_call
    call =

talos_dir = os.path.dirname(os.path.abspath(talos.__file__))

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

class TalosNames(object):

    # files for graphserver
    graphserver_sql = ''
    schema = ''

    # files for buildbot
    project_branches = ''
    buildbot_config = ''
    localconfig = ''

    # mapping file from builbot-configs name to tbpl codes:
    tbpl_map = ''

    tables = {'os_list': '(id, name text)',
              'branches': '(id, name text)',
              'machines': '(id, os_id int, is_throttling int, cpu_speed text, name text, is_active int, date_added int)',
              'pagesets': '(id, name text)',
              'tests': '(id, name text, pretty_name text, is_chrome int, is_active int, pageset_id int)'

    ### initialization functions

    def __init__(self):

        # cache
        self._talos_configs = {}
        self._tbpl_names = {}

    def setup_database(self):
        self.db = sqlite3.connect(':memory:')
        sql_lines = urllib2.urlopen(self.graphserver_sql).readlines()

        # XXX remove the machines since they require a function, unix_timestamp(), sqlite does not have
        sql_lines = [line for line in sql_lines
                     if 'unix_timestamp' not in line]
        sql = '\n'.join(sql_lines)

        cursor = self.db.cursor()
        for table, schema in self.tables.items():
            cursor.execute("""CREATE TABLE %s %s""" % (table, schema))

        # create data structures
        self.names = {} = set()
        cursor = self.db.cursor()
        cursor.execute("SELECT * FROM tests")
        for _, short_name, graphserver_name, is_chrome, _, _ in cursor.fetchall():
            self.names[short_name] = graphserver_name
            if is_chrome:

    def tbpl_mapping(self):
        self.tbpl_regexs = {}
        lines = [line.strip()
                 for line in urllib2.urlopen(self.tbpl_map).readlines()]
        lines = [line for line in lines
                 if line.startswith('/talos ')]
        regex = re.compile('\/talos (.*)\/.*\?.*\"([^"].*)\".*')
        for line in lines:
            match = regex.match(line)
            assert match
            _regex, name = match.groups()
            self.tbpl_regexs[name] = _regex

    def setup_buildbot(self):
        localconfig, project_branches, module = require.require(self.localconfig,
                                                                **{self.localconfig: ""})
        self.suites = module.SUITES
        self.buildbot_commands = {}
        self.buildbot_enabled = {}
        for key, value in self.suites.items():
            self.buildbot_commands[key] = value['suites']
            self.buildbot_enabled[key] = value['enable_by_default']

    ### functions for fetching information

    def tbpl_name(self, name):
        """returns the TBPL long name from buildbot name"""
        if name.startswith('remote-'):
            name = name[len('remote-'):]

        if name in self._tbpl_names:
            return self._tbpl_names[name]
        for tbplname, regex in self.tbpl_regexs.items():
            regex = re.compile(regex)
            if regex.match(name):
                self._tbpl_names[name] = tbplname
                return tbplname

    def buildbot_command(self, name):
        """gets the buildbot command for a particular suite"""
        return self.buildbot_commands.get(name)[:]

    def talos_config(self, name):
        """returns talos configuration for suite `name`"""
        if name in self._talos_configs:
            return self._talos_configs[name]

        command = self.buildbot_command(name)
        assert command is not None, "Suite not found: %s" % name
        outfile = tempfile.mktemp(suffix='.yml')
        command += ['-o', outfile] # add an output file
        command += ['-e', sys.executable] # add a pretend firefox so PerfConfigurator won't whine
        print "\n%s" % subprocess.list2cmdline(command)
        call(['PerfConfigurator'] + command, stdout=subprocess.PIPE, cwd=talos_dir)
        assert os.path.exists(outfile)
        config = yaml.load(file(outfile))
        self._talos_configs[name] = config
        return config

    def test_config(self, name):
        config = self.talos_config(name)
        test_config = config['tests']
        talos.run_tests.useBaseTestDefaults(config.get('basetest', {}), test_config)
        retval = {}
        for test in test_config:
            name = test.pop('name')
            retval[name] = test
        return retval

    def __call__(self, name=None):
        """returns the graphserver name prefixed with `name`"""

        retval = []
        for short_name, graphserver_name in self.names.items():
            if (name is None) or (name == short_name or short_name.startswith(name + '_')):
                retval.append((short_name, graphserver_name))
        retval.sort(key=lambda x: x[0])
        return retval

    def graphserver_name(self, name):
        for short_name, graphserver_name in self.names.items():
            if name == short_name:
                return (short_name, graphserver_name)