Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 Oct 30, 2024
7507626
Basic REPL support
philsc Nov 27, 2024
9730855
experiment with site packages
philsc Dec 1, 2024
2c3fc1b
Merge remote-tracking branch 'upstream/main' into HEAD
philsc Dec 1, 2024
f819bb0
Merge remote-tracking branch 'rickeylev/feat.tools.run' into HEAD
philsc Dec 1, 2024
286b4f1
consolidate files a bit
philsc Dec 1, 2024
314447d
revert REPL-related things
philsc Dec 15, 2024
3fb3236
Merge remote-tracking branch 'upstream/main' into HEAD
philsc Dec 15, 2024
e07a528
add integration test
philsc Dec 16, 2024
91d66df
add some docs
philsc Dec 16, 2024
c71629f
rename to :python and incorporate readme feedback
philsc Jan 6, 2025
2ee2311
move implementation to //python/private
philsc Jan 6, 2025
620bf31
Add a test using transitions instead of an integration test
philsc Jan 24, 2025
d4b7978
Add python_src tests
philsc Jan 24, 2025
c2f927f
Merge remote-tracking branch 'upstream/main' into HEAD
philsc Jan 24, 2025
7ede58a
black
philsc Jan 24, 2025
8cd6de0
delete integration test
philsc Jan 24, 2025
8c2495b
delete unused files
philsc Jan 24, 2025
d7bce25
delete unused files
philsc Jan 24, 2025
d0c8304
delete unused files
philsc Jan 24, 2025
63924fe
revert unused changes
philsc Jan 24, 2025
94d5ee3
pre-commit
philsc Jan 24, 2025
8e935f4
Fix Windows hopefully
philsc Jan 26, 2025
d39a0a1
fixup
philsc Jan 26, 2025
abcc5e9
fix: Enable location expansion for `sh_py_run_test`
philsc Jan 26, 2025
0c365e9
Merge commit 'abcc5e9d' into HEAD
philsc Jan 26, 2025
81b26c6
buildifier fixes hopefully
philsc Jan 26, 2025
cfd96c8
delete superfluous runfiles
philsc Jan 26, 2025
46b8f89
Merge remote-tracking branch 'upstream/main' into HEAD
philsc Feb 4, 2025
cadfc9a
get remote execution working
philsc Feb 4, 2025
155489f
incorporate feedback
philsc Feb 4, 2025
83a774d
buildifier
philsc Feb 4, 2025
4d15a73
explore using ctx.actions.declare_symlink
philsc Feb 10, 2025
0b8920d
Merge remote-tracking branch 'origin/main' into HEAD
philsc Feb 10, 2025
77a75d7
revert unnecessary change
philsc Feb 10, 2025
5eae601
Merge remote-tracking branch 'origin/main' into HEAD
philsc Feb 16, 2025
ddcf738
incorporate feedback
philsc Feb 16, 2025
faa37b3
add missing file
philsc Feb 16, 2025
682762d
lint
philsc Feb 16, 2025
69d647b
add docstring
philsc Feb 16, 2025
ebe1842
simplify the code a bit
philsc Feb 16, 2025
ccf7715
use full runfiles path; add basic docs
rickeylev Feb 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/bin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ _interpreter_binary(
name = "python",
binary = ":python_src",
visibility = ["//visibility:public"],
target_compatible_with = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

# The user can modify this flag to source different interpreters for the
Expand Down
16 changes: 16 additions & 0 deletions python/private/interpreter.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load("@bazel_skylib//lib:paths.bzl", "paths")
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
load(":sentinel.bzl", "SentinelInfo")
load(":toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
load(":py_executable.bzl", "relative_path", "runfiles_root_path")

def _interpreter_binary_impl(ctx):
if SentinelInfo in ctx.attr.binary:
Expand All @@ -30,6 +31,13 @@ def _interpreter_binary_impl(ctx):
# 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:
# Option 1:
# Works locally, but not remotely.
#executable = ctx.actions.declare_file(runtime.interpreter.basename)
#ctx.actions.symlink(output = executable, target_file = runtime.interpreter)

# Option 2:
# Works locally, and remotely, but not on Windows.
executable = ctx.actions.declare_file(runtime.interpreter.basename)
ctx.actions.expand_template(
template = ctx.file._template,
Expand All @@ -39,6 +47,14 @@ def _interpreter_binary_impl(ctx):
},
is_executable = True,
)

# Option 3:
# Works locally and remotely, but only in runfiles, doesn't work via "bazel run".
#executable = ctx.actions.declare_symlink(runtime.interpreter.basename)
#ctx.actions.symlink(output = executable, target_path = relative_path(
# from_ = paths.dirname(runfiles_root_path(ctx, executable.short_path)),
# to = runfiles_root_path(ctx, runtime.interpreter.short_path),
#))
else:
executable = ctx.actions.declare_symlink(paths.basename(runtime.interpreter_path))
ctx.actions.symlink(output = executable, target_path = runtime.interpreter_path)
Expand Down
12 changes: 6 additions & 6 deletions python/private/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def _create_executable(
)

def _create_zip_main(ctx, *, stage2_bootstrap, runtime_details, venv):
python_binary = _runfiles_root_path(ctx, venv.interpreter.short_path)
python_binary = runfiles_root_path(ctx, venv.interpreter.short_path)
python_binary_actual = venv.interpreter_actual_path

# The location of this file doesn't really matter. It's added to
Expand Down Expand Up @@ -522,7 +522,7 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):

