-
Notifications
You must be signed in to change notification settings - Fork 551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: provide access to arbitrary interpreters #2507
base: main
Are you sure you want to change the base?
Changes from all commits
f12f5c3
7507626
9730855
2c3fc1b
f819bb0
286b4f1
314447d
3fb3236
e07a528
91d66df
c71629f
2ee2311
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
load("//python:defs.bzl", "py_binary") | ||
load(":interpreter.bzl", "interpreter") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
filegroup( | ||
name = "distribution", | ||
srcs = glob(["**"]), | ||
visibility = ["//python:__pkg__"], | ||
) | ||
|
||
interpreter( | ||
name = "python", | ||
binary = ":python_src", | ||
) | ||
|
||
# The user can modify this flag to source different interpreters for the | ||
# `python` target above. | ||
label_flag( | ||
name = "python_src", | ||
build_setting_default = "//python:none", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
load("//python/private:interpreter.bzl", _interpeter="interpreter") | ||
|
||
interpreter = _interpeter |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
load("@bazel_skylib//lib:paths.bzl", "paths") | ||
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") | ||
load("//python:py_runtime_info.bzl", "PyRuntimeInfo") | ||
load(":sentinel.bzl", "SentinelInfo") | ||
load(":toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE") | ||
|
||
def _interpreter_impl(ctx): | ||
if SentinelInfo in ctx.attr.binary: | ||
toolchain = ctx.toolchains[TARGET_TOOLCHAIN_TYPE] | ||
runtime = toolchain.py3_runtime | ||
else: | ||
runtime = ctx.attr.binary[PyRuntimeInfo] | ||
|
||
# NOTE: We name the output filename after the underlying file name | ||
# because of things like pyenv: they use $0 to determine what to | ||
# re-exec. If it's not a recognized name, then they fail. | ||
if runtime.interpreter: | ||
executable = ctx.actions.declare_file(runtime.interpreter.basename) | ||
ctx.actions.symlink(output = executable, target_file = runtime.interpreter, is_executable = True) | ||
else: | ||
executable = ctx.actions.declare_symlink(paths.basename(runtime.interpreter_path)) | ||
ctx.actions.symlink(output = executable, target_path = runtime.interpreter_path) | ||
|
||
return [ | ||
DefaultInfo( | ||
executable = executable, | ||
runfiles = ctx.runfiles([executable], transitive_files = runtime.files), | ||
), | ||
] | ||
|
||
interpreter = rule( | ||
implementation = _interpreter_impl, | ||
toolchains = [TARGET_TOOLCHAIN_TYPE], | ||
executable = True, | ||
attrs = { | ||
"binary": attr.label( | ||
mandatory = True, | ||
), | ||
}, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
module(name = "module_under_test") | ||
|
||
bazel_dep(name = "rules_python", version = "0.0.0") | ||
local_path_override( | ||
module_name = "rules_python", | ||
path = "../../..", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
import unittest | ||
|
||
from tests.integration import runner | ||
|
||
|
||
class InterpreterTest(runner.TestCase): | ||
def _run_version_test(self, expected_version): | ||
"""Validates that we can successfully execute arbitrary code from the CLI.""" | ||
result = self.run_bazel( | ||
"run", | ||
f"--@rules_python//python/config_settings:python_version={expected_version}", | ||
"@rules_python//python/bin:python", | ||
input = "\r".join([ | ||
"import sys", | ||
"v = sys.version_info", | ||
"print(f'version: {v.major}.{v.minor}')", | ||
]), | ||
) | ||
self.assert_result_matches(result, f"version: {expected_version}") | ||
|
||
def test_run_interpreter_3_10(self): | ||
self._run_version_test("3.10") | ||
|
||
def test_run_interpreter_3_11(self): | ||
self._run_version_test("3.11") | ||
|
||
def test_run_interpreter_3_12(self): | ||
self._run_version_test("3.12") | ||
|
||
def _run_module_test(self, version): | ||
"""Validates that we can successfully invoke a module from the CLI.""" | ||
# Pass unformatted JSON to the json.tool module. | ||
result = self.run_bazel( | ||
"run", | ||
f"--@rules_python//python/config_settings:python_version={version}", | ||
"@rules_python//python/bin:python", | ||
"--", | ||
"-m", | ||
"json.tool", | ||
input = '{"json":"obj"}', | ||
) | ||
# Validate that we get formatted JSON back. | ||
self.assert_result_matches(result, r'{\n "json": "obj"\n}') | ||
|
||
def test_run_module_3_10(self): | ||
self._run_module_test("3.10") | ||
|
||
def test_run_module_3_11(self): | ||
self._run_module_test("3.11") | ||
|
||
def test_run_module_3_12(self): | ||
self._run_module_test("3.12") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great to add a test that imports:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless I'm misunderstanding something, that's currently not possible with the current implementation. The current implementation is purely there to get the interpreter, nothing else. I'd like to follow this PR up with another PR that enables the use of the REPL with a specific |
||
|
||
|
||
|
||
if __name__ == "__main__": | ||
# Enabling this makes the runner log subprocesses as the test goes along. | ||
# logging.basicConfig(level = "INFO") | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of a bazel-in-bazel integration test, use transitions to set the appropriate state. The BiB tests are slow and painful to run and debug. There is some code in //tests/support:sh_py_run_test.bzl that has most of this already you should be able to use with some modifications.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about that, but figured most use cases involve a
bazel run //python/bin:python
so I wanted to make sure that that's tested. But I can see the argument about it being annoying etc. Will change it.