build: store default/device packages in JSON
[openwrt/openwrt.git] / scripts / json_add_image_info.py
1 #!/usr/bin/env python3
2
3 from os import getenv
4 from pathlib import Path
5 from sys import argv
6 import hashlib
7 import json
8
9 if len(argv) != 2:
10 print("ERROR: JSON info script requires output arg")
11 exit(1)
12
13 json_path = Path(argv[1])
14 bin_dir = Path(getenv("BIN_DIR"))
15 image_file = bin_dir / getenv("IMAGE_NAME")
16
17 if not image_file.is_file():
18 print("Skip JSON creation for non existing image ", image_file)
19 exit(0)
20
21
22 def get_titles():
23 return [{"title": getenv("DEVICE_TITLE")}]
24
25
26 device_id = getenv("DEVICE_ID")
27 image_hash = hashlib.sha256(image_file.read_bytes()).hexdigest()
28
29 image_info = {
30 "metadata_version": 1,
31 "target": "{}/{}".format(getenv("TARGET"), getenv("SUBTARGET")),
32 "version_code": getenv("VERSION_CODE"),
33 "version_number": getenv("VERSION_NUMBER"),
34 "profiles": {
35 device_id: {
36 "image_prefix": getenv("IMAGE_PREFIX"),
37 "images": [
38 {
39 "type": getenv("IMAGE_TYPE"),
40 "name": getenv("IMAGE_NAME"),
41 "sha256": image_hash,
42 }
43 ],
44 "device_packages": getenv("DEVICE_PACKAGES").split(),
45 "supported_devices": getenv("SUPPORTED_DEVICES").split(),
46 "titles": get_titles(),
47 }
48 },
49 }
50
51 json_path.write_text(json.dumps(image_info, separators=(",", ":")))