virtualenvwrapper.plugin.zsh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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=`pwd`
  38. while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" ]]; do
  39. PROJECT_ROOT=`realpath $PROJECT_ROOT/..`
  40. done
  41. if [[ "$PROJECT_ROOT" == "/" ]]; then
  42. PROJECT_ROOT="."
  43. fi
  44. # Check for virtualenv name override
  45. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  46. ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
  47. elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
  48. ENV_NAME="$PROJECT_ROOT/.venv"
  49. elif [[ "$PROJECT_ROOT" != "." ]]; then
  50. ENV_NAME=`basename "$PROJECT_ROOT"`
  51. else
  52. ENV_NAME=""
  53. fi
  54. if [[ "$ENV_NAME" != "" ]]; then
  55. # Activate the environment only if it is not already active
  56. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  57. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  58. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  59. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  60. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  61. fi
  62. fi
  63. elif [[ -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
  64. # We've just left the repo, deactivate the environment
  65. # Note: this only happens if the virtualenv was activated automatically
  66. deactivate && unset CD_VIRTUAL_ENV
  67. fi
  68. unset PROJECT_ROOT
  69. unset WORKON_CWD
  70. fi
  71. }
  72. # Append workon_cwd to the chpwd_functions array, so it will be called on cd
  73. # http://zsh.sourceforge.net/Doc/Release/Functions.html
  74. if ! (( $chpwd_functions[(I)workon_cwd] )); then
  75. chpwd_functions+=(workon_cwd)
  76. fi
  77. fi