bazelbuild / rules_docker

Rules for building and handling Docker images with Bazel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support multiple python versions in py3_image's py_binary

jumbosushi opened this issue Β· comments

πŸš€ feature request

Relevant Rules

py3_image

Description

We have multiple versions of python toolchains installed with python_multi_register_toolchains

Current implementation of py3_image always imports py_binary from @rules_python//python:def.bzl. This results in py_binary using the default version used in python_multi_register_toolchains and there aren't ways to specify other python version.

py_binary(
name = binary_name,
python_version = "PY3",
deps = deps + layers,
exec_compatible_with = ["@io_bazel_rules_docker//platforms:run_in_container"],
**kwargs
)

Describe the solution you'd like

Not sure what the solution could look like πŸ€”

Describe alternatives you've considered

I'm using a temporarily patch to use other version's toolchain for now:

@@ -16,7 +16,9 @@
 The signature of this rule is compatible with py_binary.
 """
 
-load("@rules_python//python:defs.bzl", "py_binary")
+load("@rules_python//python:defs.bzl", py_binary_default = "py_binary")
+load("@python//3.9:defs.bzl", py_binary_3_9 = "py_binary")
+load("@python//3.11:defs.bzl", py_binary_3_11 = "py_binary")
 load(
     "//container:container.bzl",
     "container_pull",
@@ -73,7 +75,7 @@ DEFAULT_BASE = select({
     "//conditions:default": "@py3_image_base//image",
 })
 
-def py3_image(name, base = None, deps = [], layers = [], env = {}, **kwargs):
+def py3_image(name, base = None, deps = [], layers = [], env = {}, py_version = "", **kwargs):
     """Constructs a container image wrapping a py_binary target.
 
   Args:
@@ -83,6 +85,7 @@ def py3_image(name, base = None, deps = [], layers = [], env = {}, **kwargs):
     layers: Augments "deps" with dependencies that should be put into
            their own layers.
     env: Environment variables for the py_image.
+    py_version: Specific python binary version to use (e.g. 3.11)
     **kwargs: See py_binary.
   """
     binary_name = name + ".binary"
@@ -93,9 +96,14 @@ def py3_image(name, base = None, deps = [], layers = [], env = {}, **kwargs):
     # TODO(mattmoor): Consider using par_binary instead, so that
     # a single target can be used for all three.
 
+    py_binary = py_binary_default
+    if py_version == "3.9":
+      py_binary = py_binary_3_9
+    if py_version == "3.11":
+      py_binary = py_binary_3_11
+
     py_binary(
         name = binary_name,
-        python_version = "PY3",
         deps = deps + layers,
         exec_compatible_with = ["@io_bazel_rules_docker//platforms:run_in_container"],
         **kwargs

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days.
Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_docker!