• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17from typing import Any
18
19
20def parser(**kwargs: Any) -> argparse.ArgumentParser:
21    """Create a parser for the wheel_installer tool."""
22    parser = argparse.ArgumentParser(
23        **kwargs,
24    )
25    parser.add_argument(
26        "--requirement",
27        action="store",
28        required=True,
29        help="A single PEP508 requirement specifier string.",
30    )
31    parser.add_argument(
32        "--isolated",
33        action="store_true",
34        help="Whether or not to include the `--isolated` pip flag.",
35    )
36    parser.add_argument(
37        "--extra_pip_args",
38        action="store",
39        help="Extra arguments to pass down to pip.",
40    )
41    parser.add_argument(
42        "--pip_data_exclude",
43        action="store",
44        help="Additional data exclusion parameters to add to the pip packages BUILD file.",
45    )
46    parser.add_argument(
47        "--enable_implicit_namespace_pkgs",
48        action="store_true",
49        help="Disables conversion of implicit namespace packages into pkg-util style packages.",
50    )
51    parser.add_argument(
52        "--environment",
53        action="store",
54        help="Extra environment variables to set on the pip environment.",
55    )
56    parser.add_argument(
57        "--download_only",
58        action="store_true",
59        help="Use 'pip download' instead of 'pip wheel'. Disables building wheels from source, but allows use of "
60        "--platform, --python-version, --implementation, and --abi in --extra_pip_args.",
61    )
62    return parser
63
64
65def deserialize_structured_args(args):
66    """Deserialize structured arguments passed from the starlark rules.
67    Args:
68        args: dict of parsed command line arguments
69    """
70    structured_args = ("extra_pip_args", "pip_data_exclude", "environment")
71    for arg_name in structured_args:
72        if args.get(arg_name) is not None:
73            args[arg_name] = json.loads(args[arg_name])["arg"]
74        else:
75            args[arg_name] = []
76    return args
77