1
0
mirror of https://git.decapod.one/brethil/dotfiles synced 2024-11-06 04:11:31 +01:00
dotfiles/functions/python.zsh

56 lines
1.4 KiB
Bash

mkvenv(){
local source_venv
if [[ $1 == "-s" ]]; then
source_venv=y
shift
fi
# any arguments are passed on to the `venv` module as flags/arguments
if [[ -e .venv ]]; then
echo "$(color $RED Error:) $(color $BOLD .venv) already exists." >&2
return 1
fi
echo -n "Creating venv..." && python -m virtualenv $@ .venv || echo -e "Failed to create virtualenv. Is virtualenv installed? Try:\n $ pip install virtualenv"
if [[ -z ${source_venv} ]]; then
echo -n " done. Enable? [Y/n]"
read source_venv
else
echo ""
fi
if [[ $source_venv != "n" ]]; then
source .venv/bin/activate && echo -e "$(color $BOLD Enabled!) 🐍 $(color $BOLD$PURPLE $(python --version | cut -d " " -f2 )) ($(color $BOLD$GREEN $(pip --version | cut -d " " -f -2)))"
fi
}
function enable_venv() {
local root
local activate
root=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ -z $root ]]; then root=$PWD; fi
activate="$root/.venv/bin/activate"
if [[ -f "$activate" ]]; then
source "$activate"
fi
}
function disable_venv() {
if [[ -z ${VIRTUAL_ENV} ]]; then
return
fi
if [[ ! "$PWD" =~ $(dirname $VIRTUAL_ENV)}* ]]; then
echo "Deactivating venv..."
deactivate
fi
}
if [[ -n $DOTFILES_AUTOSWITCH_VIRTUALENV ]]; then
add-zsh-hook chpwd enable_venv
add-zsh-hook chpwd disable_venv
enable_venv
fi