Source

rmsfab / installers / python.py

Full commit
"""
Install and setup python virtualenv and virtualenvwrapper
"""

from os import getenv
from datetime import datetime
from textwrap import dedent
from fabric.operations import sudo
from path import path

from utils import *

DEFAULT_ENV_DIR = 'python-envs'

def install_virtualenv(env_user, env_dir=DEFAULT_ENV_DIR):
    """Install virtualenv and virtualenvwrapper, optionally setup for a user"""
    sudo("pip install virtualenv virtualenvwrapper")
    home = path(run("ls -d ~%s" % env_user))
    env = { 'user': env_user, 'dir': home/env_dir }
    sudo("test -e %(dir)s || mkdir -p %(dir)s && chown %(user)s:`id --group %(user)s` %(dir)s" % env)
    sudo('export WORKON_HOME=%(dir)s ; source /usr/local/bin/virtualenvwrapper.sh' % env, user=env_user)
    bashrc = home/".bashrc"
    if bashrc.exists():
        script = '''
        export WORKON_HOME=%(dir)s
        source /usr/local/bin/virtualenvwrapper.sh
        ''' % env
        append_file(bashrc, dedent(script), "## Updated by rmsfab on " + str(datetime.now()))