| 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 |
|
| 5 |
from typing import Any, Dict, Generator, Tuple |
| 6 |
|
| 7 |
from taskgraph.util.attributes import keymatch |
| 8 |
|
| 9 |
|
| 10 |
def iter_dot_path( |
| 11 |
container: Dict[str, Any], subfield: str |
| 12 |
) -> Generator[Tuple[Dict[str, Any], str], None, None]: |
| 13 |
"""Given a container and a subfield in dot path notation, yield the parent |
| 14 |
container of the dotpath's leaf node, along with the leaf node name that it |
| 15 |
contains. |
| 16 |
|
| 17 |
If the dot path contains a list object, each item in the list will be |
| 18 |
yielded. |
| 19 |
|
| 20 |
Args: |
| 21 |
container (dict): The container to search for the dot path. |
| 22 |
subfield (str): The dot path to search for. |
| 23 |
""" |
| 24 |
while "." in subfield: |
| 25 |
f, subfield = subfield.split(".", 1) |
| 26 |
|
| 27 |
if f.endswith("[]"): |
| 28 |
f = f[0:-2] |
| 29 |
if not isinstance(container.get(f), list): |
| 30 |
return |
| 31 |
|
| 32 |
for item in container[f]: |
| 33 |
yield from iter_dot_path(item, subfield) |
| 34 |
return |
| 35 |
|
| 36 |
if f not in container: |
| 37 |
return |
| 38 |
|
| 39 |
container = container[f] |
| 40 |
|
| 41 |
if isinstance(container, dict) and subfield in container: |
| 42 |
yield container, subfield |
| 43 |
|
| 44 |
|
| 45 |
def evaluate_keyed_by( |
| 46 |
value, item_name, attributes, defer=None, enforce_single_match=True |
| 47 |
): |
| 48 |
""" |
| 49 |
For values which can either accept a literal value, or be keyed by some |
| 50 |
attributes, perform that lookup and return the result. |
| 51 |
|
| 52 |
For example, given item:: |
| 53 |
|
| 54 |
by-test-platform: |
| 55 |
macosx-10.11/debug: 13 |
| 56 |
win.*: 6 |
| 57 |
default: 12 |
| 58 |
|
| 59 |
a call to `evaluate_keyed_by(item, 'thing-name', {'test-platform': 'linux96')` |
| 60 |
would return `12`. |
| 61 |
|
| 62 |
Items can be nested as deeply as desired:: |
| 63 |
|
| 64 |
by-test-platform: |
| 65 |
win.*: |
| 66 |
by-project: |
| 67 |
ash: .. |
| 68 |
cedar: .. |
| 69 |
linux: 13 |
| 70 |
default: 12 |
| 71 |
|
| 72 |
Args: |
| 73 |
value (str): Name of the value to perform evaluation on. |
| 74 |
item_name (str): Used to generate useful error messages. |
| 75 |
attributes (dict): Dictionary of attributes used to lookup 'by-<key>' with. |
| 76 |
defer (list): |
| 77 |
Allows evaluating a by-* entry at a later time. In the example |
| 78 |
above it's possible that the project attribute hasn't been set yet, |
| 79 |
in which case we'd want to stop before resolving that subkey and |
| 80 |
then call this function again later. This can be accomplished by |
| 81 |
setting `defer=["project"]` in this example. |
| 82 |
enforce_single_match (bool): |
| 83 |
If True (default), each task may only match a single arm of the |
| 84 |
evaluation. |
| 85 |
""" |
| 86 |
while True: |
| 87 |
if not isinstance(value, dict) or len(value) != 1: |
| 88 |
return value |
| 89 |
value_key = next(iter(value)) |
| 90 |
if not value_key.startswith("by-"): |
| 91 |
return value |
| 92 |
|
| 93 |
keyed_by = value_key[3:] # strip off 'by-' prefix |
| 94 |
|
| 95 |
if defer and keyed_by in defer: |
| 96 |
return value |
| 97 |
|
| 98 |
key = attributes.get(keyed_by) |
| 99 |
alternatives = next(iter(value.values())) |
| 100 |
|
| 101 |
if len(alternatives) == 1 and "default" in alternatives: |
| 102 |
# Error out when only 'default' is specified as only alternatives, |
| 103 |
# because we don't need to by-{keyed_by} there. |
| 104 |
raise Exception( |
| 105 |
f"Keyed-by '{keyed_by}' unnecessary with only value 'default' " |
| 106 |
f"found, when determining item {item_name}" |
| 107 |
) |
| 108 |
|
| 109 |
if key is None: |
| 110 |
if "default" in alternatives: |
| 111 |
value = alternatives["default"] |
| 112 |
continue |
| 113 |
else: |
| 114 |
raise Exception( |
| 115 |
f"No attribute {keyed_by} and no value for 'default' found " |
| 116 |
f"while determining item {item_name}" |
| 117 |
) |
| 118 |
|
| 119 |
matches = keymatch(alternatives, key) |
| 120 |
if enforce_single_match and len(matches) > 1: |
| 121 |
raise Exception( |
| 122 |
f"Multiple matching values for {keyed_by} {key!r} found while " |
| 123 |
f"determining item {item_name}" |
| 124 |
) |
| 125 |
elif matches: |
| 126 |
value = matches[0] |
| 127 |
continue |
| 128 |
|
| 129 |
raise Exception( |
| 130 |
f"No {keyed_by} matching {key!r} nor 'default' found while determining item {item_name}" |
| 131 |
) |