build: refactor JSON info files to `profiles.json`
[openwrt/openwrt.git] / scripts / json_overview_image_info.py
1 #!/usr/bin/env python3
2
3 import json
4 from pathlib import Path
5 from os import getenv
6 from sys import argv
7
8 if len(argv) != 2:
9 print("JSON info files script requires ouput file as argument")
10 exit(1)
11
12 output_path = Path(argv[1])
13
14 assert getenv("WORK_DIR"), "$WORK_DIR required"
15
16 work_dir = Path(getenv("WORK_DIR"))
17
18 assert work_dir.is_dir(), "$WORK_DIR not a directory"
19
20 output = {}
21
22 for json_file in work_dir.glob("*.json"):
23 image_info = json.loads(json_file.read_text())
24 if not output:
25 output.update(image_info)
26 else:
27 # get first (and only) profile in json file
28 device_id = next(iter(image_info["profiles"].keys()))
29 if device_id not in output["profiles"]:
30 output["profiles"].update(image_info["profiles"])
31 else:
32 output["profiles"][device_id]["images"].append(
33 image_info["profiles"][device_id]["images"][0]
34 )
35
36 if output:
37 output_path.write_text(json.dumps(output, sort_keys=True, separators=(",", ":")))
38 else:
39 print("JSON info file script could not find any JSON files for target")