build: create JSON files containing image info
[openwrt/staging/chunkeey.git] / scripts / json_add_image_info.py
1 #!/usr/bin/env python3
2
3 import json
4 import os
5 import hashlib
6
7
8 def e(variable, default=None):
9 return os.environ.get(variable, default)
10
11
12 json_path = "{}{}{}.json".format(e("BIN_DIR"), os.sep, e("IMAGE_PREFIX"))
13
14 with open(os.path.join(e("BIN_DIR"), e("IMAGE_NAME")), "rb") as image_file:
15 image_hash = hashlib.sha256(image_file.read()).hexdigest()
16
17
18 def get_titles():
19 return [{"title": e("DEVICE_TITLE")}]
20
21
22 if not os.path.exists(json_path):
23 device_info = {
24 "id": e("DEVICE_ID"),
25 "image_prefix": e("IMAGE_PREFIX"),
26 "images": [],
27 "metadata_version": 1,
28 "supported_devices": e("SUPPORTED_DEVICES").split(),
29 "target": "{}/{}".format(e("TARGET"), e("SUBTARGET", "generic")),
30 "titles": get_titles(),
31 "version_commit": e("VERSION_CODE"),
32 "version_number": e("VERSION_NUMBER"),
33 }
34 else:
35 with open(json_path, "r") as json_file:
36 device_info = json.load(json_file)
37
38 image_info = {"type": e("IMAGE_TYPE"), "name": e("IMAGE_NAME"), "sha256": image_hash}
39 device_info["images"].append(image_info)
40
41 with open(json_path, "w") as json_file:
42 json.dump(device_info, json_file, sort_keys=True, indent=" ")