virtualenvwrapper.plugin.zsh 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. deactivate && unset CD_VIRTUAL_ENV
  55. fi
  56. if [[ "$ENV_NAME" != "" ]]; then
  57. # Activate the environment only if it is not already active
  58. if [[ ! "$VIRTUAL_ENV" -ef "$WORKON_HOME/$ENV_NAME" ]]; then
  59. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  60. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  61. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  62. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  63. else
  64. ENV_NAME=""
  65. fi
  66. fi
  67. fi
  68. if [[ "$ENV_NAME" == "" && -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
  69. # We've just left the repo, deactivate the environment
  70. # Note: this only happens if the virtualenv was activated automatically
  71. deactivate && unset CD_VIRTUAL_ENV
  72. fi
  73. fi
  74. }
  75. # Append workon_cwd to the chpwd_functions array, so it will be called on cd
  76. # http://zsh.sourceforge.net/Doc/Release/Functions.html
  77. autoload -U add-zsh-hook
  78. add-zsh-hook chpwd workon_cwd
  79. fi