pip_parse: Fix when using a python wrapper script (#505)
* pip_parse: Transmit the interpreter arguments
* Clarify --python_interpreter_target doc
diff --git a/python/pip_install/parse_requirements_to_bzl/__init__.py b/python/pip_install/parse_requirements_to_bzl/__init__.py
index aa0f1b6..5163b3e 100644
--- a/python/pip_install/parse_requirements_to_bzl/__init__.py
+++ b/python/pip_install/parse_requirements_to_bzl/__init__.py
@@ -136,6 +136,16 @@
help="Path to fully resolved requirements.txt to use as the source of repos.",
)
parser.add_argument(
+ "--python_interpreter",
+ help="The python interpreter that will be used to download and unpack the wheels.",
+ )
+ parser.add_argument(
+ "--python_interpreter_target",
+ help="Bazel target of a python interpreter.\
+It will be used in repository rules so it must be an already built interpreter.\
+If set, it will take precedence over python_interpreter.",
+ )
+ parser.add_argument(
"--quiet",
type=coerce_to_bool,
default=True,
diff --git a/python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py b/python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py
index 0ac5668..65a19df 100644
--- a/python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py
+++ b/python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py
@@ -26,6 +26,8 @@
pip_data_exclude = ["**.foo"]
args.extra_pip_args = json.dumps({"arg": extra_pip_args})
args.pip_data_exclude= json.dumps({"arg": pip_data_exclude})
+ args.python_interpreter = "/custom/python3"
+ args.python_interpreter_target = "@custom_python//:exec"
args.environment= json.dumps({"arg": {}})
contents = generate_parsed_requirements_contents(args)
library_target = "@pip_parsed_deps_pypi__foo//:pkg"
@@ -38,6 +40,8 @@
all_flags = extra_pip_args + ["--require-hashes", "True"]
self.assertIn("'extra_pip_args': {}".format(repr(all_flags)), contents, contents)
self.assertIn("'pip_data_exclude': {}".format(repr(pip_data_exclude)), contents, contents)
+ self.assertIn("'python_interpreter': '/custom/python3'", contents, contents)
+ self.assertIn("'python_interpreter_target': '@custom_python//:exec'", contents, contents)
# Assert it gets set to an empty dict by default.
self.assertIn("'environment': {}", contents, contents)
diff --git a/python/pip_install/pip_repository.bzl b/python/pip_install/pip_repository.bzl
index 71d5f66..c3007e1 100644
--- a/python/pip_install/pip_repository.bzl
+++ b/python/pip_install/pip_repository.bzl
@@ -24,6 +24,24 @@
pypath = separator.join([str(p) for p in [rules_root] + thirdparty_roots])
return pypath
+def _resolve_python_interpreter(rctx):
+ """Helper function to find the python interpreter from the common attributes
+
+ Args:
+ rctx: Handle to the rule repository context.
+ Returns: Python interpreter path.
+ """
+ python_interpreter = rctx.attr.python_interpreter
+ if rctx.attr.python_interpreter_target != None:
+ target = rctx.attr.python_interpreter_target
+ python_interpreter = rctx.path(target)
+ else:
+ if "/" not in python_interpreter:
+ python_interpreter = rctx.which(python_interpreter)
+ if not python_interpreter:
+ fail("python interpreter not found")
+ return python_interpreter
+
def _parse_optional_attrs(rctx, args):
"""Helper function to parse common attributes of pip_repository and whl_library repository rules.
@@ -83,15 +101,7 @@
"""
def _pip_repository_impl(rctx):
- python_interpreter = rctx.attr.python_interpreter
- if rctx.attr.python_interpreter_target != None:
- target = rctx.attr.python_interpreter_target
- python_interpreter = rctx.path(target)
- else:
- if "/" not in python_interpreter:
- python_interpreter = rctx.which(python_interpreter)
- if not python_interpreter:
- fail("python interpreter not found")
+ python_interpreter = _resolve_python_interpreter(rctx)
if rctx.attr.incremental and not rctx.attr.requirements_lock:
fail("Incremental mode requires a requirements_lock attribute be specified.")
@@ -114,6 +124,11 @@
"--timeout",
str(rctx.attr.timeout),
]
+
+ if rctx.attr.python_interpreter:
+ args += ["--python_interpreter", rctx.attr.python_interpreter]
+ if rctx.attr.python_interpreter_target:
+ args += ["--python_interpreter_target", str(rctx.attr.python_interpreter_target)]
else:
args = [
python_interpreter,
@@ -266,10 +281,12 @@
)
def _impl_whl_library(rctx):
+ python_interpreter = _resolve_python_interpreter(rctx)
+
# pointer to parent repo so these rules rerun if the definitions in requirements.bzl change.
_parent_repo_label = Label("@{parent}//:requirements.bzl".format(parent = rctx.attr.repo))
args = [
- rctx.attr.python_interpreter,
+ python_interpreter,
"-m",
"python.pip_install.parse_requirements_to_bzl.extract_single_wheel",
"--requirement",