"""
Basic fabfile for django projects
"""
import posixpath
import os

from fabric.api import run, local, env, settings, cd, task
from fabric.contrib.files import exists
from fabric.operations import _prefix_commands, _prefix_env_vars, sudo

# Set to true if you can restart your webserver (via wsgi.py),
# false to stop/start your webserver
DJANGO_SERVER_RESTART = False

# EDIT THIS INFORMATION
env.hosts = ['']
env.password = ''
env.code_dir = ''
env.project_dir = ''
env.static_root = ''
env.virtualenv = ''
env.code_repo = ''
env.django_settings_module = ''

# Python version
PYTHON_BIN = "python3.4"


def virtualenv(venv_dir):
    """
    Context manager that establishes a virtualenv to use.
    """
    return settings(venv=venv_dir)


def run_venv(command, **kwargs):
    """
    Runs a command in a virtualenv (which has been specified using
    the virtualenv context manager
    """
    run("source %s/bin/activate" % env.virtualenv + " && " + command, **kwargs)


def install_dependencies():
    ensure_virtualenv()
    with virtualenv(env.virtualenv):
        with cd(env.code_dir):
            run_venv("pip install -r requirements/production.txt")


def ensure_virtualenv():
    if exists(env.virtualenv):
        return

    with cd(env.code_dir):
        run("virtualenv --no-site-packages --python=%s %s" %
            (PYTHON_BIN, env.virtualenv))
        run("echo %s > %s/lib/%s/site-packages/projectsource.pth" %
            (env.project_dir, env.virtualenv, PYTHON_BIN))


def ensure_src_dir():
    if not exists(env.code_dir):
        run("mkdir -p %s" % env.code_dir)
    with cd(env.code_dir):
        if not exists(posixpath.join(env.code_dir, '.git')):
            run('git clone %s .' % (env.code_repo))


@task
def update_bootstrap(tag=None):
    """
    Update Bootstrap files to tag version. If a tag isn't specified just
    get latest version.
    """
    parent_path = os.path.join(os.path.dirname(__file__),
                               '{{ project_name }}/static/vendor')
    local_path = os.path.join(parent_path, 'bootstrap')

    with settings(warn_only=True):
        # Create the source directory if it doesn't exist
        if not exists(parent_path):
            run('mkdir -p %s' % parent_path)

        with cd(local_path):
            # Since django-admin.py startproject remove the
            # hidden dirs (like .git/) we need to add the remote the first time
            if not exists(os.path.join(local_path, '.git')):
                run('git init')
                run('git remote add origin git@github.com:twbs/bootstrap.git')

            run('git pull origin master')
            # Checkout to tag if specified
            if tag:
                run('git checkout {0}'.format(tag))


@task
def push_sources():
    """
    Push source code to server
    """
    ensure_src_dir()
    local('git push origin master')
    with cd(env.code_dir):
        run('git pull origin master')


@task
def run_tests():
    """ Runs the Django test suite as is.  """
    local("./manage.py test")


@task
def version():
    """ Show last commit to the deployed repo. """
    with cd(env.code_dir):
        run('git log -1')


@task
def test():
    """
    Prints information about the host.
    Use it to check if env configuration is ok.
    """
    run("uname -a")


@task
def webserver_stop():
    """
    Stop the webserver that is running the Django instance
    """
    sudo("service apache2 stop")


@task
def webserver_start():
    """
    Starts the webserver that is running the Django instance
    """
    sudo("service apache2 start")


@task
def webserver_restart():
    """
    Restarts the webserver that is running the Django instance
    """
    if DJANGO_SERVER_RESTART:
        with cd(env.code_dir):
            run("touch %s/wsgi.py" % env.project_dir)
    else:
        with settings(warn_only=True):
            webserver_stop()
        webserver_start()


def restart():
    """ Restart the wsgi process """
    with cd(env.code_dir):
        run("touch %s/glance/wsgi.py" % env.code_dir)


def collectstatic():
    assert env.static_root.strip() != '' and env.static_root.strip() != '/'
    with virtualenv(env.virtualenv):
        with cd(env.code_dir):
            run_venv("./manage.py collectstatic --clear --noinput")

    run("chmod -R ugo+r %s" % env.static_root)


@task
def first_deployment_mode():
    """
    Use before first deployment to switch on fake migrations.
    """
    env.initial_deploy = True


@task
def run_migrations(app=None):
    """
    Run the migrations to the database
    Usage: fab run_migrations:app_name
    """
    with virtualenv(env.virtualenv):
        with cd(env.code_dir):
            if getattr(env, 'initial_deploy', False):
                run_venv("./manage.py syncdb --all")
                run_venv("./manage.py migrate --fake --noinput")
            else:
                run_venv("./manage.py syncdb --noinput")
                if app:
                    run_venv("./manage.py migrate %s --noinput" % app)
                else:
                    run_venv("./manage.py migrate --noinput")


@task
def sshagent_run(cmd):
    """
    Helper function.
    Runs a command with SSH agent forwarding enabled.

    Note:: Fabric (and paramiko) can't forward your SSH agent.
    This helper uses your system's ssh to do so.
    """
    # Handle context manager modifications
    wrapped_cmd = _prefix_commands(_prefix_env_vars(cmd), 'remote')
    try:
        host, port = env.host_string.split(':')
        return local(
            "ssh -p %s -A %s@%s '%s'" % (port, env.user, host, wrapped_cmd)
        )
    except ValueError:
        return local(
            "ssh -A %s@%s '%s'" % (env.user, env.host_string, wrapped_cmd)
        )


@task
def deploy():
    """
    Deploy the project.
    """
    with settings(warn_only=True):
        webserver_stop()
    push_sources()
    install_dependencies()
    run_migrations()
    collectstatic()
    webserver_start()

@@ -1733,1017 +1733,8 @@
 )%0A%0A%0A
-@task%0Adef update_bootstrap(tag=None):%0A    %22%22%22%0A    Update Bootstrap files to tag version. If a tag isn't specified just%0A    get latest version.%0A    %22%22%22%0A    parent_path = os.path.join(os.path.dirname(__file__),%0A                               '%7B%7B project_name %7D%7D/static/vendor')%0A    local_path = os.path.join(parent_path, 'bootstrap')%0A%0A    with settings(warn_only=True):%0A        # Create the source directory if it doesn't exist%0A        if not exists(parent_path):%0A            run('mkdir -p %25s' %25 parent_path)%0A%0A        with cd(local_path):%0A            # Since django-admin.py startproject remove the%0A            # hidden dirs (like .git/) we need to add the remote the first time%0A            if not exists(os.path.join(local_path, '.git')):%0A                run('git init')%0A                run('git remote add origin git@github.com:twbs/bootstrap.git')%0A%0A            run('git pull origin master')%0A            # Checkout to tag if specified%0A            if tag:%0A                run('git checkout %7B0%7D'.format(tag))%0A%0A%0A
 @tas

Delete code to update bootstrap