build: fix compatibility with python 3.6
[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 for json_file in work_dir.glob("*.json"):
22 image_info = json.loads(json_file.read_text())
23 if not output:
24 output.update(image_info)
25 else:
26 # get first (and only) profile in json file
27 device_id = next(iter(image_info["profiles"].keys()))
28 if device_id not in output["profiles"]:
29 output["profiles"].update(image_info["profiles"])
30 else:
31 output["profiles"][device_id]["images"].append(
32 image_info["profiles"][device_id]["images"][0]
33 )
34
35 if output:
36 default_packages, output["arch_packages"] = run(
37 [
38 "make",
39 "--no-print-directory",
40 "-C",
41 f"target/linux/{output['target'].split('/')[0]}",
42 "val.DEFAULT_PACKAGES",
43 "val.ARCH_PACKAGES",
44 ],
45 stdout=PIPE,
46 stderr=PIPE,
47 check=True,
48 env=environ.copy().update({"TOPDIR": Path().cwd()}),
49 universal_newlines=True,
50 ).stdout.splitlines()
51
52 output["default_packages"] = default_packages.split()
53 output_path.write_text(json.dumps(output, sort_keys=True, separators=(",", ":")))
54 else:
55 print("JSON info file script could not find any JSON files for target")