Skip to content

Commit bff3e8e

Browse files
authored
Merge pull request #307 from Czaki/setup_python_separate
Setup python extracted from build function
2 parents 1c88697 + 3aa97f0 commit bff3e8e

File tree

3 files changed

+114
-96
lines changed

3 files changed

+114
-96
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ build: off
1414
init:
1515
- cmd: set PATH=C:\Python37;C:\Python37\Scripts;%PATH%
1616

17-
install: python -m pip install -r requirements-dev.txt
17+
install: python -m pip install --retries 3 -r requirements-dev.txt
1818

1919
# the '-u' flag is required so the output is in the correct order.
2020
# See https://github.com/joerick/cibuildwheel/pull/24 for more info.

cibuildwheel/macos.py

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,63 @@ def install_pypy(version, url):
108108
return installation_bin_path
109109

110110

111+
def setup_python(python_configuration, dependency_constraint_flags, environment):
112+
if python_configuration.identifier.startswith('cp'):
113+
installation_bin_path = install_cpython(python_configuration.version, python_configuration.url)
114+
elif python_configuration.identifier.startswith('pp'):
115+
installation_bin_path = install_pypy(python_configuration.version, python_configuration.url)
116+
else:
117+
raise ValueError("Unknown Python implementation")
118+
119+
env = os.environ.copy()
120+
env['PATH'] = os.pathsep.join([
121+
SYMLINKS_DIR,
122+
installation_bin_path,
123+
env['PATH'],
124+
])
125+
126+
# Fix issue with site.py setting the wrong `sys.prefix`, `sys.exec_prefix`,
127+
# `sys.path`, ... for PyPy: https://foss.heptapod.net/pypy/pypy/issues/3175
128+
# Also fix an issue with the shebang of installed scripts inside the
129+
# testing virtualenv- see https://github.com/theacodes/nox/issues/44 and
130+
# https://github.com/pypa/virtualenv/issues/620
131+
# Also see https://github.com/python/cpython/pull/9516
132+
env.pop('__PYVENV_LAUNCHER__', None)
133+
env = environment.as_dictionary(prev_environment=env)
134+
135+
# check what version we're on
136+
call(['which', 'python'], env=env)
137+
call(['python', '--version'], env=env)
138+
which_python = subprocess.check_output(['which', 'python'], env=env, universal_newlines=True).strip()
139+
if which_python != '/tmp/cibw_bin/python':
140+
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)
141+
exit(1)
142+
143+
# install pip & wheel
144+
call(['python', get_pip_script] + dependency_constraint_flags, env=env, cwd="/tmp")
145+
assert os.path.exists(os.path.join(installation_bin_path, 'pip'))
146+
call(['which', 'pip'], env=env)
147+
call(['pip', '--version'], env=env)
148+
which_pip = subprocess.check_output(['which', 'pip'], env=env, universal_newlines=True).strip()
149+
if which_pip != '/tmp/cibw_bin/pip':
150+
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)
151+
exit(1)
152+
call(['pip', 'install', '--upgrade', 'setuptools', 'wheel', 'delocate'] + dependency_constraint_flags, env=env)
153+
154+
# setup target platform, only required for python 3.5
155+
if python_configuration.version == '3.5':
156+
if '_PYTHON_HOST_PLATFORM' not in env:
157+
# cross-compilation platform override
158+
env['_PYTHON_HOST_PLATFORM'] = 'macosx-10.9-x86_64'
159+
if 'ARCHFLAGS' not in env:
160+
# https://github.com/python/cpython/blob/a5ed2fe0eedefa1649aa93ee74a0bafc8e628a10/Lib/_osx_support.py#L260
161+
env['ARCHFLAGS'] = '-arch x86_64'
162+
if 'MACOSX_DEPLOYMENT_TARGET' not in env:
163+
env['MACOSX_DEPLOYMENT_TARGET'] = '10.9'
164+
165+
return env
166+
167+
111168
def build(project_dir, output_dir, test_command, before_test, test_requires, test_extras, before_build, build_verbosity, build_selector, repair_command, environment, dependency_constraints):
112169
abs_project_dir = os.path.abspath(project_dir)
113170
temp_dir = tempfile.mkdtemp(prefix='cibuildwheel')
@@ -117,64 +174,15 @@ def build(project_dir, output_dir, test_command, before_test, test_requires, tes
117174
python_configurations = get_python_configurations(build_selector)
118175

119176
for config in python_configurations:
120-
if config.identifier.startswith('cp'):
121-
installation_bin_path = install_cpython(config.version, config.url)
122-
elif config.identifier.startswith('pp'):
123-
installation_bin_path = install_pypy(config.version, config.url)
124-
else:
125-
raise ValueError("Unknown Python implementation")
126-
127-
env = os.environ.copy()
128-
env['PATH'] = os.pathsep.join([
129-
SYMLINKS_DIR,
130-
installation_bin_path,
131-
env['PATH'],
132-
])
133-
134-
# Fix issue with site.py setting the wrong `sys.prefix`, `sys.exec_prefix`,
135-
# `sys.path`, ... for PyPy: https://foss.heptapod.net/pypy/pypy/issues/3175
136-
# Also fix an issue with the shebang of installed scripts inside the
137-
# testing virtualenv- see https://github.com/theacodes/nox/issues/44 and
138-
# https://github.com/pypa/virtualenv/issues/620
139-
# Also see https://github.com/python/cpython/pull/9516
140-
env.pop('__PYVENV_LAUNCHER__', None)
141-
env = environment.as_dictionary(prev_environment=env)
142-
143-
# check what version we're on
144-
call(['which', 'python'], env=env)
145-
call(['python', '--version'], env=env)
146-
which_python = subprocess.check_output(['which', 'python'], env=env, universal_newlines=True).strip()
147-
if which_python != '/tmp/cibw_bin/python':
148-
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)
149-
exit(1)
150177