if not venvs_use_declare_symlink_enabled:
if runtime.interpreter:
interpreter_actual_path = _runfiles_root_path(ctx, runtime.interpreter.short_path)
interpreter_actual_path = runfiles_root_path(ctx, runtime.interpreter.short_path)
else:
interpreter_actual_path = runtime.interpreter_path

Expand All @@ -543,11 +543,11 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
# may choose to write what symlink() points to instead.
interpreter = ctx.actions.declare_symlink("{}/bin/{}".format(venv, py_exe_basename))

interpreter_actual_path = _runfiles_root_path(ctx, runtime.interpreter.short_path)
interpreter_actual_path = runfiles_root_path(ctx, runtime.interpreter.short_path)
rel_path = relative_path(
# dirname is necessary because a relative symlink is relative to
# the directory the symlink resides within.
from_ = paths.dirname(_runfiles_root_path(ctx, interpreter.short_path)),
from_ = paths.dirname(runfiles_root_path(ctx, interpreter.short_path)),
to = interpreter_actual_path,
)

Expand Down Expand Up @@ -646,7 +646,7 @@ def _create_stage2_bootstrap(
)
return output

def _runfiles_root_path(ctx, short_path):
def runfiles_root_path(ctx, short_path):
"""Compute a runfiles-root relative path from `File.short_path`

Args:
Expand Down Expand Up @@ -676,7 +676,7 @@ def _create_stage1_bootstrap(
runtime = runtime_details.effective_runtime

if venv:
python_binary_path = _runfiles_root_path(ctx, venv.interpreter.short_path)
python_binary_path = runfiles_root_path(ctx, venv.interpreter.short_path)
else:
python_binary_path = runtime_details.executable_interpreter_path

Expand Down
1 change: 0 additions & 1 deletion python/private/stage2_bootstrap_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ def main():

if os.environ.get("COVERAGE_DIR"):
import _bazel_site_init

coverage_enabled = _bazel_site_init.COVERAGE_SETUP
else:
coverage_enabled = False
Expand Down
67 changes: 16 additions & 51 deletions tests/support/sh_py_run_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,23 @@ def _perform_transition_impl(input_settings, attr, base_impl):
settings["//python/config_settings:bootstrap_impl"] = attr.bootstrap_impl
if attr.extra_toolchains:
settings["//command_line_option:extra_toolchains"] = attr.extra_toolchains
if attr.python_version:
settings["//python/config_settings:python_version"] = attr.python_version
if attr.python_src:
settings["//python/bin:python_src"] = attr.python_src
if attr.venvs_use_declare_symlink:
settings["//python/config_settings:venvs_use_declare_symlink"] = attr.venvs_use_declare_symlink
return settings

_perform_transition = transition(
implementation = _perform_transition_impl,
inputs = [
"//python/config_settings:bootstrap_impl",
"//command_line_option:extra_toolchains",
"//python/config_settings:python_version",
"//python/bin:python_src",
"//python/config_settings:venvs_use_declare_symlink",
],
outputs = [
"//command_line_option:build_python_zip",
"//command_line_option:extra_toolchains",
"//python/config_settings:bootstrap_impl",
"//python/config_settings:python_version",
"//python/bin:python_src",
"//python/config_settings:venvs_use_declare_symlink",
VISIBLE_FOR_TESTING,
],
)
_RECONFIG_INPUTS = [
"//python/config_settings:bootstrap_impl",
"//python/bin:python_src",
"//command_line_option:extra_toolchains",
"//python/config_settings:venvs_use_declare_symlink",
]
_RECONFIG_OUTPUTS = _RECONFIG_INPUTS + [
"//command_line_option:build_python_zip",
VISIBLE_FOR_TESTING,
]
_RECONFIG_INHERITED_OUTPUTS = [v for v in _RECONFIG_OUTPUTS if v in _RECONFIG_INPUTS]

_RECONFIG_ATTRS = {
"bootstrap_impl": attr.string(),
Expand All @@ -74,21 +64,10 @@ NOTE: You'll likely have to also specify //tests/support/cc_toolchains:all (or s
to make the RBE presubmits happy, which disable auto-detection of a CC
toolchain.
""",
),
"python_src": attr.label(),
"python_version": attr.string(),
"target": attr.label(executable = True, cfg = "target"),
"venvs_use_declare_symlink": attr.string(),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
}
return rule(
implementation = _py_reconfig_impl,
attrs = attrs,
cfg = _perform_transition,
**kwargs
)
),
"python_src": attr.label(),
"venvs_use_declare_symlink": attr.string(),
}

def _create_reconfig_rule(builder):
builder.attrs.update(_RECONFIG_ATTRS)
Expand All @@ -98,21 +77,7 @@ def _create_reconfig_rule(builder):
builder.cfg.inputs.update(_RECONFIG_INPUTS)
builder.cfg.outputs.update(_RECONFIG_OUTPUTS)

def _py_reconfig_executable(*, name, py_reconfig_rule, py_inner_rule, **kwargs):
reconfig_only_kwarg_names = [
# keep sorted
"bootstrap_impl",
"build_python_zip",
"extra_toolchains",
"python_version",
"python_src",
"venvs_use_declare_symlink",
]
reconfig_kwargs = {
key: kwargs.pop(key, None)
for key in reconfig_only_kwarg_names
}
reconfig_kwargs["target_compatible_with"] = kwargs.get("target_compatible_with")
return builder.build()

_py_reconfig_binary = _create_reconfig_rule(create_binary_rule_builder())

Expand Down
Loading