virtualenvwrapper.plugin.zsh 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. function {
  2. # search in these locations for the init script:
  3. for f 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 $f ]]; then
  12. source $f
  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. }
  19. if ! type workon &>/dev/null; then
  20. print "[oh-my-zsh] virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
  21. "Please check ${virtualenvwrapper}" >&2
  22. return
  23. fi
  24. if [[ -z "$WORKON_HOME" ]]; then
  25. WORKON_HOME="$HOME/.virtualenvs"
  26. fi
  27. if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
  28. # Automatically activate Git projects or other customized virtualenvwrapper projects based on the
  29. # directory name of the project. Virtual environment name can be overridden
  30. # by placing a .venv file in the project root with a virtualenv name in it.
  31. function workon_cwd {
  32. if [[ -z "$WORKON_CWD" ]]; then
  33. local WORKON_CWD=1
  34. # Check if this is a Git repo
  35. local GIT_REPO_ROOT=""
  36. local GIT_TOPLEVEL="$(git rev-parse --show-toplevel 2> /dev/null)"
  37. if [[ $? == 0 ]]; then
  38. GIT_REPO_ROOT="$GIT_TOPLEVEL"
  39. fi
  40. # Get absolute path, resolving symlinks
  41. local PROJECT_ROOT="${PWD:A}"
  42. while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
  43. && ! -d "$PROJECT_ROOT/.git" && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
  44. PROJECT_ROOT="${PROJECT_ROOT:h}"
  45. done
  46. if [[ "$PROJECT_ROOT" == "/" ]]; then
  47. PROJECT_ROOT="."
  48. fi
  49. # Check for virtualenv name override
  50. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  51. ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
  52. elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
  53. ENV_NAME="$PROJECT_ROOT/.venv"
  54. elif [[ "$PROJECT_ROOT" != "." ]]; then
  55. ENV_NAME="${PROJECT_ROOT:t}"
  56. else
  57. ENV_NAME=""
  58. fi
  59. if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
  60. # We've just left the repo, deactivate the environment
  61. # Note: this only happens if the virtualenv was activated automatically
  62. deactivate && unset CD_VIRTUAL_ENV
  63. fi
  64. if [[ "$ENV_NAME" != "" ]]; then
  65. # Activate the environment only if it is not already active
  66. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  67. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  68. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  69. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  70. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  71. fi
  72. fi
  73. fi
  74. fi
  75. }
  76. # Append workon_cwd to the chpwd_functions array, so it will be called on cd
  77. # http://zsh.sourceforge.net/Doc/Release/Functions.html
  78. autoload -U add-zsh-hook
  79. add-zsh-hook chpwd workon_cwd
  80. fi