virtualenvwrapper.plugin.zsh 4.0 KB

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