From 68d1290c08b561008923901863f3ca2067bb3a31 Mon Sep 17 00:00:00 2001 From: Mathias Laurin Date: Thu, 19 Dec 2024 23:18:55 +0100 Subject: [PATCH] rules_uv: allow adding local wheels to create_venv Change-Id: I2b74efa2db79d822c9a07a7edc4dfe1338360c49 --- BUILD | 1 + MODULE.bazel | 1 + MODULE.bazel.lock | 25 +++---- bazel/patches/rules_uv_create_venv_whls.patch | 69 +++++++++++++++++++ 4 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 bazel/patches/rules_uv_create_venv_whls.patch diff --git a/BUILD b/BUILD index 1d9157ba988..fb160a2bcc0 100644 --- a/BUILD +++ b/BUILD @@ -189,6 +189,7 @@ create_venv( destination_folder = ".venv", requirements_txt = "@//:requirements_all_lock.txt", site_packages_extra_files = [":sitecustomize.py"], + whls = ["@rrdtool_native//:rrdtool_python_wheel"], ) copy_file( diff --git a/MODULE.bazel b/MODULE.bazel index 4ee1b3581e8..6703d801b75 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -22,6 +22,7 @@ single_version_override( patches = [ "//bazel/patches:rules_uv_rm_venv.patch", "//bazel/patches:rules_uv_quiet.patch", + "//bazel/patches:rules_uv_create_venv_whls.patch", ], ) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 0b4e041a8cf..378bfefc1a2 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -1,6 +1,6 @@ { "lockFileVersion": 6, - "moduleFileHash": "48a12b6158e670f460260bbbc8fb73147a89d3d109fed8842bd0d48412fcacfd", + "moduleFileHash": "98d9263231aa78b72338b695a00e14e4ee6ff0a8fd0aa70d414539e45f779a64", "flags": { "cmdRegistries": [ "https://bcr.bazel.build/" @@ -64,7 +64,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 90, + "line": 91, "column": 25 } }, @@ -98,7 +98,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 113, + "line": 114, "column": 13 } }, @@ -113,7 +113,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 139, + "line": 140, "column": 10 } }, @@ -132,7 +132,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 148, + "line": 149, "column": 13 } } @@ -146,7 +146,7 @@ "usingModule": "", "location": { "file": "@@//:MODULE.bazel", - "line": 34, + "line": 35, "column": 23 }, "imports": {}, @@ -161,7 +161,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 35, + "line": 36, "column": 17 } } @@ -175,7 +175,7 @@ "usingModule": "", "location": { "file": "@@//:MODULE.bazel", - "line": 61, + "line": 62, "column": 20 }, "imports": { @@ -198,7 +198,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 62, + "line": 63, "column": 10 } } @@ -212,7 +212,7 @@ "usingModule": "", "location": { "file": "@@//:MODULE.bazel", - "line": 76, + "line": 77, "column": 22 }, "imports": { @@ -230,7 +230,7 @@ "devDependency": false, "location": { "file": "@@//:MODULE.bazel", - "line": 77, + "line": 78, "column": 19 } } @@ -794,7 +794,8 @@ "remote_patch_strip": 0, "patches": [ "@@//bazel/patches:rules_uv_rm_venv.patch", - "@@//bazel/patches:rules_uv_quiet.patch" + "@@//bazel/patches:rules_uv_quiet.patch", + "@@//bazel/patches:rules_uv_create_venv_whls.patch" ], "patch_cmds": [], "patch_args": [ diff --git a/bazel/patches/rules_uv_create_venv_whls.patch b/bazel/patches/rules_uv_create_venv_whls.patch new file mode 100644 index 00000000000..cfb4a2427b7 --- /dev/null +++ b/bazel/patches/rules_uv_create_venv_whls.patch @@ -0,0 +1,69 @@ +diff --git a/uv/private/venv.bzl b/uv/private/venv.bzl +index 1ec9035..399e8cf 100644 +--- a/uv/private/venv.bzl ++++ b/uv/private/venv.bzl +@@ -1,12 +1,12 @@ + "uv based venv generation" + ++load("@rules_python//python:packaging.bzl", "PyWheelInfo") + load(":transition_to_target.bzl", "transition_to_target") + + _PY_TOOLCHAIN = "@bazel_tools//tools/python:toolchain_type" + + def _uv_template(ctx, template, executable): + py_toolchain = ctx.toolchains[_PY_TOOLCHAIN] +- + ctx.actions.expand_template( + template = template, + output = executable, +@@ -16,14 +16,17 @@ def _uv_template(ctx, template, executable): + "{{resolved_python}}": py_toolchain.py3_runtime.interpreter.short_path, + "{{destination_folder}}": ctx.attr.destination_folder, + "{{site_packages_extra_files}}": " ".join(["'" + file.short_path + "'" for file in ctx.files.site_packages_extra_files]), +- "{{args}}": " \\\n ".join(ctx.attr.uv_args), ++ "{{args}}": " \\\n ".join( ++ ctx.attr.uv_args + ++ [whl[PyWheelInfo].wheel.short_path for whl in ctx.attr.whls], ++ ), + }, + ) + + def _runfiles(ctx): + py_toolchain = ctx.toolchains[_PY_TOOLCHAIN] + runfiles = ctx.runfiles( +- files = [ctx.file.requirements_txt] + ctx.files.site_packages_extra_files, ++ files = [ctx.file.requirements_txt] + ctx.files.site_packages_extra_files + ctx.files.whls, + transitive_files = py_toolchain.py3_runtime.files, + ) + runfiles = runfiles.merge(ctx.attr._uv[0].default_runfiles) +@@ -45,13 +48,21 @@ _venv = rule( + "_uv": attr.label(default = "@multitool//tools/uv", executable = True, cfg = transition_to_target), + "template": attr.label(allow_single_file = True), + "uv_args": attr.string_list(default = []), ++ "whls": attr.label_list(default = [], providers = [PyWheelInfo]), + }, + toolchains = [_PY_TOOLCHAIN], + implementation = _venv_impl, + executable = True, + ) + +-def create_venv(name, requirements_txt = None, target_compatible_with = None, destination_folder = None, site_packages_extra_files = [], uv_args = []): ++def create_venv( ++ name, ++ requirements_txt = None, ++ target_compatible_with = None, ++ destination_folder = None, ++ site_packages_extra_files = [], ++ uv_args = [], ++ whls = []): + _venv( + name = name, + destination_folder = destination_folder, +@@ -59,6 +70,7 @@ def create_venv(name, requirements_txt = None, target_compatible_with = None, de + requirements_txt = requirements_txt or "//:requirements.txt", + target_compatible_with = target_compatible_with, + uv_args = uv_args, ++ whls = whls, + template = "@rules_uv//uv/private:create_venv.sh", + ) +