view talosnames/api.py @ 6:82aad57c7d1d

add regex mapping
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 10 Jul 2012 14:04:34 -0700
parents 2d883dd59a1a
children 1029ddf7b806
line wrap: on
line source

import re
import sqlite3
import urllib2

class TalosNames(object):
    graphserver_sql = 'http://hg.mozilla.org/graphs/raw-file/tip/sql/data.sql'
    schema = 'http://hg.mozilla.org/graphs/raw-file/tip/sql/schema.sql'

    # mapping file from builbot-configs name to tbpl codes:
    # http://hg.mozilla.org/users/mstange_themasta.com/tinderboxpushlog/file/tip/js/Config.js
    tbpl_map = 'http://hg.mozilla.org/users/mstange_themasta.com/tinderboxpushlog/raw-file/tip/js/Data.js'

    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)'
              }

    def __init__(self):
        self.setup_database()
        self.tbpl_mapping()

    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))
        cursor.executescript(sql)
        self.db.commit()
        cursor.close()

        # create data structures
        self.names = {}
        self.chrome = 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:
                self.chrome.add(short_name)
        cursor.close()

    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 __call__(self, name=None):
        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