2020-02-07 19:06:06 +08:00
|
|
|
function {
|
|
|
|
# search in these locations for the init script:
|
2020-05-27 00:02:46 +08:00
|
|
|
for virtualenvwrapper in $commands[virtualenvwrapper_lazy.sh] \
|
|
|
|
$commands[virtualenvwrapper.sh] \
|
|
|
|
/usr/share/virtualenvwrapper/virtualenvwrapper{_lazy,}.sh \
|
|
|
|
/usr/local/bin/virtualenvwrapper{_lazy,}.sh \
|
|
|
|
/etc/bash_completion.d/virtualenvwrapper \
|
|
|
|
/usr/share/bash-completion/completions/virtualenvwrapper \
|
|
|
|
$HOME/.local/bin/virtualenvwrapper.sh
|
2020-02-07 19:06:06 +08:00
|
|
|
do
|
2020-05-27 00:02:46 +08:00
|
|
|
if [[ -f "$virtualenvwrapper" ]]; then
|
|
|
|
source "$virtualenvwrapper"
|
2020-02-07 19:06:06 +08:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
print "[oh-my-zsh] virtualenvwrapper plugin: Cannot find virtualenvwrapper.sh.\n"\
|
|
|
|
"Please install with \`pip install virtualenvwrapper\`" >&2
|
2020-05-27 00:02:46 +08:00
|
|
|
return 1
|
2020-02-07 19:06:06 +08:00
|
|
|
}
|
2013-11-22 18:48:13 +08:00
|
|
|
|
2020-05-27 00:02:46 +08:00
|
|
|
if [[ $? -eq 0 ]] && ! type workon &>/dev/null; then
|
2015-07-03 09:42:32 +08:00
|
|
|
print "[oh-my-zsh] virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
|
2015-02-10 19:20:24 +08:00
|
|
|
"Please check ${virtualenvwrapper}" >&2
|
2014-05-20 21:41:36 +08:00
|
|
|
return
|
|
|
|
fi
|
2012-06-09 09:04:58 +08:00
|
|
|
|
2020-02-07 19:06:06 +08:00
|
|
|
if [[ -z "$WORKON_HOME" ]]; then
|
|
|
|
WORKON_HOME="$HOME/.virtualenvs"
|
2015-02-10 19:20:24 +08:00
|
|
|
fi
|
2012-06-09 09:04:58 +08:00
|
|
|
|
2015-02-10 19:20:24 +08:00
|
|
|
if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
|
2015-07-03 09:42:32 +08:00
|
|
|
# Automatically activate Git projects or other customized virtualenvwrapper projects based on the
|
2015-02-10 19:20:24 +08:00
|
|
|
# directory name of the project. Virtual environment name can be overridden
|
2015-07-03 09:42:32 +08:00
|
|
|
# by placing a .venv file in the project root with a virtualenv name in it.
|
2015-02-10 19:20:24 +08:00
|
|
|
function workon_cwd {
|
2015-07-03 09:42:32 +08:00
|
|
|
if [[ -z "$WORKON_CWD" ]]; then
|
|
|
|
local WORKON_CWD=1
|
2015-06-30 06:55:58 +08:00
|
|
|
# Get absolute path, resolving symlinks
|
2015-07-03 09:42:32 +08:00
|
|
|
local PROJECT_ROOT="${PWD:A}"
|
|
|
|
while [[ "$PROJECT_ROOT" != "/" && ! -e "$PROJECT_ROOT/.venv" \
|
2021-06-12 11:52:22 +08:00
|
|
|
&& ! -d "$PROJECT_ROOT/.git" ]]; do
|
2015-06-30 06:55:58 +08:00
|
|
|
PROJECT_ROOT="${PROJECT_ROOT:h}"
|
2015-05-21 23:34:24 +08:00
|
|
|
done
|
2021-06-12 11:52:22 +08:00
|
|
|
|
2015-02-10 19:20:24 +08:00
|
|
|
# Check for virtualenv name override
|
|
|
|
if [[ -f "$PROJECT_ROOT/.venv" ]]; then
|
2015-08-08 04:45:46 +08:00
|
|
|
ENV_NAME="$(cat "$PROJECT_ROOT/.venv")"
|
2015-02-10 19:20:24 +08:00
|
|
|
elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
|
|
|
|
ENV_NAME="$PROJECT_ROOT/.venv"
|
2021-06-12 11:52:22 +08:00
|
|
|
elif [[ "$PROJECT_ROOT" != "/" ]]; then
|
2015-06-30 06:55:58 +08:00
|
|
|
ENV_NAME="${PROJECT_ROOT:t}"
|
2013-11-22 18:48:13 +08:00
|
|
|
else
|
2015-02-10 19:20:24 +08:00
|
|
|
ENV_NAME=""
|
|
|
|
fi
|
2024-03-26 16:55:16 +08:00
|
|
|
|
2019-12-21 13:08:03 +08:00
|
|
|
if [[ -n $CD_VIRTUAL_ENV && "$ENV_NAME" != "$CD_VIRTUAL_ENV" ]]; then
|
|
|
|
# We've just left the repo, deactivate the environment
|
|
|
|
# Note: this only happens if the virtualenv was activated automatically
|
2022-02-10 23:25:23 +08:00
|
|
|
if [[ -n "$VIRTUAL_ENV" ]]; then
|
|
|
|
# Only deactivate if VIRTUAL_ENV was set
|
|
|
|
# User may have deactivated manually or via another mechanism
|
|
|
|
deactivate
|
|
|
|
fi
|
|
|
|
# clean up regardless
|
|
|
|
unset CD_VIRTUAL_ENV
|
2019-12-21 13:08:03 +08:00
|
|
|
fi
|
2015-02-10 19:20:24 +08:00
|
|
|
if [[ "$ENV_NAME" != "" ]]; then
|
|
|
|
# Activate the environment only if it is not already active
|
2021-06-12 11:52:22 +08:00
|
|
|
if [[ ! "$VIRTUAL_ENV" -ef "$WORKON_HOME/$ENV_NAME" ]]; then
|
2015-02-10 19:20:24 +08:00
|
|
|
if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
|
|
|
|
workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
|
|
|
|
elif [[ -e "$ENV_NAME/bin/activate" ]]; then
|
|
|
|
source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
|
2021-06-12 11:52:22 +08:00
|
|
|
else
|
|
|
|
ENV_NAME=""
|
2015-02-10 19:20:24 +08:00
|
|
|
fi
|
|
|
|
fi
|
2013-11-22 18:48:13 +08:00
|
|
|
fi
|
2021-06-12 11:52:22 +08:00
|
|
|
if [[ "$ENV_NAME" == "" && -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
|
|
|
|
# We've just left the repo, deactivate the environment
|
|
|
|
# Note: this only happens if the virtualenv was activated automatically
|
|
|
|
deactivate && unset CD_VIRTUAL_ENV
|
|
|
|
fi
|
2013-06-09 02:25:52 +08:00
|
|
|
fi
|
2015-02-10 19:20:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Append workon_cwd to the chpwd_functions array, so it will be called on cd
|
|
|
|
# http://zsh.sourceforge.net/Doc/Release/Functions.html
|
2019-11-20 01:47:12 +08:00
|
|
|
autoload -U add-zsh-hook
|
|
|
|
add-zsh-hook chpwd workon_cwd
|
2024-01-19 02:34:30 +08:00
|
|
|
[[ $PWD != ~ ]] && workon_cwd
|
2012-06-09 09:04:58 +08:00
|
|
|
fi
|