151178
dependency_constraint_flags = []
152179
if dependency_constraints:
153180
dependency_constraint_flags = [
154181
'-c', dependency_constraints.get_for_python_version(config.version)
155182
]
183+
184+
env = setup_python(config, dependency_constraint_flags, environment)
156185

157-
# install pip & wheel
158-
call(['python', get_pip_script] + dependency_constraint_flags, env=env, cwd="/tmp")
159-
assert os.path.exists(os.path.join(installation_bin_path, 'pip'))
160-
call(['which', 'pip'], env=env)
161-
call(['pip', '--version'], env=env)
162-
which_pip = subprocess.check_output(['which', 'pip'], env=env, universal_newlines=True).strip()
163-
if which_pip != '/tmp/cibw_bin/pip':
164-
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)
165-
exit(1)
166-
call(['pip', 'install', '--upgrade', 'setuptools', 'wheel', 'delocate'] + dependency_constraint_flags, env=env)
167-
168-
# setup target platform, only required for python 3.5
169-
if config.version == '3.5':
170-
if '_PYTHON_HOST_PLATFORM' not in env:
171-
# cross-compilation platform override
172-
env['_PYTHON_HOST_PLATFORM'] = 'macosx-10.9-x86_64'
173-
if 'ARCHFLAGS' not in env:
174-
# https://github.com/python/cpython/blob/a5ed2fe0eedefa1649aa93ee74a0bafc8e628a10/Lib/_osx_support.py#L260
175-
env['ARCHFLAGS'] = '-arch x86_64'
176-
if 'MACOSX_DEPLOYMENT_TARGET' not in env:
177-
env['MACOSX_DEPLOYMENT_TARGET'] = '10.9'
178186

179187
# run the before_build command
180188
if before_build:

cibuildwheel/windows.py

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,58 @@ def install_pypy(version, arch, url):
8989
return installation_path
9090

9191

