from __future__ import with_statement, print_function from os.path import dirname, abspath, join as join_path from os import walk from functools import wraps from StringIO import StringIO from jinja2 import Environment, FileSystemLoader from fabric.api import * from fabric.context_managers import settings from fabric.contrib.files import exists, upload_template from fabric.colors import yellow, red, white, green # # GLOBALS # env.LOCAL_PATH = dirname(abspath(__file__)) if not env.hosts: env.hosts = ['localhost'] # Doctor checkups DOCTOR = { 'apps': ['virtualenv', 'python', 'npm', 'grunt'] } # # CONTEXT MANAGERS # def virtualenv(): """ Activates virtualenv first """ return prefix('source {}/.virtualenv/bin/activate'.format(env.LOCAL_PATH)) # # TASKS # @task def setup_environment(): """ Prepares environment for the application """ with cd(env.LOCAL_PATH): execute(setup_virtualenv) execute(setup_tools) execute(setup_database) @task def setup_virtualenv(appenv='local'): """ Creates or updates a virtualenv """ if not exists('.virtualenv'): print(yellow('Create virtualenv')) run('virtualenv .virtualenv') with virtualenv(): print(yellow('Installing requirements')) run('pip install -r requirements-{}.txt'.format(appenv)) @task def setup_tools(): # Setup frontend tools print(yellow('Installing npm dependencies')) run('npm install') print(yellow('Installing bower dependencies')) run('bower install') @task def setup_database(): """ Create or update the database """ with virtualenv(): print(yellow('SyncDB')) run('python manage.py syncdb') print(yellow('Migrate')) run('python manage.py migrate') @task def doctor(): print(yellow('Checking for software:')) for app in DOCTOR['apps']: print(white('{}'.format(app)), end=': ') check = run('which {}'.format(app), quiet=True) if check.succeeded: print(green('present')) else: print(red('not present')) # # LOCAL ONLY # @task @hosts(['localhost']) def runserver(): """ Executes local development server """ with cd(env.LOCAL_PATH): with virtualenv(): run('python manage.py runserver 0.0.0.0:8000') @task @hosts(['localhost']) def rungrunt(): """ Executes grunt """ with cd(env.LOCAL_PATH): run('grunt watch')