-
-
Notifications
You must be signed in to change notification settings - Fork 636
feat: provide access to arbitrary interpreters #2507
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
Merged
rickeylev
merged 42 commits into
bazel-contrib:main
from
philsc:unreviewed/philsc/interpreter
Feb 16, 2025
Merged
Changes from 10 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
f12f5c3
wip: helper to run an arbitrary interpreter or interpreter from a binary
rickeylev 7507626
Basic REPL support
philsc 9730855
experiment with site packages
philsc 2c3fc1b
Merge remote-tracking branch 'upstream/main' into HEAD
philsc f819bb0
Merge remote-tracking branch 'rickeylev/feat.tools.run' into HEAD
philsc 286b4f1
consolidate files a bit
philsc 314447d
revert REPL-related things
philsc 3fb3236
Merge remote-tracking branch 'upstream/main' into HEAD
philsc e07a528
add integration test
philsc 91d66df
add some docs
philsc c71629f
rename to :python and incorporate readme feedback
philsc 2ee2311
move implementation to //python/private
philsc 620bf31
Add a test using transitions instead of an integration test
philsc d4b7978
Add python_src tests
philsc c2f927f
Merge remote-tracking branch 'upstream/main' into HEAD
philsc 7ede58a
black
philsc 8cd6de0
delete integration test
philsc 8c2495b
delete unused files
philsc d7bce25
delete unused files
philsc d0c8304
delete unused files
philsc 63924fe
revert unused changes
philsc 94d5ee3
pre-commit
philsc 8e935f4
Fix Windows hopefully
philsc d39a0a1
fixup
philsc abcc5e9
fix: Enable location expansion for `sh_py_run_test`
philsc 0c365e9
Merge commit 'abcc5e9d' into HEAD
philsc 81b26c6
buildifier fixes hopefully
philsc cfd96c8
delete superfluous runfiles
philsc 46b8f89
Merge remote-tracking branch 'upstream/main' into HEAD
philsc cadfc9a
get remote execution working
philsc 155489f
incorporate feedback
philsc 83a774d
buildifier
philsc 4d15a73
explore using ctx.actions.declare_symlink
philsc 0b8920d
Merge remote-tracking branch 'origin/main' into HEAD
philsc 77a75d7
revert unnecessary change
philsc 5eae601
Merge remote-tracking branch 'origin/main' into HEAD
philsc ddcf738
incorporate feedback
philsc faa37b3
add missing file
philsc 682762d
lint
philsc 69d647b
add docstring
philsc ebe1842
simplify the code a bit
philsc ccf7715
use full runfiles path; add basic docs
rickeylev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
philsc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
filegroup( | ||
name = "distribution", | ||
srcs = glob(["**"]), | ||
visibility = ["//python:__pkg__"], | ||
philsc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) | ||
|
||
interpreter( | ||
name = "interpreter", | ||
binary = ":interpreter_src", | ||
) | ||
|
||
# The user can modify this flag to source different interpreters for the | ||
# `interpreter` target above. | ||
label_flag( | ||
name = "interpreter_src", | ||
build_setting_default = "//python:none", | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
load("@bazel_skylib//lib:paths.bzl", "paths") | ||
philsc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") | ||
load("//python:py_runtime_info.bzl", "PyRuntimeInfo") | ||
load("//python/private:sentinel.bzl", "SentinelInfo") | ||
load("//python/private: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, | ||
), | ||
}, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "../../..", | ||
) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:interpreter", | ||
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:interpreter", | ||
"--", | ||
"-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") | ||
rickeylev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
|
||
|
||
if __name__ == "__main__": | ||
# Enabling this makes the runner log subprocesses as the test goes along. | ||
# logging.basicConfig(level = "INFO") | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.