scripts_utils.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. """Utilities for scripts."""
  2. __copyright__ = """
  3. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  4. Exceptions. See /LICENSE for license information.
  5. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. """
  7. from enum import Enum
  8. import fcntl
  9. import hashlib
  10. import os
  11. from pathlib import Path
  12. import platform
  13. import shutil
  14. import tempfile
  15. import time
  16. from typing import NamedTuple, Optional
  17. import urllib.request
  18. # The tools we track releases for.
  19. class Release(Enum):
  20. BAZELISK = "bazelisk"
  21. BUILDIFIER = "buildifier"
  22. BUILDOZER = "buildozer"
  23. TARGET_DETERMINATOR = "target-determinator"
  24. class ReleaseInfo(NamedTuple):
  25. # The base URL for downloads. Should include the version.
  26. url: str
  27. # The separator in a binary's name, either `-` or `.`.
  28. separator: str
  29. _BAZEL_TOOLS_URL = (
  30. "https://github.com/bazelbuild/buildtools/releases/download/v8.2.0/"
  31. )
  32. # Structured information per release tool.
  33. _RELEASES = {
  34. Release.BAZELISK: ReleaseInfo(
  35. "https://github.com/bazelbuild/bazelisk/releases/download/v1.26.0/", "-"
  36. ),
  37. Release.BUILDIFIER: ReleaseInfo(_BAZEL_TOOLS_URL, "-"),
  38. Release.BUILDOZER: ReleaseInfo(_BAZEL_TOOLS_URL, "-"),
  39. Release.TARGET_DETERMINATOR: ReleaseInfo(
  40. "https://github.com/bazel-contrib/target-determinator/releases/download/v0.30.3/", # noqa: E501
  41. ".",
  42. ),
  43. }
  44. # Shas for the tools.
  45. #
  46. # To update, change the version in a tool's URL and use
  47. # `calculate_release_shas.py`. This is maintained separate from _RELEASES just
  48. # to make copy-paste updates simpler.
  49. _RELEASE_SHAS = {
  50. Release.BAZELISK: {
  51. "darwin-amd64": "5c77f33f91dd3df119d192175100cb5b50302eb7ee37859cbab79e10a76ccce8", # noqa: E501
  52. "darwin-arm64": "d1ca9911cc19e1f17483f93956908334f2b7f3dd13f20853417b68fc3c3eb370", # noqa: E501
  53. "linux-amd64": "6539c12842ad76966f3d493e8f80d67caa84ec4a000e220d5459833c967c12bc", # noqa: E501
  54. "linux-arm64": "54f85ef4c23393f835252cc882e5fea596e8ef3c4c2056b059f8067cd19f0351", # noqa: E501
  55. "windows-amd64.exe": "023734f33ed6b9c6d65468fe20bb2c5fb32473ccb8aca2fc5bf1521e61ce1622", # noqa: E501
  56. },
  57. Release.BUILDIFIER: {
  58. "darwin-amd64": "309b3c3bfcc4b1533d5f7f796adbd266235cfb6f01450f3e37423527d209a309", # noqa: E501
  59. "darwin-arm64": "e08381a3ed1d59c0a17d1cee1d4e7684c6ce1fc3b5cfa1bd92a5fe978b38b47d", # noqa: E501
  60. "linux-amd64": "3e79e6c0401b5f36f8df4dfc686127255d25c7eddc9599b8779b97b7ef4cdda7", # noqa: E501
  61. "linux-arm64": "c624a833bfa64d3a457ef0235eef0dbda03694768aab33f717a7ffd3f803d272", # noqa: E501
  62. "windows-amd64.exe": "a27fcf7521414f8214787989dcfb2ac7d3f7c28b56e44384e5fa06109953c2f1", # noqa: E501
  63. },
  64. Release.BUILDOZER: {
  65. "darwin-amd64": "b7bd7189a9d4de22c10fd94b7d1d77c68712db9bdd27150187bc677e8c22960e", # noqa: E501
  66. "darwin-arm64": "781527c5337dadba5a0611c01409c669852b73b72458650cc7c5f31473f7ae3f", # noqa: E501
  67. "linux-amd64": "0e54770aa6148384d1edde39ef20e10d2c57e8c09dd42f525e100f51b0b77ae1", # noqa: E501
  68. "linux-arm64": "a9f38f2781de41526ce934866cb79b8b5b59871c96853dc5a1aee26f4c5976bb", # noqa: E501
  69. "windows-amd64.exe": "8ce5a9a064b01551ffb8d441fa9ef4dd42c9eeeed6bc71a89f917b3474fd65f6", # noqa: E501
  70. },
  71. Release.TARGET_DETERMINATOR: {
  72. "darwin.amd64": "04adf78f763e622467181669fdf275e01edc1ec3d79940e78040127a15b7c8b2", # noqa: E501
  73. "darwin.arm64": "f59ee18404577a704bc1399907c35b546fd66ffd5a1e145e7955a3d3e57a2a13", # noqa: E501
  74. "linux.amd64": "6eaa8921e6c614c309536af3dc7ca23f52e5ced30b9032e6443bbe0d41a8ae33", # noqa: E501
  75. "linux.arm64": "1c7216426d4e2ca63b912fe2be2ab8f3f9ccbe2aefa174e2a22e7f19f5f36065", # noqa: E501
  76. "windows.amd64.exe": "53d377274c40b1a0e37db96c20fa4b701d1e5e2650af14517c49e170b2564736", # noqa: E501
  77. },
  78. }
  79. def chdir_repo_root() -> None:
  80. """Change the working directory to the repository root.
  81. This is done so that scripts run from a consistent directory.
  82. """
  83. os.chdir(Path(__file__).parents[1])
  84. def _get_hash(file: Path) -> str:
  85. """Returns the sha256 of a file."""
  86. digest = hashlib.sha256()
  87. with file.open("rb") as f:
  88. while True:
  89. chunk = f.read(1024 * 64)
  90. if not chunk:
  91. break
  92. digest.update(chunk)
  93. return digest.hexdigest()
  94. def _download(url: str, local_path: Path) -> Optional[int]:
  95. """Downloads the URL to the path. Returns an HTTP error code on failure."""
  96. with urllib.request.urlopen(url) as response:
  97. if response.code != 200:
  98. return int(response.code)
  99. with local_path.open("wb") as f:
  100. shutil.copyfileobj(response, f)
  101. return None
  102. def _get_cached_binary(name: str, url: str, want_hash: str) -> str:
  103. """Returns the path to the cached binary.
  104. If the matching version is already cached, returns it. Otherwise, downloads
  105. from the URL and verifies the hash matches.
  106. """
  107. cache_dir = Path.home().joinpath(".cache", "carbon-lang-scripts")
  108. cache_dir.mkdir(parents=True, exist_ok=True)
  109. # Hold a lock while checksumming and downloading the path. Otherwise,
  110. # parallel runs by pre-commit may conflict with one another with
  111. # simultaneous downloads.
  112. with open(cache_dir.joinpath(f"{name}.lock"), "w") as lock_file:
  113. fcntl.lockf(lock_file.fileno(), fcntl.LOCK_EX)
  114. # Check if there's a cached file that can be used.
  115. local_path = cache_dir.joinpath(name)
  116. if local_path.is_file() and want_hash == _get_hash(local_path):
  117. return str(local_path)
  118. # Download the file.
  119. retries = 5
  120. while True:
  121. err = _download(url, local_path)
  122. if err is None:
  123. break
  124. retries -= 1
  125. if retries == 0:
  126. exit(f"Failed to download {url}: HTTP {err}.")
  127. time.sleep(1)
  128. local_path.chmod(0o755)
  129. # Verify the downloaded hash.
  130. found_hash = _get_hash(local_path)
  131. if want_hash != found_hash:
  132. exit(
  133. f"Downloaded {url} but found sha256 "
  134. f"{found_hash} ({local_path.stat().st_size} bytes), wanted "
  135. f"{want_hash}"
  136. )
  137. return str(local_path)
  138. def _get_machine() -> str:
  139. machine = platform.machine()
  140. if machine == "x86_64":
  141. machine = "amd64"
  142. elif machine == "aarch64":
  143. machine = "arm64"
  144. return machine
  145. def _get_platform_ext() -> str:
  146. if platform.system() == "Windows":
  147. return ".exe"
  148. else:
  149. return ""
  150. def _select_hash(hashes: dict[str, str], version: str) -> str:
  151. # Ensure the platform version is supported and has a hash.
  152. if version not in hashes:
  153. # If this because a platform support issue, we may need to print errors.
  154. exit(f"No release available for platform: {version}")
  155. return hashes[version]
  156. def get_release(release: Release) -> str:
  157. """Install a tool to carbon-lang's cache and return its path.
  158. release: The release to cache.
  159. """
  160. info = _RELEASES[release]
  161. shas = _RELEASE_SHAS[release]
  162. # Translate platform information into Bazel's release form.
  163. ext = _get_platform_ext()
  164. platform_label = (
  165. f"{platform.system().lower()}{info.separator}{_get_machine()}{ext}"
  166. )
  167. url = f"{info.url}/{release.value}{info.separator}{platform_label}"
  168. want_hash = _select_hash(shas, platform_label)
  169. return _get_cached_binary(f"{release.value}{ext}", url, want_hash)
  170. def calculate_release_shas() -> None:
  171. """Prints sha information for tracked tool releases."""
  172. print("_RELEASE_SHAS = {")
  173. for release, info in _RELEASES.items():
  174. shas = _RELEASE_SHAS[release]
  175. print(f" {release}: {{")
  176. for platform_label in shas.keys():
  177. url = f"{info.url}/{release.value}{info.separator}{platform_label}"
  178. with tempfile.NamedTemporaryFile() as f:
  179. path = Path(f.name)
  180. _download(url, path)
  181. hash = _get_hash(path)
  182. print(f' "{platform_label}": "{hash}", # noqa: E501')
  183. print(" },")
  184. print("}")
  185. def locate_bazel() -> str:
  186. """Returns the bazel command.
  187. In order, try:
  188. 1. The `BAZEL` environment variable.
  189. 2. `bazelisk`
  190. 3. `bazel`
  191. 4. `run_bazelisk.py`
  192. """
  193. bazel = os.environ.get("BAZEL")
  194. if bazel:
  195. return bazel
  196. for cmd in ("bazelisk", "bazel"):
  197. target = shutil.which(cmd)
  198. if target:
  199. return target
  200. return str(Path(__file__).parent / "run_bazelisk.py")