Skip to content

Commit

Permalink
Add support for specifying python_platform in pip_compile
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-thm committed Apr 22, 2024
1 parent b49ba6c commit 864709e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions examples/typical/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ load("@rules_uv//uv:venv.bzl", "create_venv")

pip_compile(name = "generate_requirements_txt")

pip_compile(
name = "generate_requirements_linux_txt",
python_platform = "x86_64-unknown-linux-gnu",
requirements_txt = "requirements_linux.txt",
)

create_venv(name = "create-venv")
3 changes: 3 additions & 0 deletions examples/typical/requirements_linux.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
click==8.1.7 \
--hash=sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28 \
--hash=sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de
11 changes: 10 additions & 1 deletion uv/private/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ _PY_TOOLCHAIN = "@bazel_tools//tools/python:toolchain_type"
_common_attrs = {
"requirements_in": attr.label(mandatory = True, allow_single_file = True),
"requirements_txt": attr.label(mandatory = True, allow_single_file = True),
"python_platform": attr.string(default = ""),
"_uv": attr.label(default = "@multitool//tools/uv", executable = True, cfg = "exec"),
}

def _python_platform(maybe_python_platform):
if maybe_python_platform == "":
return ""
return "--python-platform {python_platform}".format(python_platform = maybe_python_platform)

def _uv_pip_compile(ctx, template, executable):
py_toolchain = ctx.toolchains[_PY_TOOLCHAIN]
ctx.actions.expand_template(
Expand All @@ -18,6 +24,7 @@ def _uv_pip_compile(ctx, template, executable):
"{{requirements_in}}": ctx.file.requirements_in.short_path,
"{{requirements_txt}}": ctx.file.requirements_txt.short_path,
"{{resolved_python}}": py_toolchain.py3_runtime.interpreter.short_path,
"{{python_platform}}": _python_platform(ctx.attr.python_platform),
},
)

Expand Down Expand Up @@ -64,18 +71,20 @@ _pip_compile_test = rule(
test = True,
)

def pip_compile(name, requirements_in = None, requirements_txt = None, target_compatible_with = None):
def pip_compile(name, requirements_in = None, requirements_txt = None, target_compatible_with = None, python_platform = None):
_pip_compile(
name = name,
requirements_in = requirements_in or "//:requirements.in",
requirements_txt = requirements_txt or "//:requirements.txt",
python_platform = python_platform or "",
target_compatible_with = target_compatible_with,
)

_pip_compile_test(
name = name + "_diff_test",
requirements_in = requirements_in or "//:requirements.in",
requirements_txt = requirements_txt or "//:requirements.txt",
python_platform = python_platform or "",
target_compatible_with = target_compatible_with,
tags = ["requires-network"],
)
2 changes: 2 additions & 0 deletions uv/private/pip_compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -euo pipefail

UV="{{uv}}"
PYTHON_PLATFORM="{{python_platform}}"
RESOLVED_PYTHON="{{resolved_python}}"
REQUIREMENTS_IN="{{requirements_in}}"
REQUIREMENTS_TXT="{{requirements_txt}}"
Expand All @@ -20,6 +21,7 @@ $UV pip compile \
--no-header \
--no-strip-extras \
--python-version=$PYTHON_VERSION \
$(echo $PYTHON_PLATFORM) \
-o $REQUIREMENTS_TXT \
$REQUIREMENTS_IN \
$@
2 changes: 2 additions & 0 deletions uv/private/pip_compile_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
set -euo pipefail

UV="{{uv}}"
PYTHON_PLATFORM="{{python_platform}}"
RESOLVED_PYTHON="{{resolved_python}}"
REQUIREMENTS_IN="{{requirements_in}}"
REQUIREMENTS_TXT="{{requirements_txt}}"
Expand All @@ -21,5 +22,6 @@ $UV pip compile \
--no-header \
--no-strip-extras \
--python-version=$PYTHON_VERSION \
$(echo $PYTHON_PLATFORM) \
-c $REQUIREMENTS_TXT \
$REQUIREMENTS_IN

0 comments on commit 864709e

Please sign in to comment.