| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 |
# you may not use this file except in compliance with the License. |
| 3 |
# You may obtain a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
import itertools |
| 14 |
from collections import namedtuple |
| 15 |
|
| 16 |
from .errors import Context, JenkinsJobsException |
| 17 |
from .loc_loader import LocList, LocDict |
| 18 |
from jenkins_jobs.expander import YamlObjectsExpander |
| 19 |
|
| 20 |
|
| 21 |
Dimension = namedtuple("Dimension", "axis params") |
| 22 |
|
| 23 |
|
| 24 |
def _decode_axis_value(axis, value, key_pos, value_pos): |
| 25 |
if not isinstance(value, (list, LocList)): |
| 26 |
yield {axis: value} |
| 27 |
return |
| 28 |
for idx, item in enumerate(value): |
| 29 |
if not isinstance(item, (dict, LocDict)): |
| 30 |
d = LocDict() |
| 31 |
if type(value) is LocList: |
| 32 |
d.set_item(axis, item, key_pos, value.value_pos[idx]) |
| 33 |
else: |
| 34 |
d[axis] = item |
| 35 |
yield d |
| 36 |
continue |
| 37 |
if len(item.items()) != 1: |
| 38 |
raise JenkinsJobsException( |
| 39 |
f"Expected a value or a dict with single element, but got: {item!r}", |
| 40 |
pos=value.value_pos[idx], |
| 41 |
ctx=[Context(f"In parameter {axis!r} definition", key_pos)], |
| 42 |
) |
| 43 |
value, p = next(iter(item.items())) |
| 44 |
yield LocDict.merge( |
| 45 |
{axis: value}, # Point axis value. |
| 46 |
p, # Point-specific parameters. May override axis value. |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
def enum_dimensions_params(axes, params, defaults): |
| 51 |
expander = YamlObjectsExpander() |
| 52 |
if not axes: |
| 53 |
# No axes - instantiate one job/view. |
| 54 |
yield {} |
| 55 |
return |
| 56 |
dim_values = [] |
| 57 |
for axis in axes: |
| 58 |
try: |
| 59 |
value, key_pos, value_pos = params.item_with_pos(axis) |
| 60 |
except KeyError: |
| 61 |
try: |
| 62 |
value = defaults[axis] |
| 63 |
key_pos = value_pos = None |
| 64 |
except KeyError: |
| 65 |
continue # May be, value would be received from an another axis values. |
| 66 |
expanded_value = expander.expand(value, params, key_pos, value_pos) |
| 67 |
value = [ |
| 68 |
Dimension(axis, params) |
| 69 |
for params in _decode_axis_value(axis, expanded_value, key_pos, value_pos) |
| 70 |
] |
| 71 |
dim_values.append(value) |
| 72 |
for dimensions in itertools.product(*dim_values): |
| 73 |
overrides = {} # Axis -> overridden param. |
| 74 |
for dim in dimensions: |
| 75 |
for name, value in dim.params.items(): |
| 76 |
if name != dim.axis: |
| 77 |
overrides[name] = value |
| 78 |
param_dicts = [d.params for d in dimensions] |
| 79 |
# Parameter overrides should take precedence over axis values. |
| 80 |
yield LocDict.merge(*param_dicts, overrides) |
| 81 |
|
| 82 |
|
| 83 |
def _match_exclude(params, exclude, pos): |
| 84 |
if not isinstance(exclude, dict): |
| 85 |
raise JenkinsJobsException( |
| 86 |
f"Expected a dict, but got: {exclude!r}", |
| 87 |
pos=pos, |
| 88 |
) |
| 89 |
if not exclude: |
| 90 |
raise JenkinsJobsException( |
| 91 |
f"Expected a dict, but is empty: {exclude!r}", |
| 92 |
pos=pos, |
| 93 |
) |
| 94 |
for axis, value in exclude.items(): |
| 95 |
try: |
| 96 |
v = params[axis] |
| 97 |
except KeyError: |
| 98 |
raise JenkinsJobsException( |
| 99 |
f"Unknown axis {axis!r}", |
| 100 |
pos=pos, |
| 101 |
) |
| 102 |
if value != v: |
| 103 |
return False |
| 104 |
# All required exclude values are matched. |
| 105 |
return True |
| 106 |
|
| 107 |
|
| 108 |
def is_point_included(exclude_list, params, key_pos=None): |
| 109 |
if not exclude_list: |
| 110 |
return True |
| 111 |
try: |
| 112 |
for idx, exclude in enumerate(exclude_list): |
| 113 |
if _match_exclude(params, exclude, exclude_list.value_pos[idx]): |
| 114 |
return False |
| 115 |
except JenkinsJobsException as x: |
| 116 |
raise x.with_context( |
| 117 |
"In template exclude list", |
| 118 |
pos=key_pos, |
| 119 |
) |
| 120 |
return True |