virtualenvwrapper.plugin.zsh 2.7 KB

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