virtualenvwrapper.plugin.zsh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. if [ ! $WORKON_CWD ]; then
  10. WORKON_CWD=1
  11. # Check if this is a Git repo
  12. PROJECT_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
  13. if (( $? != 0 )); then
  14. PROJECT_ROOT="."
  15. fi
  16. # Check for virtualenv name override
  17. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  18. ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
  19. elif [[ "$PROJECT_ROOT" != "." ]]; then
  20. ENV_NAME=`basename "$PROJECT_ROOT"`
  21. else
  22. ENV_NAME=""
  23. fi
  24. if [[ "$ENV_NAME" != "" ]]; then
  25. # Activate the environment only if it is not already active
  26. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  27. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  28. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  29. fi
  30. fi
  31. elif [ $CD_VIRTUAL_ENV ]; then
  32. # We've just left the repo, deactivate the environment
  33. # Note: this only happens if the virtualenv was activated automatically
  34. deactivate && unset CD_VIRTUAL_ENV
  35. fi
  36. unset PROJECT_ROOT
  37. unset WORKON_CWD
  38. fi
  39. }
  40. # New cd function that does the virtualenv magic
  41. function cd {
  42. builtin cd "$@" && workon_cwd
  43. }
  44. fi
  45. else
  46. print "zsh virtualenvwrapper plugin: Cannot find virtualenvwrapper_lazy.sh. Please install with \`pip install virtualenvwrapper\`."
  47. fi