refactor: reduce linux indentation with nicer breakup

This commit is contained in:
Henry Schreiner
2021-09-20 12:49:09 -04:00
parent bd7af00c78
commit a7f7abd10a
+57 -63
View File
@@ -87,43 +87,15 @@ def get_linux_platforms(
yield platform_configs, platform_tag, docker_image yield platform_configs, platform_tag, docker_image
def build(options: BuildOptions) -> None: def build_on_docker(
try: options: BuildOptions,
# check docker is installed platform_configs: List[PythonConfiguration],
subprocess.run(["docker", "--version"], check=True, stdout=subprocess.DEVNULL) docker: DockerContainer,
except Exception: container_project_path: PurePath,
print( container_package_dir: PurePath,
"cibuildwheel: Docker not found. Docker is required to run Linux builds. " ) -> None:
"If you're building on Travis CI, add `services: [docker]` to your .travis.yml."
"If you're building on Circle CI in Linux, add a `setup_remote_docker` step to your .circleci/config.yml",
file=sys.stderr,
)
sys.exit(2)
assert options.manylinux_images is not None
assert options.musllinux_images is not None
python_configurations = get_python_configurations(options.build_selector, options.architectures)
cwd = Path.cwd()
abs_package_dir = options.package_dir.resolve()
if cwd != abs_package_dir and cwd not in abs_package_dir.parents:
raise Exception("package_dir must be inside the working directory")
container_project_path = PurePath("/project")
container_package_dir = container_project_path / abs_package_dir.relative_to(cwd)
container_output_dir = PurePath("/output") container_output_dir = PurePath("/output")
for platform_configs, platform_tag, docker_image in get_linux_platforms(
options, python_configurations
):
try:
log.step(f"Starting Docker image {docker_image}...")
with DockerContainer(
docker_image,
simulate_32_bit=platform_tag.endswith("i686"),
cwd=container_project_path,
) as docker:
log.step("Copying project into Docker...") log.step("Copying project into Docker...")
docker.copy_into(Path.cwd(), container_project_path) docker.copy_into(Path.cwd(), container_project_path)
@@ -133,9 +105,7 @@ def build(options: BuildOptions) -> None:
env = docker.get_environment() env = docker.get_environment()
env["PATH"] = f'/opt/python/cp38-cp38/bin:{env["PATH"]}' env["PATH"] = f'/opt/python/cp38-cp38/bin:{env["PATH"]}'
env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1" env["PIP_DISABLE_PIP_VERSION_CHECK"] = "1"
env = options.environment.as_dictionary( env = options.environment.as_dictionary(env, executor=docker.environment_executor)
env, executor=docker.environment_executor
)
before_all_prepared = prepare_command( before_all_prepared = prepare_command(
options.before_all, options.before_all,
@@ -150,9 +120,7 @@ def build(options: BuildOptions) -> None:
dependency_constraint_flags: List[PathOrStr] = [] dependency_constraint_flags: List[PathOrStr] = []
if options.dependency_constraints: if options.dependency_constraints:
constraints_file = options.dependency_constraints.get_for_python_version( constraints_file = options.dependency_constraints.get_for_python_version(config.version)
config.version
)
container_constraints_file = PurePath("/constraints.txt") container_constraints_file = PurePath("/constraints.txt")
docker.copy_into(constraints_file, container_constraints_file) docker.copy_into(constraints_file, container_constraints_file)
@@ -166,14 +134,10 @@ def build(options: BuildOptions) -> None:
python_bin = config.path / "bin" python_bin = config.path / "bin"
env["PATH"] = f'{python_bin}:{env["PATH"]}' env["PATH"] = f'{python_bin}:{env["PATH"]}'
env = options.environment.as_dictionary( env = options.environment.as_dictionary(env, executor=docker.environment_executor)
env, executor=docker.environment_executor
)
# check config python is still on PATH # check config python is still on PATH
which_python = docker.call( which_python = docker.call(["which", "python"], env=env, capture_output=True).strip()
["which", "python"], env=env, capture_output=True
).strip()
if PurePath(which_python) != python_bin / "python": if PurePath(which_python) != python_bin / "python":
print( print(
"cibuildwheel: python available on PATH doesn't match our installed instance. If you have modified PATH, ensure that you don't overwrite cibuildwheel's entry or insert python above it.", "cibuildwheel: python available on PATH doesn't match our installed instance. If you have modified PATH, ensure that you don't overwrite cibuildwheel's entry or insert python above it.",
@@ -263,17 +227,10 @@ def build(options: BuildOptions) -> None:
# set up a virtual environment to install and test from, to make sure # set up a virtual environment to install and test from, to make sure
# there are no dependencies that were pulled in at build time. # there are no dependencies that were pulled in at build time.
docker.call( docker.call(["pip", "install", "virtualenv", *dependency_constraint_flags], env=env)
["pip", "install", "virtualenv", *dependency_constraint_flags], env=env venv_dir = PurePath(docker.call(["mktemp", "-d"], capture_output=True).strip()) / "venv"
)
venv_dir = (
PurePath(docker.call(["mktemp", "-d"], capture_output=True).strip())
/ "venv"
)
docker.call( docker.call(["python", "-m", "virtualenv", "--no-download", venv_dir], env=env)
["python", "-m", "virtualenv", "--no-download", venv_dir], env=env
)
virtualenv_env = env.copy() virtualenv_env = env.copy()
virtualenv_env["PATH"] = f"{venv_dir / 'bin'}:{virtualenv_env['PATH']}" virtualenv_env["PATH"] = f"{venv_dir / 'bin'}:{virtualenv_env['PATH']}"
@@ -300,9 +257,7 @@ def build(options: BuildOptions) -> None:
# Install any requirements to run the tests # Install any requirements to run the tests
if options.test_requires: if options.test_requires:
docker.call( docker.call(["pip", "install", *options.test_requires], env=virtualenv_env)
["pip", "install", *options.test_requires], env=virtualenv_env
)
# Run the tests from a different directory # Run the tests from a different directory
test_command_prepared = prepare_command( test_command_prepared = prepare_command(
@@ -310,9 +265,7 @@ def build(options: BuildOptions) -> None:
project=container_project_path, project=container_project_path,
package=container_package_dir, package=container_package_dir,
) )
docker.call( docker.call(["sh", "-c", test_command_prepared], cwd="/root", env=virtualenv_env)
["sh", "-c", test_command_prepared], cwd="/root", env=virtualenv_env
)
# clean up test environment # clean up test environment
docker.call(["rm", "-rf", venv_dir]) docker.call(["rm", "-rf", venv_dir])
@@ -327,6 +280,47 @@ def build(options: BuildOptions) -> None:
# copy the output back into the host # copy the output back into the host
docker.copy_out(container_output_dir, options.output_dir) docker.copy_out(container_output_dir, options.output_dir)
log.step_end() log.step_end()
def build(options: BuildOptions) -> None:
try:
# check docker is installed
subprocess.run(["docker", "--version"], check=True, stdout=subprocess.DEVNULL)
except Exception:
print(
"cibuildwheel: Docker not found. Docker is required to run Linux builds. "
"If you're building on Travis CI, add `services: [docker]` to your .travis.yml."
"If you're building on Circle CI in Linux, add a `setup_remote_docker` step to your .circleci/config.yml",
file=sys.stderr,
)
sys.exit(2)
assert options.manylinux_images is not None
assert options.musllinux_images is not None
python_configurations = get_python_configurations(options.build_selector, options.architectures)
cwd = Path.cwd()
abs_package_dir = options.package_dir.resolve()
if cwd != abs_package_dir and cwd not in abs_package_dir.parents:
raise Exception("package_dir must be inside the working directory")
container_project_path = PurePath("/project")
container_package_dir = container_project_path / abs_package_dir.relative_to(cwd)
for platform_configs, platform_tag, docker_image in get_linux_platforms(
options, python_configurations
):
try:
log.step(f"Starting Docker image {docker_image}...")
with DockerContainer(
docker_image,
simulate_32_bit=platform_tag.endswith("i686"),
cwd=container_project_path,
) as docker:
build_on_docker(
options, platform_configs, docker, container_project_path, container_package_dir
)
except subprocess.CalledProcessError as error: except subprocess.CalledProcessError as error:
log.step_end_with_error( log.step_end_with_error(
f"Command {error.cmd} failed with code {error.returncode}. {error.stdout}" f"Command {error.cmd} failed with code {error.returncode}. {error.stdout}"