1
0

json_overview_image_info.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. default_packages, output["arch_packages"] = run(
  36. [
  37. "make",
  38. "--no-print-directory",
  39. "-C",
  40. "target/linux/",
  41. "val.DEFAULT_PACKAGES",
  42. "val.ARCH_PACKAGES",
  43. ],
  44. stdout=PIPE,
  45. stderr=PIPE,
  46. check=True,
  47. env=environ.copy().update({"TOPDIR": Path().cwd()}),
  48. universal_newlines=True,
  49. ).stdout.splitlines()
  50. output["default_packages"] = sorted(default_packages.split())
  51. output_path.write_text(json.dumps(output, sort_keys=True, separators=(",", ":")))
  52. else:
  53. print("JSON info file script could not find any JSON files for target")