| # Copyright 2026 The Pigweed Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| # use this file except in compliance with the License. You may obtain a copy of |
| # the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations under |
| # the License. |
| |
| #!/usr/bin/env python3 |
| import argparse |
| import json |
| import os |
| import re |
| import sys |
| |
| |
| def parse_args(argv=None): |
| parser = argparse.ArgumentParser() |
| parser.add_argument( |
| "--apps-json", |
| required=True, |
| help="Path to input JSON containing discovered apps", |
| ) |
| parser.add_argument( |
| "--zephyr-root", |
| required=True, |
| help="Path to the Zephyr repository root", |
| ) |
| parser.add_argument( |
| "--output-json", |
| required=True, |
| help="Path to write the output JSON file", |
| ) |
| return parser.parse_args(argv) |
| |
| |
| def lightweight_yaml_parse_platform_allow(file_path): |
| """Internal lightweight fallback for parsing platform_allow from yaml.""" |
| platforms = [] |
| try: |
| with open(file_path, "r", encoding="utf-8") as f: |
| content = f.read() |
| |
| # Look for all platform_allow: blocks |
| for match in re.finditer( |
| r"platform_allow:\s*\n((?:\s*-\s*\S+\s*\n)+)", content |
| ): |
| list_content = match.group(1) |
| platforms.extend(re.findall(r"-\s*(\S+)", list_content)) |
| |
| except: |
| pass |
| return platforms |
| |
| |
| def main(argv=None): |
| args = parse_args(argv) |
| with open(args.apps_json, "r", encoding="utf-8") as f: |
| apps = json.load(f) |
| |
| results = {} |
| |
| for app_pkg, app_path in apps.items(): |
| metadata_file = None |
| |
| for name in ["testcase.yaml", "sample.yaml"]: |
| p = os.path.join(app_path, name) |
| if os.path.exists(p): |
| metadata_file = p |
| break |
| |
| if metadata_file: |
| # Parse it |
| platforms = lightweight_yaml_parse_platform_allow(metadata_file) |
| if platforms is not None: |
| supported_boards = platforms |
| results[app_pkg] = supported_boards |
| else: |
| # broad dynamic constraints scenario |
| results[app_pkg] = [] |
| else: |
| # No yaml metadata |
| results[app_pkg] = None |
| |
| # Dump final JSON mapping to output file. |
| with open(args.output_json, "w", encoding="utf-8") as f: |
| json.dump(results, f) |
| |
| |
| if __name__ == "__main__": |
| main() |