92+
def setup_python(python_configuration, dependency_constraint_flags, environment):
93+
nuget = 'C:\\cibw\\nuget.exe'
94+
if not os.path.exists(nuget):
95+
download('https://dist.nuget.org/win-x86-commandline/latest/nuget.exe', nuget)
96+
97+
if python_configuration.identifier.startswith('cp'):
98+
installation_path = install_cpython(python_configuration.version, python_configuration.arch, nuget)
99+
elif python_configuration.identifier.startswith('pp'):
100+
installation_path = install_pypy(python_configuration.version, python_configuration.arch, python_configuration.url)
101+
else:
102+
raise ValueError("Unknown Python implementation")
103+
104+
assert os.path.exists(os.path.join(installation_path, 'python.exe'))
105+
106+
# set up PATH and environment variables for run_with_env
107+
env = os.environ.copy()
108+
env['PYTHON_VERSION'] = python_configuration.version
109+
env['PYTHON_ARCH'] = python_configuration.arch
110+
env['PATH'] = os.pathsep.join([
111+
installation_path,
112+
os.path.join(installation_path, 'Scripts'),
113+
env['PATH']
114+
])
115+
# update env with results from CIBW_ENVIRONMENT
116+
env = environment.as_dictionary(prev_environment=env)
117+
118+
# for the logs - check we're running the right version of python
119+
shell(['where', 'python'], env=env)
120+
shell(['python', '--version'], env=env)
121+
shell(['python', '-c', '"import struct; print(struct.calcsize(\'P\') * 8)"'], env=env)
122+
where_python = subprocess.check_output(['where', 'python'], env=env, universal_newlines=True).splitlines()[0].strip()
123+
if where_python != os.path.join(installation_path, 'python.exe'):
124+
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)
125+
exit(1)
126+
127+
# make sure pip is installed
128+
if not os.path.exists(os.path.join(installation_path, 'Scripts', 'pip.exe')):
129+
shell(['python', get_pip_script] + dependency_constraint_flags, env=env, cwd="C:\\cibw")
130+
assert os.path.exists(os.path.join(installation_path, 'Scripts', 'pip.exe'))
131+
where_pip = subprocess.check_output(['where', 'pip'], env=env, universal_newlines=True).splitlines()[0].strip()
132+
if where_pip.strip() != os.path.join(installation_path, 'Scripts', 'pip.exe'):
133+
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)
134+
exit(1)
135+
136+
# prepare the Python environment
137+
shell(['python', '-m', 'pip', 'install', '--upgrade', 'pip'] + dependency_constraint_flags, env=env)
138+
shell(['pip', '--version'], env=env)
139+
shell(['pip', 'install', '--upgrade', 'setuptools', 'wheel'] + dependency_constraint_flags, env=env)
140+
141+
return env
142+
143+
92144
def build(project_dir, output_dir, test_command, before_test, test_requires, test_extras, before_build, build_verbosity, build_selector, repair_command, environment, dependency_constraints):
93145
abs_project_dir = os.path.abspath(project_dir)
94146
temp_dir = tempfile.mkdtemp(prefix='cibuildwheel')
@@ -101,56 +153,14 @@ def build(project_dir, output_dir, test_command, before_test, test_requires, tes
101153

102154
python_configurations = get_python_configurations(build_selector)
103155
for config in python_configurations:
104-
# install Python
105-
if config.identifier.startswith('cp'):
106-
installation_path = install_cpython(config.version, config.arch, nuget)
107-
elif config.identifier.startswith('pp'):
108-
installation_path = install_pypy(config.version, config.arch, config.url)
109-
else:
110-
raise ValueError("Unknown Python implementation")
111-
112-
assert os.path.exists(os.path.join(installation_path, 'python.exe'))
113-
114-
# set up PATH and environment variables for run_with_env
115-
env = os.environ.copy()
116-
env['PYTHON_VERSION'] = config.version
117-
env['PYTHON_ARCH'] = config.arch
118-
env['PATH'] = os.pathsep.join([
119-
installation_path,
120-
os.path.join(installation_path, 'Scripts'),
121-
env['PATH']
122-
])
123-
# update env with results from CIBW_ENVIRONMENT
124-
env = environment.as_dictionary(prev_environment=env)
125-
126-
# for the logs - check we're running the right version of python
127-
shell(['where', 'python'], env=env)
128-
shell(['python', '--version'], env=env)
129-
shell(['python', '-c', '"import struct; print(struct.calcsize(\'P\') * 8)"'], env=env)
130-
where_python = subprocess.check_output(['where', 'python'], env=env, universal_newlines=True).splitlines()[0].strip()
131-
if where_python != os.path.join(installation_path, 'python.exe'):
132-
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)
133-
exit(1)
134-
135156
dependency_constraint_flags = []
136157
if dependency_constraints:
137158
dependency_constraint_flags = [
138159
'-c', dependency_constraints.get_for_python_version(config.version)
139160
]
140161

141-
# make sure pip is installed
142-
if not os.path.exists(os.path.join(installation_path, 'Scripts', 'pip.exe')):
143-
shell(['python', get_pip_script] + dependency_constraint_flags, env=env, cwd="C:\\cibw")
144-
assert os.path.exists(os.path.join(installation_path, 'Scripts', 'pip.exe'))
145-
where_pip = subprocess.check_output(['where', 'pip'], env=env, universal_newlines=True).splitlines()[0].strip()
146-
if where_pip.strip() != os.path.join(installation_path, 'Scripts', 'pip.exe'):
147-
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)
148-
exit(1)
149-
150-
# prepare the Python environment
151-
shell(['python', '-m', 'pip', 'install', '--upgrade', 'pip'] + dependency_constraint_flags, env=env)
152-
shell(['pip', '--version'], env=env)
153-
shell(['pip', 'install', '--upgrade', 'setuptools', 'wheel'] + dependency_constraint_flags, env=env)
162+
# install Python
163+
env = setup_python(config, dependency_constraint_flags, environment)
154164

155165
# run the before_build command
156166
if before_build:

0 commit comments

Comments
 (0)