Skip to content

Commit 496332b

Browse files
committed
refactor: split out the standalone interpreter utility function
1 parent bd5a35c commit 496332b

File tree

8 files changed

+68
-40
lines changed

8 files changed

+68
-40
lines changed

examples/bzlmod/MODULE.bazel.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ bzl_library(
229229
name = "repositories_bzl",
230230
srcs = ["repositories.bzl"],
231231
deps = [
232+
"//python/private:is_standalone_interpreter_bzl",
232233
"//python/private:py_repositories_bzl",
233234
"//python/private:python_repositories_bzl",
234235
"//python/private:python_repository_bzl",

python/private/BUILD.bazel

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ bzl_library(
123123
deps = [":bzlmod_enabled_bzl"],
124124
)
125125

126+
bzl_library(
127+
name = "is_standalone_interpreter_bzl",
128+
srcs = ["is_standalone_interpreter.bzl"],
129+
deps = [
130+
":repo_utils_bzl",
131+
],
132+
)
133+
126134
bzl_library(
127135
name = "normalize_name_bzl",
128136
srcs = ["normalize_name.bzl"],
@@ -174,7 +182,6 @@ bzl_library(
174182
":full_version_bzl",
175183
":internal_config_repo_bzl",
176184
":python_repository_bzl",
177-
":repo_utils_bzl",
178185
":toolchains_repo_bzl",
179186
"//python:versions_bzl",
180187
"//python/private/pypi:deps_bzl",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""This file contains repository rules and macros to support toolchain registration.
16+
"""
17+
18+
load(":repo_utils.bzl", "repo_utils")
19+
20+
STANDALONE_INTERPRETER_FILENAME = "STANDALONE_INTERPRETER"
21+
22+
def is_standalone_interpreter(rctx, python_interpreter_path, *, logger = None):
23+
"""Query a python interpreter target for whether or not it's a rules_rust provided toolchain
24+
25+
Args:
26+
rctx: {type}`repository_ctx` The repository rule's context object.
27+
python_interpreter_path: {type}`path` A path representing the interpreter.
28+
logger: Optional logger to use for operations.
29+
30+
Returns:
31+
{type}`bool` Whether or not the target is from a rules_python generated toolchain.
32+
"""
33+
34+
# Only update the location when using a hermetic toolchain.
35+
if not python_interpreter_path:
36+
return False
37+
38+
# This is a rules_python provided toolchain.
39+
return repo_utils.execute_unchecked(
40+
rctx,
41+
op = "IsStandaloneInterpreter",
42+
arguments = [
43+
"ls",
44+
"{}/{}".format(
45+
python_interpreter_path.dirname,
46+
STANDALONE_INTERPRETER_FILENAME,
47+
),
48+
],
49+
logger = logger,
50+
).return_code == 0

python/private/pypi/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ bzl_library(
306306
":patch_whl_bzl",
307307
":pypi_repo_utils_bzl",
308308
":whl_target_platforms_bzl",
309-
"//python:repositories_bzl",
310309
"//python/private:auth_bzl",
311310
"//python/private:envsubst_bzl",
311+
"//python/private:is_standalone_interpreter_bzl",
312312
"//python/private:repo_utils_bzl",
313313
],
314314
)

python/private/pypi/whl_library.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth")
1818
load("//python/private:envsubst.bzl", "envsubst")
19-
load("//python/private:python_repositories.bzl", "is_standalone_interpreter")
19+
load("//python/private:is_standalone_interpreter.bzl", "is_standalone_interpreter")
2020
load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
2121
load(":attrs.bzl", "ATTRS", "use_isolated")
2222
load(":deps.bzl", "all_repo_names")

python/private/python_repositories.bzl

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ load(":bzlmod_enabled.bzl", "BZLMOD_ENABLED")
2727
load(":coverage_deps.bzl", "coverage_dep")
2828
load(":full_version.bzl", "full_version")
2929
load(":python_repository.bzl", "python_repository")
30-
load(":repo_utils.bzl", "repo_utils")
3130
load(
3231
":toolchains_repo.bzl",
3332
"host_toolchain",
@@ -36,38 +35,6 @@ load(
3635
"toolchains_repo",
3736
)
3837

39-
STANDALONE_INTERPRETER_FILENAME = "STANDALONE_INTERPRETER"
40-
41-
def is_standalone_interpreter(rctx, python_interpreter_path, *, logger = None):
42-
"""Query a python interpreter target for whether or not it's a rules_rust provided toolchain
43-
44-
Args:
45-
rctx: {type}`repository_ctx` The repository rule's context object.
46-
python_interpreter_path: {type}`path` A path representing the interpreter.
47-
logger: Optional logger to use for operations.
48-
49-
Returns:
50-
{type}`bool` Whether or not the target is from a rules_python generated toolchain.
51-
"""
52-
53-
# Only update the location when using a hermetic toolchain.
54-
if not python_interpreter_path:
55-
return False
56-
57-
# This is a rules_python provided toolchain.
58-
return repo_utils.execute_unchecked(
59-
rctx,
60-
op = "IsStandaloneInterpreter",
61-
arguments = [
62-
"ls",
63-
"{}/{}".format(
64-
python_interpreter_path.dirname,
65-
STANDALONE_INTERPRETER_FILENAME,
66-
),
67-
],
68-
logger = logger,
69-
).return_code == 0
70-
7138
# Wrapper macro around everything above, this is the primary API.
7239
def python_register_toolchains(
7340
name,

python/repositories.bzl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
"""This file contains macros to be called during WORKSPACE evaluation.
1616
"""
1717

18-
load("//python/private:py_repositories.bzl", _py_repositories = "py_repositories")
1918
load(
20-
"//python/private:python_repositories.bzl",
19+
"//python/private:is_standalone_interpreter.bzl",
2120
_STANDALONE_INTERPRETER_FILENAME = "STANDALONE_INTERPRETER_FILENAME",
2221
_is_standalone_interpreter = "is_standalone_interpreter",
22+
)
23+
load("//python/private:py_repositories.bzl", _py_repositories = "py_repositories")
24+
load(
25+
"//python/private:python_repositories.bzl",
2326
_python_register_multi_toolchains = "python_register_multi_toolchains",
2427
_python_register_toolchains = "python_register_toolchains",
2528
)

0 commit comments

Comments
 (0)