virtualenvwrapper.plugin.zsh 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. # Check if this is a Git repo
  36. local GIT_REPO_ROOT=""
  37. local GIT_TOPLEVEL="$(git rev-parse --show-toplevel 2> /dev/null)"
  38. if [[ $? == 0 ]]; then
  39. GIT_REPO_ROOT="$GIT_TOPLEVEL"
  40. fi
  41. # Get absolute path, resolving symlinks
  42. local PROJECT_ROOT="${PWD:A}"
  43. while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
  44. && ! -d "$PROJECT_ROOT/.git" && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
  45. PROJECT_ROOT="${PROJECT_ROOT:h}"
  46. done
  47. if [[ "$PROJECT_ROOT" == "/" ]]; then
  48. PROJECT_ROOT="."
  49. fi
  50. # Check for virtualenv name override
  51. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  52. ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
  53. elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
  54. ENV_NAME="$PROJECT_ROOT/.venv"
  55. elif [[ "$PROJECT_ROOT" != "." ]]; then
  56. ENV_NAME="${PROJECT_ROOT:t}"
  57. else
  58. ENV_NAME=""
  59. fi
  60. if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
  61. # We've just left the repo, deactivate the environment
  62. # Note: this only happens if the virtualenv was activated automatically
  63. deactivate && unset CD_VIRTUAL_ENV
  64. fi
  65. if [[ "$ENV_NAME" != "" ]]; then
  66. # Activate the environment only if it is not already active
  67. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  68. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  69. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  70. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  71. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  72. fi
  73. fi
  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. autoload -U add-zsh-hook
  80. add-zsh-hook chpwd workon_cwd
  81. fi