Skip to content

Instantly share code, notes, and snippets.

@wolever
Created September 4, 2012 21:23
Show Gist options
  • Save wolever/3626702 to your computer and use it in GitHub Desktop.
Save wolever/3626702 to your computer and use it in GitHub Desktop.
Better than virtualenvwrapper
#!/bin/bash
# Source this from ~/.bashrc or similar.
if [[ ! "$0" =~ "bash" ]]; then
echo "$0 should be sourced, not executed."
exit 1
fi
_workon_list_virtualenvs() {
local virtualenv
local file
for file in $WORKON_HOME/*/bin/activate; do
virtualenv="${file%/bin/activate}"
virtualenv="${virtualenv##*/}"
echo "$virtualenv"
done
}
# Set up tab completion. (from virtualenvwrapper, Adapted from Arthur Koziel's
# version at http://arthurkoziel.com/2008/10/11/virtualenvwrapper-bash-completion/)
_workon_setup_tab_completion () {
_virtualenvs () {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "$(_workon_list_virtualenvs)" -- ${cur}) )
}
complete -o default -o nospace -F _virtualenvs workon
}
_workon_initialize() {
_workon_setup_tab_completion
}
workon() {
if [[ -z "$WORKON_HOME" ]]; then
echo "$WORKON_HOME not set"
return 3
fi
local virtualenv="${1-}"
if [[ -z "$virtualenv" ]]; then
_workon_list_virtualenvs
return 2
fi
if [[ ! -d "$WORKON_HOME/$virtualenv" ]]; then
echo "ERROR: '$virtualenv' does not exist"
return 1
fi
export WORKON_OLD_PATH="$PWD"
. "$WORKON_HOME/$virtualenv/bin/activate"
cd "$VIRTUAL_ENV/../../"
}
_workon_initialize
#!/bin/bash
# Setup a virtualenv according to my preferences.
# Run this after creating a virtualenv.
# Expects that the virtualenv has been setup with::
# virtualenv --no-site-packages env/virtualenv-name
IFS="`printf "\n\t"`"
set -eu
cd "`dirname "$0"`"
if [[ ! "${VIRTUAL_ENV-}" ]]; then
echo "Not in a virtual env!"
exit 1
fi
cd "$VIRTUAL_ENV/../"
VIRTUAL_ENV_NAME="$(basename "$VIRTUAL_ENV")"
if [[ ! -e "$HOME/.virtualenvs/$VIRTUAL_ENV_NAME" ]]; then
echo "symlinking into ~/.virtualenvs/"
ln -s "$VIRTUAL_ENV" "$HOME/.virtualenvs/"
fi
if [[ ! -e "site-packages" ]]; then
if [[ -L "site-packages" ]]; then
unlink "site-packages"
fi
echo "symlinking virtualenv's site packages into env/site-packages"
ln -s "$VIRTUAL_ENV"/lib/python*/site-packages ./
fi
if [[ ! -e "activate" ]]; then
echo "symlinking activate into env/activate"
ln -s "$VIRTUAL_ENV/bin/activate" ./
fi
# Because pip can't properly intsall GNU readline, I have used easy_install to
# put it in the global site-packages, and i'll just copy it into new
# virtualenvs.
readline=( site-packages/readline* )
if [[ ! -e $readline ]]; then
echo "installing readline"
easy_install readline
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment