How to use the cibuildwheel.util.prepare_command function in cibuildwheel

To help you get started, we’ve selected a few cibuildwheel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github joerick / cibuildwheel / cibuildwheel / linux.py View on Github external
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()
                    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)
                        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)
                        exit(1)

                    if options.before_build:
                        before_build_prepared = prepare_command(options.before_build, project='/project', package=container_package_dir)
                        docker.call(['sh', '-c', before_build_prepared], env=env)

                    temp_dir = PurePath('/tmp/cibuildwheel')
                    built_wheel_dir = temp_dir / 'built_wheel'
                    docker.call(['rm', '-rf', built_wheel_dir])
                    docker.call(['mkdir', '-p', built_wheel_dir])

                    docker.call([
                        'pip', 'wheel',
                        container_package_dir,
                        '-w', built_wheel_dir,
                        '--no-deps',
                        *get_build_verbosity_extra_flags(options.build_verbosity)
                    ], env=env)

                    built_wheel = docker.glob(built_wheel_dir / '*.whl')[0]
github joerick / cibuildwheel / cibuildwheel / linux.py View on Github external
# Install the wheel we just built
                        # Note: If auditwheel produced two wheels, it's because the earlier produced wheel
                        # conforms to multiple manylinux standards. These multiple versions of the wheel are
                        # functionally the same, differing only in name, wheel metadata, and possibly include
                        # 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)

                        # Install any requirements to run the tests
                        if options.test_requires:
                            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='/project', 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])

                    # move repaired wheels to output
                    docker.call(['mkdir', '-p', container_output_dir])
                    docker.call(['mv', *repaired_wheels, container_output_dir])

                # copy the output back into the host
                docker.copy_out(container_output_dir, options.output_dir)
        except subprocess.CalledProcessError as error:
            print(f'Command {error.cmd} failed with code {error.returncode}. {error.stdout}')
            troubleshoot(options.package_dir, error)
            exit(1)
github joerick / cibuildwheel / cibuildwheel / linux.py View on Github external
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)]
        if not platform_configs:
            continue

        try:
            with DockerContainer(docker_image, simulate_32_bit=platform_tag.endswith('i686')) as docker:
                docker.copy_into(Path.cwd(), Path('/project'))

                if options.before_all:
                    env = docker.get_environment()
                    env['PATH'] = f'/opt/python/cp38-cp38:{env["PATH"]}'
                    env = options.environment.as_dictionary(env, executor=docker.environment_executor)

                    before_all_prepared = prepare_command(options.before_all, project='/project', package=container_package_dir)
                    docker.call(['sh', '-c', before_all_prepared], env=env)

                for config in platform_configs:
                    dependency_constraint_flags: List[Union[str, PathLike]] = []

                    if options.dependency_constraints:
                        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)
                        dependency_constraint_flags = ['-c', container_constraints_file]

                    env = docker.get_environment()

                    # put this config's python top of the list
                    python_bin = config.path / 'bin'
github joerick / cibuildwheel / cibuildwheel / windows.py View on Github external
project='.',
                    package=options.package_dir
                )
                shell([before_test_prepared], env=virtualenv_env)

            # install the wheel
            shell(['pip', 'install', str(repaired_wheel) + options.test_extras], env=virtualenv_env)

            # test the wheel
            if options.test_requires:
                shell(['pip', 'install'] + options.test_requires, env=virtualenv_env)

            # run the tests from c:\, with an absolute path in the command
            # (this ensures that Python runs the tests against the installed wheel
            # and not the repo code)
            test_command_prepared = prepare_command(
                options.test_command,
                project=Path('.').resolve(),
                package=options.package_dir.resolve()
            )
            shell([test_command_prepared], cwd='c:\\', env=virtualenv_env)

            # clean up
            shutil.rmtree(venv_dir)

        # we're all done here; move it to output (remove if already exists)
        repaired_wheel.replace(options.output_dir / repaired_wheel.name)
github joerick / cibuildwheel / cibuildwheel / windows.py View on Github external
shell([before_all_prepared], env=env)

    python_configurations = get_python_configurations(options.build_selector)
    for config in python_configurations:
        dependency_constraint_flags = []
        if options.dependency_constraints:
            dependency_constraint_flags = [
                '-c', str(options.dependency_constraints.get_for_python_version(config.version))
            ]

        # install Python
        env = setup_python(config, dependency_constraint_flags, options.environment)

        # run the before_build command
        if options.before_build:
            before_build_prepared = prepare_command(options.before_build, project='.', package=options.package_dir)
            shell([before_build_prepared], env=env)

        # build the wheel
        if built_wheel_dir.exists():
            shutil.rmtree(built_wheel_dir)
        built_wheel_dir.mkdir(parents=True)
        # Path.resolve() is needed. Without it pip wheel may try to fetch package from pypi.org
        # see https://github.com/joerick/cibuildwheel/pull/369
        shell(['pip', 'wheel', str(options.package_dir.resolve()), '-w', str(built_wheel_dir), '--no-deps'] + get_build_verbosity_extra_flags(options.build_verbosity), env=env)
        built_wheel = next(built_wheel_dir.glob('*.whl'))

        # repair the wheel
        if repaired_wheel_dir.exists():
            shutil.rmtree(repaired_wheel_dir)
        repaired_wheel_dir.mkdir(parents=True)
        if built_wheel.name.endswith('none-any.whl') or not options.repair_command:
github joerick / cibuildwheel / cibuildwheel / windows.py View on Github external
shutil.rmtree(built_wheel_dir)
        built_wheel_dir.mkdir(parents=True)
        # Path.resolve() is needed. Without it pip wheel may try to fetch package from pypi.org
        # see https://github.com/joerick/cibuildwheel/pull/369
        shell(['pip', 'wheel', str(options.package_dir.resolve()), '-w', str(built_wheel_dir), '--no-deps'] + get_build_verbosity_extra_flags(options.build_verbosity), env=env)
        built_wheel = next(built_wheel_dir.glob('*.whl'))

        # repair the wheel
        if repaired_wheel_dir.exists():
            shutil.rmtree(repaired_wheel_dir)
        repaired_wheel_dir.mkdir(parents=True)
        if built_wheel.name.endswith('none-any.whl') or not options.repair_command:
            # pure Python wheel or empty repair command
            built_wheel.rename(repaired_wheel_dir / built_wheel.name)
        else:
            repair_command_prepared = prepare_command(options.repair_command, wheel=built_wheel, dest_dir=repaired_wheel_dir)
            shell([repair_command_prepared], env=env)
        repaired_wheel = next(repaired_wheel_dir.glob('*.whl'))

        if options.test_command:
            # set up a virtual environment to install and test from, to make sure
            # there are no dependencies that were pulled in at build time.
            shell(['pip', 'install', 'virtualenv'] + dependency_constraint_flags, env=env)
            venv_dir = Path(tempfile.mkdtemp())

            # Use --no-download to ensure determinism by using seed libraries
            # built into virtualenv
            shell(['python', '-m', 'virtualenv', '--no-download', str(venv_dir)], env=env)

            virtualenv_env = env.copy()
            virtualenv_env['PATH'] = os.pathsep.join([
                str(venv_dir / 'Scripts'),
github joerick / cibuildwheel / cibuildwheel / macos.py View on Github external
call([before_all_prepared], shell=True, env=env)

    python_configurations = get_python_configurations(options.build_selector)

    for config in python_configurations:
        dependency_constraint_flags = []
        if options.dependency_constraints:
            dependency_constraint_flags = [
                '-c', str(options.dependency_constraints.get_for_python_version(config.version))
            ]

        env = setup_python(config, dependency_constraint_flags, options.environment)

        # run the before_build command
        if options.before_build:
            before_build_prepared = prepare_command(options.before_build, project='.', package=options.package_dir)
            call(before_build_prepared, env=env, shell=True)

        # build the wheel
        if built_wheel_dir.exists():
            shutil.rmtree(built_wheel_dir)
        built_wheel_dir.mkdir(parents=True)
        # Path.resolve() is needed. Without it pip wheel may try to fetch package from pypi.org
        # see https://github.com/joerick/cibuildwheel/pull/369
        call(['pip', 'wheel', str(options.package_dir.resolve()), '-w', str(built_wheel_dir), '--no-deps'] + get_build_verbosity_extra_flags(options.build_verbosity), env=env)
        built_wheel = next(built_wheel_dir.glob('*.whl'))

        # repair the wheel
        if repaired_wheel_dir.exists():
            shutil.rmtree(repaired_wheel_dir)
        repaired_wheel_dir.mkdir(parents=True)
        if built_wheel.name.endswith('none-any.whl') or not options.repair_command:
github joerick / cibuildwheel / cibuildwheel / linux.py View on Github external
repaired_wheels = docker.glob(repaired_wheel_dir / '*.whl')

                    if options.test_command:
                        # 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(['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='/project', package=container_package_dir)
                            docker.call(['sh', '-c', before_test_prepared], env=virtualenv_env)

                        # Install the wheel we just built
                        # Note: If auditwheel produced two wheels, it's because the earlier produced wheel
                        # conforms to multiple manylinux standards. These multiple versions of the wheel are
                        # functionally the same, differing only in name, wheel metadata, and possibly include
                        # 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)

                        # Install any requirements to run the tests
                        if options.test_requires:
                            docker.call(['pip', 'install', *options.test_requires], env=virtualenv_env)

                        # Run the tests from a different directory
github joerick / cibuildwheel / cibuildwheel / linux.py View on Github external
container_package_dir,
                        '-w', built_wheel_dir,
                        '--no-deps',
                        *get_build_verbosity_extra_flags(options.build_verbosity)
                    ], env=env)

                    built_wheel = docker.glob(built_wheel_dir / '*.whl')[0]

                    repaired_wheel_dir = temp_dir / 'repaired_wheel'
                    docker.call(['rm', '-rf', repaired_wheel_dir])
                    docker.call(['mkdir', '-p', repaired_wheel_dir])

                    if built_wheel.name.endswith('none-any.whl') or not options.repair_command:
                        docker.call(['mv', built_wheel, repaired_wheel_dir])
                    else:
                        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)

                    repaired_wheels = docker.glob(repaired_wheel_dir / '*.whl')

                    if options.test_command:
                        # 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(['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:
github joerick / cibuildwheel / cibuildwheel / windows.py View on Github external
# Use --no-download to ensure determinism by using seed libraries
            # built into virtualenv
            shell(['python', '-m', 'virtualenv', '--no-download', str(venv_dir)], env=env)

            virtualenv_env = env.copy()
            virtualenv_env['PATH'] = os.pathsep.join([
                str(venv_dir / 'Scripts'),
                virtualenv_env['PATH'],
            ])

            # check that we are using the Python from the virtual environment
            shell(['which', 'python'], env=virtualenv_env)

            if options.before_test:
                before_test_prepared = prepare_command(
                    options.before_test,
                    project='.',
                    package=options.package_dir
                )
                shell([before_test_prepared], env=virtualenv_env)

            # install the wheel
            shell(['pip', 'install', str(repaired_wheel) + options.test_extras], env=virtualenv_env)

            # test the wheel
            if options.test_requires:
                shell(['pip', 'install'] + options.test_requires, env=virtualenv_env)

            # run the tests from c:\, with an absolute path in the command
            # (this ensures that Python runs the tests against the installed wheel
            # and not the repo code)