Skip to content

Commit

Permalink
Merge pull request #1461 from hoodmane/platform-interface
Browse files Browse the repository at this point in the history
chore: Add a PlatformModule type
  • Loading branch information
joerick committed Apr 14, 2023
2 parents 1c1df68 + 420ef86 commit 7c9b837
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
54 changes: 27 additions & 27 deletions cibuildwheel/__main__.py
Expand Up @@ -244,6 +244,28 @@ def _compute_platform(args: CommandLineArguments) -> PlatformName:
return _compute_platform_ci()


class PlatformModule(typing.Protocol):
# note that as per PEP544, the self argument is ignored when the protocol
# is applied to a module
def get_python_configurations(
self, build_selector: BuildSelector, architectures: Set[Architecture]
) -> Sequence[GenericPythonConfiguration]:
...

def build(self, options: Options, tmp_path: Path) -> None:
...


def get_platform_module(platform: PlatformName) -> PlatformModule:

Check warning on line 259 in cibuildwheel/__main__.py

View workflow job for this annotation

GitHub Actions / Linters (mypy, flake8, etc.)

Either all return statements in a function should return an expression, or none of them should.
if platform == "linux":
return cibuildwheel.linux
if platform == "windows":
return cibuildwheel.windows
if platform == "macos":
return cibuildwheel.macos
assert_never(platform) # noqa: RET503


def build_in_directory(args: CommandLineArguments) -> None:
platform: PlatformName = _compute_platform(args)
options = compute_options(platform=platform, command_line_arguments=args, env=os.environ)
Expand All @@ -257,8 +279,9 @@ def build_in_directory(args: CommandLineArguments) -> None:
print(msg, file=sys.stderr)
sys.exit(2)

platform_module = get_platform_module(platform)
identifiers = get_build_identifiers(
platform=platform,
platform_module=platform_module,
build_selector=options.globals.build_selector,
architectures=options.globals.architectures,
)
Expand Down Expand Up @@ -304,14 +327,7 @@ def build_in_directory(args: CommandLineArguments) -> None:
with cibuildwheel.util.print_new_wheels(
"\n{n} wheels produced in {m:.0f} minutes:", output_dir
):
if platform == "linux":
cibuildwheel.linux.build(options, tmp_path)
elif platform == "windows":
cibuildwheel.windows.build(options, tmp_path)
elif platform == "macos":
cibuildwheel.macos.build(options, tmp_path)
else:
assert_never(platform)
platform_module.build(options, tmp_path)
finally:
# avoid https://github.com/python/cpython/issues/86962 by performing
# cleanup manually
Expand Down Expand Up @@ -354,25 +370,9 @@ def print_preamble(platform: str, options: Options, identifiers: list[str]) -> N


def get_build_identifiers(
platform: PlatformName, build_selector: BuildSelector, architectures: Set[Architecture]
platform_module: PlatformModule, build_selector: BuildSelector, architectures: Set[Architecture]
) -> list[str]:
python_configurations: Sequence[GenericPythonConfiguration]

if platform == "linux":
python_configurations = cibuildwheel.linux.get_python_configurations(
build_selector, architectures
)
elif platform == "windows":
python_configurations = cibuildwheel.windows.get_python_configurations(
build_selector, architectures
)
elif platform == "macos":
python_configurations = cibuildwheel.macos.get_python_configurations(
build_selector, architectures
)
else:
assert_never(platform)

python_configurations = platform_module.get_python_configurations(build_selector, architectures)
return [config.identifier for config in python_configurations]


Expand Down
5 changes: 3 additions & 2 deletions unit_test/options_test.py
Expand Up @@ -7,7 +7,7 @@

import pytest

from cibuildwheel.__main__ import get_build_identifiers
from cibuildwheel.__main__ import get_build_identifiers, get_platform_module
from cibuildwheel.bashlex_eval import local_environment_executor
from cibuildwheel.environment import parse_environment
from cibuildwheel.options import (
Expand Down Expand Up @@ -49,8 +49,9 @@ def test_options_1(tmp_path, monkeypatch):

options = Options(platform="linux", command_line_arguments=args, env={})

module = get_platform_module("linux")
identifiers = get_build_identifiers(
platform="linux",
platform_module=module,
build_selector=options.globals.build_selector,
architectures=options.globals.architectures,
)
Expand Down

0 comments on commit 7c9b837

Please sign in to comment.