Skip to content

Commit

Permalink
Allow the --isolated pip flag to be optionally unset (#512)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
UebelAndre and hrfuller authored Sep 7, 2021
1 parent bef2244 commit 9f59762
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
4 changes: 3 additions & 1 deletion python/pip_install/extract_wheels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def main() -> None:
arguments.deserialize_structured_args(deserialized_args)

pip_args = (
[sys.executable, "-m", "pip", "--isolated", "wheel", "-r", args.requirements] +
[sys.executable, "-m", "pip"] +
(["--isolated"] if args.isolated else []) +
["wheel", "-r", args.requirements] +
deserialized_args["extra_pip_args"]
)

Expand Down
3 changes: 3 additions & 0 deletions python/pip_install/extract_wheels/lib/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def parse_common_args(parser: ArgumentParser) -> ArgumentParser:
required=True,
help="The external repo name to install dependencies. In the format '@{REPO_NAME}'",
)
parser.add_argument(
"--isolated", action="store_true", help="Whether or not to include the `--isolated` pip flag.",
)
parser.add_argument(
"--extra_pip_args", action="store", help="Extra arguments to pass down to pip.",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def main() -> None:
configure_reproducible_wheels()

pip_args = (
[sys.executable, "-m", "pip", "--isolated", "wheel", "--no-deps"] +
[sys.executable, "-m", "pip"] +
(["--isolated"] if args.isolated else []) +
["wheel", "--no-deps"] +
deserialized_args["extra_pip_args"]
)

Expand Down
28 changes: 28 additions & 0 deletions python/pip_install/pip_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ def _parse_optional_attrs(rctx, args):
Returns: Augmented args list.
"""

# Determine whether or not to pass the pip `--isloated` flag to the pip invocation
use_isolated = rctx.attr.isolated

# The environment variable will take precedence over the attribute
isolated_env = rctx.os.environ.get("RULES_PYTHON_PIP_ISOLATED", None)
if isolated_env != None:
if isolated_env.lower() in ("0", "false"):
use_isolated = False
else:
use_isolated = True

if use_isolated:
args.append("--isolated")

# Check for None so we use empty default types from our attrs.
# Some args want to be list, and some want to be dict.
if rctx.attr.extra_pip_args != None:
Expand Down Expand Up @@ -125,6 +139,10 @@ def _pip_repository_impl(rctx):

return

common_env = [
"RULES_PYTHON_PIP_ISOLATED",
]

common_attrs = {
"enable_implicit_namespace_pkgs": attr.bool(
default = False,
Expand All @@ -149,6 +167,14 @@ can be passed.
"extra_pip_args": attr.string_list(
doc = "Extra arguments to pass on to pip. Must not contain spaces.",
),
"isolated": attr.bool(
doc = """\
Whether or not to pass the [--isolated](https://pip.pypa.io/en/stable/cli/pip/#cmdoption-isolated) flag to
the underlying pip command. Alternatively, the `RULES_PYTHON_PIP_ISOLATED` enviornment varaible can be used
to control this flag.
""",
default = True,
),
"pip_data_exclude": attr.string_list(
doc = "Additional data exclusion parameters to add to the pip packages BUILD file.",
),
Expand Down Expand Up @@ -236,6 +262,7 @@ py_binary(
```
""",
implementation = _pip_repository_impl,
environ = common_env,
)

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

0 comments on commit 9f59762

Please sign in to comment.