-
Notifications
You must be signed in to change notification settings - Fork 6
/
bazel
executable file
·65 lines (50 loc) · 1.71 KB
/
bazel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python3
# SPDX-License-Identifier: BSD-2-Clause
"""If you don't have Bazel installed on your $PATH, you can use this program
to run a copy of Bazel that's automatically downloaded from the internet, e.g.:
./bazel test //...
If you have Bazel or Bazelisk already on your $PATH, you can run that instead,
e.g.:
bazel test //...
"""
import hashlib
import os
from pathlib import Path
import sys
import urllib.request as request
# The pinned version and checksum of bazelisk.
_VERSION = "1.23.0"
_SHA256 = "cba873b7a873b4ee545749fdc3c30dd56475348e2d64858bb37aa6c640e1be04"
_URL = f"https://raw.githubusercontent.com/bazelbuild/bazelisk/v{_VERSION}/bazelisk.py" # noqa
def _fetch_bazelisk():
# Create a home for our downloaded bazelisk.
dot_bazel = Path(__file__).resolve().with_name(".bazel")
dot_bazel.mkdir(exist_ok=True)
bazelisk = dot_bazel / "bazelisk"
# Check if we've already fetched it.
try:
hasher = hashlib.sha256()
with open(bazelisk, "rb") as f:
hasher.update(f.read())
if hasher.hexdigest() == _SHA256:
return bazelisk
except IOError:
pass
# Fetch it.
print(f"Downloading {_URL} ...")
with request.urlopen(url=_URL, timeout=10) as response:
content = response.read()
hasher = hashlib.sha256()
hasher.update(content)
digest = hasher.hexdigest()
if digest != _SHA256:
raise RuntimeError(f"Got wrong sha256 {digest} but wanted {_SHA256}.")
with open(bazelisk, "wb") as f:
f.write(content)
bazelisk.chmod(0o755)
return bazelisk
def _main():
bazelisk = _fetch_bazelisk()
os.execv(bazelisk, [str(bazelisk)] + sys.argv[1:])
if __name__ == "__main__":
_main()