Skip to content

Commit 65792bd

Browse files
Merge pull request #264 from YannickJadoul/check-python-path
Checking python in PATH is the one `cibuildwheel` installed/expects
2 parents 4f47ab8 + d14427d commit 65792bd

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

cibuildwheel/linux.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,30 @@ def build(project_dir, output_dir, test_command, test_requires, test_extras, bef
6363
mkdir /output
6464
cd /project
6565
66-
{environment_exports}
66+
for PYBIN in {pybin_paths}; do (
67+
# Temporary hack/workaround, putting loop body in subshell; fixed in PR #256
68+
69+
export PATH="$PYBIN:$PATH"
70+
{environment_exports}
71+
72+
# check the active python and pip are in PYBIN
73+
if [ "$(which pip)" != "$PYBIN/pip" ]; then
74+
echo "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."
75+
exit 1
76+
fi
77+
if [ "$(which python)" != "$PYBIN/python" ]; then
78+
echo "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."
79+
exit 1
80+
fi
6781
68-
for PYBIN in {pybin_paths}; do
6982
if [ ! -z {before_build} ]; then
70-
PATH="$PYBIN:$PATH" sh -c {before_build}
83+
sh -c {before_build}
7184
fi
7285
7386
# Build the wheel
7487
rm -rf /tmp/built_wheel
7588
mkdir /tmp/built_wheel
76-
PATH="$PYBIN:$PATH" "$PYBIN/pip" wheel . -w /tmp/built_wheel --no-deps {build_verbosity_flag}
89+
pip wheel . -w /tmp/built_wheel --no-deps {build_verbosity_flag}
7790
built_wheel=(/tmp/built_wheel/*.whl)
7891
7992
# repair the wheel
@@ -92,9 +105,9 @@ def build(project_dir, output_dir, test_command, test_requires, test_extras, bef
92105
if [ ! -z {test_command} ]; then
93106
# Set up a virtual environment to install and test from, to make sure
94107
# there are no dependencies that were pulled in at build time.
95-
"$PYBIN/pip" install virtualenv
108+
pip install virtualenv
96109
venv_dir=`mktemp -d`/venv
97-
"$PYBIN/python" -m virtualenv "$venv_dir"
110+
python -m virtualenv "$venv_dir"
98111
99112
# run the tests in a subshell to keep that `activate`
100113
# script from polluting the env
@@ -123,7 +136,7 @@ def build(project_dir, output_dir, test_command, test_requires, test_extras, bef
123136
)
124137
# exit if tests failed (needed for older bash versions)
125138
if [ $? -ne 0 ]; then
126-
exit 1;
139+
exit 1;
127140
fi
128141
129142
# clean up
@@ -133,7 +146,7 @@ def build(project_dir, output_dir, test_command, test_requires, test_extras, bef
133146
# we're all done here; move it to output
134147
mv "${{repaired_wheels[@]}}" /output
135148
for repaired_wheel in "${{repaired_wheels[@]}}"; do chown {uid}:{gid} "/output/$(basename "$repaired_wheel")"; done
136-
done
149+
) done
137150
'''.format(
138151
pybin_paths=' '.join(c.path + '/bin' for c in platform_configs),
139152
test_requires=' '.join(test_requires),

cibuildwheel/macos.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import shlex
33
import shutil
44
import subprocess
5+
import sys
56
import tempfile
67
from collections import namedtuple
78
from glob import glob
@@ -137,11 +138,20 @@ def build(project_dir, output_dir, test_command, test_requires, test_extras, bef
137138
# check what version we're on
138139
call(['which', 'python'], env=env)
139140
call(['python', '--version'], env=env)
141+
which_python = subprocess.check_output(['which', 'python'], env=env, universal_newlines=True).strip()
142+
if which_python != '/tmp/cibw_bin/python':
143+
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)
144+
exit(1)
140145

141146
# install pip & wheel
142147
call(['python', get_pip_script], env=env, cwd="/tmp")
143148
assert os.path.exists(os.path.join(installation_bin_path, 'pip'))
149+
call(['which', 'pip'], env=env)
144150
call(['pip', '--version'], env=env)
151+
which_pip = subprocess.check_output(['which', 'pip'], env=env, universal_newlines=True).strip()
152+
if which_pip != '/tmp/cibw_bin/pip':
153+
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)
154+
exit(1)
145155
call(['pip', 'install', '--upgrade', 'setuptools', 'wheel', 'delocate'], env=env)
146156

147157
# setup target platform, only required for python 3.5

cibuildwheel/windows.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import shutil
33
import subprocess
4+
import sys
45
import tempfile
56
from collections import namedtuple
67
from glob import glob
@@ -141,11 +142,19 @@ def build(project_dir, output_dir, test_command, test_requires, test_extras, bef
141142
simple_shell(['where', 'python'], env=env)
142143
simple_shell(['python', '--version'], env=env)
143144
simple_shell(['python', '-c', '"import struct; print(struct.calcsize(\'P\') * 8)"'], env=env)
145+
where_python = subprocess.check_output(['where', 'python'], env=env, universal_newlines=True).splitlines()[0].strip()
146+
if where_python != os.path.join(installation_path, 'python.exe'):
147+
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)
148+
exit(1)
144149

145150
# make sure pip is installed
146151
if not os.path.exists(os.path.join(installation_path, 'Scripts', 'pip.exe')):
147152
simple_shell(['python', get_pip_script], env=env, cwd="C:\\cibw")
148153
assert os.path.exists(os.path.join(installation_path, 'Scripts', 'pip.exe'))
154+
where_pip = subprocess.check_output(['where', 'pip'], env=env, universal_newlines=True).splitlines()[0].strip()
155+
if where_pip.strip() != os.path.join(installation_path, 'Scripts', 'pip.exe'):
156+
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)
157+
exit(1)
149158

150159
# prepare the Python environment
151160
simple_shell(['python', '-m', 'pip', 'install', '--upgrade', 'pip'], env=env)

test/05_environment/cibuildwheel_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
2-
2+
import pytest
3+
import subprocess
34
import utils
45

56

@@ -17,3 +18,15 @@ def test():
1718
# also check that we got the right wheels built
1819
expected_wheels = utils.expected_wheels('spam', '0.1.0')
1920
assert set(actual_wheels) == set(expected_wheels)
21+
22+
23+
def test_overridden_path(tmp_path):
24+
project_dir = os.path.dirname(__file__)
25+
26+
# mess up PATH, somehow
27+
with pytest.raises(subprocess.CalledProcessError):
28+
utils.cibuildwheel_run(project_dir, output_dir=tmp_path, add_env={
29+
'CIBW_ENVIRONMENT': '''SOMETHING="$(mkdir new_path && touch new_path/python)" PATH="$(realpath new_path):$PATH"''',
30+
'CIBW_ENVIRONMENT_WINDOWS': '''SOMETHING="$(mkdir new_path && type nul > new_path/python.exe)" PATH="$CD\\new_path;$PATH"''',
31+
})
32+
assert len(os.listdir(str(tmp_path))) == 0

0 commit comments

Comments
 (0)