Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move pip_compile macro to public API file #114

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 68 additions & 2 deletions uv/pip.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,71 @@
"uv based pip compile rules"

load("//uv/private:pip.bzl", _pip_compile = "pip_compile")
load("//uv/private:pip.bzl", "pip_compile_test", _pip_compile = "pip_compile")

pip_compile = _pip_compile
def pip_compile(
name,
requirements_in = None,
requirements_txt = None,
target_compatible_with = None,
python_platform = None,
args = None,
data = None,
tags = None,
**kwargs):
"""
Produce targets to compile a requirements.in or pyproject.toml file into a requirements.txt file.

Args:
name: name of the primary compilation target.
requirements_in: (optional, default "//:requirements.in") a label for the requirements.in file.
requirements_txt: (optional, default "//:requirements.txt") a label for the requirements.txt file.
python_platform: (optional) a uv pip compile compatible value for --python-platform.
target_compatible_with: (optional) specify that a particular target is compatible only with certain
Bazel platforms.
args: (optional) override the default arguments passed to uv pip compile, default arguments are:
--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
**kwargs: (optional) other fields passed through to all underlying rules

Targets produced by this macro are:
[name]: a runnable target that will use requirements_in to generate and overwrite requirements_txt
[name].update: an alias for [name]
[name]_test: a testable target that will check that requirements_txt is up to date with requirements_in
"""
requirements_in = requirements_in or "//:requirements.in"
requirements_txt = requirements_txt or "//:requirements.txt"
tags = tags or []

_pip_compile(
name = name,
requirements_in = requirements_in,
requirements_txt = requirements_txt,
python_platform = python_platform,
target_compatible_with = target_compatible_with,
data = data,
uv_args = args,
**kwargs
)

# Also allow 'bazel run' with a "custom verb" https://bazel.build/rules/verbs-tutorial
# Provides compatibility with rules_python's compile_pip_requirements [name].update target.
native.alias(
name = name + ".update",
actual = name,
)

pip_compile_test(
name = name + "_test",
generator_label = name,
requirements_in = requirements_in,
requirements_txt = requirements_txt,
python_platform = python_platform or "",
target_compatible_with = target_compatible_with,
data = data,
uv_args = args,
tags = ["requires-network"] + tags,
**kwargs
)
70 changes: 2 additions & 68 deletions uv/private/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def _pip_compile_impl(ctx):
runfiles = _runfiles(ctx),
)

_pip_compile = rule(
pip_compile = rule(
attrs = _COMMON_ATTRS | {
"_template": attr.label(default = "//uv/private:pip_compile.sh", allow_single_file = True),
},
Expand Down Expand Up @@ -117,7 +117,7 @@ def _pip_compile_test_impl(ctx):
),
]

_pip_compile_test = rule(
pip_compile_test = rule(
attrs = _COMMON_ATTRS | {
"generator_label": attr.label(mandatory = True),
"_template": attr.label(default = "//uv/private:pip_compile_test.sh", allow_single_file = True),
Expand All @@ -126,69 +126,3 @@ _pip_compile_test = rule(
implementation = _pip_compile_test_impl,
test = True,
)

def pip_compile(
name,
requirements_in = None,
requirements_txt = None,
target_compatible_with = None,
python_platform = None,
args = None,
data = None,
tags = None,
**kwargs):
"""
Produce targets to compile a requirements.in or pyproject.toml file into a requirements.txt file.

Args:
name: name of the primary compilation target.
requirements_in: (optional, default "//:requirements.in") a label for the requirements.in file.
requirements_txt: (optional, default "//:requirements.txt") a label for the requirements.txt file.
python_platform: (optional) a uv pip compile compatible value for --python-platform.
target_compatible_with: (optional) specify that a particular target is compatible only with certain
Bazel platforms.
args: (optional) override the default arguments passed to uv pip compile, default arguments are:
--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
**kwargs: (optional) other fields passed through to all underlying rules

Targets produced by this macro are:
[name]: a runnable target that will use requirements_in to generate and overwrite requirements_txt
[name].update: an alias for [name]
[name]_test: a testable target that will check that requirements_txt is up to date with requirements_in
"""
tags = tags or []

_pip_compile(
name = name,
requirements_in = requirements_in or "//:requirements.in",
requirements_txt = requirements_txt or "//:requirements.txt",
python_platform = python_platform,
target_compatible_with = target_compatible_with,
data = data,
uv_args = args,
**kwargs
)

# Also allow 'bazel run' with a "custom verb" https://bazel.build/rules/verbs-tutorial
# Provides compatibility with rules_python's compile_pip_requirements [name].update target.
native.alias(
name = name + ".update",
actual = name,
)

_pip_compile_test(
name = name + "_test",
generator_label = 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,
data = data,
uv_args = args,
tags = ["requires-network"] + tags,
**kwargs
)