diff options
Diffstat (limited to '.config/shell/functions.d')
-rw-r--r-- | .config/shell/functions.d/venv | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/.config/shell/functions.d/venv b/.config/shell/functions.d/venv new file mode 100644 index 0000000..c23a02d --- /dev/null +++ b/.config/shell/functions.d/venv @@ -0,0 +1,48 @@ +#!/bin/sh + +venv() { + if ! command -V "venv_$1" | grep -q " function " + then + cat <<EOF +Usage: + venv on + venv off + +Manage the python venv for a given project or directory. + +Commands: + on Enable the venv for \$PWD + off Deactivate the current venv +EOF + + return 1 + fi + + "venv_$1" +} + +venv_on() { + export PYTHON_VENV_DIR="$PWD/.venv" + export VIRTUAL_ENV_DISABLE_PROMPT=1 + + if [ ! -d "$PYTHON_VENV_DIR" ] + then + printf "Initializing venv directory at %s\n" "$PYTHON_VENV_DIR" + python3 -m venv "$PYTHON_VENV_DIR" + fi + + . "$PYTHON_VENV_DIR/bin/activate" +} + +venv_off() { + if [ -z "$PYTHON_VENV_DIR" ] + then + printf "No venv active?\n" + return 1 + fi + + deactivate + + unset PYTHON_VENV_DIR + unset VIRTUAL_ENV_DISABLE_PROMPT +} |