138 lines
3.4 KiB
Bash
Executable file
138 lines
3.4 KiB
Bash
Executable file
#!/bin/sh -eux
|
|
|
|
# set default version to %{python3_version} if available
|
|
VERSION=${VERSION:-$(rpm --eval '%{?python3_version}')} || true
|
|
# ...or 3.8 if that macro is not available or rpm fails for any reason
|
|
VERSION=${VERSION:-3.8}
|
|
|
|
PYTHON=${PYTHON:-python$VERSION}
|
|
METHOD=${METHOD:-venv}
|
|
TOX=${TOX:-true}
|
|
CYTHON=${CYTHON:-true}
|
|
INSTALL_OR_SKIP=${INSTALL_OR_SKIP:-false}
|
|
|
|
# Work from a custom directory
|
|
workdir="$(mktemp --directory)"
|
|
cd "$workdir"
|
|
|
|
if [ "$INSTALL_OR_SKIP" == "true" ]; then
|
|
[ ! -f "/usr/bin/$PYTHON" ] && dnf -y install "/usr/bin/$PYTHON" || :
|
|
dnf -y install "$PYTHON-devel" || :
|
|
if [ ! -f "/usr/bin/$PYTHON" ]; then
|
|
echo "/usr/bin/$PYTHON not installable, skipping this test"
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
# check the we can create the venv
|
|
if [ "$METHOD" == "venv" ]; then
|
|
$PYTHON -m venv venv
|
|
elif [ "$METHOD" == "virtualenv" ]; then
|
|
virtualenv --python=$PYTHON venv
|
|
elif [ "$METHOD" == "virtualenv-no-download" ]; then
|
|
virtualenv --no-download --python=$PYTHON venv
|
|
elif [ "$METHOD" == "virtualenv-seeder-pip" ]; then
|
|
virtualenv --seeder pip --python=$PYTHON venv
|
|
else
|
|
echo 'Unsupported $METHOD' $METHOD >&2
|
|
exit 1
|
|
fi
|
|
|
|
|
|
# and activate it
|
|
# unset variables on 3.5, it's known
|
|
set +u
|
|
source venv/bin/activate
|
|
set -u
|
|
|
|
# run python in it
|
|
python -c 'import sys; print(sys.version)' | head -n1 | grep $VERSION
|
|
|
|
# install packages with pip
|
|
python -m pip install pytest
|
|
if [ "$CYTHON" == "true" ]; then
|
|
if [ "$VERSION" == "3.11" ]; then
|
|
# Cython support for 3.11.0a1+ is not yet released
|
|
python -m pip install https://github.com/cython/cython/archive/master.tar.gz --install-option="--no-cython-compile"
|
|
else
|
|
# We try to fetch a wheel only and if that fails, we disable compilation
|
|
# Useful for fresh CPythons, where wheels are not yet ready, but Cython defaults to compiling
|
|
python -m pip install Cython --only-binary :all: || python -m pip install Cython --install-option="--no-cython-compile"
|
|
fi
|
|
fi
|
|
|
|
# run tests
|
|
cat > test_foo.py << EOF
|
|
def test_foo():
|
|
assert True
|
|
EOF
|
|
|
|
python -m pytest -v test_foo.py
|
|
|
|
if [ "$CYTHON" == "true" ]; then
|
|
cat > module.pyx << EOF
|
|
cdef int add(int a, int b):
|
|
return a + b
|
|
|
|
def two():
|
|
cdef int a = 1
|
|
cdef int b = 1
|
|
return add(a, b)
|
|
EOF
|
|
|
|
cat > setup.py << EOF
|
|
from setuptools import setup
|
|
from Cython.Build import cythonize
|
|
|
|
setup(ext_modules = cythonize('module.pyx'))
|
|
EOF
|
|
|
|
python setup.py build_ext --inplace
|
|
|
|
python -c 'import module; print(module.two())' | grep '^2$'
|
|
fi
|
|
|
|
# deactivate has unset variable, it's known
|
|
set +u
|
|
deactivate
|
|
set -u
|
|
|
|
# ensurepip test (when testing virtualenv, this was not covered)
|
|
# with the main Python, this will say "Requirement already satisfied", but that's OK
|
|
$PYTHON -m ensurepip --root ensurepiptestroot
|
|
|
|
# use it with tox
|
|
[[ "$TOX" != "true" ]] && exit 0
|
|
if [[ $PYTHON == python* ]]; then
|
|
export TOXENV=py${VERSION/./}
|
|
elif [[ $PYTHON == pypy* ]]; then
|
|
export TOXENV=pypy${VERSION/./}
|
|
else
|
|
export TOXENV=$PYTHON
|
|
fi
|
|
|
|
cat > tox.ini << EOF
|
|
[tox]
|
|
skipsdist = True
|
|
[testenv]
|
|
commands = python -c 'import sys; print(sys.version)'
|
|
EOF
|
|
|
|
# Tests that the Python version used in tox is really the Python version we want
|
|
tox | tee toxlog
|
|
grep "^$VERSION\.[0-9]" toxlog
|
|
|
|
cat > tox.ini << EOF
|
|
[tox]
|
|
skipsdist = True
|
|
[testenv]
|
|
deps = pytest
|
|
commands = python -m pytest -v test_foo.py
|
|
EOF
|
|
|
|
# A more complex example with pytest
|
|
tox
|
|
|
|
# Clean up if everything else went well
|
|
cd -
|
|
rm -rf "$workdir"
|