summaryrefslogtreecommitdiff
path: root/taskcluster/taskgraph/parameters.py
blob: 51b9e77c7edb11e4d1edce16957866d0e549b2d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-

# 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 json
import yaml
from mozbuild.util import ReadOnlyDict

# Please keep this list sorted and in sync with taskcluster/docs/parameters.rst
PARAMETER_NAMES = set([
    'base_repository',
    'build_date',
    'head_ref',
    'head_repository',
    'head_rev',
    'level',
    'message',
    'moz_build_date',
    'optimize_target_tasks',
    'owner',
    'project',
    'pushdate',
    'pushlog_id',
    'target_tasks_method',
    'triggered_by',
])


class Parameters(ReadOnlyDict):
    """An immutable dictionary with nicer KeyError messages on failure"""
    def check(self):
        names = set(self)
        msg = []

        missing = PARAMETER_NAMES - names
        if missing:
            msg.append("missing parameters: " + ", ".join(missing))

        extra = names - PARAMETER_NAMES
        if extra:
            msg.append("extra parameters: " + ", ".join(extra))

        if msg:
            raise Exception("; ".join(msg))

    def __getitem__(self, k):
        if k not in PARAMETER_NAMES:
            raise KeyError("no such parameter {!r}".format(k))
        try:
            return super(Parameters, self).__getitem__(k)
        except KeyError:
            raise KeyError("taskgraph parameter {!r} not found".format(k))


def load_parameters_file(options):
    """
    Load parameters from the --parameters option
    """
    filename = options['parameters']
    if not filename:
        return Parameters()
    with open(filename) as f:
        if filename.endswith('.yml'):
            return Parameters(**yaml.safe_load(f))
        elif filename.endswith('.json'):
            return Parameters(**json.load(f))
        else:
            raise TypeError("Parameters file `{}` is not JSON or YAML".format(filename))