|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2024, Nordic Semiconductor ASA |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +""" |
| 8 | +This script generates a module.yml file for the Zephyr project. The module.yml file contains |
| 9 | +information about the blobs. The script computes the SHA-256 hash for each blob and renders |
| 10 | +the Jinja2 template with the blob information. |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import hashlib |
| 15 | +import requests |
| 16 | +import logging |
| 17 | +from jinja2 import Environment, FileSystemLoader |
| 18 | +from typing import Dict, Any, List |
| 19 | +from collections import namedtuple |
| 20 | + |
| 21 | +WIFI_FW_BIN_NAME: str = "nrf70.bin" |
| 22 | + |
| 23 | +# Paths are relative to the sdk-nrfxlib repository |
| 24 | +BlobInfo = namedtuple( |
| 25 | + "BlobInfo", ["name", "description", "version", "rpath", "lpath", "docpath"] |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def get_wifi_blob_info(name: str) -> BlobInfo: |
| 30 | + return BlobInfo( |
| 31 | + name, |
| 32 | + f"nRF70 Wi-Fi firmware for {name} mode", |
| 33 | + "1.0.0", |
| 34 | + f"nrf_wifi/bin/zephyr/{name}/{WIFI_FW_BIN_NAME}", |
| 35 | + f"wifi_fw_bins/{name}/{WIFI_FW_BIN_NAME}", |
| 36 | + f"nrf_wifi/doc", |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +nordic_blobs: List[BlobInfo] = [ |
| 41 | + get_wifi_blob_info("default"), |
| 42 | + get_wifi_blob_info("scan_only"), |
| 43 | + get_wifi_blob_info("radio_test"), |
| 44 | + get_wifi_blob_info("system_with_raw"), |
| 45 | +] |
| 46 | + |
| 47 | +logger: logging.Logger = logging.getLogger(__name__) |
| 48 | +logging.basicConfig(level=logging.INFO) |
| 49 | + |
| 50 | + |
| 51 | +def compute_sha256(url: str) -> str: |
| 52 | + response = requests.get(url) |
| 53 | + response.raise_for_status() |
| 54 | + sha256_hash: str = hashlib.sha256(response.content).hexdigest() |
| 55 | + return sha256_hash |
| 56 | + |
| 57 | + |
| 58 | +def render_template(template_path: str, output_path: str, latest_sha: str) -> None: |
| 59 | + # Load the Jinja2 template |
| 60 | + env: Environment = Environment(loader=FileSystemLoader(".")) |
| 61 | + template = env.get_template(template_path) |
| 62 | + |
| 63 | + # list of dictionaries containing blob information |
| 64 | + blobs: Dict[str, Dict[str, Any]] = {} |
| 65 | + # Compute SHA-256 for each blob based on the URL |
| 66 | + for blob in nordic_blobs: |
| 67 | + logger.debug(f"Processing blob: {blob.name}") |
| 68 | + nrfxlib_url = f"https://github.com/nrfconnect/sdk-nrfxlib/raw/{latest_sha}" |
| 69 | + blob_info: Dict[str, Any] = {} |
| 70 | + blob_info["path"] = blob.lpath |
| 71 | + blob_info["rpath"] = blob.rpath |
| 72 | + blob_info["version"] = blob.version |
| 73 | + blob_info["url"] = f"{nrfxlib_url}/{blob.rpath}" |
| 74 | + blob_info["doc_url"] = f"{nrfxlib_url}/{blob.docpath}" |
| 75 | + blob_info["sha256"] = compute_sha256(blob_info["url"]) |
| 76 | + blob_info["description"] = blob.description |
| 77 | + blobs[blob] = blob_info |
| 78 | + |
| 79 | + logger.debug(blobs) |
| 80 | + # Render the template with the provided context |
| 81 | + rendered_content: str = template.render(blobs=blobs, latest_sha=latest_sha) |
| 82 | + |
| 83 | + # Write the rendered content to the output file |
| 84 | + with open(output_path, "w") as output_file: |
| 85 | + output_file.write(rendered_content) |
| 86 | + |
| 87 | + |
| 88 | +def main() -> None: |
| 89 | + parser: argparse.ArgumentParser = argparse.ArgumentParser( |
| 90 | + description="Generate a module.yml file for the Zephyr project." |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "-t", |
| 94 | + "--template", |
| 95 | + default="utils/module.yml.j2", |
| 96 | + help="Path to the Jinja2 template file.", |
| 97 | + ) |
| 98 | + parser.add_argument( |
| 99 | + "-o", |
| 100 | + "--output", |
| 101 | + default="zephyr/module.yml", |
| 102 | + help="Path to the output YAML file.", |
| 103 | + ) |
| 104 | + parser.add_argument( |
| 105 | + "-c", |
| 106 | + "--commit", |
| 107 | + required=True, |
| 108 | + help="The latest commit SHA for the nrfxlib repository.", |
| 109 | + ) |
| 110 | + parser.add_argument( |
| 111 | + "-d", "--debug", action="store_true", help="Enable debug logging." |
| 112 | + ) |
| 113 | + |
| 114 | + args: argparse.Namespace = parser.parse_args() |
| 115 | + |
| 116 | + if args.debug: |
| 117 | + logger.setLevel(logging.DEBUG) |
| 118 | + |
| 119 | + # Render the template |
| 120 | + render_template(args.template, args.output, args.commit) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments