From 09e013d3cd89803b412b1001662b16844b45788b Mon Sep 17 00:00:00 2001 From: Jonathon Belotti Date: Wed, 11 May 2022 14:26:28 +1000 Subject: [PATCH 1/2] Add ability for Bazel >=5.0.0 users to use downloaded py interpreter in py_binary stub script. More hermetic --- python/repositories.bzl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/python/repositories.bzl b/python/repositories.bzl index bd08ec466c..a4f305fac3 100644 --- a/python/repositories.bzl +++ b/python/repositories.bzl @@ -35,6 +35,13 @@ def py_repositories(): # Remaining content of the file is only used to support toolchains. ######## +# Parse the bazel version string from `native.bazel_version`. +# e.g. +# "0.10.0rc1 abc123d" => (0, 10, 0) +# "0.3.0" => (0, 3, 0) +def _parse_native_bazel_version(bazel_version): + return tuple([int(n) for n in bazel_version.split(".")]) + def _python_repository_impl(rctx): if rctx.attr.distutils and rctx.attr.distutils_content: fail("Only one of (distutils, distutils_content) should be set.") @@ -104,6 +111,13 @@ def _python_repository_impl(rctx): fail(exec_result.stderr) python_bin = "python.exe" if ("windows" in platform) else "bin/python3" + # An empty shebang inputs falls back to PyRuntimeInfoApi.DEFAULT_STUB_SHEBANG. + print(native.bazel_version) + if _parse_native_bazel_version(native.bazel_version) >= (5,0,0): + abs_python_bin_path = rctx.path(python_bin) + stub_shebang_assignment = "stub_shebang = \"#!{}\"".format(abs_python_bin_path) + else: + stub_shebang_assignment = "" build_content = """\ # Generated by python/repositories.bzl @@ -154,6 +168,7 @@ py_runtime( files = [":files"], interpreter = "{python_path}", python_version = "PY3", + {stub_shebang_assignment} ) py_runtime_pair( @@ -164,6 +179,7 @@ py_runtime_pair( """.format( python_path = python_bin, python_version = python_short_version, + stub_shebang_assignment = stub_shebang_assignment, ) rctx.file("BUILD.bazel", build_content) From 7fecaaa5fb5f5a4c7ba0d270b0d2dcd49442523e Mon Sep 17 00:00:00 2001 From: Jonathon Belotti Date: Wed, 11 May 2022 14:34:51 +1000 Subject: [PATCH 2/2] remove obsolete comment --- python/repositories.bzl | 1 - 1 file changed, 1 deletion(-) diff --git a/python/repositories.bzl b/python/repositories.bzl index a4f305fac3..525fbc8408 100644 --- a/python/repositories.bzl +++ b/python/repositories.bzl @@ -111,7 +111,6 @@ def _python_repository_impl(rctx): fail(exec_result.stderr) python_bin = "python.exe" if ("windows" in platform) else "bin/python3" - # An empty shebang inputs falls back to PyRuntimeInfoApi.DEFAULT_STUB_SHEBANG. print(native.bazel_version) if _parse_native_bazel_version(native.bazel_version) >= (5,0,0): abs_python_bin_path = rctx.path(python_bin)