virtualenvwrapper.plugin.zsh 2.9 KB

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