virtualenvwrapper.plugin.zsh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. do
  10. if [[ -f $f ]]; then
  11. source $f
  12. return
  13. fi
  14. done
  15. print "[oh-my-zsh] virtualenvwrapper plugin: Cannot find virtualenvwrapper.sh.\n"\
  16. "Please install with \`pip install virtualenvwrapper\`" >&2
  17. }
  18. if ! type workon &>/dev/null; then
  19. print "[oh-my-zsh] virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
  20. "Please check ${virtualenvwrapper}" >&2
  21. return
  22. fi
  23. if [[ -z "$WORKON_HOME" ]]; then
  24. WORKON_HOME="$HOME/.virtualenvs"
  25. fi
  26. if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
  27. # Automatically activate Git projects or other customized virtualenvwrapper projects based on the
  28. # directory name of the project. Virtual environment name can be overridden
  29. # by placing a .venv file in the project root with a virtualenv name in it.
  30. function workon_cwd {
  31. if [[ -z "$WORKON_CWD" ]]; then
  32. local WORKON_CWD=1
  33. # Check if this is a Git repo
  34. local GIT_REPO_ROOT=""
  35. local GIT_TOPLEVEL="$(git rev-parse --show-toplevel 2> /dev/null)"
  36. if [[ $? == 0 ]]; then
  37. GIT_REPO_ROOT="$GIT_TOPLEVEL"
  38. fi
  39. # Get absolute path, resolving symlinks
  40. local PROJECT_ROOT="${PWD:A}"
  41. while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
  42. && ! -d "$PROJECT_ROOT/.git" && "$PROJECT_ROOT" != "$GIT_REPO_ROOT" ]]; do
  43. PROJECT_ROOT="${PROJECT_ROOT:h}"
  44. done
  45. if [[ "$PROJECT_ROOT" == "/" ]]; then
  46. PROJECT_ROOT="."
  47. fi
  48. # Check for virtualenv name override
  49. if [[ -f "$PROJECT_ROOT/.venv" ]]; then
  50. ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
  51. elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
  52. ENV_NAME="$PROJECT_ROOT/.venv"
  53. elif [[ "$PROJECT_ROOT" != "." ]]; then
  54. ENV_NAME="${PROJECT_ROOT:t}"
  55. else
  56. ENV_NAME=""
  57. fi
  58. if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
  59. # We've just left the repo, deactivate the environment
  60. # Note: this only happens if the virtualenv was activated automatically
  61. deactivate && unset CD_VIRTUAL_ENV
  62. fi
  63. if [[ "$ENV_NAME" != "" ]]; then
  64. # Activate the environment only if it is not already active
  65. if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
  66. if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
  67. workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
  68. elif [[ -e "$ENV_NAME/bin/activate" ]]; then
  69. source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
  70. fi
  71. fi
  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