-
Notifications
You must be signed in to change notification settings - Fork 541
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
feat(bzlmod): support entry_point in bzlmod hub repos #1294
Closed
aignas
wants to merge
9
commits into
bazelbuild:main
from
aignas:refactor/remove-bzlmod-requirements-usage
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
36199b0
feat: support alias rendering for python aware toolchain targets
aignas 6313822
refactor: use render_pkg_aliases
aignas 5eab849
test: add a helper to distinguish between bazel 6
aignas 79fc058
feat: add a full_version helper
aignas 590830b
refactor: use full_version
aignas d1f62cb
feat: generate entry_points.bzl for each whl_library
aignas b9a2f5f
feat: consolidate bzlmod pip hub repos and support entry_points
aignas 9d28b0f
docs: regenerate docs
aignas 1b88ae3
A different external API for entrypoints without a macro
aignas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# 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 | ||
# | ||
# 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. | ||
|
||
""" | ||
A pip_hub_repository rule used to create bzlmod hub repos for PyPI packages. | ||
|
||
It assumes that version aware toolchain is used and is responsible for setting up | ||
aliases for entry points and the actual package targets. | ||
""" | ||
|
||
load("//python/private:render_pkg_aliases.bzl", "render_pkg_aliases") | ||
|
||
_BUILD_FILE_CONTENTS = """\ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
# Ensure the `requirements.bzl` source can be accessed by stardoc, since users load() from it | ||
exports_files(["requirements.bzl"]) | ||
""" | ||
|
||
def _impl(rctx): | ||
bzl_packages = rctx.attr.whl_map.keys() | ||
repo_name = rctx.attr.repo_name | ||
|
||
aliases = render_pkg_aliases( | ||
repo_name = repo_name, | ||
whl_map = rctx.attr.whl_map, | ||
whl_entry_points = { | ||
whl_name: json.decode(values) | ||
for whl_name, values in rctx.attr.whl_entry_points.items() | ||
}, | ||
default_version = rctx.attr.default_version, | ||
rules_python = rctx.attr._template.workspace_name, | ||
) | ||
for path, contents in aliases.items(): | ||
rctx.file(path, contents) | ||
|
||
# NOTE: we are using the canonical name with the double '@' in order to | ||
# always uniquely identify a repository, as the labels are being passed as | ||
# a string and the resolution of the label happens at the call-site of the | ||
# `requirement`, et al. macros. | ||
macro_tmpl = "@@{name}//{{}}:{{}}".format(name = rctx.attr.name) | ||
|
||
rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS) | ||
rctx.template("requirements.bzl", rctx.attr._template, substitutions = { | ||
"%%ALL_DATA_REQUIREMENTS%%": repr([ | ||
macro_tmpl.format(p, "data") | ||
for p in bzl_packages | ||
]), | ||
"%%ALL_REQUIREMENTS%%": repr([ | ||
macro_tmpl.format(p, p) | ||
for p in bzl_packages | ||
]), | ||
"%%ALL_WHL_REQUIREMENTS%%": repr([ | ||
macro_tmpl.format(p, "whl") | ||
for p in bzl_packages | ||
]), | ||
"%%DEFAULT_PY_VERSION%%": repr(rctx.attr.default_version), | ||
"%%MACRO_TMPL%%": macro_tmpl, | ||
"%%NAME%%": rctx.attr.name, | ||
"%%PACKAGE_AVAILABILITY%%": repr({ | ||
k: [v for v in versions] | ||
for k, versions in rctx.attr.whl_map.items() | ||
}), | ||
"%%RULES_PYTHON%%": rctx.attr._template.workspace_name, | ||
}) | ||
|
||
pip_hub_repository = repository_rule( | ||
attrs = { | ||
"default_version": attr.string( | ||
mandatory = True, | ||
doc = """\ | ||
This is the default python version in the format of X.Y.Z. This should match | ||
what is setup by the 'python' extension using the 'is_default = True' | ||
setting.""", | ||
), | ||
"repo_name": attr.string( | ||
mandatory = True, | ||
doc = "The apparent name of the repo. This is needed because in bzlmod, the name attribute becomes the canonical name.", | ||
), | ||
"whl_entry_points": attr.string_dict( | ||
mandatory = False, | ||
doc = "The entry points that we will create aliases for.", | ||
), | ||
"whl_map": attr.string_list_dict( | ||
mandatory = True, | ||
doc = "The wheel map where values are python versions", | ||
), | ||
"_template": attr.label(default = ":requirements.bzl.tmpl"), | ||
}, | ||
doc = """A rule for creating bzlmod hub repo for PyPI packages. PRIVATE USE ONLY.""", | ||
implementation = _impl, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rickeylev, instead of
entry_point
macros, what do you think about the following extension API? You can see thebzlmod
exampleentry_point
test for how it works.