|
| 1 | +"""Materialises the resolved exec-tools toolchain payload for assertion. |
| 2 | +
|
| 3 | +Reads the fields rules_python's consumers access — `exec_tools.exec_runtime` |
| 4 | +and `exec_tools.precompiler` — so the test pins that rules_py's registration |
| 5 | +under rules_python's toolchain type keeps their expected shape. |
| 6 | +""" |
| 7 | + |
| 8 | +EXEC_TOOLS_TOOLCHAIN = "@rules_python//python:exec_tools_toolchain_type" |
| 9 | + |
| 10 | +def _exec_tools_facts_impl(ctx): |
| 11 | + exec_tools = ctx.toolchains[EXEC_TOOLS_TOOLCHAIN].exec_tools |
| 12 | + out = ctx.actions.declare_file(ctx.label.name + ".txt") |
| 13 | + ctx.actions.write(out, "{}\n{}\n".format( |
| 14 | + exec_tools.exec_runtime.interpreter.path, |
| 15 | + exec_tools.precompiler, |
| 16 | + )) |
| 17 | + return [DefaultInfo(files = depset([out]))] |
| 18 | + |
| 19 | +exec_tools_facts = rule( |
| 20 | + implementation = _exec_tools_facts_impl, |
| 21 | + toolchains = [EXEC_TOOLS_TOOLCHAIN], |
| 22 | +) |
| 23 | + |
| 24 | +def _set_python_version_impl(settings, attr): |
| 25 | + # Both flags together so the uv constraints mismatch guard stays satisfied. |
| 26 | + return { |
| 27 | + "@aspect_rules_py//py/private/interpreter:python_version": attr.python_version, |
| 28 | + "@rules_python//python/config_settings:python_version": attr.python_version, |
| 29 | + } |
| 30 | + |
| 31 | +_set_python_version = transition( |
| 32 | + implementation = _set_python_version_impl, |
| 33 | + inputs = [], |
| 34 | + outputs = [ |
| 35 | + "@aspect_rules_py//py/private/interpreter:python_version", |
| 36 | + "@rules_python//python/config_settings:python_version", |
| 37 | + ], |
| 38 | +) |
| 39 | + |
| 40 | +def _with_python_version_impl(ctx): |
| 41 | + return [DefaultInfo(files = depset(transitive = [ |
| 42 | + dep[DefaultInfo].files |
| 43 | + for dep in ctx.attr.deps |
| 44 | + ]))] |
| 45 | + |
| 46 | +with_python_version = rule( |
| 47 | + doc = """Forwards deps analyzed under an explicit Python version. |
| 48 | +
|
| 49 | +py_* data edges reset the Python flags to the caller baseline (#1294), so a |
| 50 | +data file that must be produced under a specific version pins it here rather |
| 51 | +than inheriting the consuming terminal's transition.""", |
| 52 | + implementation = _with_python_version_impl, |
| 53 | + attrs = { |
| 54 | + "deps": attr.label_list(mandatory = True), |
| 55 | + "python_version": attr.string(mandatory = True), |
| 56 | + "_allowlist_function_transition": attr.label( |
| 57 | + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", |
| 58 | + ), |
| 59 | + }, |
| 60 | + cfg = _set_python_version, |
| 61 | +) |
0 commit comments