virtualenvwrapper.plugin.zsh 1.6 KB

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