|
3 | 3 | import argparse |
4 | 4 | import os |
5 | 5 | import re |
6 | | -from typing import Dict, Optional, Sequence, Union |
| 6 | +from typing import Dict, List, Optional, Sequence, Union |
7 | 7 |
|
8 | 8 | import yaml |
9 | 9 |
|
@@ -212,11 +212,51 @@ def transpile_releases_for_pip( |
212 | 212 | filehandler.write("\n".join(pip_packages)) |
213 | 213 |
|
214 | 214 |
|
| 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 | + |
215 | 249 | def transpile(args): |
| 250 | + if args.auto_custom_coordinates: |
| 251 | + args.matrix_coordinates.update(detect_custom_coordinates(args.matrix_file)) |
| 252 | + |
216 | 253 | transpile_releases(args.matrix_file, args.output_folder, args.matrix_coordinates) |
217 | 254 |
|
218 | 255 |
|
219 | 256 | def transpile_for_pip(args: Dict): |
| 257 | + if args.auto_custom_coordinates: |
| 258 | + args.matrix_coordinates.update(detect_custom_coordinates(args.matrix_file)) |
| 259 | + |
220 | 260 | transpile_releases_for_pip( |
221 | 261 | args.matrix_file, |
222 | 262 | args.output_folder, |
@@ -264,6 +304,13 @@ def main(): |
264 | 304 | required=False, |
265 | 305 | default="{rhel: ['8'], py: ['3.11']}", |
266 | 306 | ) |
| 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 | + |
267 | 314 | transpile_for_pip_parser = subparsers.add_parser( |
268 | 315 | "transpile-for-pip", |
269 | 316 | description="transpile a matrix file into separate pip requirement files.", |
@@ -291,6 +338,12 @@ def main(): |
291 | 338 | required=False, |
292 | 339 | default="{rhel: ['8'], py: ['3.11']}", |
293 | 340 | ) |
| 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 | + ) |
294 | 347 | args = parser.parse_args() |
295 | 348 | args.func(args) |
296 | 349 |
|
|
0 commit comments