From c98e80d30a183a419fbadeb54e52ca90c0c30e88 Mon Sep 17 00:00:00 2001 From: Eric Barbour Date: Jun 23 2016 17:25:31 +0000 Subject: Add a tasks.py for invoke Invoke is a task running tool that makes development tasks easier to run. Examples: $ inv server # runs python runserver.py $ inv req # runs pip install -r requirements.txt $ inv req --test # runs ^ plus pip install -r test-requrements.txt --- diff --git a/requirements.txt b/requirements.txt index c3666e6..71d0a80 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,7 @@ fmn.lib fmn.rules gunicorn html5lib +invoke>=0.13 munch psycopg2 pytz diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..91da05a --- /dev/null +++ b/tasks.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python + +from invoke import run, task + + +@task(aliases=['a']) +def assets(ctx, watch=False): + cmd = './node_modules/.bin/webpack' + if watch: + cmd += ' -w' + run(cmd, echo=True) + + +@task(aliases=['c']) +def clean(ctx): + cmd = 'find . -name "*.pyc" -delete' + run(cmd, echo=True) + + +@task +def credentials(ctx): + content = """config = { + # Put your FAS credentials here... + # but leave them commented out. Note that, if you leave your fas + # credentials out, some widgets won't be as accurate as they could be.. + # but it also makes the app faster than it would otherwise be (which is + # nice for development). + #'fas_credentials': { + # 'username': 'YOUR_FAS_USERNAME', + # 'password': 'YOUR_FAS_PASSWORD', + #}, + + # Go and generate a token for this here: + # https://github.com/settings/tokens + 'github.oauth_token': 'YOUR_GITHUB_TOKEN', +}""" + run('touch fedmsg.d/credentials.py', echo=True) + print('Writing credential template') + print(content) + with open('fedmsg.d/credentials.py', 'w') as fp: + fp.write(content) + + +@task(aliases=['flake8']) +def flake(ctx): + run('flake8 .', echo=True) + + +@task +def populate(ctx): + run('python populate.py', echo=True) + + +@task(aliases=['rdb']) +def restart_db(ctx, serve=False): + cmd = 'rm /var/tmp/hubs.db;\ + rm /var/tmp/fedora-hubs-cache.db;\ + PYTHONPATH=. python populate.py;' + + if serve: + cmd += 'gunicorn -w 8 -t 240 --log-config logging.ini hubs.app:app' + + run(cmd, echo=True) + + +@task(aliases=['req']) +def requirements(ctx, test=False): + run('pip install -r requirements.txt', echo=True) + if test: + run('pip install -r test-requirements.txt', echo=True) + + +@task(aliases=['s', 'serve']) +def server(ctx, gunicorn=False, log=False): + cmd = '' + if gunicorn: + cmd = 'gunicorn -w 8 -t 240 --log-config logging.ini hubs.app:app' + else: + cmd = 'python runserver.py' + + if log: + cmd += ' >>log 2>&1' + run(cmd, echo=True) + + +@task +def shell(ctx): + ''' + required: + pip install ipython + ''' + import requests + import fedmsg.config + import fedmsg.meta + + import hubs.models + import hubs.widgets.base + + from hubs.app import app + fedmsg_config = fedmsg.config.load_config() + fedmsg.meta.make_processors(**fedmsg_config) + + session = hubs.models.init(fedmsg_config['hubs.sqlalchemy.uri']) + + context = { + 'requests': requests, + 'fedmsg_config': fedmsg_config, + 'session': session, + 'models': hubs.models, + 'widgets.base': hubs.widgets.base, + 'app': app + } + + try: + try: + # 0.10.x + from IPython.Shell import IPShellEmbed + ipshell = IPShellEmbed() + ipshell(global_ns={}, local_ns=context) + except ImportError: + # 0.12+ + from IPython import embed + embed(user_ns=context) + return + except ImportError: + pass + + +@task +def stream(ctx): + cmd = 'python ev-server/hubs-stream-server.py' + run(cmd, echo=True) + + +@task(aliases=['t', 'tests']) +def test(ctx, coverage=False): + cmd = 'PYTHON_PATH=. nosetests ' + if coverage: + cmd += '--with-coverage --cover-erase --cover-package=hubs --cover-html' + run(cmd, echo=True) + + +@task(default=True) +def usage(ctx): + run('invoke --list')