Skip to content

Commit

Permalink
Add memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri committed Sep 7, 2024
1 parent 11efb12 commit 09ed135
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ You can control the user agent that Bazelisk sends in all HTTP requests by setti

# .bazeliskrc configuration file

A `.bazeliskrc` file in the root directory of a workspace or the user home directory allows users to set environment variables persistently.
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.)

Example file content:

Expand Down
33 changes: 26 additions & 7 deletions bazelisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,22 @@

BAZEL_UPSTREAM = "bazelbuild"

_dotfiles_dict = None

def get_env_or_config(name, default=None):
"""Reads a configuration value from the environment, but falls back to
reading it from .bazeliskrc in the workspace root."""
if name in os.environ:
return os.environ[name]

def get_dotfiles_dict():
"""Loads all supported dotfiles and returns a unified name=value dictionary
for their config settings. The dictionary is only loaded on the first call
to this function; subsequent calls used a cached result, so won't change.
"""
global _dotfiles_dict
if _dotfiles_dict is not None:
return _dotfiles_dict
_dotfiles_dict = dict()
env_files = []
# Here we're only checking the workspace root. Ideally, we would also check
# the user's home directory to match the Go version. When making that edit,
# be sure to obey the correctly prioritization of workspace / home rcfiles.
root = find_workspace_root()
if root:
env_files.append(os.path.join(root, ".bazeliskrc"))
Expand All @@ -88,8 +97,18 @@ def get_env_or_config(name, default=None):
if not line:
continue
some_name, some_value = line.split("=", 1)
if some_name == name:
return some_value
_dotfiles_dict[some_name] = some_value
return _dotfiles_dict


def get_env_or_config(name, default=None):
"""Reads a configuration value from the environment, but falls back to
reading it from .bazeliskrc in the workspace root."""
if name in os.environ:
return os.environ[name]
dotfiles = get_dotfiles_dict()
if name in dotfiles:
return dotfiles[name]
return default


Expand Down

0 comments on commit 09ed135

Please sign in to comment.