Skip to content
This repository was archived by the owner on Mar 14, 2025. It is now read-only.

Commit abab701

Browse files
committed
Add ability to detect custom coordinates in release files
1 parent 341bbac commit abab701

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

komodo/release_transpiler.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import argparse
44
import os
55
import re
6-
from typing import Dict, Optional, Sequence, Union
6+
from typing import Dict, List, Optional, Sequence, Union
77

88
import yaml
99

@@ -212,11 +212,51 @@ def transpile_releases_for_pip(
212212
filehandler.write("\n".join(pip_packages))
213213

214214

215+
def detect_custom_coordinates(matrix_file: str) -> Dict[str, List[str]]:
216+
release_base = os.path.splitext(os.path.basename(matrix_file))[0]
217+
release_folder = os.path.dirname(matrix_file)
218+
release_matrix = load_yaml(f"{os.path.join(release_folder, release_base)}.yml")
219+
220+
def traverse_for_custom_coordinates(coords: Dict[str, List[str]]):
221+
def split_text_and_number(s):
222+
parts = re.findall(r"(\D+)(\d+)", s)
223+
return parts[0] if parts else (s, "")
224+
225+
detected_coordinates = {}
226+
227+
for val in coords.values():
228+
if isinstance(val, dict):
229+
for k, v in val.items():
230+
if not re.search(r"rhel", k) and not re.match(r"^py", k):
231+
package, version = split_text_and_number(k)
232+
233+
if (
234+
package in detected_coordinates
235+
and version not in detected_coordinates[package]
236+
):
237+
detected_coordinates[package].append(version)
238+
else:
239+
detected_coordinates = {package: [version]}
240+
241+
if isinstance(v, dict):
242+
detected_coordinates.update(traverse_for_custom_coordinates(v))
243+
244+
return detected_coordinates
245+
246+
return traverse_for_custom_coordinates(release_matrix)
247+
248+
215249
def transpile(args):
250+
if args.auto_custom_coordinates:
251+
args.matrix_coordinates.update(detect_custom_coordinates(args.matrix_file))
252+
216253
transpile_releases(args.matrix_file, args.output_folder, args.matrix_coordinates)
217254

218255

219256
def transpile_for_pip(args: Dict):
257+
if args.auto_custom_coordinates:
258+
args.matrix_coordinates.update(detect_custom_coordinates(args.matrix_file))
259+
220260
transpile_releases_for_pip(
221261
args.matrix_file,
222262
args.output_folder,
@@ -264,6 +304,13 @@ def main():
264304
required=False,
265305
default="{rhel: ['8'], py: ['3.11']}",
266306
)
307+
transpile_parser.add_argument(
308+
"--auto-custom-coordinates",
309+
help="Deduce custom coordinates from yaml input file",
310+
action="store_true",
311+
required=False,
312+
)
313+
267314
transpile_for_pip_parser = subparsers.add_parser(
268315
"transpile-for-pip",
269316
description="transpile a matrix file into separate pip requirement files.",
@@ -291,6 +338,12 @@ def main():
291338
required=False,
292339
default="{rhel: ['8'], py: ['3.11']}",
293340
)
341+
transpile_for_pip_parser.add_argument(
342+
"--auto-custom-coordinates",
343+
help="Deduce custom coordinates from yaml input file",
344+
action="store_true",
345+
required=False,
346+
)
294347
args = parser.parse_args()
295348
args.func(args)
296349

tests/test_release_transpiler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import yaml
66

77
from komodo.release_transpiler import (
8+
detect_custom_coordinates,
89
transpile_releases,
910
transpile_releases_for_pip,
1011
)
@@ -83,6 +84,15 @@ def test_transpile_custom_coordinate_releases(tmpdir, matrix, expectation):
8384
assert k not in content
8485

8586

87+
def test_automatic_custom_coordinate_detection():
88+
release_file = os.path.join(
89+
_get_test_root(), "input", "test_custom_coordinate_release.yml"
90+
)
91+
92+
coords = detect_custom_coordinates(release_file)
93+
assert coords == {"numpy": ["1", "2"]}
94+
95+
8696
@pytest.mark.parametrize(
8797
("matrix", "error_message_content"),
8898
[

0 commit comments

Comments
 (0)