virtualenvwrapper.plugin.zsh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. virtualenvwrapper='virtualenvwrapper.sh'
  2. virtualenvwrapper_lazy='virtualenvwrapper_lazy.sh'
  3. if (( $+commands[$virtualenvwrapper_lazy] )); then
  4. function {
  5. setopt local_options
  6. unsetopt equals
  7. virtualenvwrapper=${${virtualenvwrapper_lazy}:c}
  8. source ${${virtualenvwrapper_lazy}:c}
  9. [[ -z "$WORKON_HOME" ]] && WORKON_HOME="$HOME/.virtualenvs"
  10. }
  11. elif (( $+commands[$virtualenvwrapper] )); then
  12. function {
  13. setopt local_options
  14. unsetopt equals
  15. source ${${virtualenvwrapper}:c}
  16. }
  17. elif [[ -f "/usr/local/bin/virtualenvwrapper.sh" ]]; then
  18. function {
  19. setopt local_options
  20. unsetopt equals
  21. virtualenvwrapper="/usr/local/bin/virtualenvwrapper.sh"
  22. source "/usr/local/bin/virtualenvwrapper.sh"
  23. }
  24. elif [[ -f "/etc/bash_completion.d/virtualenvwrapper" ]]; then
  25. function {
  26. setopt local_options
  27. unsetopt equals
  28. virtualenvwrapper="/etc/bash_completion.d/virtualenvwrapper"
  29. source "/etc/bash_completion.d/virtualenvwrapper"
  30. }
  31. else
  32. print "[oh-my-zsh] virtualenvwrapper plugin: Cannot find ${virtualenvwrapper}.\n"\
  33. "Please install with \`pip install virtualenvwrapper\`" >&2
  34. return
  35. fi
  36. if ! type workon &>/dev/null; then
  37. print "[oh-my-zsh] virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
  38. "Please check ${virtualenvwrapper}" >&2
  39. return
  40. fi
  41. if [[ "$WORKON_HOME" == "" ]]; then
  42. print "[oh-my-zsh] \$WORKON_HOME is not defined so plugin virtualenvwrapper will not work" >&2
  43. return
  44. fi
  45. if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
  46. # Automatically activate Git projects or other customized virtualenvwrapper projects based on the
  47. # directory name of the project. Virtual environment name can be overridden
  48. # by placing a .venv file in the project root with a virtualenv name in it.
  49. function workon_cwd {
  50. if [[ -z "$WORKON_CWD" ]]; then
  51. local WORKON_CWD=1
  52. # Check if this is a Git repo
  53. local GIT_REPO_ROOT=""
  54. local GIT_TOPLEVEL="$(git rev-parse --show-toplevel 2> /dev/null)"
  55. if [[ $? == 0 ]]; then
  56. GIT_REPO_ROOT="$GIT_TOPLEVEL"
  57. fi
  58. # Get absolute path, resolving symlinks
  59. local PROJECT_ROOT="${PWD:A}"
  60. while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
  61. && ! -d "$PROJECT_ROOT/.git" && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
  62. PROJECT_ROOT="${PROJECT_ROOT:h}"
  63. done
  64. if [[ "$PROJECT_ROOT" == "/" ]]; then
  65. PROJECT_ROOT="."
  66. fi
  67. # Check for virtualenv name override
  68. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  69. ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
  70. elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
  71. ENV_NAME="$PROJECT_ROOT/.venv"
  72. elif [[ "$PROJECT_ROOT" != "." ]]; then
  73. ENV_NAME="${PROJECT_ROOT:t}"
  74. else
  75. ENV_NAME=""
  76. fi
  77. if [[ "$ENV_NAME" != "" ]]; then
  78. # Activate the environment only if it is not already active
  79. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  80. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  81. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  82. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  83. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  84. fi
  85. fi
  86. elif [[ -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
  87. # We've just left the repo, deactivate the environment
  88. # Note: this only happens if the virtualenv was activated automatically
  89. deactivate && unset CD_VIRTUAL_ENV
  90. fi
  91. fi
  92. }
  93. # Append workon_cwd to the chpwd_functions array, so it will be called on cd
  94. # http://zsh.sourceforge.net/Doc/Release/Functions.html
  95. if ! (( $chpwd_functions[(I)workon_cwd] )); then
  96. chpwd_functions+=(workon_cwd)
  97. fi
  98. fi