2021-04-28 19:12:09 +02:00
|
|
|
function activate_venv {
|
|
|
|
# check if virtualenv is set up
|
2021-10-02 23:07:47 +02:00
|
|
|
if [ -d "${workdir}/venv" ]; then
|
2023-09-05 17:31:35 +02:00
|
|
|
# It's possible for activate to fail without stderr; as a result the cat operation (below) could fail.
|
|
|
|
# To mitigate this, create an empty error log.
|
2024-01-10 02:59:13 +01:00
|
|
|
# We're relying on the evergreen provided tmp directory here because Amazon Linux 2023 has an issue
|
|
|
|
# writing to /tmp/ on startup.
|
|
|
|
touch $TMPDIR/activate_error.log
|
2021-04-28 19:12:09 +02:00
|
|
|
if [ "Windows_NT" = "$OS" ]; then
|
|
|
|
# Need to quote the path on Windows to preserve the separator.
|
2024-01-10 02:59:13 +01:00
|
|
|
. "${workdir}/venv/Scripts/activate" 2> $TMPDIR/activate_error.log
|
2021-04-28 19:12:09 +02:00
|
|
|
else
|
2024-01-10 02:59:13 +01:00
|
|
|
. ${workdir}/venv/bin/activate 2> $TMPDIR/activate_error.log
|
2021-04-28 19:12:09 +02:00
|
|
|
fi
|
|
|
|
if [ $? -ne 0 ]; then
|
2024-01-10 02:59:13 +01:00
|
|
|
echo "Failed to activate virtualenv: $(cat $TMPDIR/activate_error.log)"
|
2021-06-22 16:21:01 +02:00
|
|
|
exit 1
|
2021-04-28 19:12:09 +02:00
|
|
|
fi
|
|
|
|
python=python
|
|
|
|
else
|
2021-05-18 22:19:17 +02:00
|
|
|
if [ -z "$python" ]; then
|
|
|
|
echo "\$python is unset. This should never happen"
|
2021-06-22 16:21:01 +02:00
|
|
|
exit 1
|
2021-05-18 22:19:17 +02:00
|
|
|
fi
|
|
|
|
python=${python}
|
2023-09-05 16:32:12 +02:00
|
|
|
echo "Could not find venv. Setting python to $python."
|
2021-04-28 19:12:09 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "Windows_NT" = "$OS" ]; then
|
|
|
|
export PYTHONPATH="$PYTHONPATH;$(cygpath -w ${workdir}/src)"
|
2023-04-22 01:47:18 +02:00
|
|
|
elif [ "$(uname)" = "Darwin" ]; then
|
|
|
|
#SERVER-75626 After activating the virtual environment under the mocos host. the PYTHONPATH setting
|
|
|
|
#is incorrect, and the site-packages directory of the virtual environment cannot be found in the sys.path.
|
|
|
|
python_version=$($python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
|
|
|
|
export PYTHONPATH="${workdir}/venv/lib/python${python_version}/site-packages:${PYTHONPATH}:${workdir}/src"
|
2021-04-28 19:12:09 +02:00
|
|
|
else
|
2023-05-01 21:21:44 +02:00
|
|
|
python_version=$($python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
|
|
|
|
site_packages="${workdir}/venv/lib/python${python_version}/site-packages"
|
|
|
|
python -c "import sys; print(sys.path)"
|
|
|
|
|
|
|
|
# Check if site_packages is already in sys.path
|
|
|
|
in_sys_path=$($python -c "import sys; print('$site_packages' in sys.path)")
|
|
|
|
if [ "$in_sys_path" = "False" ]; then
|
|
|
|
export PYTHONPATH="${site_packages}:${PYTHONPATH}:${workdir}/src"
|
|
|
|
else
|
|
|
|
export PYTHONPATH="$PYTHONPATH:${workdir}/src"
|
|
|
|
fi
|
|
|
|
python -c "import sys; print(sys.path)"
|
2021-04-28 19:12:09 +02:00
|
|
|
fi
|
|
|
|
|
2023-09-05 16:32:12 +02:00
|
|
|
echo "python set to $(which $python) and python version: $($python --version)"
|
2021-04-28 19:12:09 +02:00
|
|
|
}
|