From 51ff86b44efcd6f925be48358a5d3df0c0048022 Mon Sep 17 00:00:00 2001 From: thesayyn Date: Fri, 4 Oct 2024 14:35:56 -0700 Subject: [PATCH 1/3] fix: set runfiles and workdir for py3_image --- {{ .ProjectSnake }}/tools/oci/py3_image.bzl | 22 ++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/{{ .ProjectSnake }}/tools/oci/py3_image.bzl b/{{ .ProjectSnake }}/tools/oci/py3_image.bzl index 34a9709..eafcb1f 100644 --- a/{{ .ProjectSnake }}/tools/oci/py3_image.bzl +++ b/{{ .ProjectSnake }}/tools/oci/py3_image.bzl @@ -4,9 +4,28 @@ load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") load("@aspect_rules_py//py:defs.bzl", "py_image_layer") load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load") -def py3_image(name, binary, root = "/", layer_groups = {}, base = "@python_base"): +def py3_image(name, binary, root = "/", layer_groups = {}, env = {}, workdir = None, base = "@python_base"): + """Create a Python 3 image from a Python binary. + + Args: + name: The name of the image. + binary: The Python binary to create the image from. + root: The root directory where everything will be put into + layer_groups: The layer groups to use for the image. + env: The environment variables to set in the image. + workdir: The working directory to set in the image. + base: The base image to use for the image. + """ binary = native.package_relative_label(binary) binary_path = "{}{}/{}".format(root, binary.package, binary.name) + runfiles_dir = "{}.runfiles".format(binary_path) + repo_name = binary.repo_name or "_main" + env = dict({ + "BAZEL_WORKSPACE": repo_name, + "RUNFILES_DIR": runfiles_dir, + }, **env) + + workdir = "{}/{}".format(runfiles_dir, repo_name) oci_image( name = name + "_image", base = base, @@ -17,6 +36,7 @@ def py3_image(name, binary, root = "/", layer_groups = {}, base = "@python_base" layer_groups = layer_groups, ), entrypoint = [binary_path], + workdir = workdir, ) platform_transition_filegroup( name = name, From 1a4b7964b29c81e7aa2661500c2c274af5602c57 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Fri, 4 Oct 2024 14:40:18 -0700 Subject: [PATCH 2/3] Update py3_image.bzl --- {{ .ProjectSnake }}/tools/oci/py3_image.bzl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/{{ .ProjectSnake }}/tools/oci/py3_image.bzl b/{{ .ProjectSnake }}/tools/oci/py3_image.bzl index eafcb1f..00e7e45 100644 --- a/{{ .ProjectSnake }}/tools/oci/py3_image.bzl +++ b/{{ .ProjectSnake }}/tools/oci/py3_image.bzl @@ -25,7 +25,6 @@ def py3_image(name, binary, root = "/", layer_groups = {}, env = {}, workdir = N "RUNFILES_DIR": runfiles_dir, }, **env) - workdir = "{}/{}".format(runfiles_dir, repo_name) oci_image( name = name + "_image", base = base, @@ -36,7 +35,7 @@ def py3_image(name, binary, root = "/", layer_groups = {}, env = {}, workdir = N layer_groups = layer_groups, ), entrypoint = [binary_path], - workdir = workdir, + workdir = workdir or "{}/{}".format(runfiles_dir, repo_name), ) platform_transition_filegroup( name = name, From d8eec8d6bad2f06de794f647f526b56110294ebf Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Fri, 4 Oct 2024 15:01:09 -0700 Subject: [PATCH 3/3] Update py3_image.bzl --- {{ .ProjectSnake }}/tools/oci/py3_image.bzl | 1 + 1 file changed, 1 insertion(+) diff --git a/{{ .ProjectSnake }}/tools/oci/py3_image.bzl b/{{ .ProjectSnake }}/tools/oci/py3_image.bzl index 00e7e45..b866ce8 100644 --- a/{{ .ProjectSnake }}/tools/oci/py3_image.bzl +++ b/{{ .ProjectSnake }}/tools/oci/py3_image.bzl @@ -35,6 +35,7 @@ def py3_image(name, binary, root = "/", layer_groups = {}, env = {}, workdir = N layer_groups = layer_groups, ), entrypoint = [binary_path], + env = env, workdir = workdir or "{}/{}".format(runfiles_dir, repo_name), ) platform_transition_filegroup(