virtualenvwrapper.plugin.zsh 3.4 KB

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