-
Notifications
You must be signed in to change notification settings - Fork 543
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
refactor/fix: store dists in parse_requirements output #1917
Merged
Merged
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d65b3fb
parse_requirements: add whls and sdists attr
aignas 1d7be8d
refactor: split out a function for selecting a list of wheels instead…
aignas 50ff113
Handle correctly when the target platform has a python version in the…
aignas 823cc03
add a note
aignas cb1ecd1
wip
aignas 1800f77
docs: add bzl_library targets
aignas 6dff723
fixes for #1930
aignas 552c526
filter out non cp or py wheels
aignas 348ca06
rewrite the filtering algorithm and harden the code
aignas 341cf84
Merge branch 'main' into refactor/store-dists-in-reqs
aignas 0261471
minor cleanup
aignas 098835f
add a logger to the pip extension
aignas 900e617
comment: add a logger to repo_utils and accept a lambda instead of st…
aignas 90eaf3b
comment: clarify docstring
aignas aef1bb8
comment: clarify parameter s/want_version/want_python_version
aignas 6dc2e62
comment: cryptic comment
aignas adc7708
comment: set usage
aignas 24fa6a5
comment: describe why musl is special cased
aignas dec7455
comment: why we are getting sdists[0]
aignas 2cd9502
comment: the add_dists is too coupled
aignas fed02eb
cleanup
aignas 6e4ee51
fixup tests
aignas c9158aa
fixup debugging statements
aignas 48b8a71
finish the debugging setup
aignas 0fad448
clenaup bzl_library
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
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 |
---|---|---|
|
@@ -25,6 +25,7 @@ load( | |
load("//python/private:auth.bzl", "AUTH_ATTRS") | ||
load("//python/private:normalize_name.bzl", "normalize_name") | ||
load("//python/private:parse_requirements.bzl", "host_platform", "parse_requirements", "select_requirement") | ||
load("//python/private:parse_requirements_add_dists.bzl", "parse_requirements_add_dists") | ||
load("//python/private:parse_whl_name.bzl", "parse_whl_name") | ||
load("//python/private:pypi_index.bzl", "simpleapi_download") | ||
load("//python/private:render_pkg_aliases.bzl", "whl_alias") | ||
|
@@ -99,7 +100,30 @@ You cannot use both the additive_build_content and additive_build_content_file a | |
whl_mods = whl_mods, | ||
) | ||
|
||
def _new_logger(verbosity_level = None): | ||
verbosity = { | ||
"DEBUG": 2, | ||
"INFO": 1, | ||
"TRACE": 3, | ||
}.get(verbosity_level, 0) | ||
|
||
# buildifier: disable=print | ||
def _log(enabled_on_verbosity, level, *args): | ||
if verbosity < enabled_on_verbosity: | ||
return | ||
print("{}: ".format(level.upper()), *args) | ||
|
||
return struct( | ||
trace = lambda *args: _log(3, "TRACE", *args), | ||
debug = lambda *args: _log(2, "DEBUG", *args), | ||
info = lambda *args: _log(1, "INFO", *args), | ||
# buildifier: disable=print | ||
warn = lambda *args: print("WARNING: ", *args), | ||
fail = lambda *args: fail(*args), | ||
) | ||
|
||
def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides, group_map, simpleapi_cache): | ||
logger = _new_logger(pip_attr.verbosity) | ||
python_interpreter_target = pip_attr.python_interpreter_target | ||
|
||
# if we do not have the python_interpreter set in the attributes | ||
|
@@ -170,7 +194,6 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides, group_map, s | |
extra_pip_args = pip_attr.extra_pip_args, | ||
) | ||
|
||
index_urls = {} | ||
if pip_attr.experimental_index_url: | ||
if pip_attr.download_only: | ||
fail("Currently unsupported to use `download_only` and `experimental_index_url`") | ||
|
@@ -194,6 +217,12 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides, group_map, s | |
cache = simpleapi_cache, | ||
parallel_download = pip_attr.parallel_download, | ||
) | ||
parse_requirements_add_dists( | ||
requirements_by_platform, | ||
index_urls, | ||
python_version = major_minor, | ||
logger = logger, | ||
) | ||
|
||
repository_platform = host_platform(module_ctx.os) | ||
for whl_name, requirements in requirements_by_platform.items(): | ||
|
@@ -255,37 +284,22 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides, group_map, s | |
) | ||
whl_library_args.update({k: v for k, (v, default) in maybe_args_with_default.items() if v == default}) | ||
|
||
if index_urls: | ||
whls = [] | ||
sdist = None | ||
for sha256 in requirement.srcs.shas: | ||
# For now if the artifact is marked as yanked we just ignore it. | ||
# | ||
# See https://packaging.python.org/en/latest/specifications/simple-repository-api/#adding-yank-support-to-the-simple-api | ||
|
||
maybe_whl = index_urls[whl_name].whls.get(sha256) | ||
if maybe_whl and not maybe_whl.yanked: | ||
whls.append(maybe_whl) | ||
continue | ||
|
||
maybe_sdist = index_urls[whl_name].sdists.get(sha256) | ||
if maybe_sdist and not maybe_sdist.yanked: | ||
sdist = maybe_sdist | ||
continue | ||
|
||
print("WARNING: Could not find a whl or an sdist with sha256={}".format(sha256)) # buildifier: disable=print | ||
|
||
if requirement.whls or requirement.sdists: | ||
logger.debug("Selecting a compatible dist for {} from dists:\n{}".format( | ||
repository_platform, | ||
json.encode( | ||
struct( | ||
whls = requirement.whls, | ||
sdists = requirement.sdists, | ||
), | ||
), | ||
)) | ||
distribution = select_whl( | ||
whls = whls, | ||
want_abis = [ | ||
"none", | ||
"abi3", | ||
"cp" + major_minor.replace(".", ""), | ||
# Older python versions have wheels for the `*m` ABI. | ||
"cp" + major_minor.replace(".", "") + "m", | ||
], | ||
whls = requirement.whls, | ||
want_platform = repository_platform, | ||
) or sdist | ||
) or (requirement.sdists[0] if requirement.sdists else None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the 0th sdist special? Or is this just picking an arbitrary element to have something valid? |
||
|
||
logger.debug("Selected: {}".format(distribution)) | ||
|
||
if distribution: | ||
whl_library_args["requirement"] = requirement.srcs.requirement | ||
|
@@ -303,7 +317,7 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides, group_map, s | |
# This is no-op because pip is not used to download the wheel. | ||
whl_library_args.pop("download_only", None) | ||
else: | ||
print("WARNING: falling back to pip for installing the right file for {}".format(requirement.requirement_line)) # buildifier: disable=print | ||
logger.warn("falling back to pip for installing the right file for {}".format(requirement.requirement_line)) | ||
|
||
# We sort so that the lock-file remains the same no matter the order of how the | ||
# args are manipulated in the code going before. | ||
|
@@ -569,6 +583,13 @@ The Python version the dependencies are targetting, in Major.Minor format | |
If an interpreter isn't explicitly provided (using `python_interpreter` or | ||
`python_interpreter_target`), then the version specified here must have | ||
a corresponding `python.toolchain()` configured. | ||
""", | ||
), | ||
"verbosity": attr.string( | ||
default = "", | ||
values = ["TRACE", "DEBUG", "INFO"], | ||
doc = """ | ||
The verbosity with which we should print diagnostic messages when 'quiet = False'. | ||
""", | ||
), | ||
"whl_modifications": attr.label_keyed_string_dict( | ||
|
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,79 @@ | ||
# 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. | ||
|
||
""" | ||
A simple helper to populate the distribution attributes for each entry returned | ||
by the parse_requirements function. | ||
|
||
TODO @aignas 2024-05-23: The name is up for bikeshedding. For the time being I | ||
am keeping it together with parse_requirements.bzl. | ||
""" | ||
|
||
load(":whl_target_platforms.bzl", "select_whls") | ||
|
||
def parse_requirements_add_dists(requirements_by_platform, index_urls, python_version, logger = None): | ||
"""Populate dists based on the information from the PyPI index. | ||
|
||
This function will modify the given requirements_by_platform data structure. | ||
|
||
Args: | ||
requirements_by_platform: The result of parse_requirements function. | ||
index_urls: The result of simpleapi_download. | ||
python_version: The version of the python interpreter. | ||
logger: A logger for printing diagnostic info. | ||
""" | ||
for whl_name, requirements in requirements_by_platform.items(): | ||
for requirement in requirements: | ||
whls = [] | ||
sdist = None | ||
|
||
# TODO @aignas 2024-05-22: it is in theory possible to add all | ||
# requirements by version instead of by sha256. This may be useful | ||
# for some projects. | ||
for sha256 in requirement.srcs.shas: | ||
# For now if the artifact is marked as yanked we just ignore it. | ||
# | ||
# See https://packaging.python.org/en/latest/specifications/simple-repository-api/#adding-yank-support-to-the-simple-api | ||
|
||
maybe_whl = index_urls[whl_name].whls.get(sha256) | ||
if maybe_whl and not maybe_whl.yanked: | ||
whls.append(maybe_whl) | ||
continue | ||
|
||
maybe_sdist = index_urls[whl_name].sdists.get(sha256) | ||
if maybe_sdist and not maybe_sdist.yanked: | ||
sdist = maybe_sdist | ||
continue | ||
|
||
if logger: | ||
logger.warn("Could not find a whl or an sdist with sha256={}".format(sha256)) | ||
|
||
# Filter out the wheels that are incompatible with the target_platforms. | ||
whls = select_whls( | ||
whls = whls, | ||
want_abis = [ | ||
"none", | ||
"abi3", | ||
"cp" + python_version.replace(".", ""), | ||
# Older python versions have wheels for the `*m` ABI. | ||
"cp" + python_version.replace(".", "") + "m", | ||
], | ||
want_platforms = requirement.target_platforms, | ||
want_version = python_version, | ||
logger = logger, | ||
) | ||
|
||
requirement.whls.extend(whls) | ||
if sdist: | ||
requirement.sdists.append(sdist) |
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.
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.
Some extra format() calls probably aren't a big deal, but serializing a json object as part of a debug call is a bit much. Perhaps passing a lambda instead, to defer evaluation?
Also, repo_utils.debug_print exists to base it upon an env var