3 from os
import getenv
, environ
4 from pathlib
import Path
5 from subprocess
import run
, PIPE
10 print("JSON info files script requires output file as argument")
13 output_path
= Path(argv
[1])
15 assert getenv("WORK_DIR"), "$WORK_DIR required"
17 work_dir
= Path(getenv("WORK_DIR"))
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"]:
31 for json_file
in work_dir
.glob("*.json"):
32 image_info
= json
.loads(json_file
.read_text())
35 output
= get_initial_output(image_info
)
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
42 output
["profiles"][device_id
]["images"].extend(profile
["images"])
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())
50 default_packages
, output
["arch_packages"] = run(
53 "--no-print-directory",
56 "val.DEFAULT_PACKAGES",
62 env
=environ
.copy().update({"TOPDIR": Path().cwd()}),
63 universal_newlines
=True,
66 output
["default_packages"] = sorted(default_packages
.split())
68 output_path
.write_text(json
.dumps(output
, sort_keys
=True, separators
=(",", ":")))
70 print("JSON info file script could not find any JSON files for target")