start-colima.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import annotations
  2. import os
  3. import platform
  4. import subprocess
  5. import sys
  6. from collections.abc import Sequence
  7. def main(argv: Sequence[str] | None = None) -> int:
  8. # platform.processor() changed at some point between these:
  9. # 11.2.3: arm
  10. # 12.3.1: arm64
  11. APPLE_ARM64 = sys.platform == "darwin" and platform.processor() in {"arm", "arm64"}
  12. cpus = os.cpu_count()
  13. if cpus is None:
  14. raise SystemExit("failed to determine cpu count")
  15. # SC_PAGE_SIZE is POSIX 2008
  16. # SC_PHYS_PAGES is a linux addition but also supported by more recent MacOS versions
  17. SC_PAGE_SIZE = os.sysconf("SC_PAGE_SIZE")
  18. SC_PHYS_PAGES = os.sysconf("SC_PHYS_PAGES")
  19. if SC_PAGE_SIZE == -1 or SC_PHYS_PAGES == -1:
  20. raise SystemExit("failed to determine memsize_bytes")
  21. memsize_bytes = os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")
  22. args = [
  23. "--cpu",
  24. f"{cpus//2}",
  25. "--memory",
  26. f"{memsize_bytes//(2*1024**3)}",
  27. ]
  28. if APPLE_ARM64:
  29. args = [*args, "--vm-type=vz", "--vz-rosetta", "--mount-type=virtiofs"]
  30. HOME = os.path.expanduser("~")
  31. rc = subprocess.call(
  32. (
  33. f"{HOME}/.local/share/sentry-devenv/bin/colima",
  34. "start",
  35. f"--mount=/var/folders:w,/private/tmp/colima:w,{HOME}:r",
  36. *args,
  37. )
  38. )
  39. if rc != 0:
  40. return rc
  41. return subprocess.call(("docker", "context", "use", "colima"))
  42. if __name__ == "__main__":
  43. raise SystemExit(main())