Files
mesa/bin/python-venv.sh
Maíra Canal eecf94cc8e bin: explicitly use python3 instead of python
The `python` command's default behaviour can vary between OSes and even
different versions of the same OS. On some systems, `python` might still
point to Python 2 for backward compatibility, while on others, it might
point to Python 3.

As Mesa already requires "Python 3.6 or newer", use `python3` explicitly
to ensure that the script is using a Python 3 interpreter.

Moreover, this commit allows this script to run on macOS, as macOS doesn't
have a `python` symlink or alias by default. Therefore, currently, when
running this script in macOS, you get a "python: command not found" error.

Signed-off-by: Maíra Canal <mcanal@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/35918>
2025-07-03 20:52:48 +00:00

48 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eu
readonly requirements_file=$1
shift
venv_dir="$(dirname "$requirements_file")"/.venv
readonly venv_dir
readonly venv_req=$venv_dir/requirements.txt
readonly venv_python_version=$venv_dir/python-version.txt
if [ -d "$venv_dir" ]
then
if [ ! -r "$venv_python_version" ]
then
echo "Python environment predates Python version checks."
echo "It might be invalid and needs to be regenerated."
rm -rf "$venv_dir"
elif ! cmp --quiet <(python3 --version) "$venv_python_version"
then
old=$(cat "$venv_python_version")
new=$(python3 --version)
echo "Python version has changed ($old -> $new)."
echo "Python environment needs to be regenerated."
unset old new
rm -rf "$venv_dir"
fi
fi
if ! [ -r "$venv_dir/bin/activate" ]
then
echo "Creating Python environment..."
python3 -m venv "$venv_dir"
python3 --version > "$venv_python_version"
fi
# shellcheck disable=1091
source "$venv_dir/bin/activate"
if ! cmp --quiet "$requirements_file" "$venv_req"
then
echo "$(realpath --relative-to="$PWD" "$requirements_file") has changed, re-installing..."
pip --disable-pip-version-check install --requirement "$requirements_file"
cp "$requirements_file" "$venv_req"
fi
python3 "$@"