aboutsummaryrefslogtreecommitdiff
path: root/run-on-venv
diff options
context:
space:
mode:
Diffstat (limited to 'run-on-venv')
-rwxr-xr-xrun-on-venv51
1 files changed, 51 insertions, 0 deletions
diff --git a/run-on-venv b/run-on-venv
new file mode 100755
index 0000000..e6fdf7e
--- /dev/null
+++ b/run-on-venv
@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+#
+# Helper script to run a command inside a virtualenv
+#
+# This is too simple, and you might want to use
+# virtualenvwrapper instead:
+#
+# https://tracker.debian.org/pkg/virtualenvwrapper
+# https://virtualenvwrapper.readthedocs.io/en/latest/
+# https://doughellmann.com/projects/virtualenvwrapper/
+#
+# Or pipx:
+#
+# https://pipx.pypa.io/stable/
+# https://tracker.debian.org/pkg/python-pipx
+#
+
+# Parameters
+BASENAME="`basename $0`"
+COMMAND="$1"
+VENV="${VENV:$HOME/.virtualenvs/$COMMAND}"
+
+# Check for a test
+if [ -z "$COMMAND" ]; then
+ echo "usage: $BASENAME <command> [args]"
+ exit 1
+fi
+
+# Deactivate any active virtualenv
+deactivate &> /dev/null
+
+# Create and activate a virtualenv as needed
+if [ ! -e "$VENV" ]; then
+ mkdir -p `dirname $VENV`
+ python -m venv $VENV
+ . $VENV/bin/activate
+ pip install --upgrade pip
+ pip install $COMMAND
+else
+ # Check for a virtualenv
+ if [ ! -e "$VENV/bin/activate" ]; then
+ echo "$BASENAME: missing or invalid virtualenv"
+ exit 1
+ fi
+
+ # Activate the virtualenv
+ . $VENV/bin/activate
+fi
+
+# Run
+$*