xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/pypi/evaluate_markers.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"""A simple function that evaluates markers using a python interpreter."""
16
17load(":pypi_repo_utils.bzl", "pypi_repo_utils")
18
19# Used as a default value in a rule to ensure we fetch the dependencies.
20SRCS = [
21    # When the version, or any of the files in `packaging` package changes,
22    # this file will change as well.
23    Label("@pypi__packaging//:packaging-24.0.dist-info/RECORD"),
24    Label("//python/private/pypi/requirements_parser:resolve_target_platforms.py"),
25    Label("//python/private/pypi/whl_installer:platform.py"),
26]
27
28def evaluate_markers(mrctx, *, requirements, python_interpreter, python_interpreter_target, srcs, logger = None):
29    """Return the list of supported platforms per requirements line.
30
31    Args:
32        mrctx: repository_ctx or module_ctx.
33        requirements: list[str] of the requirement file lines to evaluate.
34        python_interpreter: str, path to the python_interpreter to use to
35            evaluate the env markers in the given requirements files. It will
36            be only called if the requirements files have env markers. This
37            should be something that is in your PATH or an absolute path.
38        python_interpreter_target: Label, same as python_interpreter, but in a
39            label format.
40        srcs: list[Label], the value of SRCS passed from the `rctx` or `mctx` to this function.
41        logger: repo_utils.logger or None, a simple struct to log diagnostic
42            messages. Defaults to None.
43
44    Returns:
45        dict of string lists with target platforms
46    """
47    if not requirements:
48        return {}
49
50    in_file = mrctx.path("requirements_with_markers.in.json")
51    out_file = mrctx.path("requirements_with_markers.out.json")
52    mrctx.file(in_file, json.encode(requirements))
53
54    pypi_repo_utils.execute_checked(
55        mrctx,
56        op = "ResolveRequirementEnvMarkers({})".format(in_file),
57        arguments = [
58            pypi_repo_utils.resolve_python_interpreter(
59                mrctx,
60                python_interpreter = python_interpreter,
61                python_interpreter_target = python_interpreter_target,
62            ),
63            "-m",
64            "python.private.pypi.requirements_parser.resolve_target_platforms",
65            in_file,
66            out_file,
67        ],
68        srcs = srcs,
69        environment = {
70            "PYTHONPATH": [
71                Label("@pypi__packaging//:BUILD.bazel"),
72                Label("//:BUILD.bazel"),
73            ],
74        },
75        logger = logger,
76    )
77    return json.decode(mrctx.read(out_file))
78