json_overview_image_info.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. from os import getenv, environ
  3. from pathlib import Path
  4. from subprocess import run, PIPE
  5. from sys import argv
  6. import json
  7. if len(argv) != 2:
  8. print("JSON info files script requires output file as argument")
  9. exit(1)
  10. output_path = Path(argv[1])
  11. assert getenv("WORK_DIR"), "$WORK_DIR required"
  12. work_dir = Path(getenv("WORK_DIR"))
  13. output = {}
  14. def get_initial_output(image_info):
  15. # preserve existing profiles.json
  16. if output_path.is_file():
  17. profiles = json.loads(output_path.read_text())
  18. if profiles["version_code"] == image_info["version_code"]:
  19. return profiles
  20. return image_info
  21. for json_file in work_dir.glob("*.json"):
  22. image_info = json.loads(json_file.read_text())
  23. if not output:
  24. output = get_initial_output(image_info)
  25. # get first and only profile in json file
  26. device_id, profile = next(iter(image_info["profiles"].items()))
  27. if device_id not in output["profiles"]:
  28. output["profiles"][device_id] = profile
  29. else:
  30. output["profiles"][device_id]["images"].extend(profile["images"])
  31. # make image lists unique by name, keep last/latest
  32. for device_id, profile in output.get("profiles", {}).items():
  33. profile["images"] = list({e["name"]: e for e in profile["images"]}.values())
  34. if output:
  35. (
  36. default_packages,
  37. output["arch_packages"],
  38. linux_version,
  39. linux_release,
  40. linux_vermagic,
  41. ) = run(
  42. [
  43. "make",
  44. "--no-print-directory",
  45. "-C",
  46. "target/linux/",
  47. "val.DEFAULT_PACKAGES",
  48. "val.ARCH_PACKAGES",
  49. "val.LINUX_VERSION",
  50. "val.LINUX_RELEASE",
  51. "val.LINUX_VERMAGIC",
  52. "V=s",
  53. ],
  54. stdout=PIPE,
  55. check=True,
  56. env=environ.copy().update({"TOPDIR": Path().cwd()}),
  57. universal_newlines=True,
  58. ).stdout.splitlines()
  59. output["default_packages"] = sorted(default_packages.split())
  60. output["linux_kernel"] = {
  61. "version": linux_version,
  62. "release": linux_release,
  63. "vermagic": linux_vermagic,
  64. }
  65. output_path.write_text(json.dumps(output, sort_keys=True, separators=(",", ":")))
  66. else:
  67. print("JSON info file script could not find any JSON files for target")