summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorEric Engestrom <eric@igalia.com>2023-05-03 17:52:57 +0100
committerMarge Bot <emma+marge@anholt.net>2023-08-03 23:21:31 +0000
commit75f44bd79a01b8bec4b7cbb561bbb10cadc3db8c (patch)
tree049c2c6279b0d9a5760f2648d34dfc2282536f94 /bin
parentf34bae7d8cd0b667d69d8c718d0e22670b9cc0a0 (diff)
downloadmesa-75f44bd79a01b8bec4b7cbb561bbb10cadc3db8c.tar.gz
mesa-75f44bd79a01b8bec4b7cbb561bbb10cadc3db8c.tar.bz2
mesa-75f44bd79a01b8bec4b7cbb561bbb10cadc3db8c.zip
bin: add wrapper to run scripts in a python venv
This isolates the script environment from the rest of the machine, avoiding missing/incompatible dependencies and avoiding polluting the rest of the machine with python packages. Signed-off-by: Eric Engestrom <eric@igalia.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/24367>
Diffstat (limited to 'bin')
-rwxr-xr-xbin/python-venv.sh27
1 files changed, 27 insertions, 0 deletions
diff --git a/bin/python-venv.sh b/bin/python-venv.sh
new file mode 100755
index 00000000000..2c4f6dccdbe
--- /dev/null
+++ b/bin/python-venv.sh
@@ -0,0 +1,27 @@
+#!/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
+
+if ! [ -r "$venv_dir/bin/activate" ]
+then
+ echo "Creating Python environment..."
+ python -m venv "$venv_dir"
+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
+
+python "$@"