-
-
Notifications
You must be signed in to change notification settings - Fork 594
refactor(pkg_aliases): create a macro for creating whl aliases #2391
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
354f7a2
add a new macro for setting up aliases
aignas a7de3d4
refactor legacy aliases
aignas 47e81a1
refactor: create group aliases
aignas 913fba9
refactor
aignas 5397407
refactor
aignas 0060270
Merge branch 'main' into refactor/pkg-aliases
aignas 3af7cc2
chore: pre-commit
aignas 36fec60
cleanup bzl_library
aignas 69c0bf7
Merge branch 'main' into refactor/pkg-aliases
aignas 52655d8
simplify the code
aignas 282b696
remove the config_setting setting in the whl_alias constructor
aignas 300c994
avoid allocation of a dict when constructing a whl_alias
aignas e265a5b
Update python/private/pypi/pkg_aliases.bzl
aignas 7765c89
Update python/private/pypi/pkg_aliases.bzl
aignas e287d3d
Update python/private/pypi/pkg_aliases.bzl
aignas 5e3aba0
move the alias dict construction to the pypi extension
aignas 09fd92e
fixup extension tests
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 hidden or 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,135 @@ | ||
# Copyright 2024 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. | ||
|
||
"""pkg_aliases is a macro to generate aliases for selecting the right wheel for the right target platform. | ||
|
||
This is used in bzlmod and non-bzlmod setups.""" | ||
|
||
load("//python/private:text_util.bzl", "render") | ||
load( | ||
":labels.bzl", | ||
"DATA_LABEL", | ||
"DIST_INFO_LABEL", | ||
"PY_LIBRARY_IMPL_LABEL", | ||
"PY_LIBRARY_PUBLIC_LABEL", | ||
"WHEEL_FILE_IMPL_LABEL", | ||
"WHEEL_FILE_PUBLIC_LABEL", | ||
) | ||
|
||
_NO_MATCH_ERROR_TEMPLATE = """\ | ||
No matching wheel for current configuration's Python version. | ||
|
||
The current build configuration's Python version doesn't match any of the Python | ||
wheels available for this wheel. This wheel supports the following Python | ||
configuration settings: | ||
{config_settings} | ||
|
||
To determine the current configuration's Python version, run: | ||
`bazel config <config id>` (shown further below) | ||
and look for | ||
{rules_python}//python/config_settings:python_version | ||
|
||
If the value is missing, then the "default" Python version is being used, | ||
which has a "null" version value and will not match version constraints. | ||
""" | ||
|
||
def _no_match_error(actual): | ||
if type(actual) != type({}): | ||
return None | ||
|
||
if "//conditions:default" in actual: | ||
return None | ||
|
||
return _NO_MATCH_ERROR_TEMPLATE.format( | ||
config_settings = render.indent( | ||
"\n".join(sorted(actual.keys())), | ||
).lstrip(), | ||
rules_python = "rules_python", | ||
) | ||
|
||
def pkg_aliases( | ||
*, | ||
name, | ||
actual, | ||
group_name = None, | ||
extra_aliases = None, | ||
native = native, | ||
select = select): | ||
"""Create aliases for an actual package. | ||
|
||
Args: | ||
name: {type}`str` The name of the package. | ||
actual: {type}`dict[Label, str] | str` The config settings for the package | ||
mapping to repositories. | ||
group_name: {type}`str` The group name that the pkg belongs to. | ||
extra_aliases: {type}`list[str]` The extra aliases to be created. | ||
native: {type}`struct` used in unit tests. | ||
select: {type}`select` used in unit tests. | ||
""" | ||
_ = actual # buildifier: @unused | ||
aignas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
native.alias( | ||
name = name, | ||
actual = ":" + PY_LIBRARY_PUBLIC_LABEL, | ||
) | ||
|
||
target_names = { | ||
PY_LIBRARY_PUBLIC_LABEL: PY_LIBRARY_IMPL_LABEL if group_name else PY_LIBRARY_PUBLIC_LABEL, | ||
WHEEL_FILE_PUBLIC_LABEL: WHEEL_FILE_IMPL_LABEL if group_name else WHEEL_FILE_PUBLIC_LABEL, | ||
DATA_LABEL: DATA_LABEL, | ||
DIST_INFO_LABEL: DIST_INFO_LABEL, | ||
} | { | ||
x: x | ||
for x in extra_aliases or [] | ||
} | ||
no_match_error = _no_match_error(actual) | ||
|
||
for name, target_name in target_names.items(): | ||
if type(actual) == type(""): | ||
_actual = "@{repo}//:{target_name}".format( | ||
repo = actual, | ||
target_name = name, | ||
) | ||
elif type(actual) == type({}): | ||
_actual = select( | ||
{ | ||
config_setting: "@{repo}//:{target_name}".format( | ||
repo = repo, | ||
target_name = name, | ||
) | ||
for config_setting, repo in actual.items() | ||
}, | ||
no_match_error = no_match_error, | ||
) | ||
else: | ||
fail("BUG: should have a dictionary or a string") | ||
aignas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
kwargs = {} | ||
if target_name.startswith("_"): | ||
kwargs["visibility"] = ["//_groups:__subpackages__"] | ||
|
||
native.alias( | ||
name = target_name, | ||
actual = _actual, | ||
**kwargs | ||
) | ||
|
||
if group_name: | ||
native.alias( | ||
name = PY_LIBRARY_PUBLIC_LABEL, | ||
actual = "//_groups:{}_pkg".format(group_name), | ||
) | ||
native.alias( | ||
name = WHEEL_FILE_PUBLIC_LABEL, | ||
actual = "//_groups:{}_whl".format(group_name), | ||
) |
This file contains hidden or 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 hidden or 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,3 @@ | ||
load(":pkg_aliases_test.bzl", "pkg_aliases_test_suite") | ||
|
||
pkg_aliases_test_suite(name = "pkg_aliases_tests") |
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.
Uh oh!
There was an error while loading. Please reload this page.