Skip to content

Commit

Permalink
Support python interpreter target in pip_repository
Browse files Browse the repository at this point in the history
This allows users to use an in-build python interpreter instead of
a system one with pip_repository.
The in-build python interpreter can be one that is used through
http_archive or built by another repository rule.

Port of bazelbuild/rules_python#312 made by @kku1993.
  • Loading branch information
gergelyfabian authored May 26, 2020
1 parent 3aacabb commit 766fa8b
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ DEFAULT_REPOSITORY_NAME = "pip"

def _pip_repository_impl(rctx):
python_interpreter = rctx.attr.python_interpreter
if '/' not in python_interpreter:
python_interpreter = rctx.which(python_interpreter)
if not python_interpreter:
fail("python interpreter not found")
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")

rctx.file("BUILD", "")

Expand Down Expand Up @@ -48,6 +52,12 @@ pip_repository = repository_rule(
"requirements": attr.label(allow_single_file=True, mandatory=True,),
"wheel_env": attr.string_dict(),
"python_interpreter": attr.string(default="python3"),
"python_interpreter_target": attr.label(allow_single_file = True, doc = """
If you are using a custom python interpreter built by another repository rule,
use this attribute to specify its BUILD target. This allows pip_repository to invoke
pip using the same interpreter as your toolchain. If set, takes precedence over
python_interpreter.
"""),
# 600 is documented as default here: https://docs.bazel.build/versions/master/skylark/lib/repository_ctx.html#execute
"timeout": attr.int(default = 600),
},
Expand Down

0 comments on commit 766fa8b

Please sign in to comment.