Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aignas committed Jan 25, 2025
1 parent db49396 commit ff98687
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 46 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ Unreleased changes template.
* (rules) deprecation warnings for deprecated symbols have been turned off by
default for now and can be enabled with `RULES_PYTHON_DEPRECATION_WARNINGS`
env var.
* (uv) Now the extension can be fully configured via `bzlmod` APIs without the
need to patch `rules_python`. The documentation has been added to `rules_python`
docs but usage of the extension may result in your setup breaking without any
notice. What is more, the URLs and SHA256 values will be retrieved from the
GitHub releases page metadata published by the `uv` project.

{#v0-0-0-fixed}
### Fixed
Expand Down
6 changes: 3 additions & 3 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ uv_dev = use_extension(
dev_dependency = True,
)
uv_dev.toolchain(
name = "uv_0_5_24",
name = "uv_toolchains",
version = "0.5.24",
)
use_repo(uv_dev, "uv_0_5_24")
use_repo(uv_dev, "uv_toolchains")

register_toolchains(
"@uv_0_5_24//:all",
"@uv_toolchains//:all",
dev_dependency = True,
)
8 changes: 6 additions & 2 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@dev_pip//:requirements.bzl", "requirement")
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility
load("//python/uv/private:lock.bzl", "lock") # buildifier: disable=bzl-visibility
load("//python/uv:uv_lock.bzl", "uv_lock")
load("//sphinxdocs:readthedocs.bzl", "readthedocs_install")
load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs")
load("//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
Expand Down Expand Up @@ -105,6 +105,10 @@ sphinx_stardocs(
"//python/private/api:py_common_api_bzl",
"//python/private/pypi:config_settings_bzl",
"//python/private/pypi:pkg_aliases_bzl",
"//python/uv:extensions_bzl",
"//python/uv:uv_lock_bzl",
"//python/uv:uv_toolchain_bzl",
"//python/uv:uv_toolchain_info_bzl",
] + ([
# Bazel 6 + Stardoc isn't able to parse something about the python bzlmod extension
"//python/extensions:python_bzl",
Expand Down Expand Up @@ -155,7 +159,7 @@ sphinx_build_binary(
)

# Run bazel run //docs:requirements.update
lock(
uv_lock(
name = "requirements",
srcs = ["pyproject.toml"],
out = "requirements.txt",
Expand Down
25 changes: 17 additions & 8 deletions python/uv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,39 @@ current_toolchain(
)

bzl_library(
name = "extensions",
name = "extensions_bzl",
srcs = ["extensions.bzl"],
# EXPERIMENTAL: Visibility is restricted to allow for changes.
visibility = ["//:__subpackages__"],
deps = ["//python/uv/private:uv"],
deps = ["//python/uv/private:uv_bzl"],
)

bzl_library(
name = "repositories",
srcs = ["repositories.bzl"],
name = "uv_lock_bzl",
srcs = ["uv_lock.bzl"],
# EXPERIMENTAL: Visibility is restricted to allow for changes.
visibility = ["//:__subpackages__"],
deps = [
"//python/uv/private:toolchains_repo",
"//python/uv/private:versions",
"//python/uv/private:lock_bzl",
],
)

bzl_library(
name = "uv_toolchain",
name = "uv_toolchain_bzl",
srcs = ["uv_toolchain.bzl"],
# EXPERIMENTAL: Visibility is restricted to allow for changes.
visibility = ["//:__subpackages__"],
deps = [
"//python/uv/private:uv_toolchain",
"//python/uv/private:uv_toolchain_bzl",
],
)

bzl_library(
name = "uv_toolchain_info_bzl",
srcs = ["uv_toolchain_info.bzl"],
# EXPERIMENTAL: Visibility is restricted to allow for changes.
visibility = ["//:__subpackages__"],
deps = [
"//python/uv/private:uv_toolchain_info_bzl",
],
)
2 changes: 1 addition & 1 deletion python/uv/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ EXPERIMENTAL: This is experimental and may be removed without notice
A module extension for working with uv.
"""

load("//python/uv/private:extension.bzl", _uv = "uv")
load("//python/uv/private:uv.bzl", _uv = "uv")

uv = _uv
52 changes: 44 additions & 8 deletions python/uv/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,70 @@ filegroup(
)

bzl_library(
name = "current_toolchain",
name = "current_toolchain_bzl",
srcs = ["current_toolchain.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = [
":toolchain_types_bzl",
],
)

bzl_library(
name = "extensions",
srcs = ["extensions.bzl"],
name = "uv_bzl",
srcs = ["uv.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = [
":uv_repositories_bzl",
],
)

bzl_library(
name = "toolchain_types",
name = "lock_bzl",
srcs = ["lock.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = [
"//python:py_binary_bzl",
"//python/private:bzlmod_enabled_bzl",
"@bazel_skylib//rules:write_file",
],
)

bzl_library(
name = "toolchain_types_bzl",
srcs = ["toolchain_types.bzl"],
visibility = ["//python/uv:__subpackages__"],
)

bzl_library(
name = "toolchains_repo",
srcs = ["toolchains_repo.bzl"],
name = "uv_toolchain_bzl",
srcs = ["uv_toolchain.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = [
":uv_toolchain_info_bzl",
],
)

bzl_library(
name = "uv_toolchain_info_bzl",
srcs = ["uv_toolchain_info.bzl"],
visibility = ["//python/uv:__subpackages__"],
)

bzl_library(
name = "uv_toolchains_repo_bzl",
srcs = ["uv_toolchains_repo.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = [
"//python/private:text_util_bzl",
],
)

bzl_library(
name = "versions",
srcs = ["versions.bzl"],
name = "uv_repositories_bzl",
srcs = ["uv_repositories.bzl"],
visibility = ["//python/uv:__subpackages__"],
deps = [
":toolchain_types_bzl",
":uv_toolchains_repo_bzl",
],
)
22 changes: 9 additions & 13 deletions python/uv/private/lock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ _REQUIREMENTS_TARGET_COMPATIBLE_WITH = select({
"//conditions:default": [],
}) if BZLMOD_ENABLED else ["@platforms//:incompatible"]

def lock(*, name, srcs, out, upgrade = False, universal = True, python_version = None, args = [], **kwargs):
def lock(*, name, srcs, out, upgrade = False, universal = True, args = [], **kwargs):
"""Pin the requirements based on the src files.
Differences with the current {obj}`compile_pip_requirements` rule:
- This is implemented in shell and `uv`.
- This does not error out if the output file does not exist yet.
- Supports transitions out of the box.
- The execution of the lock file generation is happening inside of a build
action in a `genrule`.
Args:
name: The name of the target to run for updating the requirements.
srcs: The srcs to use as inputs.
out: The output file.
upgrade: Tell `uv` to always upgrade the dependencies instead of
keeping them as they are.
universal: Tell `uv` to generate a universal lock file.
python_version: Tell `rules_python` to use a particular version.
Defaults to the default py toolchain.
args: Extra args to pass to the rule.
**kwargs: Extra kwargs passed to the binary rule.
Differences with the current pip-compile rule:
- This is implemented in shell and uv.
- This does not error out if the output file does not exist yet.
- Supports transitions out of the box.
"""
pkg = native.package_name()
update_target = name + ".update"
Expand Down Expand Up @@ -92,10 +92,6 @@ def lock(*, name, srcs, out, upgrade = False, universal = True, python_version =
Label("//python:current_py_toolchain"),
],
)
if python_version:
py_binary_rule = lambda *args, **kwargs: py_binary(python_version = python_version, *args, **kwargs)
else:
py_binary_rule = py_binary

# Write a script that can be used for updating the in-tree version of the
# requirements file
Expand All @@ -116,7 +112,7 @@ def lock(*, name, srcs, out, upgrade = False, universal = True, python_version =
],
)

py_binary_rule(
py_binary(
name = update_target,
srcs = [update_target + ".py"],
main = update_target + ".py",
Expand Down
26 changes: 24 additions & 2 deletions python/uv/private/extension.bzl → python/uv/private/uv.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,32 @@ EXPERIMENTAL: This is experimental and may be removed without notice
A module extension for working with uv.
"""

load(":repositories.bzl", "uv_register_toolchains")
load(":uv_repositories.bzl", "uv_repositories")

_DOC = """\
A module extension for working with uv.
Use it in your own setup by:
```starlark
uv = use_extension(
"@rules_python//python/uv:extensions.bzl",
"uv",
dev_dependency = True,
)
uv.toolchain(
name = "uv_toolchains",
version = "0.5.24",
)
use_repo(uv, "uv_toolchains")
register_toolchains(
"@uv_toolchains//:all",
dev_dependency = True,
)
```
Since this is only for locking the requirements files, it should be always
marked as a `dev_dependency`.
"""

_DIST_MANIFEST_JSON = "dist-manifest.json"
Expand Down Expand Up @@ -100,7 +122,7 @@ def _uv_toolchain_extension(module_ctx):
module_ctx,
base_url = "{base_url}/{version}".format(**config),
)
uv_register_toolchains(
uv_repositories(
name = config["name"],
platforms = config["platforms"],
urls = config["urls"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Create repositories for uv toolchain dependencies
"""

load(":toolchain_types.bzl", "UV_TOOLCHAIN_TYPE")
load(":toolchains_repo.bzl", "uv_toolchains_repo")
load(":uv_toolchains_repo.bzl", "uv_toolchains_repo")

UV_BUILD_TMPL = """\
# Generated by repositories.bzl
Expand Down Expand Up @@ -76,7 +76,7 @@ uv_repository = repository_rule(
},
)

def uv_register_toolchains(*, name, version, platforms, urls):
def uv_repositories(*, name, version, platforms, urls):
"""Convenience macro which does typical toolchain setup
Skip this macro if you need more control over the toolchain setup.
Expand Down
2 changes: 1 addition & 1 deletion python/uv/private/uv_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ EXPERIMENTAL: This is experimental and may be removed without notice
This module implements the uv toolchain rule
"""

load("//python/uv/private:providers.bzl", "UvToolchainInfo")
load(":uv_toolchain_info.bzl", "UvToolchainInfo")

def _uv_toolchain_impl(ctx):
uv = ctx.attr.uv
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 9 additions & 4 deletions python/uv/private/versions.bzl → python/uv/uv_lock.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Version and integrity information for downloaded artifacts"""
"""
EXPERIMENTAL: This is experimental and may be removed without notice
UV_PLATFORMS = {
}
The locking rule using uv.
"""

load("//python/uv/private:lock.bzl", "lock")

uv_lock = lock
4 changes: 2 additions & 2 deletions python/uv/uv_toolchain_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"""
EXPERIMENTAL: This is experimental and may be removed without notice
A toolchain for uv
The toolchain info provider.
"""

load("//python/uv/private:providers.bzl", _UvToolchainInfo = "UvToolchainInfo")
load("//python/uv/private:uv_toolchain_info.bzl", _UvToolchainInfo = "UvToolchainInfo")

UvToolchainInfo = _UvToolchainInfo

0 comments on commit ff98687

Please sign in to comment.