virtualenvwrapper.plugin.zsh 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. function {
  2. # search in these locations for the init script:
  3. for virtualenvwrapper in $commands[virtualenvwrapper_lazy.sh] \
  4. $commands[virtualenvwrapper.sh] \
  5. /usr/share/virtualenvwrapper/virtualenvwrapper{_lazy,}.sh \
  6. /usr/local/bin/virtualenvwrapper{_lazy,}.sh \
  7. /etc/bash_completion.d/virtualenvwrapper \
  8. /usr/share/bash-completion/completions/virtualenvwrapper \
  9. $HOME/.local/bin/virtualenvwrapper.sh
  10. do
  11. if [[ -f "$virtualenvwrapper" ]]; then
  12. source "$virtualenvwrapper"
  13. return
  14. fi
  15. done
  16. print "[oh-my-zsh] virtualenvwrapper plugin: Cannot find virtualenvwrapper.sh.\n"\
  17. "Please install with \`pip install virtualenvwrapper\`" >&2
  18. return 1
  19. }
  20. if [[ $? -eq 0 ]] && ! 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 [[ -z "$WORKON_HOME" ]]; then
  26. WORKON_HOME="$HOME/.virtualenvs"
  27. fi
  28. if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
  29. # Automatically activate Git projects or other customized virtualenvwrapper projects based on the
  30. # directory name of the project. Virtual environment name can be overridden
  31. # by placing a .venv file in the project root with a virtualenv name in it.
  32. function workon_cwd {
  33. if [[ -z "$WORKON_CWD" ]]; then
  34. local WORKON_CWD=1
  35. # Get absolute path, resolving symlinks
  36. local PROJECT_ROOT="${PWD:A}"
  37. while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
  38. && ! -d "$PROJECT_ROOT/.git" ]]; do
  39. PROJECT_ROOT="${PROJECT_ROOT:h}"
  40. done
  41. # Check for virtualenv name override
  42. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  43. ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
  44. elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
  45. ENV_NAME="$PROJECT_ROOT/.venv"
  46. elif [[ "$PROJECT_ROOT" != "/" ]]; then
  47. ENV_NAME="${PROJECT_ROOT:t}"
  48. else
  49. ENV_NAME=""
  50. fi
  51. if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
  52. # We've just left the repo, deactivate the environment
  53. # Note: this only happens if the virtualenv was activated automatically
  54. if [[ -n "$VIRTUAL_ENV" ]]; then
  55. # Only deactivate if VIRTUAL_ENV was set
  56. # User may have deactivated manually or via another mechanism
  57. deactivate
  58. fi
  59. # clean up regardless
  60. unset CD_VIRTUAL_ENV
  61. fi
  62. if [[ "$ENV_NAME" != "" ]]; then
  63. # Activate the environment only if it is not already active
  64. if [[ ! "$VIRTUAL_ENV" -ef "$WORKON_HOME/$ENV_NAME" ]]; then
  65. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  66. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  67. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  68. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  69. else
  70. ENV_NAME=""
  71. fi
  72. fi
  73. fi
  74. if [[ "$ENV_NAME" == "" && -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
  75. # We've just left the repo, deactivate the environment
  76. # Note: this only happens if the virtualenv was activated automatically
  77. deactivate && unset CD_VIRTUAL_ENV
  78. fi
  79. fi
  80. }
  81. # Append workon_cwd to the chpwd_functions array, so it will be called on cd
  82. # http://zsh.sourceforge.net/Doc/Release/Functions.html
  83. autoload -U add-zsh-hook
  84. add-zsh-hook chpwd workon_cwd
  85. [[ $PWD != ~ ]] && workon_cwd
  86. fi