Skip to content

Commit

Permalink
Support multiple input files and requirements-file references
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-thm committed Jun 16, 2024
1 parent 4535b34 commit b2b4b26
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/multiple-inputs/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
common --enable_bzlmod

common --lockfile_mode=off

test --test_output=errors
1 change: 1 addition & 0 deletions examples/multiple-inputs/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.0.0
9 changes: 9 additions & 0 deletions examples/multiple-inputs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@rules_uv//uv:pip.bzl", "pip_compile")
load("@rules_uv//uv:venv.bzl", "create_venv")

pip_compile(
name = "generate_requirements_txt",
data = ["//:requirements.test.in"],
)

create_venv(name = "create-venv")
14 changes: 14 additions & 0 deletions examples/multiple-inputs/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"example of rules_uv with multiple .in files"

module(
name = "uv__examples__multiple_inputs",
version = "0.0.0",
)

bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "rules_uv", version = "0.0.0")
local_path_override(
module_name = "rules_uv",
path = "../..",
)
Empty file.
3 changes: 3 additions & 0 deletions examples/multiple-inputs/requirements.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-r requirements.test.in

click~=8.1.7
1 change: 1 addition & 0 deletions examples/multiple-inputs/requirements.test.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest~=8.2.2
24 changes: 24 additions & 0 deletions examples/multiple-inputs/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This file was autogenerated by uv via the following command:
# bazel run @@//:generate_requirements_txt
--index-url https://pypi.org/simple

click==8.1.7 \
--hash=sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28 \
--hash=sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de
# via -r requirements.in
iniconfig==2.0.0 \
--hash=sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3 \
--hash=sha256:b6a85871a79d2e3b22d2d1b94ac2824226a63c6b741c88f7ae975f18b6778374
# via pytest
packaging==24.1 \
--hash=sha256:026ed72c8ed3fcce5bf8950572258698927fd1dbda10a5e981cdf0ac37f4f002 \
--hash=sha256:5b8f2217dbdbd2f7f384c41c628544e6d52f2d0f53c6d0c3ea61aa5d1d7ff124
# via pytest
pluggy==1.5.0 \
--hash=sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1 \
--hash=sha256:44e1ad92c8ca002de6377e165f3e0f1be63266ab4d554740532335b9d75ea669
# via pytest
pytest==8.2.2 \
--hash=sha256:c434598117762e2bd304e526244f67bf66bbd7b5d6cf22138be51ff661980343 \
--hash=sha256:de4bb8104e201939ccdc688b27a89a7be2079b22e2bd2b07f806b6ba71117977
# via -r requirements.test.in
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Additionally, you can specify the following optional args:

- `python_platform`: the `uv pip compile` compatible `--python-platform` value to pass to uv
- `args`: override the default arguments passed to uv (`--generate-hashes`, `--emit-index-url` and `--no-strip-extras`)
- `data`: pass additional files to be present when generating and testing requirements txt files (see also [examples/multiple-inputs](examples/multiple-inputs/))
- `tags`: tags to apply to the test target
- `target_compatible_with`: restrict targets to running on the specified Bazel platform

Expand Down
7 changes: 6 additions & 1 deletion uv/private/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ _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(),
"data": attr.label_list(allow_files = True),
"uv_args": attr.string_list(default = _DEFAULT_ARGS),
"_uv": attr.label(default = "@multitool//tools/uv", executable = True, cfg = "exec"),
}
Expand Down Expand Up @@ -54,7 +55,7 @@ def _uv_pip_compile(
def _runfiles(ctx):
py_toolchain = ctx.toolchains[_PY_TOOLCHAIN]
runfiles = ctx.runfiles(
files = [ctx.file.requirements_in, ctx.file.requirements_txt],
files = [ctx.file.requirements_in, ctx.file.requirements_txt] + ctx.files.data,
transitive_files = py_toolchain.py3_runtime.files,
)
runfiles = runfiles.merge(ctx.attr._uv.default_runfiles)
Expand Down Expand Up @@ -114,6 +115,7 @@ def pip_compile(
target_compatible_with = None,
python_platform = None,
args = None,
data = None,
tags = None):
"""
Produce targets to compile a requirements.in or pyproject.toml file into a requirements.txt file.
Expand All @@ -129,6 +131,7 @@ def pip_compile(
--generate-hashes (Include distribution hashes in the output file)
--emit-index-url (Include `--index-url` and `--extra-index-url` entries in the generated output file)
--no-strip-extras (Include extras in the output file)
data: (optional) a list of labels of additional files to include
tags: (optional) tags to apply to the generated test target
Targets produced by this macro are:
Expand All @@ -143,6 +146,7 @@ def pip_compile(
requirements_txt = requirements_txt or "//:requirements.txt",
python_platform = python_platform,
target_compatible_with = target_compatible_with,
data = data,
uv_args = args,
)

Expand All @@ -153,6 +157,7 @@ def pip_compile(
requirements_txt = requirements_txt or "//:requirements.txt",
python_platform = python_platform or "",
target_compatible_with = target_compatible_with,
data = data,
uv_args = args,
tags = ["requires-network"] + tags,
)

0 comments on commit b2b4b26

Please sign in to comment.