Created
April 23, 2012 19:13
-
-
Save hoov/2473171 to your computer and use it in GitHub Desktop.
virtualenv autoactivate
This file contains 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
#!/usr/bin/env zsh | |
__virtualenv_activate() | |
{ | |
export __virtualenv_activate_lock | |
: __virtualenv_activate_lock:${__virtualenv_activate_lock:=0} | |
: __virtualenv_activate_lock:$((__virtualenv_activate_lock+=1)) | |
if (( __virtualenv_activate_lock > 1 )) | |
then return 0 # no nesting | |
fi | |
typeset working_dir venv_name | |
working_dir="${1:-"$PWD"}" | |
while : | |
do | |
if [[ -z "$working_dir" || "$HOME" == "$working_dir" || "/" == "$working_dir" ]] | |
then | |
if [[ -n "${VIRTUAL_ENV:-""}" ]] | |
then | |
echo "Deactivating virtualenv" | |
deactivate | |
fi | |
break | |
else | |
if [[ -d "$working_dir/.venv" ]] | |
then | |
if [[ "${VIRTUAL_ENV:-""}" != "${working_dir}/.venv" ]] | |
then | |
if [[ -n "${VIRTUAL_ENV:-""}" ]] | |
then | |
echo "Deactivating virtualenv" | |
deactivate | |
fi | |
echo "Activating virtualenv" | |
\. "${working_dir}/.venv/bin/activate" | |
fi | |
break | |
else | |
working_dir="$(dirname "$working_dir")" | |
fi | |
fi | |
done | |
unset __virtualenv_activate_lock | |
return $? | |
} | |
# Help from ~/.rvm/scripts/cd | |
if [[ -n "${ZSH_VERSION:-}" ]] | |
then | |
autoload is-at-least | |
if is-at-least 4.3.4 >/dev/null 2>&1; then | |
# On zsh, use chpwd_functions | |
export -a chpwd_functions | |
chpwd_functions=( "${chpwd_functions[@]}" __virtualenv_activate ) | |
fi | |
# TODO: Else case | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment