1
0

json_add_image_info.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. from os import getenv
  3. from pathlib import Path
  4. from sys import argv
  5. import hashlib
  6. import json
  7. if len(argv) != 2:
  8. print("ERROR: JSON info script requires output arg")
  9. exit(1)
  10. json_path = Path(argv[1])
  11. file_path = Path(getenv("FILE_DIR")) / getenv("FILE_NAME")
  12. if not file_path.is_file():
  13. print("Skip JSON creation for non existing file", file_path)
  14. exit(0)
  15. def get_titles():
  16. titles = []
  17. for prefix in ["", "ALT0_", "ALT1_", "ALT2_", "ALT3_", "ALT4_"]:
  18. title = {}
  19. for var in ["vendor", "model", "variant"]:
  20. if getenv("DEVICE_{}{}".format(prefix, var.upper())):
  21. title[var] = getenv("DEVICE_{}{}".format(prefix, var.upper()))
  22. if title:
  23. titles.append(title)
  24. if not titles:
  25. titles.append({"title": getenv("DEVICE_TITLE")})
  26. return titles
  27. device_id = getenv("DEVICE_ID")
  28. sha256_hash = hashlib.sha256()
  29. with open(str(file_path),"rb") as f:
  30. # Read and update hash string value in blocks of 4K
  31. for byte_block in iter(lambda: f.read(4096),b""):
  32. sha256_hash.update(byte_block)
  33. hash_file = sha256_hash.hexdigest()
  34. if file_path.with_suffix(file_path.suffix + ".sha256sum").exists():
  35. hash_unsigned = (
  36. file_path.with_suffix(file_path.suffix + ".sha256sum").read_text().strip()
  37. )
  38. else:
  39. hash_unsigned = hash_file
  40. file_info = {
  41. "metadata_version": 1,
  42. "target": "{}/{}".format(getenv("TARGET"), getenv("SUBTARGET")),
  43. "version_code": getenv("VERSION_CODE"),
  44. "version_number": getenv("VERSION_NUMBER"),
  45. "source_date_epoch": int(getenv("SOURCE_DATE_EPOCH")),
  46. "profiles": {
  47. device_id: {
  48. "image_prefix": getenv("DEVICE_IMG_PREFIX"),
  49. "images": [
  50. {
  51. "type": getenv("FILE_TYPE"),
  52. "name": getenv("FILE_NAME"),
  53. "sha256": hash_file,
  54. "sha256_unsigned": hash_unsigned,
  55. }
  56. ],
  57. "device_packages": getenv("DEVICE_PACKAGES").split(),
  58. "supported_devices": getenv("SUPPORTED_DEVICES").split(),
  59. "titles": get_titles(),
  60. }
  61. },
  62. }
  63. if getenv("FILE_FILESYSTEM"):
  64. file_info["profiles"][device_id]["images"][0]["filesystem"] = getenv(
  65. "FILE_FILESYSTEM"
  66. )
  67. json_path.write_text(json.dumps(file_info, separators=(",", ":")))