Skip to content

Commit f12f5c3

Browse files
committed
wip: helper to run an arbitrary interpreter or interpreter from a binary
Run a specific interpreter: * `bazel run @rules_python//tools/run --@rules_python//python/config_settings:python_version=3.12` Run interpreter from a binary: * `bazel run @rules_python//tools/run --@rules_python//tools/run:bin=//my:binary`
1 parent 3e552df commit f12f5c3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tools/run/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load(":run.bzl", "interpreter")
2+
3+
interpreter(
4+
name = "run",
5+
)
6+
7+
label_flag(
8+
name = "bin",
9+
build_setting_default = "//python:none",
10+
)

tools/run/run.bzl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
load("@bazel_skylib//lib:paths.bzl", "paths")
2+
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
3+
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
4+
load("//python/private:sentinel.bzl", "SentinelInfo")
5+
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
6+
7+
def _interpreter_impl(ctx):
8+
if SentinelInfo in ctx.attr.binary:
9+
toolchain = ctx.toolchains[TARGET_TOOLCHAIN_TYPE]
10+
runtime = toolchain.py3_runtime
11+
else:
12+
runtime = ctx.attr.binary[PyRuntimeInfo]
13+
14+
# NOTE: We name the output filename after the underlying file name
15+
# because of things like pyenv: they use $0 to determine what to
16+
# re-exec. If it's not a recognized name, then they fail.
17+
if runtime.interpreter:
18+
executable = ctx.actions.declare_file(runtime.interpreter.basename)
19+
ctx.actions.symlink(output = executable, target_file = runtime.interpreter, is_executable = True)
20+
else:
21+
executable = ctx.actions.declare_symlink(paths.basename(runtime.interpreter_path))
22+
ctx.actions.symlink(output = executable, target_path = runtime.interpreter_path)
23+
24+
return [
25+
DefaultInfo(
26+
executable = executable,
27+
runfiles = ctx.runfiles([executable], transitive_files = runtime.files),
28+
),
29+
]
30+
31+
interpreter = rule(
32+
implementation = _interpreter_impl,
33+
toolchains = [TARGET_TOOLCHAIN_TYPE],
34+
executable = True,
35+
attrs = {
36+
"binary": attr.label(
37+
default = "//tools/run:bin",
38+
),
39+
},
40+
)

0 commit comments

Comments
 (0)