|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Tool to create a build matrix for the current tree |
| 4 | +
|
| 5 | +SPDX-License-Identifier: MIT |
| 6 | +Copyright (C) 2025 Texas Instruments Incorporated - https://www.ti.com |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import logging |
| 11 | +import os |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +CONFIG_PATH = Path("configs/") |
| 15 | +GITHUB_PATH = Path(os.environ.get("GITHUB_OUTPUT", "test_output.txt")) |
| 16 | +REQUIRED_CONFIG_FRAGMENTS = {"config.txt", "tags.py", "toc.txt"} |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +def output_matrix(matrix): |
| 21 | + """Write the output of the matrix to the GitHub CI managed file |
| 22 | +
|
| 23 | + :param matrix: List of dictionaries of valid os and platform combinations |
| 24 | + """ |
| 25 | + matrix_string = json.dumps(matrix) |
| 26 | + logging.info("Writing matrix data to: %s", GITHUB_PATH) |
| 27 | + with GITHUB_PATH.open("a", encoding="utf-8") as file: |
| 28 | + file.write(f"matrix='{matrix_string}'\n") |
| 29 | + |
| 30 | + |
| 31 | +def unpack_os_set(platform, os_set): |
| 32 | + """Produce a list of dictionaries of valid platform to os mappings. |
| 33 | +
|
| 34 | + :param platform: String name of platform to use |
| 35 | + :param os_set: Set of string os names supported |
| 36 | + :return: List of dictionaries with os and platform mappings |
| 37 | + """ |
| 38 | + platform_matrix = [] |
| 39 | + for name in os_set: |
| 40 | + platform_matrix.append({"device": platform, "os": name}) |
| 41 | + return platform_matrix |
| 42 | + |
| 43 | + |
| 44 | +def valid_os_set(platform): |
| 45 | + """Get a set of valid OS options for a give platform path. This is a little more complex than it |
| 46 | + needs to be so that we can produce some more verbose warnings. |
| 47 | +
|
| 48 | + :param platform: Pathlib path to platform config directory |
| 49 | + :return: Set of valid OS options |
| 50 | + """ |
| 51 | + config_fragments = set() |
| 52 | + for fragment in REQUIRED_CONFIG_FRAGMENTS: |
| 53 | + config_fragments |= { |
| 54 | + x.name for x in platform.glob(f"{platform.stem}_*_{fragment}") |
| 55 | + } |
| 56 | + |
| 57 | + os_names = set() |
| 58 | + for name in config_fragments: |
| 59 | + os_names.add(name.split("_")[1]) |
| 60 | + |
| 61 | + valid_os = set() |
| 62 | + for name in os_names: |
| 63 | + invalid = False |
| 64 | + for fragment in REQUIRED_CONFIG_FRAGMENTS: |
| 65 | + required_file = f"{platform.stem}_{name}_{fragment}" |
| 66 | + if required_file not in config_fragments: |
| 67 | + invalid = True |
| 68 | + logging.warning("Missing the required file: %s", required_file) |
| 69 | + |
| 70 | + if invalid: |
| 71 | + logging.warning("Skipping the invalid OS: %s", name) |
| 72 | + else: |
| 73 | + valid_os.add(name) |
| 74 | + |
| 75 | + return valid_os |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + """Main processing loop""" |
| 80 | + logging.basicConfig(level=logging.INFO) |
| 81 | + |
| 82 | + matrix_list = [] |
| 83 | + for platform in CONFIG_PATH.glob("*/"): |
| 84 | + if not platform.is_dir(): |
| 85 | + continue |
| 86 | + logging.info("Parsing platform at: %s", platform) |
| 87 | + os_set = valid_os_set(platform) |
| 88 | + matrix_list.extend(unpack_os_set(platform.stem, os_set)) |
| 89 | + |
| 90 | + output_matrix(matrix_list) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments