Source

dotfiles / bin / setupvirtualenv

#!/bin/bash
# Setup a virtualenv according to my preferences.
# 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