-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
ci-matrix.py
216 lines (170 loc) · 5.75 KB
/
ci-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
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "packaging",
# "pyyaml",
# ]
# ///
import argparse
import json
from typing import Any, Optional
import yaml
from packaging.version import Version
CI_TARGETS_YAML = "ci-targets.yaml"
CI_EXTRA_SKIP_LABELS = ["documentation"]
def meets_conditional_version(version: str, min_version: str) -> bool:
return Version(version) >= Version(min_version)
def parse_labels(labels: Optional[str]) -> dict[str, set[str]]:
"""Parse labels into a dict of category filters."""
if not labels:
return {}
result: dict[str, set[str]] = {
"platform": set(),
"python": set(),
"build": set(),
"arch": set(),
"libc": set(),
"directives": set(),
}
for label in labels.split(","):
label = label.strip()
# Handle special labels
if label in CI_EXTRA_SKIP_LABELS:
result["directives"].add("skip")
continue
if not label or ":" not in label:
continue
category, value = label.split(":", 1)
if category == "ci":
category = "directives"
if category in result:
result[category].add(value)
return result
def should_include_entry(entry: dict[str, str], filters: dict[str, set[str]]) -> bool:
"""Check if an entry satisfies the label filters."""
if filters.get("directives") and "skip" in filters["directives"]:
return False
if filters.get("platform") and entry["platform"] not in filters["platform"]:
return False
if filters.get("python") and entry["python"] not in filters["python"]:
return False
if filters.get("arch") and entry["arch"] not in filters["arch"]:
return False
if (
filters.get("libc")
and entry.get("libc")
and entry["libc"] not in filters["libc"]
):
return False
if filters.get("build"):
build_options = set(entry.get("build_options", "").split("+"))
if not all(f in build_options for f in filters["build"]):
return False
return True
def generate_matrix_entries(
config: dict[str, Any],
platform_filter: Optional[str] = None,
label_filters: Optional[dict[str, set[str]]] = None,
) -> list[dict[str, str]]:
matrix_entries = []
for platform, platform_config in config.items():
if platform_filter and platform != platform_filter:
continue
for target_triple, target_config in platform_config.items():
add_matrix_entries_for_config(
matrix_entries,
target_triple,
target_config,
platform,
label_filters.get("directives", set()),
)
# Apply label filters if present
if label_filters:
matrix_entries = [
entry
for entry in matrix_entries
if should_include_entry(entry, label_filters)
]
return matrix_entries
def add_matrix_entries_for_config(
matrix_entries: list[dict[str, str]],
target_triple: str,
config: dict[str, Any],
platform: str,
directives: set[str],
) -> None:
python_versions = config["python_versions"]
build_options = config["build_options"]
arch = config["arch"]
# Create base entry that will be used for all variants
base_entry = {
"arch": arch,
"target_triple": target_triple,
"platform": platform,
}
# Add optional fields if they exist
if "arch_variant" in config:
base_entry["arch_variant"] = config["arch_variant"]
if "libc" in config:
base_entry["libc"] = config["libc"]
if "vcvars" in config:
base_entry["vcvars"] = config["vcvars"]
if "run" in config:
base_entry["run"] = str(config["run"]).lower()
if "dry-run" in directives:
base_entry["dry-run"] = "true"
# Process regular build options
for python_version in python_versions:
for build_option in build_options:
entry = base_entry.copy()
entry.update(
{
"python": python_version,
"build_options": build_option,
}
)
matrix_entries.append(entry)
# Process conditional build options (e.g., freethreaded)
for conditional in config.get("build_options_conditional", []):
min_version = conditional["minimum-python-version"]
for python_version in python_versions:
if not meets_conditional_version(python_version, min_version):
continue
for build_option in conditional["options"]:
entry = base_entry.copy()
entry.update(
{
"python": python_version,
"build_options": build_option,
}
)
matrix_entries.append(entry)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Generate a JSON matrix for building distributions in CI"
)
parser.add_argument(
"--platform",
choices=["darwin", "linux", "windows"],
help="Filter matrix entries by platform",
)
parser.add_argument(
"--labels",
help="Comma-separated list of labels to filter by (e.g., 'platform:darwin,python:3.13,build:debug'), all must match.",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
labels = parse_labels(args.labels)
with open(CI_TARGETS_YAML, "r") as f:
config = yaml.safe_load(f)
matrix = {
"include": generate_matrix_entries(
config,
args.platform,
labels,
)
}
print(json.dumps(matrix))
if __name__ == "__main__":
main()