]> git.friedersdorff.com Git - max/saltfiles.git/commitdiff
Automagically activate virtualenvs
authorMaximilian Friedersdorff <maximilian.friedersdorff@envsys.co.uk>
Wed, 3 Aug 2022 11:03:00 +0000 (12:03 +0100)
committerMaximilian Friedersdorff <maximilian.friedersdorff@envsys.co.uk>
Wed, 3 Aug 2022 11:03:00 +0000 (12:03 +0100)
states/zsh/files/zshrc

index e1659e5e47dd06f43d4ca06cd246952842a17ecb..57c58ec874b7ef601b65ccb439096c6aa588a872 100644 (file)
@@ -71,3 +71,31 @@ fi
 
 export WORKON_HOME=~/.virtualenvs
 source /usr/bin/virtualenvwrapper_lazy.sh
+
+chpwd() {
+    src_dir="$(readlink -f ~/src/)/"
+    if [[ $PWD == "$src_dir"* ]]; then
+        if git status &>/dev/null; then
+            venv_name=$(basename $(git rev-parse --show-toplevel))
+            if [[ -n "$VIRTUAL_ENV" ]]; then
+                current_venv_name=$(basename "$VIRTUAL_ENV")
+                if [[ "$current_venv_name" != "$venv_name" ]]; then
+                    deactivate
+                    workon "$venv_name"
+                fi
+            else
+                if workon | grep -q "$venv_name"; then
+                    workon "$venv_name"
+                fi
+            fi
+        else
+            if [[ -n "$VIRTUAL_ENV" ]]; then
+                deactivate
+            fi
+        fi
+    else
+        if [[ -n "$VIRTUAL_ENV" ]]; then
+            deactivate
+        fi
+    fi
+}