Skip to content

Instantly share code, notes, and snippets.

@mythmon
Created August 12, 2013 17:16
Show Gist options
  • Save mythmon/6212983 to your computer and use it in GitHub Desktop.
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.
#!/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
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