|
#!/bin/sh |
|
# https://gist.github.com/smoser/2d4100a6a5d230ca937f |
|
|
|
# this is where your virtual envs live, it is searched if the |
|
# virtual-env argument is not a path |
|
VENV_BASE_D="$HOME/pub/venvs" |
|
|
|
list_environments() { |
|
local venv_base_d="$1" prefix=" " |
|
echo "existing environments in $venv_base_d" |
|
( cd "$venv_base_d" && |
|
for d in *; do [ -f "$d/bin/activate" ] && echo "${prefix}$d"; done ) |
|
} |
|
|
|
error() { echo "$@" 1>&2; } |
|
fail() { [ $# -eq 0 ] || error "$@"; exit 1; } |
|
|
|
Usage() { |
|
local venv_base_d="$1" |
|
cat <<EOF |
|
Usage: ${0##*/} [mode] your-venv [command [args]] |
|
run command with provided arguments in 'your-venv' |
|
command defaults to \${SHELL:-/bin/sh}. |
|
|
|
if the first argument is not a valid mode, then 'activate' |
|
is assumed. |
|
|
|
modes are: |
|
activate: enter the named environment and execute a command |
|
--change-dir change the working directory to the virtual env dir |
|
|
|
create: create a new virtual env |
|
--py2 | --py3 create with python2 or python3 |
|
--activate enter after creating |
|
this is the default if you give additional args |
|
|
|
destroy: delete the named environment |
|
--force do not prompt before deleting |
|
|
|
list: list enviroments in ${VENV_BASE_D} |
|
|
|
Example: |
|
# create it and run 'pip install restview' inside |
|
venv create restview pip install restview |
|
|
|
# now run restview inside |
|
venv restview restview |
|
|
|
# now I'm all done |
|
venv destroy restview |
|
EOF |
|
if [ -d "$venv_base_d" ]; then |
|
echo |
|
list_environments "$venv_base_d" |
|
fi |
|
} |
|
|
|
list() { |
|
list_environments "$VENV_BASE_D" |
|
return |
|
} |
|
|
|
find_env() { |
|
local error=false |
|
[ "$1" = "--error" ] && { error=true; shift; } |
|
|
|
local env="$1" activate="" env_d="" cand="" |
|
if [ -f "$env" ]; then |
|
activate="$env" |
|
env_d="${env%/*/*}" |
|
elif [ -f "$env/bin/activate" ]; then |
|
activate="$env/bin/activate" |
|
env_d="$env" |
|
elif [ -f "$VENV_BASE_D/$env/bin/activate" ]; then |
|
activate="$VENV_BASE_D/$env/bin/activate" |
|
env_d="$VENV_BASE_D/$env" |
|
else |
|
if $error; then |
|
error "$env: not a valid env?" |
|
error "try one of:" |
|
list_environments "$VENV_BASE_D" 1>&2 |
|
fi |
|
return 1 |
|
fi |
|
_R_activate="$activate" |
|
_R_env_d="$env_d" |
|
return 0 |
|
} |
|
|
|
activate() { |
|
local change_d=false |
|
[ "$1" = "--change-dir" -o "$1" = "-C" ] && shift && change_d=true |
|
local activate="" env="$1" env_d="" |
|
shift |
|
|
|
find_env --error "$env" && |
|
activate="$_R_activate" && env_d="$_R_env_d" || return 1 |
|
. "$activate" |
|
|
|
! $change_d || cd "$env_d" |
|
[ "$#" -gt 0 ] || set -- ${SHELL:-/bin/bash} |
|
debian_chroot="venv:$env" VENV_D="$env_d" exec "$@" |
|
} |
|
|
|
create() { |
|
local python="" pyexe="" activate=false |
|
if [ "$1" = "--py3" -o "$1" = "--py2" ]; then |
|
[ "$1" = "--py3" ] && python="python${1#--py}" |
|
pyexe=$(which "$python") || { error "no $pyexe"; return 1; } |
|
shift |
|
fi |
|
if [ "$1" = "--activate" ]; then |
|
shift |
|
activate=true |
|
fi |
|
|
|
local env="$1" venv_base_d="${VENV_BASE_D}" env_d="" |
|
shift |
|
case "$env" in |
|
*/*|/*) venv_base_d=$(dirname "$env");; |
|
*) venv_base_d="$VENV_BASE_D";; |
|
esac |
|
env_d="${venv_base_d}/${env##*/}" |
|
[ -d "$venv_base_d" ] || mkdir -p "$venv_base_d" || |
|
{ error "failed mkdir $venv_base_d"; return 1; } |
|
virtualenv ${pyexe:+--python=$pyexe} "$env_d" |
|
|
|
[ $# -eq 0 ] || activate=true |
|
if $activate; then |
|
activate "$env_d" "$@" |
|
fi |
|
} |
|
|
|
destroy() { |
|
local force=false |
|
[ "$1" = "--force" ] && force=true |
|
local env="$1" env_d="" |
|
find_env --error "$env" && env_d="$_R_env_d" || return 1 |
|
[ -d "$env_d" ] || { error "$env_d: not a dir?"; return 1; } |
|
if ! $force; then |
|
local resp="" |
|
error "remove '$env_d' [y/n]?" |
|
read resp || { error "failed read response"; return 1; } |
|
[ "$resp" = "y" -o "$resp" = "Y" ] || |
|
return 0 |
|
fi |
|
rm -Rf "$env_d" |
|
} |
|
|
|
|
|
[ $# -eq 0 ] && { Usage "$VENV_BASE_D" 1>&2; exit 1; } |
|
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage "$VENV_BASE_D"; exit 0; } |
|
|
|
mode=activate |
|
case "$1" in |
|
create|list|destroy|activate) mode="$1"; shift;; |
|
esac |
|
|
|
"$mode" "$@" |