build: preserve profiles.json between builds
[openwrt/openwrt.git] / scripts / json_overview_image_info.py
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 ouput 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["profiles"].items():
46 profile["images"] = list({e["name"]: e for e in profile["images"]}.values())
47
48
49 if output:
50 default_packages, output["arch_packages"] = run(
51 [
52 "make",
53 "--no-print-directory",
54 "-C",
55 "target/linux/",
56 "val.DEFAULT_PACKAGES",
57 "val.ARCH_PACKAGES",
58 ],
59 stdout=PIPE,
60 stderr=PIPE,
61 check=True,
62 env=environ.copy().update({"TOPDIR": Path().cwd()}),
63 universal_newlines=True,
64 ).stdout.splitlines()
65
66 output["default_packages"] = sorted(default_packages.split())
67
68 output_path.write_text(json.dumps(output, sort_keys=True, separators=(",", ":")))
69 else:
70 print("JSON info file script could not find any JSON files for target")