aboutsummaryrefslogtreecommitdiff
path: root/.config
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2023-10-24 15:31:45 +0200
committerPatrick Spek <p.spek@tyil.nl>2023-10-24 15:31:45 +0200
commit2667e44ca957ec75f61266305e375c011fc52cd8 (patch)
treee650843ebe02dea61f9734f44dcc6244aa211b3f /.config
parent29b5b5b74d0553b98086459a614366ec320f0dfb (diff)
Add shell functions for Python venvs
It is an ugly system, and it seems it is practically inescapable. Truly an homage to the quality of the programming language and the devs who love it without question.
Diffstat (limited to '.config')
-rw-r--r--.config/shell/functions.d/venv48
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..50a91e9
--- /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
+}