Skip to content

Commit c3aa711

Browse files
authored
Teach bazelisk.py about .bazeliskrc (#494)
* Teach bazelisk.py about .bazeliskrc * Add memoization
1 parent 801dd34 commit c3aa711

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ You can control the user agent that Bazelisk sends in all HTTP requests by setti
180180

181181
# .bazeliskrc configuration file
182182

183-
The Go version supports a `.bazeliskrc` file in the root directory of a workspace and the user home directory. This file allows users to set environment variables persistently.
183+
A `.bazeliskrc` file in the root directory of a workspace or the user home directory allows users to set environment variables persistently. (The Python implementation of Bazelisk doesn't check the user home directory yet, only the workspace directory.)
184184

185185
Example file content:
186186

bazelisk.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,50 @@
6262

6363
BAZEL_UPSTREAM = "bazelbuild"
6464

65+
_dotfiles_dict = None
66+
67+
68+
def get_dotfiles_dict():
69+
"""Loads all supported dotfiles and returns a unified name=value dictionary
70+
for their config settings. The dictionary is only loaded on the first call
71+
to this function; subsequent calls used a cached result, so won't change.
72+
"""
73+
global _dotfiles_dict
74+
if _dotfiles_dict is not None:
75+
return _dotfiles_dict
76+
_dotfiles_dict = dict()
77+
env_files = []
78+
# Here we're only checking the workspace root. Ideally, we would also check
79+
# the user's home directory to match the Go version. When making that edit,
80+
# be sure to obey the correctly prioritization of workspace / home rcfiles.
81+
root = find_workspace_root()
82+
if root:
83+
env_files.append(os.path.join(root, ".bazeliskrc"))
84+
for env_file in env_files:
85+
try:
86+
with open(env_file, "r") as f:
87+
rcdata = f.read()
88+
except Exception:
89+
continue
90+
for line in rcdata.splitlines():
91+
line = line.split("#", 1)[0].strip()
92+
if not line:
93+
continue
94+
some_name, some_value = line.split("=", 1)
95+
_dotfiles_dict[some_name] = some_value
96+
return _dotfiles_dict
97+
98+
99+
def get_env_or_config(name, default=None):
100+
"""Reads a configuration value from the environment, but falls back to
101+
reading it from .bazeliskrc in the workspace root."""
102+
if name in os.environ:
103+
return os.environ[name]
104+
dotfiles = get_dotfiles_dict()
105+
if name in dotfiles:
106+
return dotfiles[name]
107+
return default
108+
65109

66110
def decide_which_bazel_version_to_use():
67111
# Check in this order:
@@ -74,8 +118,9 @@ def decide_which_bazel_version_to_use():
74118
# - workspace_root/.bazelversion exists -> read contents, that version.
75119
# - workspace_root/WORKSPACE contains a version -> that version. (TODO)
76120
# - fallback: latest release
77-
if "USE_BAZEL_VERSION" in os.environ:
78-
return os.environ["USE_BAZEL_VERSION"]
121+
use_bazel_version = get_env_or_config("USE_BAZEL_VERSION")
122+
if use_bazel_version is not None:
123+
return use_bazel_version
79124

80125
workspace_root = find_workspace_root()
81126
if workspace_root:
@@ -226,7 +271,7 @@ def determine_bazel_filename(version):
226271

227272
filename_suffix = determine_executable_filename_suffix()
228273
bazel_flavor = "bazel"
229-
if os.environ.get("BAZELISK_NOJDK", "0") != "0":
274+
if get_env_or_config("BAZELISK_NOJDK", "0") != "0":
230275
bazel_flavor = "bazel_nojdk"
231276
return "{}-{}-{}-{}{}".format(bazel_flavor, version, operating_system, machine, filename_suffix)
232277

@@ -277,8 +322,9 @@ def determine_url(version, is_commit, bazel_filename):
277322
# Example: '0.19.1' -> ('0.19.1', None), '0.20.0rc1' -> ('0.20.0', 'rc1')
278323
(version, rc) = re.match(r"(\d*\.\d*(?:\.\d*)?)(rc\d+)?", version).groups()
279324

280-
if "BAZELISK_BASE_URL" in os.environ:
281-
return "{}/{}/{}".format(os.environ["BAZELISK_BASE_URL"], version, bazel_filename)
325+
bazelisk_base_url = get_env_or_config("BAZELISK_BASE_URL")
326+
if bazelisk_base_url is not None:
327+
return "{}/{}/{}".format(bazelisk_base_url, version, bazel_filename)
282328
else:
283329
return "https://releases.bazel.build/{}/{}/{}".format(
284330
version, rc if rc else "release", bazel_filename
@@ -342,7 +388,7 @@ def download_bazel_into_directory(version, is_commit, directory):
342388
def download(url, destination_path):
343389
sys.stderr.write("Downloading {}...\n".format(url))
344390
request = Request(url)
345-
if "BAZELISK_BASE_URL" in os.environ:
391+
if get_env_or_config("BAZELISK_BASE_URL") is not None:
346392
parts = urlparse(url)
347393
creds = None
348394
try:
@@ -357,7 +403,7 @@ def download(url, destination_path):
357403

358404

359405
def get_bazelisk_directory():
360-
bazelisk_home = os.environ.get("BAZELISK_HOME")
406+
bazelisk_home = get_env_or_config("BAZELISK_HOME")
361407
if bazelisk_home is not None:
362408
return bazelisk_home
363409

bazelisk_test.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,14 @@ echo "# test_bazel_version_from_environment"
497497
test_bazel_version_from_environment
498498
echo
499499

500+
echo "# test_bazel_version_prefer_environment_to_bazeliskrc"
501+
test_bazel_version_prefer_environment_to_bazeliskrc
502+
echo
503+
504+
echo "# test_bazel_version_from_workspace_bazeliskrc"
505+
test_bazel_version_from_workspace_bazeliskrc
506+
echo
507+
500508
echo "# test_bazel_version_from_file"
501509
test_bazel_version_from_file
502510
echo
@@ -530,14 +538,6 @@ if [[ $BAZELISK_VERSION == "GO" ]]; then
530538
test_bazel_version_from_base_url
531539
echo
532540

533-
echo "# test_bazel_version_prefer_environment_to_bazeliskrc"
534-
test_bazel_version_prefer_environment_to_bazeliskrc
535-
echo
536-
537-
echo "# test_bazel_version_from_workspace_bazeliskrc"
538-
test_bazel_version_from_workspace_bazeliskrc
539-
echo
540-
541541
echo "# test_bazel_version_from_user_home_bazeliskrc"
542542
test_bazel_version_from_user_home_bazeliskrc
543543
echo

0 commit comments

Comments
 (0)