From 3bd759f50c9f0aa3fcfcf51c6720a202d42d2469 Mon Sep 17 00:00:00 2001 From: Joe Rickerby Date: Mon, 27 Jun 2022 17:26:31 +0100 Subject: [PATCH] Add some type safety to the container_engine option --- cibuildwheel/docker_container.py | 10 ++++++---- cibuildwheel/options.py | 13 +++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cibuildwheel/docker_container.py b/cibuildwheel/docker_container.py index de41ded9..7f01b2fe 100644 --- a/cibuildwheel/docker_container.py +++ b/cibuildwheel/docker_container.py @@ -13,7 +13,9 @@ from typing import IO, Dict, List, Optional, Sequence, Type, cast from cibuildwheel.util import CIProvider, detect_ci_provider -from .typing import PathOrStr, PopenBytes +from .typing import Literal, PathOrStr, PopenBytes + +ContainerEngine = Literal["docker", "podman"] class DockerContainer: @@ -54,7 +56,7 @@ class DockerContainer: docker_image: str, simulate_32_bit: bool = False, cwd: Optional[PathOrStr] = None, - container_engine: str = "docker", + container_engine: ContainerEngine = "docker", ): if not docker_image: raise ValueError("Must have a non-empty docker image to run.") @@ -193,8 +195,6 @@ class DockerContainer: to_path.mkdir(parents=True, exist_ok=True) if self.container_engine == "podman": - # There is a bug in docker that prevents this simple implementation - # from working https://github.com/moby/moby/issues/38995 subprocess.run( [ self.container_engine, @@ -206,6 +206,8 @@ class DockerContainer: cwd=to_path, ) elif self.container_engine == "docker": + # There is a bug in docker that prevents a simple 'cp' invocation + # from working https://github.com/moby/moby/issues/38995 command = f"{self.container_engine} exec -i {self.name} tar -cC {shell_quote(from_path)} -f - . | tar -xf -" subprocess.run( command, diff --git a/cibuildwheel/options.py b/cibuildwheel/options.py index 80ee5327..e30cd47f 100644 --- a/cibuildwheel/options.py +++ b/cibuildwheel/options.py @@ -16,6 +16,7 @@ from typing import ( Set, Tuple, Union, + cast, ) if sys.version_info >= (3, 11): @@ -26,6 +27,7 @@ else: from packaging.specifiers import SpecifierSet from .architecture import Architecture +from .docker_container import ContainerEngine from .environment import EnvironmentParseError, ParsedEnvironment, parse_environment from .projectfiles import get_requires_python_str from .typing import PLATFORMS, Literal, PlatformName, TypedDict @@ -79,7 +81,7 @@ class BuildOptions(NamedTuple): test_extras: str build_verbosity: int build_frontend: BuildFrontend - container_engine: str + container_engine: ContainerEngine @property def package_dir(self) -> Path: @@ -423,7 +425,14 @@ class Options: test_requires = self.reader.get("test-requires", sep=" ").split() test_extras = self.reader.get("test-extras", sep=",") build_verbosity_str = self.reader.get("build-verbosity") - container_engine = self.reader.get("container-engine") + container_engine_str = self.reader.get("container-engine") + + if container_engine_str not in ["docker", "podman"]: + msg = f"cibuildwheel: Unrecognised container_engine '{container_engine_str}', only 'docker' and 'podman' are supported" + print(msg, file=sys.stderr) + sys.exit(2) + + container_engine = cast(ContainerEngine, container_engine_str) build_frontend: BuildFrontend if build_frontend_str == "build":