virtualenvwrapper.plugin.zsh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. WRAPPER_FOUND=0
  2. for wrapsource in "/usr/bin/virtualenvwrapper.sh" "/usr/local/bin/virtualenvwrapper.sh" "/etc/bash_completion.d/virtualenvwrapper" ; do
  3. if [[ -e $wrapsource ]] ; then
  4. WRAPPER_FOUND=1
  5. source $wrapsource
  6. if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
  7. # Automatically activate Git projects' virtual environments based on the
  8. # directory name of the project. Virtual environment name can be overridden
  9. # by placing a .venv file in the project root with a virtualenv name in it
  10. function workon_cwd {
  11. # Check that this is a Git repo
  12. PROJECT_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
  13. if (( $? == 0 )); then
  14. # Check for virtualenv name override
  15. ENV_NAME=`basename "$PROJECT_ROOT"`
  16. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  17. ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
  18. fi
  19. # Activate the environment only if it is not already active
  20. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  21. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  22. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  23. fi
  24. fi
  25. elif [ $CD_VIRTUAL_ENV ]; then
  26. # We've just left the repo, deactivate the environment
  27. # Note: this only happens if the virtualenv was activated automatically
  28. deactivate && unset CD_VIRTUAL_ENV
  29. fi
  30. unset PROJECT_ROOT
  31. }
  32. # New cd function that does the virtualenv magic
  33. function cd {
  34. builtin cd "$@" && workon_cwd
  35. }
  36. fi
  37. break
  38. fi
  39. done
  40. if [ $WRAPPER_FOUND -eq 0 ] ; then
  41. print "zsh virtualenvwrapper plugin: Couldn't activate virtualenvwrapper. Please run \`pip install virtualenvwrapper\`."
  42. fi