build: include size-limits to device-metadata
[openwrt/staging/blocktrron.git] / scripts / json_add_image_info.py
1 #!/usr/bin/env python3
2
3 from os import getenv, path
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 file_path = Path(getenv("FILE_DIR")) / getenv("FILE_NAME")
15
16 if not file_path.is_file():
17 print("Skip JSON creation for non existing file", file_path)
18 exit(0)
19
20
21 def get_titles():
22 titles = []
23 for prefix in ["", "ALT0_", "ALT1_", "ALT2_", "ALT3_", "ALT4_", "ALT5_"]:
24 title = {}
25 for var in ["vendor", "model", "variant"]:
26 if getenv("DEVICE_{}{}".format(prefix, var.upper())):
27 title[var] = getenv("DEVICE_{}{}".format(prefix, var.upper()))
28
29 if title:
30 titles.append(title)
31
32 if not titles:
33 titles.append({"title": getenv("DEVICE_TITLE")})
34
35 return titles
36
37
38 def get_numerical_size(image_size):
39 if image_size.endswith("g"):
40 return int(image_size[:-1]) * 1024 * 1024 * 1024
41 elif image_size.endswith("m"):
42 return int(image_size[:-1]) * 1024 * 1024
43 elif image_size.endswith("k"):
44 return int(image_size[:-1]) * 1024
45 else:
46 return int(image_size)
47
48
49 device_id = getenv("DEVICE_ID")
50
51 sha256_hash = hashlib.sha256()
52 with open(str(file_path),"rb") as f:
53 # Read and update hash string value in blocks of 4K
54 for byte_block in iter(lambda: f.read(4096),b""):
55 sha256_hash.update(byte_block)
56
57 hash_file = sha256_hash.hexdigest()
58
59 if file_path.with_suffix(file_path.suffix + ".sha256sum").exists():
60 hash_unsigned = (
61 file_path.with_suffix(file_path.suffix + ".sha256sum").read_text().strip()
62 )
63 else:
64 hash_unsigned = hash_file
65
66 file_size = path.getsize(file_path)
67
68 file_info = {
69 "metadata_version": 1,
70 "target": "{}/{}".format(getenv("TARGET"), getenv("SUBTARGET")),
71 "version_code": getenv("VERSION_CODE"),
72 "version_number": getenv("VERSION_NUMBER"),
73 "source_date_epoch": int(getenv("SOURCE_DATE_EPOCH")),
74 "profiles": {
75 device_id: {
76 "image_prefix": getenv("DEVICE_IMG_PREFIX"),
77 "images": [
78 {
79 "type": getenv("FILE_TYPE"),
80 "name": getenv("FILE_NAME"),
81 "sha256": hash_file,
82 "sha256_unsigned": hash_unsigned,
83 "size": file_size,
84 }
85 ],
86 "device_packages": getenv("DEVICE_PACKAGES").split(),
87 "supported_devices": getenv("SUPPORTED_DEVICES").split(),
88 "titles": get_titles(),
89 }
90 },
91 }
92
93 if getenv("IMAGE_SIZE") or getenv("KERNEL_SIZE"):
94 file_info["profiles"][device_id]["limits"] = {}
95 if getenv("IMAGE_SIZE"):
96 file_info["profiles"][device_id]["limits"]["image_size"] = get_numerical_size(
97 getenv("IMAGE_SIZE")
98 )
99 if getenv("KERNEL_SIZE"):
100 file_info["profiles"][device_id]["limits"]["kernel_size"] = get_numerical_size(
101 getenv("KERNEL_SIZE")
102 )
103
104 if getenv("FILE_FILESYSTEM"):
105 file_info["profiles"][device_id]["images"][0]["filesystem"] = getenv(
106 "FILE_FILESYSTEM"
107 )
108
109 json_path.write_text(json.dumps(file_info, separators=(",", ":")))