xref: /aosp_15_r20/external/bazelbuild-rules_python/python/private/pypi/whl_installer/arguments.py (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1# Copyright 2023 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
15import argparse
16import json
17import pathlib
18from typing import Any, Dict, Set
19
20from python.private.pypi.whl_installer.platform import Platform
21
22
23def parser(**kwargs: Any) -> argparse.ArgumentParser:
24    """Create a parser for the wheel_installer tool."""
25    parser = argparse.ArgumentParser(
26        **kwargs,
27    )
28    parser.add_argument(
29        "--requirement",
30        action="store",
31        required=True,
32        help="A single PEP508 requirement specifier string.",
33    )
34    parser.add_argument(
35        "--isolated",
36        action="store_true",
37        help="Whether or not to include the `--isolated` pip flag.",
38    )
39    parser.add_argument(
40        "--extra_pip_args",
41        action="store",
42        help="Extra arguments to pass down to pip.",
43    )
44    parser.add_argument(
45        "--platform",
46        action="extend",
47        type=Platform.from_string,
48        help="Platforms to target dependencies. Can be used multiple times.",
49    )
50    parser.add_argument(
51        "--pip_data_exclude",
52        action="store",
53        help="Additional data exclusion parameters to add to the pip packages BUILD file.",
54    )
55    parser.add_argument(
56        "--enable_implicit_namespace_pkgs",
57        action="store_true",
58        help="Disables conversion of implicit namespace packages into pkg-util style packages.",
59    )
60    parser.add_argument(
61        "--environment",
62        action="store",
63        help="Extra environment variables to set on the pip environment.",
64    )
65    parser.add_argument(
66        "--download_only",
67        action="store_true",
68        help="Use 'pip download' instead of 'pip wheel'. Disables building wheels from source, but allows use of "
69        "--platform, --python-version, --implementation, and --abi in --extra_pip_args.",
70    )
71    parser.add_argument(
72        "--whl-file",
73        type=pathlib.Path,
74        help="Extract a whl file to be used within Bazel.",
75    )
76    return parser
77
78
79def deserialize_structured_args(args: Dict[str, str]) -> Dict:
80    """Deserialize structured arguments passed from the starlark rules.
81
82    Args:
83        args: dict of parsed command line arguments
84    """
85    structured_args = ("extra_pip_args", "pip_data_exclude", "environment")
86    for arg_name in structured_args:
87        if args.get(arg_name) is not None:
88            args[arg_name] = json.loads(args[arg_name])["arg"]
89        else:
90            args[arg_name] = []
91    return args
92
93
94def get_platforms(args: argparse.Namespace) -> Set:
95    """Aggregate platforms into a single set.
96
97    Args:
98        args: dict of parsed command line arguments
99    """
100    platforms = set()
101    if args.platform is None:
102        return platforms
103
104    platforms.update(args.platform)
105
106    return platforms
107