json_add_image_info.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. from os import getenv, path
  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_", "ALT5_"]:
  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. def get_numerical_size(image_size):
  28. if image_size.endswith("g"):
  29. return int(image_size[:-1]) * 1024 * 1024 * 1024
  30. elif image_size.endswith("m"):
  31. return int(image_size[:-1]) * 1024 * 1024
  32. elif image_size.endswith("k"):
  33. return int(image_size[:-1]) * 1024
  34. else:
  35. return int(image_size)
  36. device_id = getenv("DEVICE_ID")
  37. sha256_hash = hashlib.sha256()
  38. with open(str(file_path),"rb") as f:
  39. # Read and update hash string value in blocks of 4K
  40. for byte_block in iter(lambda: f.read(4096),b""):
  41. sha256_hash.update(byte_block)
  42. hash_file = sha256_hash.hexdigest()
  43. if file_path.with_suffix(file_path.suffix + ".sha256sum").exists():
  44. hash_unsigned = (
  45. file_path.with_suffix(file_path.suffix + ".sha256sum").read_text().strip()
  46. )
  47. else:
  48. hash_unsigned = hash_file
  49. file_size = path.getsize(file_path)
  50. file_info = {
  51. "metadata_version": 1,
  52. "target": "{}/{}".format(getenv("TARGET"), getenv("SUBTARGET")),
  53. "version_code": getenv("VERSION_CODE"),
  54. "version_number": getenv("VERSION_NUMBER"),
  55. "source_date_epoch": int(getenv("SOURCE_DATE_EPOCH")),
  56. "profiles": {
  57. device_id: {
  58. "image_prefix": getenv("DEVICE_IMG_PREFIX"),
  59. "images": [
  60. {
  61. "type": getenv("FILE_TYPE"),
  62. "name": getenv("FILE_NAME"),
  63. "sha256": hash_file,
  64. "sha256_unsigned": hash_unsigned,
  65. "size": file_size,
  66. }
  67. ],
  68. "device_packages": getenv("DEVICE_PACKAGES").split(),
  69. "supported_devices": getenv("SUPPORTED_DEVICES").split(),
  70. "titles": get_titles(),
  71. }
  72. },
  73. }
  74. if getenv("IMAGE_SIZE") or getenv("KERNEL_SIZE"):
  75. file_info["profiles"][device_id]["file_size_limits"] = {}
  76. if getenv("IMAGE_SIZE"):
  77. file_info["profiles"][device_id]["file_size_limits"]["image"] = get_numerical_size(
  78. getenv("IMAGE_SIZE")
  79. )
  80. if getenv("KERNEL_SIZE"):
  81. file_info["profiles"][device_id]["file_size_limits"]["kernel"] = get_numerical_size(
  82. getenv("KERNEL_SIZE")
  83. )
  84. if getenv("FILE_FILESYSTEM"):
  85. file_info["profiles"][device_id]["images"][0]["filesystem"] = getenv(
  86. "FILE_FILESYSTEM"
  87. )
  88. json_path.write_text(json.dumps(file_info, separators=(",", ":")))