author Dustin J. Mitchell <dustin@mozilla.com>
Mon, 16 May 2016 22:53:22 +0000
changeset 338662 c3e24e94ab5148ad92a3f137d6cab9d8bebdd6a7
child 340219 2555e6b571f37b4a2315339858af46ac0c0713a2
permissions -rw-r--r--
Bug 1258497: Implement a new taskgraph generation system; r=gps The `taskgraph` package generates TaskCluster task graphs based on collections of task "kinds". Initially, there is only one kind, the "legacy" kind, which reads the YAML files from `testing/taskcluster/tasks` to generate the task graph. Try syntax is implemented by filtering the tasks in the taskgraph after it has been created, then extending the result to include any prerequisite tasks. A collection of `mach taskgraph` subcommands are provided for developers to extend or debug the task-graph generation process. MozReview-Commit-ID: 1TJCns4XxZ8

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function, unicode_literals

import argparse
import copy
import re
import shlex


# The build type aliases are very cryptic and only used in try flags these are
# mappings from the single char alias to a longer more recognizable form.
    'o': 'opt',
    'd': 'debug'

# mapping from shortcut name (usable with -u) to a boolean function identifying
# matching test names
def alias_prefix(prefix):
    return lambda name: name.startswith(prefix)

def alias_contains(infix):
    return lambda name: infix in name

def alias_matches(pattern):
    pattern = re.compile(pattern)
    return lambda name: pattern.match(name)

    'cppunit': alias_prefix('cppunit'),
    'crashtest': alias_prefix('crashtest'),
    'crashtest-e10s': alias_prefix('crashtest-e10s'),
    'e10s': alias_contains('e10s'),
    'firefox-ui-functional': alias_prefix('firefox-ui-functional'),
    'firefox-ui-functional-e10s': alias_prefix('firefox-ui-functional-e10s'),
    'gaia-js-integration': alias_contains('gaia-js-integration'),
    'gtest': alias_prefix('gtest'),
    'jittest': alias_prefix('jittest'),
    'jittests': alias_prefix('jittest'),
    'jsreftest': alias_prefix('jsreftest'),
    'jsreftest-e10s': alias_prefix('jsreftest-e10s'),
    'luciddream': alias_prefix('luciddream'),
    'marionette': alias_prefix('marionette'),
    'marionette-e10s': alias_prefix('marionette-e10s'),
    'mochitest': alias_prefix('mochitest'),
    'mochitests': alias_prefix('mochitest'),
    'mochitest-e10s': alias_prefix('mochitest-e10s'),
    'mochitests-e10s': alias_prefix('mochitest-e10s'),
    'mochitest-debug': alias_prefix('mochitest-debug-'),
    'mochitest-a11y': alias_contains('mochitest-a11y'),
    'mochitest-bc': alias_prefix('mochitest-browser-chrome'),
    'mochitest-bc-e10s': alias_prefix('mochitest-browser-chrome-e10s'),
    'mochitest-browser-chrome': alias_prefix('mochitest-browser-chrome'),
    'mochitest-browser-chrome-e10s': alias_prefix('mochitest-browser-chrome-e10s'),
    'mochitest-chrome': alias_contains('mochitest-chrome'),
    'mochitest-dt': alias_prefix('mochitest-devtools-chrome'),
    'mochitest-dt-e10s': alias_prefix('mochitest-devtools-chrome-e10s'),
    'mochitest-gl': alias_prefix('mochitest-webgl'),
    'mochitest-gl-e10s': alias_prefix('mochitest-webgl-e10s'),
    'mochitest-jetpack': alias_prefix('mochitest-jetpack'),
    'mochitest-media': alias_prefix('mochitest-media'),
    'mochitest-media-e10s': alias_prefix('mochitest-media-e10s'),
    'mochitest-vg': alias_prefix('mochitest-valgrind'),
    'reftest': alias_matches(r'^(plain-)?reftest.*$'),
    'reftest-no-accel': alias_matches(r'^(plain-)?reftest-no-accel.*$'),
    'reftests': alias_matches(r'^(plain-)?reftest.*$'),
    'reftests-e10s': alias_matches(r'^(plain-)?reftest-e10s.*$'),
    'robocop': alias_prefix('robocop'),
    'web-platform-test': alias_prefix('web-platform-tests'),
    'web-platform-tests': alias_prefix('web-platform-tests'),
    'web-platform-tests-e10s': alias_prefix('web-platform-tests-e10s'),
    'web-platform-tests-reftests': alias_prefix('web-platform-tests-reftests'),
    'web-platform-tests-reftests-e10s': alias_prefix('web-platform-tests-reftests-e10s'),
    'xpcshell': alias_prefix('xpcshell'),

