-
Notifications
You must be signed in to change notification settings - Fork 3
/
entrypoint.py
executable file
·212 lines (181 loc) · 7.52 KB
/
entrypoint.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
#!env python3
"""Action body."""
import json
import os
import re
from pathlib import Path
from typing import Any
from actions_toolkit import core
KNOWN_PYTHONS = ("3.7", "3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14-dev")
PYTHON_REDIRECTS = {
"3.14": "3.14-dev", # Remove once GHA allows 3.14 as a valid version
}
PLATFORM_MAP = {
"linux": "ubuntu-24.04",
"macos": "macos-13",
"windows": "windows-latest",
}
IMPLICIT_PLATFORM = "linux"
IMPLICIT_MIN_PYTHON = "3.8"
IMPLICIT_MAX_PYTHON = "3.12"
IMPLICIT_DEFAULT_PYTHON = "3.9"
IMPLICIT_SKIP_EXPLODE = "0"
def sort_human(data: list[str]) -> list[str]:
"""Sort a list using human logic, so 'py39' comes before 'py311'."""
def convert(text: str) -> str | float:
return float(text) if text.isdigit() else text
def alphanumeric(key: str) -> list[str | float]:
return [convert(c) for c in re.split(r"([-+]?\d*\\.?\d*)", key)]
data.sort(key=alphanumeric)
return data
def add_job(result: dict[str, dict[str, str]], name: str, data: dict[str, str]) -> None:
"""Adds a new job to the list of generated jobs."""
if name in result:
core.set_failed(
f"Action failed as it tried add an already a job with duplicate name {name}: {result[name]} already present while trying to add {data}",
)
result[name] = data
def get_platforms() -> list[str]:
"""Retrieve effective list of platforms."""
platforms = []
for v in core.get_input("platforms", required=False).split(","):
platform, run_on = v.split(":") if ":" in v else (v, None)
if not platform:
continue
if run_on:
core.debug(
f"Add platform '{platform}' with run_on={run_on} to known platforms",
)
PLATFORM_MAP[platform] = run_on
platforms.append(platform)
return platforms
def produce_output(output: dict[str, Any]) -> None:
"""Produce the output."""
if "TEST_GITHUB_OUTPUT_JSON" in os.environ:
with Path(os.environ["TEST_GITHUB_OUTPUT_JSON"]).open(
"w",
encoding="utf-8",
) as f:
json.dump(output, f)
for key, value in output.items():
core.set_output(key, value)
# loop list staring with given item
# pylint: disable=too-many-locals,too-many-branches,too-many-statements
def main() -> None: # noqa: C901,PLR0912,PLR0915
"""Main."""
# print all env vars starting with INPUT_
for k, v in os.environ.items():
if k.startswith("INPUT_"):
core.info(f"Env var {k}={v}")
try:
# ignore empty lines
other_names = [
x for x in core.get_input("other_names", required=False).split("\n") if x
]
platforms = get_platforms()
core.info(f"Effective platforms: {platforms}")
core.info(f"Platform map: {PLATFORM_MAP}")
min_python = core.get_input("min_python") or IMPLICIT_MIN_PYTHON
max_python = core.get_input("max_python") or IMPLICIT_MAX_PYTHON
default_python = core.get_input("default_python") or IMPLICIT_DEFAULT_PYTHON
skip_explode = int(core.get_input("skip_explode") or IMPLICIT_SKIP_EXPLODE)
strategies = {}
for platform in PLATFORM_MAP:
strategies[platform] = core.get_input(platform, required=False)
core.debug(f"Testing strategy: {strategies}")
result: dict[str, dict[str, str]] = {}
if max_python == "3.14":
python_names = KNOWN_PYTHONS[KNOWN_PYTHONS.index(min_python) :]
else:
python_names = KNOWN_PYTHONS[
KNOWN_PYTHONS.index(min_python) : (KNOWN_PYTHONS.index(max_python) + 1)
]
python_flavours = len(python_names)
def sort_key(s: str) -> tuple[int, str]:
"""Sorts longer strings first."""
return -len(s), s
# we put longer names first in order to pick the most specific platforms
platform_names_sorted = sorted(PLATFORM_MAP.keys(), key=sort_key)
core.info(f"Known platforms sorted: {platform_names_sorted}")
for line in other_names:
# line can look like:
# - name
# - name:command1;command2
# - name:command:runner=ubuntu-20.04
segments = line.split(":")
name = segments[0]
commands = [f"tox -e {name}"] # implicit commands if not provided
args = {}
if len(segments) > 1 and segments[1]:
commands = segments[1].split(";")
if len(segments) > 2: # noqa: PLR2004
# we have arguments foo=bar;baz=qux
try:
args = dict(x.split("=") for x in segments[2].split(";"))
except ValueError:
core.set_failed(
f"Action failed due to optional args not having the expected format 'a=b;c=d', value being '{segments[2]}'",
)
env_python = default_python
# Check for using correct python version for other_names like py310-devel.
pythons: list[str] = []
for py_version in re.findall(r"py(\d+)", line):
env_python = f"{py_version[0]}.{py_version[1:]}"
pythons.append(PYTHON_REDIRECTS.get(env_python, env_python))
if not pythons:
pythons.append(default_python)
if "runner" not in args:
for platform_name in platform_names_sorted:
if platform_name in name:
break
else:
platform_name = "linux" # implicit platform (os) to use
args["runner"] = PLATFORM_MAP[platform_name]
data = {
"name": name,
"command": commands[0],
"python_version": "\n".join(pythons),
"os": args["runner"],
}
for index, command in enumerate(commands[1:]):
data[f"command{index + 2}"] = command
add_job(
result,
name,
data,
)
if not skip_explode:
for platform in platforms:
for i, python in enumerate(python_names):
py_name = re.sub(r"\D", "", python.strip("."))
suffix = "" if platform == IMPLICIT_PLATFORM else f"-{platform}"
if strategies[platform] == "minmax" and (
i not in (0, python_flavours - 1)
):
continue
add_job(
result,
f"py{py_name}{suffix}",
{
"python_version": python,
"os": PLATFORM_MAP.get(platform, platform),
"command": f"tox -e py{py_name}",
},
)
core.info(f"Generated {len(result)} matrix entries.")
names = sort_human(list(result.keys()))
core.info(f"Job names: {', '.join(names)}")
matrix_include = []
matrix_include = [
dict(sorted(dict(result[name], name=name).items())) for name in names
]
core.info(
f"Matrix jobs ordered by their name: {json.dumps(matrix_include, indent=2)}",
)
output = {"matrix": {"include": matrix_include}}
produce_output(output)
# pylint: disable=broad-exception-caught
except Exception as exc: # noqa: BLE001
core.set_failed(f"Action failed due to {exc}")
if __name__ == "__main__":
main()