Skip to content

Instantly share code, notes, and snippets.

@sashahart
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sashahart/3ee7dcfe1c2a51ebede9 to your computer and use it in GitHub Desktop.
Save sashahart/3ee7dcfe1c2a51ebede9 to your computer and use it in GitHub Desktop.
some minimal virtualenvwrapper type shell functions
#!/bin/sh
_ave_usage="Usage: ave VIRTUALENVNAME
For example, to activate a virtualenv named FOO, use:
ave FOO
To activate a virtualenv with a space in its name like 'FOO BAR':
ave FOO BAR
Or more explicitly:
ave 'FOO BAR'"
_mve_usage="Usage: mve VIRTUALENVNAME
For example, to make a virtualenv named FOO, use:
mve FOO
To make a virtualenv with a space in its name, quote it:
mve 'FOO BAR'
The name of the virtualenv you want to make should always be first.
Any additional arguments (such as switches) are simply passed through to
virtualenv after the directory name. E.g.:
mve foobar -p python3.4
For help on these options, run:
virtualenv --help
"
_rve_usage="Usage: rve VIRTUALENVNAME
For example, to remove a virtualenv named FOO, use:
rve FOO
To remove a virtualenv with a space in its name, quote it:
rve 'FOO BAR'"
# n.b.: we don't clobber the name 'workon' to avoid clashing with concurrent
# virtualenvwrapper installs. But user can alias or whatever
ave () {
# Find and source the activate script of the named virtualenv.
# Use virtualenvwrapper's WORKON_HOME if present, otherwise default to
# ~/.virtualenvs.
local ve_home="${WORKON_HOME:-${HOME}/.virtualenvs}"
local ve_name="$*"
local ve_dir="${ve_home}/${ve_name}"
local ve_script="${ve_dir}/bin/activate"
if ! test -n "${ve_name}"; then
echo "Oops: name a virtualenv to activate."
echo "$_ave_usage"
return 1
fi
if ! test -e "${ve_script}"; then
if ! test -d "${ve_dir}"; then
echo "Virtualenv '${ve_name}' not found (looked for '${ve_dir}')"
else
echo "Virtualenv '${ve_name}' is broken (no script at '${ve_script}')"
fi
return 1
fi
. "${ve_script}"
}
mve () {
# TODO: bleh, name is required to be first.
local ve_home="${WORKON_HOME:-${HOME}/.virtualenvs}"
local ve_name="$1"
local ve_dir="${ve_home}/${ve_name}"
if test $# -lt 1 || ! test -n "${ve_name}"; then
echo "Oops: give a name for the new virtualenv you want to make."
echo "$_mve_usage"
return 1
fi
shift
command virtualenv "${ve_dir}" "$@"
}
rve () {
local ve_home="${WORKON_HOME:-${HOME}/.virtualenvs}"
local ve_name="$1" #"$@"
local ve_dir="${ve_home}/${ve_name}"
if ! test -n "${ve_home}" || ! test -n "${ve_dir}"; then
echo "Oops: couldn't figure out what to remove."
echo "Aborting without removing anything."
return 1
fi
if test $# -ne 1 || ! test -n "${ve_name}"; then
echo "Name exactly one virtualenv name to remove."
echo ""
echo "$_rve_usage"
return 1
fi
if ! test -d "${ve_dir}"; then
echo "Virtualenv '${ve_name}' not found (looked for '${ve_dir}')"
return 1
fi
if ! trash -v "${ve_dir}"; then
echo "Failed to remove virtualenv '${ve_name}' (at '${ve_dir}')."
return 1
fi
if test "$VIRTUAL_ENV" = "${ve_dir}"; then
deactivate
fi
}
dve () {
deactivate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment