Skip to content

Commit

Permalink
refactor: use passenv on Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Nov 13, 2021
1 parent 4b8632c commit 7f379b1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
12 changes: 10 additions & 2 deletions cibuildwheel/docker_container.py
Expand Up @@ -7,7 +7,7 @@
import uuid
from pathlib import Path, PurePath
from types import TracebackType
from typing import IO, Dict, List, Optional, Sequence, Type, cast
from typing import IO, Dict, List, Optional, Sequence, Tuple, Type, cast

from .typing import PathOrStr, PopenBytes

Expand All @@ -31,7 +31,12 @@ class DockerContainer:
bash_stdout: IO[bytes]

def __init__(
self, *, docker_image: str, simulate_32_bit: bool = False, cwd: Optional[PathOrStr] = None
self,
*,
docker_image: str,
simulate_32_bit: bool = False,
cwd: Optional[PathOrStr] = None,
passenv: Tuple[str, ...] = (),
):
if not docker_image:
raise ValueError("Must have a non-empty docker image to run.")
Expand All @@ -40,16 +45,19 @@ def __init__(
self.simulate_32_bit = simulate_32_bit
self.cwd = cwd
self.name: Optional[str] = None
self.passenv = passenv

def __enter__(self) -> "DockerContainer":
self.name = f"cibuildwheel-{uuid.uuid4()}"
cwd_args = ["-w", str(self.cwd)] if self.cwd else []
shell_args = ["linux32", "/bin/bash"] if self.simulate_32_bit else ["/bin/bash"]
passenv = (f"--env={name}" for name in self.passenv)
subprocess.run(
[
"docker",
"create",
"--env=CIBUILDWHEEL",
*passenv,
f"--name={self.name}",
"--interactive",
"--volume=/:/host", # ignored on CircleCI
Expand Down
3 changes: 0 additions & 3 deletions cibuildwheel/environment.py
Expand Up @@ -84,9 +84,6 @@ def as_dictionary(

return environment

def add(self, name: str, value: str) -> None:
self.assignments.append(EnvironmentAssignment(f'{name}="{value}"'))

def as_shell_commands(self) -> List[str]:
return [a.as_shell_assignment() for a in self.assignments]

Expand Down
10 changes: 8 additions & 2 deletions cibuildwheel/linux.py
Expand Up @@ -32,6 +32,7 @@ class BuildStep(NamedTuple):
platform_configs: List[PythonConfiguration]
platform_tag: str
docker_image: str
passenv: Tuple[str, ...]


def get_python_configurations(
Expand Down Expand Up @@ -85,15 +86,19 @@ def get_build_steps(
_, platform_tag = config.identifier.split("-", 1)

before_all = options.build_options(config.identifier).before_all
passenv = options.build_options(config.identifier).passenv
docker_image = docker_image_for_python_configuration(config, options)

step_key = (platform_tag, docker_image, before_all)
step_key = (platform_tag, docker_image, before_all, passenv)

if step_key in steps:
steps[step_key].platform_configs.append(config)
else:
steps[step_key] = BuildStep(
platform_configs=[config], platform_tag=platform_tag, docker_image=docker_image
platform_configs=[config],
platform_tag=platform_tag,
docker_image=docker_image,
passenv=passenv,
)

yield from steps.values()
Expand Down Expand Up @@ -339,6 +344,7 @@ def build(options: Options) -> None:
docker_image=build_step.docker_image,
simulate_32_bit=build_step.platform_tag.endswith("i686"),
cwd=container_project_path,
passenv=build_step.passenv,
) as docker:

build_on_docker(
Expand Down
12 changes: 3 additions & 9 deletions cibuildwheel/options.py
Expand Up @@ -60,6 +60,7 @@ class GlobalOptions(NamedTuple):
class BuildOptions(NamedTuple):
globals: GlobalOptions
environment: ParsedEnvironment
passenv: Tuple[str, ...]
before_all: str
before_build: Optional[str]
repair_command: str
Expand Down Expand Up @@ -408,7 +409,7 @@ def build_options(self, identifier: Optional[str]) -> BuildOptions:
environment_config = self.reader.get(
"environment", table={"item": '{k}="{v}"', "sep": " "}
)
passenv = self.reader.get("passenv", sep=",").split(",")
passenv = tuple(self.reader.get("passenv", sep=" ").split())
before_build = self.reader.get("before-build", sep=" && ")
repair_command = self.reader.get("repair-wheel-command", sep=" && ")

Expand Down Expand Up @@ -439,14 +440,6 @@ def build_options(self, identifier: Optional[str]) -> BuildOptions:
traceback.print_exc(None, sys.stderr)
sys.exit(2)

# Pass through environment variables
if self.platform == "linux":
for passedenv in passenv:
try:
environment.add(passedenv, os.environ[passedenv])
except KeyError:
pass

if dependency_versions == "pinned":
dependency_constraints: Optional[
DependencyConstraints
Expand Down Expand Up @@ -515,6 +508,7 @@ def build_options(self, identifier: Optional[str]) -> BuildOptions:
build_verbosity=build_verbosity,
repair_command=repair_command,
environment=environment,
passenv=passenv,
dependency_constraints=dependency_constraints,
manylinux_images=manylinux_images or None,
musllinux_images=musllinux_images or None,
Expand Down
2 changes: 1 addition & 1 deletion unit_test/options_test.py
Expand Up @@ -85,4 +85,4 @@ def test_passthrough(tmp_path, monkeypatch):

default_build_options = options.build_options(identifier=None)

assert default_build_options.environment == parse_environment('FOO="BAR" EXAMPLE_ENV="ONE"')
assert default_build_options.passenv == ("EXAMPLE_ENV",)

0 comments on commit 7f379b1

Please sign in to comment.