Skip to content

Commit fd43414

Browse files
authored
pip_parse: Fix when using a python wrapper script (#505)
* pip_parse: Transmit the interpreter arguments * Clarify --python_interpreter_target doc
1 parent 7609526 commit fd43414

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

python/pip_install/parse_requirements_to_bzl/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@ def main() -> None:
135135
required=True,
136136
help="Path to fully resolved requirements.txt to use as the source of repos.",
137137
)
138+
parser.add_argument(
139+
"--python_interpreter",
140+
help="The python interpreter that will be used to download and unpack the wheels.",
141+
)
142+
parser.add_argument(
143+
"--python_interpreter_target",
144+
help="Bazel target of a python interpreter.\
145+
It will be used in repository rules so it must be an already built interpreter.\
146+
If set, it will take precedence over python_interpreter.",
147+
)
138148
parser.add_argument(
139149
"--quiet",
140150
type=coerce_to_bool,

python/pip_install/parse_requirements_to_bzl/parse_requirements_to_bzl_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def test_generated_requirements_bzl(self) -> None:
2626
pip_data_exclude = ["**.foo"]
2727
args.extra_pip_args = json.dumps({"arg": extra_pip_args})
2828
args.pip_data_exclude= json.dumps({"arg": pip_data_exclude})
29+
args.python_interpreter = "/custom/python3"
30+
args.python_interpreter_target = "@custom_python//:exec"
2931
args.environment= json.dumps({"arg": {}})
3032
contents = generate_parsed_requirements_contents(args)
3133
library_target = "@pip_parsed_deps_pypi__foo//:pkg"
@@ -38,6 +40,8 @@ def test_generated_requirements_bzl(self) -> None:
3840
all_flags = extra_pip_args + ["--require-hashes", "True"]
3941
self.assertIn("'extra_pip_args': {}".format(repr(all_flags)), contents, contents)
4042
self.assertIn("'pip_data_exclude': {}".format(repr(pip_data_exclude)), contents, contents)
43+
self.assertIn("'python_interpreter': '/custom/python3'", contents, contents)
44+
self.assertIn("'python_interpreter_target': '@custom_python//:exec'", contents, contents)
4145
# Assert it gets set to an empty dict by default.
4246
self.assertIn("'environment': {}", contents, contents)
4347

python/pip_install/pip_repository.bzl

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ def _construct_pypath(rctx):
2424
pypath = separator.join([str(p) for p in [rules_root] + thirdparty_roots])
2525
return pypath
2626

27+
def _resolve_python_interpreter(rctx):
28+
"""Helper function to find the python interpreter from the common attributes
29+
30+
Args:
31+
rctx: Handle to the rule repository context.
32+
Returns: Python interpreter path.
33+
"""
34+
python_interpreter = rctx.attr.python_interpreter
35+
if rctx.attr.python_interpreter_target != None:
36+
target = rctx.attr.python_interpreter_target
37+
python_interpreter = rctx.path(target)
38+
else:
39+
if "/" not in python_interpreter:
40+
python_interpreter = rctx.which(python_interpreter)
41+
if not python_interpreter:
42+
fail("python interpreter not found")
43+
return python_interpreter
44+
2745
def _parse_optional_attrs(rctx, args):
2846
"""Helper function to parse common attributes of pip_repository and whl_library repository rules.
2947
@@ -83,15 +101,7 @@ exports_files(["requirements.bzl"])
83101
"""
84102

85103
def _pip_repository_impl(rctx):
86-
python_interpreter = rctx.attr.python_interpreter
87-
if rctx.attr.python_interpreter_target != None:
88-
target = rctx.attr.python_interpreter_target
89-
python_interpreter = rctx.path(target)
90-
else:
91-
if "/" not in python_interpreter:
92-
python_interpreter = rctx.which(python_interpreter)
93-
if not python_interpreter:
94-
fail("python interpreter not found")
104+
python_interpreter = _resolve_python_interpreter(rctx)
95105

96106
if rctx.attr.incremental and not rctx.attr.requirements_lock:
97107
fail("Incremental mode requires a requirements_lock attribute be specified.")
@@ -114,6 +124,11 @@ def _pip_repository_impl(rctx):
114124
"--timeout",
115125
str(rctx.attr.timeout),
116126
]
127+
128+
if rctx.attr.python_interpreter:
129+
args += ["--python_interpreter", rctx.attr.python_interpreter]
130+
if rctx.attr.python_interpreter_target:
131+
args += ["--python_interpreter_target", str(rctx.attr.python_interpreter_target)]
117132
else:
118133
args = [
119134
python_interpreter,
@@ -266,10 +281,12 @@ py_binary(
266281
)
267282

268283
def _impl_whl_library(rctx):
284+
python_interpreter = _resolve_python_interpreter(rctx)
285+
269286
# pointer to parent repo so these rules rerun if the definitions in requirements.bzl change.
270287
_parent_repo_label = Label("@{parent}//:requirements.bzl".format(parent = rctx.attr.repo))
271288
args = [
272-
rctx.attr.python_interpreter,
289+
python_interpreter,
273290
"-m",
274291
"python.pip_install.parse_requirements_to_bzl.extract_single_wheel",
275292
"--requirement",

0 commit comments

Comments
 (0)