8000 Allow the `--isolated` pip flag to be optionally unset (#512) · lapointexavier/rules_python@9f59762 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f59762

Browse files
UebelAndrehrfuller
andauthored
Allow the --isolated pip flag to be optionally unset (bazel-contrib#512)
* Allow the `--isolated` flag to be optionally unset * Update python/pip_install/pip_repository.bzl Co-authored-by: Henry Fuller <hrofuller@gmail.com> Co-authored-by: Henry Fuller <hrofuller@gmail.com>
1 parent bef2244 commit 9f59762

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

python/pip_install/extract_wheels/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ def main() -> None:
6464
arguments.deserialize_structured_args(deserialized_args)
6565

6666
pip_args = (
67-
[sys.executable, "-m", "pip", "--isolated", "wheel", "-r", args.requirements] +
67+
[sys.executable, "-m", "pip"] +
68+
(["--isolated"] if args.isolated else []) +
69+
["wheel", "-r", args.requirements] +
6870
deserialized_args["extra_pip_args"]
6971
)
7072

python/pip_install/extract_wheels/lib/arguments.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ def parse_common_args(parser: ArgumentParser) -> ArgumentParser:
99
required=True,
1010
help="The external repo name to install dependencies. In the format '@{REPO_NAME}'",
1111
)
12+
parser.add_argument(
13+
"--isolated", action="store_true", help="Whether or not to include the `--isolated` pip flag.",
14+
)
1215
parser.add_argument(
1316
"--extra_pip_args", action="store", help="Extra arguments to pass down to pip.",
1417
)

python/pip_install/parse_requirements_to_bzl/extract_single_wheel/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def main() -> None:
2929
configure_reproducible_wheels()
3030

3131
pip_args = (
32-
[sys.executable, "-m", "pip", "--isolated", "wheel", "--no-deps"] +
32+
[sys.executable, "-m", "pip"] +
33+
(["--isolated"] if args.isolated else []) +
34+
["wheel", "--no-deps"] +
3335
deserialized_args["extra_pip_args"]
3436
)
3537

python/pip_install/pip_repository.bzl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ def _parse_optional_attrs(rctx, args):
3636
Returns: Augmented args list.
3737
"""
3838

39+
# Determine whether or not to pass the pip `--isloated` flag to the pip invocation
40+
use_isolated = rctx.attr.isolated
41+
42+
# The environment variable will take precedence over the attribute
43+
isolated_env = rctx.os.environ.get("RULES_PYTHON_PIP_ISOLATED", None)
44+
if isolated_env != None:
45+
if isolated_env.lower() in ("0", "false"):
46+
use_isolated = False
47+
else:
48+
use_isolated = True
49+
50+
if use_isolated:
51+
args.append("--isolated")
52+
3953
# Check for None so we use empty default types from our attrs.
4054
# Some args want to be list, and some want to be dict.
4155
if rctx.attr.extra_pip_args != None:
@@ -125,6 +139,10 @@ def _pip_repository_impl(rctx):
125139

126140
return
127141

142+
common_env = [
143+
"RULES_PYTHON_PIP_ISOLATED",
144+
]
145+
128146
common_attrs = {
129147
"enable_implicit_namespace_pkgs": attr.bool(
130148
default = False,
@@ -149,6 +167,14 @@ can be passed.
149167
"extra_pip_args": attr.string_list(
150168
doc = "Extra arguments to pass on to pip. Must not contain spaces.",
151169
),
170+
"isolated": attr.bool(
171+
doc = """\
172+
Whether or not to pass the [--isolated](https://pip.pypa.io/en/stable/cli/pip/#cmdoption-isolated) flag to
173+
the underlying pip command. Alternatively, the `RULES_PYTHON_PIP_ISOLATED` enviornment varaible can be used
174+
to control this flag.
175+
""",
176+
default = True,
177+
),
152178
"pip_data_exclude": attr.string_list(
153179
doc = "Additional data exclusion parameters to add to the pip packages BUILD file.",
154180
),
@@ -236,6 +262,7 @@ py_binary(
236262
```
237263
""",
238264
implementation = _pip_repository_impl,
265+
environ = common_env,
239266
)
240267

241268
def _impl_whl_library(rctx):
@@ -284,4 +311,5 @@ whl_library = repository_rule(
284311
Download and extracts a single wheel based into a bazel repo based on the requirement string passed in.
285312
Instantiated from pip_repository and inherits config options from there.""",
286313
implementation = _impl_whl_library,
314+
environ = common_env,
287315
)

0 commit comments

Comments
 (0)
0