virtualenvwrapper.plugin.zsh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. WRAPPER_FOUND=0
  2. for wrapsource in "/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. GIT_DIR=`git rev-parse --git-dir 2> /dev/null`
  13. if (( $? == 0 )); then
  14. # Find the repo root and check for virtualenv name override
  15. GIT_DIR=`readlink -f $GIT_DIR`
  16. PROJECT_ROOT=`dirname "$GIT_DIR"`
  17. ENV_NAME=`basename "$PROJECT_ROOT"`
  18. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  19. ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
  20. fi
  21. # Activate the environment only if it is not already active
  22. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  23. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  24. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  25. fi
  26. fi
  27. elif [ $CD_VIRTUAL_ENV ]; then
  28. # We've just left the repo, deactivate the environment
  29. # Note: this only happens if the virtualenv was activated automatically
  30. deactivate && unset CD_VIRTUAL_ENV
  31. fi
  32. }
  33. # New cd function that does the virtualenv magic
  34. function cd {
  35. builtin cd "$@" && workon_cwd
  36. }
  37. fi
  38. break
  39. fi
  40. done
  41. if [ $WRAPPER_FOUND -eq 0 ] ; then
  42. print "zsh virtualenvwrapper plugin: Couldn't activate virtualenvwrapper. Please run \`pip install virtualenvwrapper\`."
  43. fi