virtualenvwrapper.plugin.zsh 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "[oh-my-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 "[oh-my-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 "[oh-my-zsh] \$WORKON_HOME is not defined so plugin virtualenvwrapper will not work" >&2
  27. return
  28. fi
  29. if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
  30. # Automatically activate Git projects or other customized virtualenvwrapper projects 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 [[ -z "$WORKON_CWD" ]]; then
  35. local WORKON_CWD=1
  36. # Check if this is a Git repo
  37. local GIT_REPO_ROOT=""
  38. local GIT_TOPLEVEL=$(git rev-parse --show-toplevel 2> /dev/null)
  39. if [[ $? == 0 ]]; then
  40. GIT_REPO_ROOT="$GIT_TOPLEVEL"
  41. fi
  42. # Get absolute path, resolving symlinks
  43. local PROJECT_ROOT="${PWD:A}"
  44. while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
  45. && ! -d "$PROJECT_ROOT/.git" && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
  46. PROJECT_ROOT="${PROJECT_ROOT:h}"
  47. done
  48. if [[ "$PROJECT_ROOT" == "/" ]]; then
  49. PROJECT_ROOT="."
  50. fi
  51. # Check for virtualenv name override
  52. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  53. ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
  54. elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
  55. ENV_NAME="$PROJECT_ROOT/.venv"
  56. elif [[ "$PROJECT_ROOT" != "." ]]; then
  57. ENV_NAME="${PROJECT_ROOT:t}"
  58. else
  59. ENV_NAME=""
  60. fi
  61. if [[ "$ENV_NAME" != "" ]]; then
  62. # Activate the environment only if it is not already active
  63. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  64. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  65. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  66. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  67. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  68. fi
  69. fi
  70. elif [[ -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
  71. # We've just left the repo, deactivate the environment
  72. # Note: this only happens if the virtualenv was activated automatically
  73. deactivate && unset CD_VIRTUAL_ENV
  74. fi
  75. fi
  76. }
  77. # Append workon_cwd to the chpwd_functions array, so it will be called on cd
  78. # http://zsh.sourceforge.net/Doc/Release/Functions.html
  79. if ! (( $chpwd_functions[(I)workon_cwd] )); then
  80. chpwd_functions+=(workon_cwd)
  81. fi
  82. fi