virtualenvwrapper.plugin.zsh 3.6 KB

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