scripts: qemustart: change armvirt references to armsr
[openwrt/staging/stintel.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 file_path = Path(getenv("FILE_DIR")) / getenv("FILE_NAME")
15
16
17 if not file_path.is_file():
18 print("Skip JSON creation for non existing file", file_path)
19 exit(0)
20
21
22 def get_titles():
23 titles = []
24 for prefix in ["", "ALT0_", "ALT1_", "ALT2_", "ALT3_", "ALT4_"]:
25 title = {}
26 for var in ["vendor", "model", "variant"]:
27 if getenv("DEVICE_{}{}".format(prefix, var.upper())):
28 title[var] = getenv("DEVICE_{}{}".format(prefix, var.upper()))
29
30 if title:
31 titles.append(title)
32
33 if not titles:
34 titles.append({"title": getenv("DEVICE_TITLE")})
35
36 return titles
37
38
39 device_id = getenv("DEVICE_ID")
40 hash_file = hashlib.sha256(file_path.read_bytes()).hexdigest()
41
42 if file_path.with_suffix(file_path.suffix + ".sha256sum").exists():
43 hash_unsigned = (
44 file_path.with_suffix(file_path.suffix + ".sha256sum").read_text().strip()
45 )
46 else:
47 hash_unsigned = hash_file
48
49 file_info = {
50 "metadata_version": 1,
51 "target": "{}/{}".format(getenv("TARGET"), getenv("SUBTARGET")),
52 "version_code": getenv("VERSION_CODE"),
53 "version_number": getenv("VERSION_NUMBER"),
54 "source_date_epoch": int(getenv("SOURCE_DATE_EPOCH")),
55 "profiles": {
56 device_id: {
57 "image_prefix": getenv("DEVICE_IMG_PREFIX"),
58 "images": [
59 {
60 "type": getenv("FILE_TYPE"),
61 "name": getenv("FILE_NAME"),
62 "sha256": hash_file,
63 "sha256_unsigned": hash_unsigned,
64 }
65 ],
66 "device_packages": getenv("DEVICE_PACKAGES").split(),
67 "supported_devices": getenv("SUPPORTED_DEVICES").split(),
68 "titles": get_titles(),
69 }
70 },
71 }
72
73 if getenv("FILE_FILESYSTEM"):
74 file_info["profiles"][device_id]["images"][0]["filesystem"] = getenv(
75 "FILE_FILESYSTEM"
76 )
77
78 json_path.write_text(json.dumps(file_info, separators=(",", ":")))