style: apply black via pre-commit run -a

This commit is contained in:
Henry Schreiner
2021-05-03 13:12:36 -04:00
committed by Henry Schreiner
parent 9cbed6a9be
commit 178aaea6c7
53 changed files with 1479 additions and 714 deletions
+115 -36
View File
@@ -41,7 +41,8 @@ def get_python_configurations(
# return all configurations whose arch is in our `architectures` set,
# and match the build/skip rules
return [
c for c in python_configurations
c
for c in python_configurations
if any(c.identifier.endswith(arch.value) for arch in architectures)
and build_selector(c.identifier)
]
@@ -52,10 +53,12 @@ def build(options: BuildOptions) -> None:
# 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)
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
@@ -79,13 +82,21 @@ def build(options: BuildOptions) -> None:
container_output_dir = PurePath('/output')
for implementation, platform_tag, docker_image in platforms:
platform_configs = [c for c in python_configurations if c.identifier.startswith(implementation) and c.identifier.endswith(platform_tag)]
platform_configs = [
c
for c in python_configurations
if c.identifier.startswith(implementation) and c.identifier.endswith(platform_tag)
]
if not platform_configs:
continue
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:
with DockerContainer(
docker_image,
simulate_32_bit=platform_tag.endswith('i686'),
cwd=container_project_path,
) as docker:
log.step('Copying project into Docker...')
docker.copy_into(Path.cwd(), container_project_path)
@@ -96,9 +107,15 @@ def build(options: BuildOptions) -> None:
env = docker.get_environment()
env['PATH'] = f'/opt/python/cp38-cp38/bin:{env["PATH"]}'
env['PIP_DISABLE_PIP_VERSION_CHECK'] = '1'
env = options.environment.as_dictionary(env, executor=docker.environment_executor)
env = options.environment.as_dictionary(
env, executor=docker.environment_executor
)
before_all_prepared = prepare_command(options.before_all, project=container_project_path, package=container_package_dir)
before_all_prepared = prepare_command(
options.before_all,
project=container_project_path,
package=container_package_dir,
)
docker.call(['sh', '-c', before_all_prepared], env=env)
for config in platform_configs:
@@ -112,12 +129,24 @@ def build(options: BuildOptions) -> None:
patch_docker_path = PurePath('/pypy_venv.patch')
docker.copy_into(patch_path, patch_docker_path)
try:
docker.call(['patch', '--force', '-p1', '-d', config.path, '-i', patch_docker_path])
docker.call(
[
'patch',
'--force',
'-p1',
'-d',
config.path,
'-i',
patch_docker_path,
]
)
except subprocess.CalledProcessError:
print("PyPy patch not applied", file=sys.stderr)
if options.dependency_constraints:
constraints_file = options.dependency_constraints.get_for_python_version(config.version)
constraints_file = options.dependency_constraints.get_for_python_version(
config.version
)
container_constraints_file = PurePath('/constraints.txt')
docker.copy_into(constraints_file, container_constraints_file)
@@ -131,22 +160,36 @@ def build(options: BuildOptions) -> None:
python_bin = config.path / 'bin'
env['PATH'] = f'{python_bin}:{env["PATH"]}'
env = options.environment.as_dictionary(env, executor=docker.environment_executor)
env = options.environment.as_dictionary(
env, executor=docker.environment_executor
)
# check config python and pip are still on PATH
which_python = docker.call(['which', 'python'], env=env, capture_output=True).strip()
which_python = docker.call(
['which', 'python'], env=env, capture_output=True
).strip()
if PurePath(which_python) != python_bin / 'python':
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.", file=sys.stderr)
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.",
file=sys.stderr,
)
sys.exit(1)
which_pip = docker.call(['which', 'pip'], env=env, capture_output=True).strip()
if PurePath(which_pip) != python_bin / 'pip':
print("cibuildwheel: pip 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 pip above it.", file=sys.stderr)
print(
"cibuildwheel: pip 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 pip above it.",
file=sys.stderr,
)
sys.exit(1)
if options.before_build:
log.step('Running before_build...')
before_build_prepared = prepare_command(options.before_build, project=container_project_path, package=container_package_dir)
before_build_prepared = prepare_command(
options.before_build,
project=container_project_path,
package=container_package_dir,
)
docker.call(['sh', '-c', before_build_prepared], env=env)
log.step('Building wheel...')
@@ -156,13 +199,18 @@ def build(options: BuildOptions) -> None:
docker.call(['rm', '-rf', built_wheel_dir])
docker.call(['mkdir', '-p', built_wheel_dir])
docker.call([
'pip', 'wheel',
container_package_dir,
'--wheel-dir', built_wheel_dir,
'--no-deps',
*get_build_verbosity_extra_flags(options.build_verbosity)
], env=env)
docker.call(
[
'pip',
'wheel',
container_package_dir,
'--wheel-dir',
built_wheel_dir,
'--no-deps',
*get_build_verbosity_extra_flags(options.build_verbosity),
],
env=env,
)
built_wheel = docker.glob(built_wheel_dir, '*.whl')[0]
@@ -175,7 +223,9 @@ def build(options: BuildOptions) -> None:
if options.repair_command:
log.step('Repairing wheel...')
repair_command_prepared = prepare_command(options.repair_command, wheel=built_wheel, dest_dir=repaired_wheel_dir)
repair_command_prepared = prepare_command(
options.repair_command, wheel=built_wheel, dest_dir=repaired_wheel_dir
)
docker.call(['sh', '-c', repair_command_prepared], env=env)
else:
docker.call(['mv', built_wheel, repaired_wheel_dir])
@@ -187,16 +237,27 @@ def build(options: BuildOptions) -> None:
# set up a virtual environment to install and test from, to make sure
# there are no dependencies that were pulled in at build time.
docker.call(['pip', 'install', 'virtualenv', *dependency_constraint_flags], env=env)
venv_dir = PurePath(docker.call(['mktemp', '-d'], capture_output=True).strip()) / 'venv'
docker.call(
['pip', 'install', 'virtualenv', *dependency_constraint_flags], env=env
)
venv_dir = (
PurePath(docker.call(['mktemp', '-d'], capture_output=True).strip())
/ 'venv'
)
docker.call(['python', '-m', 'virtualenv', '--no-download', venv_dir], env=env)
docker.call(
['python', '-m', 'virtualenv', '--no-download', venv_dir], env=env
)
virtualenv_env = env.copy()
virtualenv_env['PATH'] = f"{venv_dir / 'bin'}:{virtualenv_env['PATH']}"
if options.before_test:
before_test_prepared = prepare_command(options.before_test, project=container_project_path, package=container_package_dir)
before_test_prepared = prepare_command(
options.before_test,
project=container_project_path,
package=container_package_dir,
)
docker.call(['sh', '-c', before_test_prepared], env=virtualenv_env)
# Install the wheel we just built
@@ -206,15 +267,26 @@ def build(options: BuildOptions) -> None:
# different external shared libraries. so it doesn't matter which one we run the tests on.
# Let's just pick the first one.
wheel_to_test = repaired_wheels[0]
docker.call(['pip', 'install', str(wheel_to_test) + options.test_extras], env=virtualenv_env)
docker.call(
['pip', 'install', str(wheel_to_test) + options.test_extras],
env=virtualenv_env,
)
# Install any requirements to run the tests
if options.test_requires:
docker.call(['pip', 'install', *options.test_requires], env=virtualenv_env)
docker.call(
['pip', 'install', *options.test_requires], env=virtualenv_env
)
# Run the tests from a different directory
test_command_prepared = prepare_command(options.test_command, project=container_project_path, package=container_package_dir)
docker.call(['sh', '-c', test_command_prepared], cwd='/root', env=virtualenv_env)
test_command_prepared = prepare_command(
options.test_command,
project=container_project_path,
package=container_package_dir,
)
docker.call(
['sh', '-c', test_command_prepared], cwd='/root', env=virtualenv_env
)
# clean up test environment
docker.call(['rm', '-rf', venv_dir])
@@ -230,19 +302,23 @@ def build(options: BuildOptions) -> None:
docker.copy_out(container_output_dir, options.output_dir)
log.step_end()
except subprocess.CalledProcessError as error:
log.step_end_with_error(f'Command {error.cmd} failed with code {error.returncode}. {error.stdout}')
log.step_end_with_error(
f'Command {error.cmd} failed with code {error.returncode}. {error.stdout}'
)
troubleshoot(options.package_dir, error)
sys.exit(1)
def troubleshoot(package_dir: Path, error: Exception) -> None:
if (isinstance(error, subprocess.CalledProcessError) and error.cmd[0:2] == ['pip', 'wheel']):
if isinstance(error, subprocess.CalledProcessError) and error.cmd[0:2] == ['pip', 'wheel']:
# the 'pip wheel' step failed.
print('Checking for common errors...')
so_files = list(package_dir.glob('**/*.so'))
if so_files:
print(textwrap.dedent('''
print(
textwrap.dedent(
'''
NOTE: Shared object (.so) files found in this project.
These files might be built against the wrong OS, causing problems with
@@ -250,7 +326,10 @@ def troubleshoot(package_dir: Path, error: Exception) -> None:
If you're using Cython and have previously done an in-place build,
remove those build files (*.so and *.c) before starting cibuildwheel.
'''), file=sys.stderr)
'''
),
file=sys.stderr,
)
print(' Files detected:')
print('\n'.join(f' {f}' for f in so_files))