xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/is_standalone_interpreter.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
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
18load(":repo_utils.bzl", "repo_utils")
19
20STANDALONE_INTERPRETER_FILENAME = "STANDALONE_INTERPRETER"
21
22def 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
51