-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: override setuptools build backend
- Add custom build backend which overrides certain hooks - Include custom build backend in sdists via `MANIFEST.in` - Remove `build>=1.0.0` workaround from `build-and-sign.sh` - Add build backend tests, update order of tests and coverage config
- Loading branch information
1 parent
97b5e18
commit 194d9bc
Showing
8 changed files
with
141 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import shlex | ||
from typing import List, Optional, Tuple | ||
|
||
from setuptools import build_meta as _build_meta | ||
|
||
# re-export everything from `setuptools.build_meta`, so that we don't have to worry about any hooks which we don't override | ||
# https://peps.python.org/pep-0517/ | ||
# https://peps.python.org/pep-0660/ | ||
# noinspection PyUnresolvedReferences | ||
from setuptools.build_meta import * # noqa: F403 | ||
from setuptools.command.egg_info import egg_info as _egg_info | ||
|
||
|
||
# ---- | ||
|
||
|
||
def get_requires_for_build_wheel( # type: ignore[no-redef] | ||
config_settings: Optional[dict] = None, | ||
) -> List[str]: # pragma: no cover | ||
# Streamlink publishes three wheels on PyPI: the generic "any" wheel, the "win32" wheel and the "win-amd64" wheel: | ||
# The Windows-wheels are special, because they include a "gui_scripts" entry point, which is used by the `pip` frontend | ||
# to generate the "streamlinkw" launcher, which doesn't open a terminal window when launching it from a GUI application. | ||
# | ||
# In order to build these special Windows-wheels, the `--plat-name=...` CLI argument needs to get passed | ||
# to the `bdist_wheel` setuptools command (provided by the `wheel` package). With the introduction of PEP517 however, | ||
# setuptools's CLI is not used directly anymore, and instead, we have to build the wheels using the `build` package | ||
# and the `--wheel --config-setting=--build-option=...` args, which are then forwarded to setuptools via the PEP517 hooks. | ||
# | ||
# Since `build==1.0.0` though, the `--config-setting` data now gets passed to all build-backend hooks involved, | ||
# even `get_requires_for_build_wheel()`. This results in the build failing, as it executes setuptools's `egg_info` command, | ||
# which doesn't accept the `--plat-name` argument or any other `bdist_wheel` command options. | ||
# | ||
# As a consequence of this, we need to override the build-backend and the `get_requires_for_build_wheel()` hook, so that | ||
# we can filter out arguments which are not relevant to the `egg_info` command. | ||
_filter_cmd_option_args(config_settings, "--build-option", _egg_info.user_options) | ||
|
||
return _build_meta.get_requires_for_build_wheel(config_settings) | ||
|
||
|
||
# ---- | ||
|
||
|
||
def _filter_cmd_option_args( | ||
config_settings: Optional[dict], | ||
key: str, | ||
options: List[Tuple[str, Optional[str], str]], | ||
) -> None: | ||
"""Filter out args which are not recognized by a specific command and its options""" | ||
|
||
if not config_settings or not config_settings.get(key): | ||
return | ||
|
||
parsed = shlex.split(config_settings[key]) | ||
|
||
result = [] | ||
val_next = False | ||
for item in parsed: | ||
if val_next: | ||
val_next = False | ||
result.append(item) | ||
continue | ||
for full, shorthand, *_ in options: | ||
is_boolean = full[-1] != "=" | ||
is_shorthand = shorthand is not None and item == f"-{shorthand}" | ||
if not is_boolean and (is_shorthand or item == f"--{full[:-1]}"): | ||
val_next = True | ||
result.append(item) | ||
break | ||
if ( | ||
is_boolean and (is_shorthand or item == f"--{full}") | ||
or not is_boolean and item.startswith(f"--{full}") | ||
): | ||
result.append(item) | ||
break | ||
|
||
if result: | ||
config_settings[key] = shlex.join(result) | ||
else: | ||
del config_settings[key] |
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,41 @@ | ||
import pytest | ||
from setuptools.command.egg_info import egg_info | ||
|
||
from build_backend import _filter_cmd_option_args | ||
|
||
|
||
@pytest.mark.parametrize(("config_settings", "expected", "options"), [ | ||
pytest.param( | ||
None, | ||
None, | ||
egg_info.user_options, | ||
id="Empty config_settings", | ||
), | ||
pytest.param( | ||
{"foo": "bar"}, | ||
{"foo": "bar"}, | ||
egg_info.user_options, | ||
id="No --build-option key", | ||
), | ||
pytest.param( | ||
{"--build-option": "--egg-base=foo/bar -e baz/qux --tag-build foo -b bar --tag-date --no-date -D"}, | ||
{"--build-option": "--egg-base=foo/bar -e baz/qux --tag-build foo -b bar --tag-date --no-date -D"}, | ||
egg_info.user_options, | ||
id="All egg_info options", | ||
), | ||
pytest.param( | ||
{"--build-option": "--foo --bar --baz"}, | ||
{}, | ||
egg_info.user_options, | ||
id="Options unknown to egg_info", | ||
), | ||
pytest.param( | ||
{"--build-option": "-p win32 --plat-name win32 --plat-name=win32"}, | ||
{}, | ||
egg_info.user_options, | ||
id="bdist_wheel --plat-name option", | ||
), | ||
]) | ||
def test_filter_cmd_option_args(config_settings: dict, expected: str, options: list): | ||
_filter_cmd_option_args(config_settings, "--build-option", options) | ||
assert config_settings == expected |
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ lxml-stubs | |
trio-typing | ||
types-freezegun | ||
types-requests | ||
types-setuptools | ||
types-urllib3 | ||
|
||
# scripts | ||
|
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
} | ||
|
||
_TEST_PRIORITIES = ( | ||
"build_backend/", | ||
"tests/testutils/", | ||
"tests/utils/", | ||
None, | ||
|