1#  This Source Code Form is subject to the terms of the Mozilla Public
2#  License, v. 2.0. If a copy of the MPL was not distributed with this
3#  file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import, print_function, unicode_literals
6
7import logging
8import voluptuous
9from six import text_type
10
11from mozbuild import schedules
12
13logger = logging.getLogger(__name__)
14
15
16default_optimizations = (
17    # always run this task (default)
18    None,
19    # always optimize this task
20    {"always": None},
21    # optimize strategy aliases for build kind
22    {"build": list(schedules.ALL_COMPONENTS)},
23    # search the index for the given index namespaces, and replace this task if found
24    # the search occurs in order, with the first match winning
25    {"index-search": [text_type]},
26    # never optimize this task
27    {"never": None},
28    # skip the task except for every Nth push
29    {"skip-unless-expanded": None},
30    {"skip-unless-backstop": None},
31    # skip this task if none of the given file patterns match
32    {"skip-unless-changed": [text_type]},
33    # skip this task if unless the change files' SCHEDULES contains any of these components
34    {"skip-unless-schedules": list(schedules.ALL_COMPONENTS)},
35    # optimize strategy aliases for the test kind
36    {"test": list(schedules.ALL_COMPONENTS)},
37    {"test-inclusive": list(schedules.ALL_COMPONENTS)},
38    # optimize strategy alias for test-verify tasks
39    {"test-verify": list(schedules.ALL_COMPONENTS)},
40    # optimize strategy alias for upload-symbols tasks
41    {"upload-symbols": None},
42)
43
44OptimizationSchema = voluptuous.Any(*default_optimizations)
45
46
47def set_optimization_schema(schema_tuple):
48    """Sets OptimizationSchema so it can be imported by the task transform.
49    This function is called by projects that extend Firefox's taskgraph.
50    It should be called by the project's taskgraph:register function before
51    any transport or job runner code is imported.
52
53    :param tuple schema_tuple: Tuple of possible optimization strategies
54    """
55    global OptimizationSchema
56    if OptimizationSchema.validators == default_optimizations:
57        logger.info("OptimizationSchema updated.")
58        OptimizationSchema = voluptuous.Any(*schema_tuple)
59    else:
60        raise Exception("Can only call set_optimization_schema once.")
61