-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
240 lines (190 loc) · 7.58 KB
/
matrix.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# Copyright (c) 2025 Marcin Zdun
# This code is licensed under MIT license (see LICENSE for details)
"""
The **proj_flow.base.matrix** operates on lists of
:class:`run configurations <proj_flow.api.env.Config>`. It provides means to
build cartesian products for multiple sets, to load the GitHub Actions Job
strategy matrix from a set of YAML files, or to put additional filters on the
resulting set of configurations.
"""
import os
import sys
from typing import Dict, Iterable, List, Tuple, TypeVar
import yaml
T = TypeVar("T")
def find_compiler(
compiler: str, config_names: Dict[str, List[str]]
) -> Tuple[str, List[str]]:
"""
Locates the C and C++ names of compilers corresponding to the current
compiler.
:param compiler: Name ofthe compiuler to map, taken from the ``"compiler"``
entry in run :class:`config<proj_flow.api.env.Config>`.
:param config_names: Dictionary of mapping from compiler name to C/C++
names, taken from ``"compiler.names"`` entry in flow config file.
"""
dirname = os.path.dirname(compiler)
filename = os.path.basename(compiler)
if sys.platform == "win32":
filename = os.path.splitext(filename)[0]
chunks = filename.split("-", 1)
if len(chunks) == 1:
version = None
else:
version = chunks[1]
filename = chunks[0].lower()
try:
compiler_names = config_names[filename]
except:
compiler_names = [filename]
compilers = [
os.path.join(dirname, name if version is None else f"{name}-{version}")
for name in compiler_names
]
if filename == "stdclang":
filename = "clang"
return filename, compilers
def flatten(array: Iterable[List[T]]) -> List[T]:
"""
Turns list of lists into a list.
:param array: List of lists to flatten
:returns: a list with all items expanded
"""
return [item for sublist in array for item in sublist]
def matches(tested: dict, test: dict) -> bool:
"""
Checks, if the tested dictionary contains all the values from test
dictionary.
:param tested: Dictionary to check
:param test: Dictionary to check against
:returns: `True`, if all keys from `test` are in `tested` and have the same
values, `False` otherwise.
"""
for key, value in test.items():
val = tested.get(key)
if val != value:
return False
return True
def matches_any(tested: dict, tests: List[dict]):
"""
Checks, if the tested dictionary contains all the values from at least one
of test dictionaries.
:param tested: Dictionary to check
:param tests: List of dictionaries to check against
:returns: `True`, if at least one test :func:`matches` `tested`
dictionary, `False` otherwise.
"""
for test in tests:
if matches(tested, test):
return True
return False
def cartesian(input: Dict[str, list]) -> List[dict]:
"""
Calculates the cartesian product of all axes in `input`.
The input dictionary is source of all axes this product is calculated from.
The key to the dictionary names the set and value under each key represents
the set to be multiplied.
The output list contains each possible dictionary, where key set is
identical to the input key set and each value is one of the values in the
input axis.
:param input: A dictionary with each axis and its values
:returns: A list of all possible permutation of axes values; order in which
each permutation appears in resulting list is not guaranteed.
Example:
--------
.. code-block:: python
:caption: Input
{
"key-1": ["value-1", "value-2"],
"key-2": [True, False],
"key-3": [1, 2, 3],
}
.. code-block:: python
:caption: Possible output
[
{ "key-1": "value-1", "key-2": [True], "key-3": 1 },
{ "key-1": "value-1", "key-2": [True], "key-3": 2 },
{ "key-1": "value-1", "key-2": [True], "key-3": 3 },
{ "key-1": "value-1", "key-2": [False], "key-3": 1 },
{ "key-1": "value-1", "key-2": [False], "key-3": 2 },
{ "key-1": "value-1", "key-2": [False], "key-3": 3 },
{ "key-1": "value-2", "key-2": [True], "key-3": 1 },
{ "key-1": "value-2", "key-2": [True], "key-3": 2 },
{ "key-1": "value-2", "key-2": [True], "key-3": 3 },
{ "key-1": "value-2", "key-2": [False], "key-3": 1 },
{ "key-1": "value-2", "key-2": [False], "key-3": 2 },
{ "key-1": "value-2", "key-2": [False], "key-3": 3 },
]
"""
product = [{}]
for key, values in input.items():
next_level = []
for value in values:
for obj in product:
next_level.append({**obj, key: value})
product = next_level
return product
def _split_keys(includes: List[dict], keys: List[str]) -> List[Tuple[dict, dict]]:
result = []
for include in includes:
expand_key = {}
expand_value = {}
for key, value in include.items():
if key in keys:
expand_key[key] = value
else:
expand_value[key] = value
result.append((expand_key, expand_value))
return result
def load_matrix(*matrix_paths: str) -> Tuple[List[dict], List[str]]:
"""
Loads config definitions from files under `matrix_paths` to produce
cartesian product of the matrix, expanded and/or contracted according to
rules used by GitHub Action workflow `strategies`_. Please note when
comparing to GitHub documentation:
- only scalar values are supported in the ``matrix`` dictionary.
- only ``matrix``, ``include`` and ``exclude`` keys are allowed.
:param matrix_paths: The list of filenames pointing to YAML files with
``matrix``, ``include`` and ``exclude`` entries.
:returns: A tuple of cartesian product for the config matrix, with
inclusions and exclusions as described in GitHub docs and a list of key
names in the ``matrix`` dictionary.
.. _strategies: https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#jobsjob_idstrategy
"""
setups: List[dict] = []
for matrix_path in matrix_paths:
try:
with open(matrix_path, encoding="UTF-8") as f:
setups.append(yaml.load(f, Loader=yaml.Loader))
except FileNotFoundError:
pass
if len(setups) == 0:
return [], []
setup = setups[0]
for additional in setups[1:]:
src_matrix = setup.get("matrix", {})
src_exclude = setup.get("exclude", [])
src_include = setup.get("include", [])
for key, value in additional.get("matrix", {}).items():
old = src_matrix.get(key)
if isinstance(old, list) and isinstance(value, list):
old.extend(value)
elif isinstance(old, list):
old.append(value)
else:
src_matrix[key] = value
src_exclude.extend(additional.get("exclude", []))
src_include.extend(additional.get("include", []))
raw = setup.get("matrix", {})
keys = list(raw.keys())
full = cartesian(raw)
includes = _split_keys(setup.get("include", []), keys)
for obj in full:
for include_key, include_value in includes:
if not matches(obj, include_key):
continue
for key, value in include_value.items():
obj[key] = value
excludes = setup.get("exclude", [])
matrix = [obj for obj in full if not matches_any(obj, excludes)]
return matrix, keys