commit de3a169fa5a7b0b17a60dfd348a527a1b7d8b218 Author: Julio Biason Date: Tue Jan 14 13:32:57 2014 -0200 initial version of the magical script diff --git a/auto-virtualenv.sh b/auto-virtualenv.sh new file mode 100644 index 0000000..9ff64d2 --- /dev/null +++ b/auto-virtualenv.sh @@ -0,0 +1,41 @@ +# --- activate a virtualenv without the need of going through a series of +# paths +function venv { source $WORKON_HOME/$1/bin/activate; } + +# --- create a virtualenv +function mkenv { virtualenv $WORKON_HOME/$1; venv $1; echo "$1" > ./.venv; } + +# --- automagically load virtualenvs, based on the ".venv" file in the directory + +# -- search for the ".venv" from the current directory and, if it can't be found, +# go through the parent directories all the way to the system root +function _upwards_search { + venv="" + curdir=`pwd` + + while [[ `pwd` != '/' ]]; do + if [ -f ./.venv ]; then + venv=`cat ./.venv` + break + fi + cd .. + done + + cd $curdir + echo $venv; +} + +# -- replacement for "cd", which will check the virtualenv file +function _venv_cd { + if [ ! -f $PWD/$1 -a "$VIRTUAL_ENV." != "." ]; then + deactivate + fi; + \cd $1 + venv=$(_upwards_search) + if [ -n $venv ]; then + venv $venv + fi +} + +# -- replace "cd" with our magical "cd" +alias cd=_venv_cd