build/json: add filesystem information
[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 titles = []
24 for prefix in ["", "ALT0_", "ALT1_", "ALT2_"]:
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 image_hash = hashlib.sha256(image_file.read_bytes()).hexdigest()
41
42 image_info = {
43 "metadata_version": 1,
44 "target": "{}/{}".format(getenv("TARGET"), getenv("SUBTARGET")),
45 "version_code": getenv("VERSION_CODE"),
46 "version_number": getenv("VERSION_NUMBER"),
47 "source_date_epoch": getenv("SOURCE_DATE_EPOCH"),
48 "profiles": {
49 device_id: {
50 "image_prefix": getenv("IMAGE_PREFIX"),
51 "images": [
52 {
53 "type": getenv("IMAGE_TYPE"),
54 "filesystem": getenv("IMAGE_FILESYSTEM"),
55 "name": getenv("IMAGE_NAME"),
56 "sha256": image_hash,
57 }
58 ],
59 "device_packages": getenv("DEVICE_PACKAGES").split(),
60 "supported_devices": getenv("SUPPORTED_DEVICES").split(),
61 "titles": get_titles(),
62 }
63 },
64 }
65
66 json_path.write_text(json.dumps(image_info, separators=(",", ":")))