Skip to content

Commit

Permalink
refactor: switch to using --only
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Aug 23, 2022
1 parent 24bc96c commit cd6d467
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 18 deletions.
35 changes: 32 additions & 3 deletions cibuildwheel/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ def main() -> None:
)

parser.add_argument(
"--build",
"--only",
default=None,
help="The identifier expression for wheels to build. Overrides CIBW_BUILD.",
help="""
Force a single wheel build when given an identifier. Overrides
CIBW_BUILD/CIBW_SKIP. --platform and --arch cannot be specified
if this is given.
""",
)

parser.add_argument(
Expand Down Expand Up @@ -157,7 +161,32 @@ def main() -> None:
def build_in_directory(args: CommandLineArguments) -> None:
platform: PlatformName

if args.platform != "auto":
if args.only is not None:
if "linux_" in args.only:
platform = "linux"
elif "macosx_" in args.only:
platform = "macos"
elif "win_" in args.only:
platform = "windows"
else:
print(
f"Invalid --only='{args.only}', must be a build selector with a known platform",
file=sys.stderr,
)
sys.exit(2)
if args.platform != "auto":
print(
"--platform cannot be specified with --only, it is computed from --only",
file=sys.stderr,
)
sys.exit(2)
if args.archs is not None:
print(
"--arch cannot be specified with --only, it is computed from --only",
file=sys.stderr,
)
sys.exit(2)
elif args.platform != "auto":
platform = args.platform
else:
ci_provider = detect_ci_provider()
Expand Down
16 changes: 11 additions & 5 deletions cibuildwheel/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CommandLineArguments:
platform: Literal["auto", "linux", "macos", "windows"]
archs: str | None
output_dir: Path
build: str | None
only: str | None
config_file: str
package_dir: Path
print_build_identifiers: bool
Expand Down Expand Up @@ -374,7 +374,7 @@ def globals(self) -> GlobalOptions:
package_dir = args.package_dir
output_dir = args.output_dir

build_config = args.build or self.reader.get("build", env_plat=False, sep=" ") or "*"
build_config = self.reader.get("build", env_plat=False, sep=" ") or "*"
skip_config = self.reader.get("skip", env_plat=False, sep=" ")
test_skip = self.reader.get("test-skip", env_plat=False, sep=" ")

Expand All @@ -389,6 +389,15 @@ def globals(self) -> GlobalOptions:
)
requires_python = None if requires_python_str is None else SpecifierSet(requires_python_str)

archs_config_str = args.archs or self.reader.get("archs", sep=" ")
architectures = Architecture.parse_config(archs_config_str, platform=self.platform)

# Process `--only`
if args.only is not None:
build_config = args.only
skip_config = ""
architectures = Architecture.all_archs(self.platform)

build_selector = BuildSelector(
build_config=build_config,
skip_config=skip_config,
Expand All @@ -397,9 +406,6 @@ def globals(self) -> GlobalOptions:
)
test_selector = TestSelector(skip_config=test_skip)

archs_config_str = args.archs or self.reader.get("archs", sep=" ")
architectures = Architecture.parse_config(archs_config_str, platform=self.platform)

container_engine_str = self.reader.get("container-engine")

if container_engine_str not in ["docker", "podman"]:
Expand Down
71 changes: 62 additions & 9 deletions unit_test/main_tests/main_platform_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
from ..conftest import MOCK_PACKAGE_DIR


class ArgsInterceptor:
def __call__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs


def test_unknown_platform_non_ci(monkeypatch, capsys):
monkeypatch.delenv("CI", raising=False)
monkeypatch.delenv("BITRISE_BUILD_NUMBER", raising=False)
Expand Down Expand Up @@ -193,15 +199,62 @@ def test_archs_platform_all(platform, intercepted_build_args, monkeypatch):
}


@pytest.mark.parametrize("use_env_var", [False, True])
def test_build_argument(intercepted_build_args, monkeypatch, use_env_var):
@pytest.mark.parametrize(
"only,plat",
(
("cp311-manylinux_x86_64", "linux"),
("cp310-win_amd64", "windows"),
("cp311-macosx_x86_64", "macos"),
),
)
def test_only_argument(monkeypatch, only, plat):
from cibuildwheel import linux, macos, windows

if use_env_var:
monkeypatch.setenv("CIBW_BUILD", "cp310-*")
else:
monkeypatch.setenv("CIBW_BUILD", "unused")
monkeypatch.setattr(sys, "argv", sys.argv + ["--build", "cp310-*"])
monkeypatch.setenv("CIBW_BUILD", "unused")
monkeypatch.setenv("CIBW_SKIP", "unused")
monkeypatch.setattr(sys, "argv", sys.argv + ["--only", only])

intercepted = ArgsInterceptor()

if plat == "linux":
monkeypatch.setattr(linux, "build", intercepted)
elif plat == "macos":
monkeypatch.setattr(macos, "build", intercepted)
elif plat == "windows":
monkeypatch.setattr(windows, "build", intercepted)

main()
options = intercepted_build_args.args[0]
assert options.globals.build_selector.build_config == "cp310-*"
options = intercepted.args[0]
assert options.globals.build_selector.build_config == only
assert options.globals.build_selector.skip_config == ""
assert options.platform == plat
assert options.globals.architectures == Architecture.all_archs(plat)


@pytest.mark.parametrize("only", ("cp311-manylxinux_x86_64", "some_linux_thing"))
def test_only_failed(intercepted_build_args, monkeypatch, only):
monkeypatch.setattr(sys, "argv", sys.argv + ["--only", only])
monkeypatch.delenv("CIBW_PLATFORM")

with pytest.raises(SystemExit):
main()


def test_only_no_platform(intercepted_build_args, monkeypatch):
monkeypatch.delenv("CIBW_PLATFORM")
monkeypatch.setattr(
sys, "argv", sys.argv + ["--only", "cp311-manylinux_x86_64", "--platform", "macos"]
)

with pytest.raises(SystemExit):
main()


def test_only_no_archs(intercepted_build_args, monkeypatch):
monkeypatch.delenv("CIBW_PLATFORM")
monkeypatch.setattr(
sys, "argv", sys.argv + ["--only", "cp311-manylinux_x86_64", "--archs", "x86_64"]
)

with pytest.raises(SystemExit):
main()
2 changes: 1 addition & 1 deletion unit_test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def get_default_command_line_arguments() -> CommandLineArguments:
platform="auto",
allow_empty=False,
archs=None,
build=None,
only=None,
config_file="",
output_dir=Path("wheelhouse"),
package_dir=Path("."),
Expand Down

0 comments on commit cd6d467

Please sign in to comment.