Get an interpreter from toolchains
This patch teaches Zapp how to read Python toolchains to get an interpreter, rather than hard-coding "python3" somewhat opaquely. This enables the use of hermetic or otherwise selected interpreters. Fixes #2
This commit is contained in:
parent
e5ff423318
commit
edd7dd0103
3 changed files with 51 additions and 6 deletions
|
@ -3,12 +3,39 @@ load("@rules_zapp//zapp:zapp.bzl",
|
|||
"zapp_test",
|
||||
)
|
||||
|
||||
load("@rules_python//python:defs.bzl", "py_library")
|
||||
load("@rules_python//python:defs.bzl",
|
||||
"py_library",
|
||||
"py_runtime_pair"
|
||||
)
|
||||
|
||||
load("@my_deps//:requirements.bzl",
|
||||
py_requirement="requirement",
|
||||
)
|
||||
|
||||
# Configuring a Python runtime
|
||||
|
||||
py_runtime(
|
||||
name = "python3_runtime",
|
||||
files = [],
|
||||
interpreter_path = "/usr/bin/python3",
|
||||
python_version = "PY3",
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
py_runtime_pair(
|
||||
name = "python_runtime",
|
||||
py2_runtime = None,
|
||||
py3_runtime = ":python3_runtime",
|
||||
)
|
||||
|
||||
toolchain(
|
||||
name = "python3_toolchain",
|
||||
toolchain = ":python_runtime",
|
||||
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
|
||||
)
|
||||
|
||||
# Zapp examples & tests
|
||||
|
||||
zapp_test(
|
||||
name = "hello_script",
|
||||
main = "hello.py",
|
||||
|
|
|
@ -29,6 +29,8 @@ git_repository(
|
|||
tag = "0.3.0",
|
||||
)
|
||||
|
||||
register_toolchains("//:python3_toolchain")
|
||||
|
||||
# git_repository(
|
||||
# name = "rules_zapp",
|
||||
# remote = "https://github.com/arrdem/rules_zapp.git",
|
||||
|
|
|
@ -121,10 +121,20 @@ def _zapp_impl(ctx):
|
|||
|
||||
# Write the list to the manifest file
|
||||
manifest_file = ctx.actions.declare_file(ctx.label.name + ".zapp-manifest.json")
|
||||
|
||||
# Figure out the Python 3 toolchain to use
|
||||
runtime = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"].py3_runtime
|
||||
if runtime.interpreter_path:
|
||||
py3 = runtime.interpreter_path
|
||||
elif runtime.interpreter:
|
||||
py3 = runtime.interpreter.path
|
||||
else:
|
||||
fail("No Python 3 toolchain available")
|
||||
|
||||
ctx.actions.write(
|
||||
output = manifest_file,
|
||||
content = json.encode({
|
||||
"shebang": ctx.attr.shebang,
|
||||
"shebang": ctx.attr.shebang.replace("%py3%", py3),
|
||||
"sources": {d: {"hashes": [], "source": s} for d, s in sources_map.items()},
|
||||
"zip_safe": ctx.attr.zip_safe,
|
||||
"prelude_points": ctx.attr.prelude_points,
|
||||
|
@ -172,7 +182,7 @@ _zapp_attrs = {
|
|||
executable = True,
|
||||
cfg = "host",
|
||||
),
|
||||
"shebang": attr.string(default = "/usr/bin/env python3"),
|
||||
"shebang": attr.string(default = "/usr/bin/env %py3%"),
|
||||
"zip_safe": attr.bool(default = True),
|
||||
"root_import": attr.bool(default = False),
|
||||
}
|
||||
|
@ -181,6 +191,9 @@ _zapp = rule(
|
|||
attrs = _zapp_attrs,
|
||||
executable = True,
|
||||
implementation = _zapp_impl,
|
||||
toolchains = [
|
||||
"@bazel_tools//tools/python:toolchain_type",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
|
@ -193,7 +206,7 @@ def zapp_binary(name,
|
|||
test=False,
|
||||
compiler=None,
|
||||
zip_safe=True,
|
||||
rule=_zapp,
|
||||
_rule=_zapp,
|
||||
**kwargs):
|
||||
"""A self-contained, single-file Python program, with a .zapp file extension.
|
||||
|
||||
|
@ -240,7 +253,7 @@ def zapp_binary(name,
|
|||
**kwargs
|
||||
)
|
||||
|
||||
rule(
|
||||
_rule(
|
||||
name = name,
|
||||
src = name + ".lib",
|
||||
compiler = compiler,
|
||||
|
@ -256,10 +269,13 @@ _zapp_test = rule(
|
|||
attrs = _zapp_attrs,
|
||||
test = True,
|
||||
implementation = _zapp_impl,
|
||||
toolchains = [
|
||||
"@bazel_tools//tools/python:toolchain_type",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def zapp_test(name, **kwargs):
|
||||
"""Same as zapp_binary, just sets the test=True bit."""
|
||||
|
||||
zapp_binary(name, rule=_zapp_test, **kwargs)
|
||||
zapp_binary(name, _rule=_zapp_test, **kwargs)
|
||||
|
|
Loading…
Reference in a new issue