Skip to content

Instantly share code, notes, and snippets.

@eddy-22
Forked from erszk/ve.sh
Last active June 14, 2021 02:15
Show Gist options
  • Save eddy-22/c24592b7dcf37509f068866eb1b9f1b4 to your computer and use it in GitHub Desktop.
Save eddy-22/c24592b7dcf37509f068866eb1b9f1b4 to your computer and use it in GitHub Desktop.
[VE.sh] virtualenv wrapper in bash #virtualenv #shell
# -*- mode: sh -*-
# vim: syn=sh
# check to make sure virtualenv installed
if ! command which -s virtualenv; then
>&2 echo "You don't have virtualenv installed in your path. To install it:"
>&2 echo "pip install virtualenv"
return 1
fi
function ve {
function _usage {
echo '
usage: ve [-h]
ve [-d ROOT] command [ENV]'
}
function _help {
echo "ve - a bash wrapper for managing Python virtual environments"
_usage
echo '
commands:
mk, mkenv make a virtual environment
sw, swenv switch to a virtual environment
del, rm, rmenv delete a virtual environment
ls, lsenv list your virtual environments
positional arguments:
ENV the name of a virtual env, defaults to the name of the
current working directory
optional arguments:
-h, --help print this help message
-d DIR, --dir DIR directory where virtual envs are stored;
overrides VE_ROOT if set [default: ~/.virtualenv]
environment variables:
VE_ROOT directory where virtual envs are stored
DO NOT DO:
ve mkenv ./
ve sw ./
TO USE: put something like the following in your .bashrc or .bash_profile:
[ -f ~/some/path/ve.sh ] && . ~/some/path/ve.sh
NOTE: Each virtual environment within a given root virtual env directory must
have a unique name. Virtual environment names can be arbitrary but it is best
to name them after project directories so that way you can just issue the
command "ve sw" from the project root'
}
if [ $# -eq 0 ]; then
_help
return
fi
local root="$VE_ROOT"
case "$1" in
-d|--dir )
root="$2"
shift
shift;;
esac
# target is what the name of the virtual environment will be
local target
target="$(basename "${2:-$(pwd)}")"
# allow user to use environment variable to set VE_ROOT directory and default to
# ~/.virtualenv
[ -z "$root" ] && root="$HOME/.virtualenv"
# make root directory if it does not exist, exit on failure
function _mkroot {
if [ ! -d "$root" ]; then
echo "Creating root directory: $root"
if ! mkdir -p -- "$root" 2>/dev/null; then
echo "Could not create root directory."
return 2
fi
fi
}
# the full path to the virtual environment
local dir="$root/$target"
# make a virtual environment if directory does not exist
function mkenv {
_mkroot
if [ ! -d "$dir" ]; then
virtualenv "$dir"
else
>&2 echo "That virtual environment already exists. To switch to it:"
>&2 echo "ve sw $target"
fi
}
# switch to virtual environment by sourcing activate file of said environment if
# it exists
function swenv {
if [ -f "$dir/bin/activate" ]; then
. "$dir/bin/activate"
else
>&2 echo "That virtual environment does not exist. To make it:"
>&2 echo "ve mk $target"
fi
}
# deactivate and delete a virtual environment if it exists
function rmenv {
if [ -d "$dir" ]; then
if [ "$(command which pip)" == "$dir/bin/pip" ]; then
deactivate
fi
command rm -rf "$dir"
else
>&2 echo "That virtual environment does not exist."
fi
}
function lsenv {
command ls -1 "$root"
}
# main program
case "$1" in
mk|mkenv )
mkenv "$dir";;
sw|swenv )
swenv "$dir";;
del|rm|rmenv )
rmenv "$dir";;
ls|lsenv )
lsenv;;
-h|--help )
_help;;
* )
>&2 echo "invalid argument: $1"
_usage;;
esac
}
# TODO:
# assert args valid before making a root directory, BUG-M-MH
# handle paths as arguments properly e.g. ./, BUG-ML-M: POSIX
# improve lsenv to do a `pip list` for a venv or list bin, FTR-M-MH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment