Skip to content

Commit 612ca2e

Browse files
authored
chore: cleanup the usage of rctx.getenv (bazel-contrib#3640)
Remove most of the usage of os.environ.get and use getenv instead. The question is if we should just replace all usages blindly?
1 parent 2be0dd9 commit 612ca2e

File tree

17 files changed

+32
-62
lines changed

17 files changed

+32
-62
lines changed

python/private/auth.bzl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,7 @@ def get_auth(ctx, urls, ctx_attr = None):
9595
if ctx_attr.netrc:
9696
netrc = read_netrc(ctx, ctx_attr.netrc)
9797
elif "NETRC" in ctx.os.environ:
98-
# This can be used on newer bazel versions
99-
if hasattr(ctx, "getenv"):
100-
netrc = read_netrc(ctx, ctx.getenv("NETRC"))
101-
else:
102-
netrc = read_netrc(ctx, ctx.os.environ["NETRC"])
98+
netrc = read_netrc(ctx, ctx.getenv("NETRC"))
10399
else:
104100
netrc = read_user_netrc(ctx)
105101

python/private/envsubst.bzl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ def envsubst(template_string, varnames, getenv):
2020
Supports `$VARNAME`, `${VARNAME}` and `${VARNAME:-default}`
2121
syntaxes in the `template_string`, looking up each `VARNAME`
2222
listed in the `varnames` list in the environment defined by the
23-
`getenv` function. Typically called with `getenv = rctx.getenv`
24-
(if it is available) or `getenv = rctx.os.environ.get` (on e.g.
25-
Bazel 6 or Bazel 7, which don't have `rctx.getenv` yet).
23+
`getenv` function. Typically called with `getenv = rctx.getenv`.
2624
2725
Limitations: Unlike the shell, we don't support `${VARNAME}` and
2826
`${VARNAME:-default}` in the default expression for a different

python/private/internal_config_repo.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,4 @@ internal_config_repo = repository_rule(
147147
)
148148

149149
def _bool_from_environ(rctx, key, default):
150-
return bool(int(repo_utils.getenv(rctx, key, default)))
150+
return bool(int(rctx.getenv(key, default)))

python/private/local_runtime_repo.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def _resolve_interpreter_path(rctx):
359359
if "/" not in interpreter_path and "\\" not in interpreter_path:
360360
# Provide a bit nicer integration with pyenv: recalculate the runtime if the
361361
# user changes the python version using e.g. `pyenv shell`
362-
repo_utils.getenv(rctx, "PYENV_VERSION")
362+
rctx.getenv("PYENV_VERSION")
363363
result = repo_utils.which_unchecked(rctx, interpreter_path)
364364
resolved_path = result.binary
365365
describe_failure = result.describe_failure

python/private/pypi/attrs.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def use_isolated(ctx, attr):
240240
use_isolated = attr.isolated
241241

242242
# The environment variable will take precedence over the attribute
243-
isolated_env = ctx.os.environ.get("RULES_PYTHON_PIP_ISOLATED", None)
243+
isolated_env = ctx.getenv("RULES_PYTHON_PIP_ISOLATED", None)
244244
if isolated_env != None:
245245
if isolated_env.lower() in ("0", "false"):
246246
use_isolated = False

python/private/pypi/pip_repository.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def use_isolated(ctx, attr):
6565
use_isolated = attr.isolated
6666

6767
# The environment variable will take precedence over the attribute
68-
isolated_env = ctx.os.environ.get("RULES_PYTHON_PIP_ISOLATED", None)
68+
isolated_env = ctx.getenv("RULES_PYTHON_PIP_ISOLATED", None)
6969
if isolated_env != None:
7070
if isolated_env.lower() in ("0", "false"):
7171
use_isolated = False

python/private/pypi/simpleapi_download.bzl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,7 @@ def _read_simpleapi(ctx, url, attr, cache, get_auth = None, **download_kwargs):
189189
# them to ctx.download if we want to correctly handle the relative URLs.
190190
# TODO: Add a test that env subbed index urls do not leak into the lock file.
191191

192-
real_url = strip_empty_path_segments(envsubst(
193-
url,
194-
attr.envsubst,
195-
ctx.getenv if hasattr(ctx, "getenv") else ctx.os.environ.get,
196-
))
192+
real_url = strip_empty_path_segments(envsubst(url, attr.envsubst, ctx.getenv))
197193

198194
cache_key = real_url
199195
cached_result = cache.get(cache_key)

python/private/pypi/whl_library.bzl

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,13 @@ def _parse_optional_attrs(rctx, args, extra_pip_args = None):
151151
if use_isolated(rctx, rctx.attr):
152152
args.append("--isolated")
153153

154-
# Bazel version 7.1.0 and later (and rolling releases from version 8.0.0-pre.20240128.3)
155-
# support rctx.getenv(name, default): When building incrementally, any change to the value of
156-
# the variable named by name will cause this repository to be re-fetched.
157-
if "getenv" in dir(rctx):
158-
getenv = rctx.getenv
159-
else:
160-
getenv = rctx.os.environ.get
161-
162154
# Check for None so we use empty default types from our attrs.
163155
# Some args want to be list, and some want to be dict.
164156
if extra_pip_args != None:
165157
args += [
166158
"--extra_pip_args",
167159
json.encode(struct(arg = [
168-
envsubst(pip_arg, rctx.attr.envsubst, getenv)
160+
envsubst(pip_arg, rctx.attr.envsubst, rctx.getenv)
169161
for pip_arg in extra_pip_args
170162
])),
171163
]

python/private/python.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def parse_modules(*, module_ctx, logger, _fail = fail):
5656
platform suffix.
5757
* register_coverage_tool: bool
5858
"""
59-
if module_ctx.os.environ.get("RULES_PYTHON_BZLMOD_DEBUG", "0") == "1":
59+
if module_ctx.getenv("RULES_PYTHON_BZLMOD_DEBUG", "0") == "1":
6060
debug_info = {
6161
"toolchains_registered": [],
6262
}

python/private/repo_utils.bzl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _is_repo_debug_enabled(mrctx):
2929
Returns:
3030
True if enabled, False if not.
3131
"""
32-
return _getenv(mrctx, REPO_DEBUG_ENV_VAR) == "1"
32+
return mrctx.getenv(REPO_DEBUG_ENV_VAR) == "1"
3333

3434
def _logger(mrctx = None, name = None, verbosity_level = None, printer = None):
3535
"""Creates a logger instance for printing messages.
@@ -56,7 +56,7 @@ def _logger(mrctx = None, name = None, verbosity_level = None, printer = None):
5656
else:
5757
verbosity_level = "WARN"
5858

59-
env_var_verbosity = _getenv(mrctx, REPO_VERBOSITY_ENV_VAR)
59+
env_var_verbosity = mrctx.getenv(REPO_VERBOSITY_ENV_VAR)
6060
verbosity_level = env_var_verbosity or verbosity_level
6161

6262
verbosity = {
@@ -302,7 +302,7 @@ def _which_unchecked(mrctx, binary_name):
302302
mrctx.watch(binary)
303303
describe_failure = None
304304
else:
305-
path = _getenv(mrctx, "PATH", "")
305+
path = mrctx.getenv("PATH", "")
306306
describe_failure = lambda: _which_describe_failure(binary_name, path)
307307

308308
return struct(
@@ -319,10 +319,6 @@ def _which_describe_failure(binary_name, path):
319319
path = path,
320320
)
321321

322-
def _getenv(mrctx, name, default = None):
323-
# Bazel 7+ API has (repository|module)_ctx.getenv
324-
return getattr(mrctx, "getenv", mrctx.os.environ.get)(name, default)
325-
326322
def _args_to_str(arguments):
327323
return " ".join([_arg_repr(a) for a in arguments])
328324

@@ -467,7 +463,6 @@ repo_utils = struct(
467463
extract = _extract,
468464
get_platforms_cpu_name = _get_platforms_cpu_name,
469465
get_platforms_os_name = _get_platforms_os_name,
470-
getenv = _getenv,
471466
is_repo_debug_enabled = _is_repo_debug_enabled,
472467
logger = _logger,
473468
which_checked = _which_checked,

0 commit comments

Comments
 (0)