virtualenvwrapper.plugin.zsh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. virtualenvwrapper='virtualenvwrapper.sh'
  2. if (( $+commands[$virtualenvwrapper] )); then
  3. source ${${virtualenvwrapper}:c}
  4. elif [[ -f "/etc/bash_completion.d/virtualenvwrapper" ]]; then
  5. virtualenvwrapper="/etc/bash_completion.d/virtualenvwrapper"
  6. source "/etc/bash_completion.d/virtualenvwrapper"
  7. else
  8. print "zsh virtualenvwrapper plugin: Cannot find ${virtualenvwrapper}. Please install with \`pip install virtualenvwrapper\`."
  9. return
  10. fi
  11. if type workon 2>&1 >/dev/null; then
  12. if [[ "$WORKON_HOME" == "" ]]; then
  13. echo "\$WORKON_HOME is not defined so ZSH plugin virtualenvwrapper will not work"
  14. else
  15. if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
  16. # Automatically activate Git projects's virtual environments based on the
  17. # directory name of the project. Virtual environment name can be overridden
  18. # by placing a .venv file in the project root with a virtualenv name in it
  19. function workon_cwd {
  20. if [ ! $WORKON_CWD ]; then
  21. WORKON_CWD=1
  22. # Check if this is a Git repo
  23. PROJECT_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
  24. if (( $? != 0 )); then
  25. PROJECT_ROOT="."
  26. fi
  27. # Check for virtualenv name override
  28. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  29. ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
  30. elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
  31. ENV_NAME="$PROJECT_ROOT/.venv"
  32. elif [[ "$PROJECT_ROOT" != "." ]]; then
  33. ENV_NAME=`basename "$PROJECT_ROOT"`
  34. else
  35. ENV_NAME=""
  36. fi
  37. if [[ "$ENV_NAME" != "" ]]; then
  38. # Activate the environment only if it is not already active
  39. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  40. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  41. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  42. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  43. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  44. fi
  45. fi
  46. elif [[ -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
  47. # We've just left the repo, deactivate the environment
  48. # Note: this only happens if the virtualenv was activated automatically
  49. deactivate && unset CD_VIRTUAL_ENV
  50. fi
  51. unset PROJECT_ROOT
  52. unset WORKON_CWD
  53. fi
  54. }
  55. # Append workon_cwd to the chpwd_functions array, so it will be called on cd
  56. # http://zsh.sourceforge.net/Doc/Release/Functions.html
  57. # TODO: replace with 'add-zsh-hook chpwd workon_cwd' when oh-my-zsh min version is raised above 4.3.4
  58. if (( ${+chpwd_functions} )); then
  59. if (( $chpwd_functions[(I)workon_cwd] == 0 )); then
  60. set -A chpwd_functions $chpwd_functions workon_cwd
  61. fi
  62. else
  63. set -A chpwd_functions workon_cwd
  64. fi
  65. fi
  66. fi
  67. else
  68. print "zsh virtualenvwrapper plugin: shell function 'workon' not defined. Please check ${virtualenvwrapper}." >&2
  69. fi