b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | from os import getenv, environ |
| 4 | from pathlib import Path |
| 5 | from subprocess import run, PIPE |
| 6 | from sys import argv |
| 7 | import json |
| 8 | |
| 9 | if len(argv) != 2: |
| 10 | print("JSON info files script requires output file as argument") |
| 11 | exit(1) |
| 12 | |
| 13 | output_path = Path(argv[1]) |
| 14 | |
| 15 | assert getenv("WORK_DIR"), "$WORK_DIR required" |
| 16 | |
| 17 | work_dir = Path(getenv("WORK_DIR")) |
| 18 | |
| 19 | output = {} |
| 20 | |
| 21 | |
| 22 | def get_initial_output(image_info): |
| 23 | # preserve existing profiles.json |
| 24 | if output_path.is_file(): |
| 25 | profiles = json.loads(output_path.read_text()) |
| 26 | if profiles["version_code"] == image_info["version_code"]: |
| 27 | return profiles |
| 28 | return image_info |
| 29 | |
| 30 | |
| 31 | for json_file in work_dir.glob("*.json"): |
| 32 | image_info = json.loads(json_file.read_text()) |
| 33 | |
| 34 | if not output: |
| 35 | output = get_initial_output(image_info) |
| 36 | |
| 37 | # get first and only profile in json file |
| 38 | device_id, profile = next(iter(image_info["profiles"].items())) |
| 39 | if device_id not in output["profiles"]: |
| 40 | output["profiles"][device_id] = profile |
| 41 | else: |
| 42 | output["profiles"][device_id]["images"].extend(profile["images"]) |
| 43 | |
| 44 | # make image lists unique by name, keep last/latest |
| 45 | for device_id, profile in output.get("profiles", {}).items(): |
| 46 | profile["images"] = list({e["name"]: e for e in profile["images"]}.values()) |
| 47 | |
| 48 | |
| 49 | if output: |
| 50 | ( |
| 51 | default_packages, |
| 52 | output["arch_packages"], |
| 53 | linux_version, |
| 54 | linux_release, |
| 55 | linux_vermagic, |
| 56 | ) = run( |
| 57 | [ |
| 58 | "make", |
| 59 | "--no-print-directory", |
| 60 | "-C", |
| 61 | "target/linux/", |
| 62 | "val.DEFAULT_PACKAGES", |
| 63 | "val.ARCH_PACKAGES", |
| 64 | "val.LINUX_VERSION", |
| 65 | "val.LINUX_RELEASE", |
| 66 | "val.LINUX_VERMAGIC", |
| 67 | "V=s", |
| 68 | ], |
| 69 | stdout=PIPE, |
| 70 | check=True, |
| 71 | env=environ.copy().update({"TOPDIR": Path().cwd()}), |
| 72 | universal_newlines=True, |
| 73 | ).stdout.splitlines() |
| 74 | |
| 75 | output["default_packages"] = sorted(default_packages.split()) |
| 76 | output["linux_kernel"] = { |
| 77 | "version": linux_version, |
| 78 | "release": linux_release, |
| 79 | "vermagic": linux_vermagic, |
| 80 | } |
| 81 | output_path.write_text(json.dumps(output, sort_keys=True, separators=(",", ":"))) |
| 82 | else: |
| 83 | print("JSON info file script could not find any JSON files for target") |