# unittest platforms can be specified by substring of the "pretty name", which
# is basically the old Buildbot builder name.  This dict has {pretty name,
# [test_platforms]} translations, This includes only the most commonly-used
# substrings.  This is intended only for backward-compatibility.  New test
# platforms should have their `test_platform` spelled out fully in try syntax.
    'Ubuntu': ['linux', 'linux64'],
    'x64': ['linux64'],
    # other commonly-used substrings for platforms not yet supported with
    # in-tree taskgraphs:
    #'10.10': [..TODO..],
    #'10.10.5': [..TODO..],
    #'10.6': [..TODO..],
    #'10.8': [..TODO..],
    #'Android 2.3 API9': [..TODO..],
    #'Android 4.3 API15+': [..TODO..],
    #'Windows 7':  [..TODO..],
    #'Windows 7 VM': [..TODO..],
    #'Windows 8':  [..TODO..],
    #'Windows XP': [..TODO..],
    #'win32': [..TODO..],
    #'win64': [..TODO..],

# We have a few platforms for which we want to do some "extra" builds, or at
# least build-ish things.  Sort of.  Anyway, these other things are implemented
# as different "platforms".
    'linux64': [

TEST_CHUNK_SUFFIX = re.compile('(.*)-([0-9]+)$')

class TryOptionSyntax(object):

    def __init__(self, message, full_task_graph):
        Parse a "try syntax" formatted commit message.  This is the old "-b do -p
        win32 -u all" format.  Aliases are applied to map short names to full

        The resulting object has attributes:

        - build_types: a list containing zero or more of 'opt' and 'debug'
        - platforms: a list of selected platform names, or None for all
        - unittests: a list of tests, of the form given below, or None for all
        - jobs: a list of requested job names, or None for all
        - trigger_tests: the number of times tests should be triggered
        - interactive; true if --interactive

        Note that -t is currently completely ignored.

        The unittests and talos lists contain dictionaries of the form:

            'test': '<suite name>',
            'platforms': [..platform names..], # to limit to only certain platforms
            'only_chunks': set([..chunk numbers..]), # to limit only to certain chunks
        self.jobs = []
        self.build_types = []
        self.platforms = []
        self.unittests = []
        self.trigger_tests = 0
        self.interactive = False

        # shlex used to ensure we split correctly when giving values to argparse.
        parts = shlex.split(self.escape_whitespace_in_brackets(message))
        try_idx = None
        for idx, part in enumerate(parts):
            if part == TRY_DELIMITER:
                try_idx = idx

        if try_idx is None:

        # Argument parser based on try flag flags
        parser = argparse.ArgumentParser()
        parser.add_argument('-b', '--build', dest='build_types')
        parser.add_argument('-p', '--platform', nargs='?', dest='platforms', const='all', default='all')
        parser.add_argument('-u', '--unittests', nargs='?', dest='unittests', const='all', default='all')
        parser.add_argument('-i', '--interactive', dest='interactive', action='store_true', default=False)
        parser.add_argument('-j', '--job', dest='jobs', action='append')
        # In order to run test jobs multiple times
        parser.add_argument('--trigger-tests', dest='trigger_tests', type=int, default=1)
        args, _ = parser.parse_known_args(parts[try_idx:])

        self.jobs = self.parse_jobs(args.jobs)
        self.build_types = self.parse_build_types(args.build_types)
        self.platforms = self.parse_platforms(args.platforms)
        self.unittests = self.parse_unittests(args.unittests, full_task_graph)
        self.trigger_tests = args.trigger_tests
        self.interactive = args.interactive

    def parse_jobs(self, jobs_arg):
        if not jobs_arg or jobs_arg == ['all']:
            return None
        expanded = []
        for job in jobs_arg:
            expanded.extend(j.strip() for j in job.split(','))
        return expanded

    def parse_build_types(self, build_types_arg):
        if build_types_arg is None:
            build_types_arg = []
        build_types = filter(None, [ BUILD_TYPE_ALIASES.get(build_type) for
                build_type in build_types_arg ])
        return build_types

    def parse_platforms(self, platform_arg):
        if platform_arg == 'all':
            return None

        results = []
        for build in platform_arg.split(','):
            if build in RIDEALONG_BUILDS:

        return results

    def parse_unittests(self, unittest_arg, full_task_graph):
        Parse a unittest (-u) option, in the context of a full task graph containing
        available `unittest_try_name` attributes.  There are three cases:

            - unittest_arg is == 'none' (meaning an empty list)
            - unittest_arg is == 'all' (meaning use the list of jobs for that job type)
            - unittest_arg is comma string which needs to be parsed

        # Empty job list case...
        if unittest_arg is None or unittest_arg == 'none':
            return []

        all_platforms = set(t.attributes['test_platform']
                            for t in full_task_graph.tasks.itervalues()
                            if 'test_platform' in t.attributes)

        tests = self.parse_test_opts(unittest_arg, all_platforms)

        if not tests:
            return []

        all_tests = set(t.attributes['unittest_try_name']
                        for t in full_task_graph.tasks.itervalues()
                        if 'unittest_try_name' in t.attributes)

        # Special case where tests is 'all' and must be expanded
        if tests[0]['test'] == 'all':
            results = []
            all_entry = tests[0]
            for test in all_tests:
                entry = {'test': test}
                # If there are platform restrictions copy them across the list.
                if 'platforms' in all_entry:
                    entry['platforms'] = list(all_entry['platforms'])
            return self.parse_test_chunks(all_tests, results)
            return self.parse_test_chunks(all_tests, tests)

    def parse_test_opts(self, input_str, all_platforms):
        Parse `testspec,testspec,..`, where each testspec is a test name
        optionally followed by a list of test platforms or negated platforms in

        No brackets indicates that tests should run on all platforms for which
        builds are available.  If testspecs are provided, then each is treated,
        from left to right, as an instruction to include or (if negated)
        exclude a set of test platforms.  A single spec may expand to multiple
        test platforms via UNITTEST_PLATFORM_PRETTY_NAMES.  If the first test
        spec is negated, processing begins with the full set of available test
        platforms; otherwise, processing begins with an empty set of test

        # Final results which we will return.
        tests = []

        cur_test = {}
        token = ''
        in_platforms = False

        def normalize_platforms():
            if 'platforms' not in cur_test:
            # if the first spec is a negation, start with all platforms
            if cur_test['platforms'][0][0] == '-':
                platforms = all_platforms.copy()
                platforms = []
            for platform in cur_test['platforms']:
                if platform[0] == '-':
                    platforms = [p for p in platforms if p != platform[1:]]
            cur_test['platforms'] = platforms

        def add_test(value):
            cur_test['test'] = value.strip()
            tests.insert(0, cur_test)

        def add_platform(value):
            platform = value.strip()
            if platform[0] == '-':
                negated = True
                platform = platform[1:]
                negated = False
            platforms = UNITTEST_PLATFORM_PRETTY_NAMES.get(platform, [platform])
            if negated:
                platforms = ["-" + p for p in platforms]
            cur_test['platforms'] = platforms + cur_test.get('platforms', [])

        # This might be somewhat confusing but we parse the string _backwards_ so
        # there is no ambiguity over what state we are in.
        for char in reversed(input_str):

            # , indicates exiting a state
            if char == ',':

                # Exit a particular platform.
                if in_platforms:

                # Exit a particular test.
                    cur_test = {}

                # Token must always be reset after we exit a state
                token = ''
            elif char == '[':
                # Exiting platform state entering test state.
                token = ''
                in_platforms = False
            elif char == ']':
                # Entering platform state.
                in_platforms = True
                # Accumulator.
                token = char + token

        # Handle any left over tokens.
        if token:

        return tests

    def handle_alias(self, test, all_tests):
        Expand a test if its name refers to an alias, returning a list of test
        dictionaries cloned from the first (to maintain any metadata).
        if test['test'] not in UNITTEST_ALIASES:
            return [test]

        alias = UNITTEST_ALIASES[test['test']]
        def mktest(name):
            newtest = copy.deepcopy(test)
            newtest['test'] = name
            return newtest

        def exprmatch(alias):
            return [t for t in all_tests if alias(t)]

        return [mktest(t) for t in exprmatch(alias)]

    def parse_test_chunks(self, all_tests, tests):
        Test flags may include parameters to narrow down the number of chunks in a
        given push. We don't model 1 chunk = 1 job in taskcluster so we must check
        each test flag to see if it is actually specifying a chunk.
        results = []
        seen_chunks = {}
        for test in tests:
            matches = TEST_CHUNK_SUFFIX.match(test['test'])

            if not matches:
                results.extend(self.handle_alias(test, all_tests))

            name = matches.group(1)
            chunk = matches.group(2)
            test['test'] = name

            for test in self.handle_alias(test, all_tests):
                name = test['test']
                if name in seen_chunks:
                    seen_chunks[name] = {chunk}
                    test['test'] = name
                    test['only_chunks'] = seen_chunks[name]

        # uniquify the results over the test names
        results = {test['test']: test for test in results}.values()
        return results

    def find_all_attribute_suffixes(self, graph, prefix):
        rv = set()
        for t in graph.tasks.itervalues():
            for a in t.attributes:
                if a.startswith(prefix):
        return sorted(rv)

    def escape_whitespace_in_brackets(self, input_str):
        In tests you may restrict them by platform [] inside of the brackets
        whitespace may occur this is typically invalid shell syntax so we escape it
        with backslash sequences    .
        result = ""
        in_brackets = False
        for char in input_str:
            if char == '[':
                in_brackets = True
                result += char

            if char == ']':
                in_brackets = False
                result += char

            if char == ' ' and in_brackets:
                result += '\ '

            result += char

        return result

    def task_matches(self, attributes):
        attr = attributes.get
        if attr('kind') == 'legacy':
            if attr('legacy_kind') in ('build', 'post_build'):
                if attr('build_type') not in self.build_types:
                    return False
                if self.platforms is not None:
                    if attr('build_platform') not in self.platforms:
                        return False
                return True
            elif attr('legacy_kind') == 'job':
                if self.jobs is not None:
                    if attr('job') not in self.jobs:
                        return False
                return True
            elif attr('legacy_kind') == 'unittest':
                if attr('build_type') not in self.build_types:
                    return False
                if self.platforms is not None:
                    if attr('build_platform') not in self.platforms:
                        return False
                if self.unittests is not None:
                    # TODO: optimize this search a bit
                    for ut in self.unittests:
                        if attr('unittest_try_name') == ut['test']:
                        return False
                    if 'platforms' in ut and attr('test_platform') not in ut['platforms']:
                        return False
                    if 'only_chunks' in ut and attr('test_chunk') not in ut['only_chunks']:
                        return False
                    return True
                return True
            return False
            # TODO: match other kinds
            return False

    def __str__(self):
        def none_for_all(list):
            if list is None:
                return '<all>'
            return ', '.join(str (e) for e in list)

        return "\n".join([
            "build_types: " + ", ".join(self.build_types),
            "platforms: " + none_for_all(self.platforms),
            "unittests: " + none_for_all(self.unittests),
            "jobs: " + none_for_all(self.jobs),
            "trigger_tests: " + str(self.trigger_tests),
            "interactive: " + str(self.interactive),