Created
August 12, 2013 17:16
-
-
Save mythmon/6212983 to your computer and use it in GitHub Desktop.
Automatic environment set up and tear down. A zsh hook looks for .venv files and activates/deactivates them upon entering/leaving a directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DIR=$(dirname $0) | |
REDISBIN="/usr/bin/redis-server" | |
REDISCONF="configs/redis-mine" | |
source $DIR/bin/activate | |
v_activate() { | |
MANAGE="${VIRTUAL_ENV}/manage.py" | |
function r() { | |
${DIR}/scripts/run | |
} | |
function s() { | |
${MANAGE} shell $@ | |
} | |
function t() { | |
sudo $REDISBIN "$REDISCONF/sumo-test.conf" | |
pid=$! | |
${VIRTUAL_ENV}/manage.py test -s --noinput --logging-clear-handlers \ | |
--with-id --with-progressive $@ | |
kill $pid | |
} | |
} | |
v_deactivate() { | |
deactivate | |
unfunction r | |
unfunction s | |
unfunction t | |
} | |
# vim: filetype=zsh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export AUTO_VENV_LAST_DIR="" | |
chpwd_auto_venv () { | |
dir=$(pwd) | |
last="" | |
while true; do | |
if [[ -f $dir/.venv ]]; then | |
if [[ $dir = $AUTO_VENV_LAST_DIR ]]; then | |
return 0 | |
fi | |
source $dir/.venv | |
v_activate | |
export AUTO_VENV_LAST_DIR=$dir | |
return 0 | |
fi | |
if [[ -n $last ]]; then | |
if [[ -n "$AUTO_VENV_LAST_DIR" ]]; then | |
source "${AUTO_VENV_LAST_DIR}/.venv" | |
v_deactivate | |
export AUTO_VENV_LAST_DIR="" | |
return 0 | |
fi | |
return 0 | |
fi | |
dir=$(dirname $dir) | |
if [[ "/" = $dir ]]; then | |
last=1 | |
fi | |
done | |
} | |
add-zsh-hook chpwd chpwd_auto_venv | |
chpwd_auto_venv | |
#EOF vim: ft=zsh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment