|
#!/usr/bin/env python |
|
""" |
|
Provides script to manage development tasks |
|
""" |
|
|
|
from os import path |
|
from subprocess import check_call, CalledProcessError |
|
from urllib.parse import urlsplit |
|
|
|
from flask import current_app as app |
|
from flask_script import Server, Manager |
|
|
|
from src.factory import create_app |
|
|
|
DEF_PORT = 5000 |
|
|
|
manager = Manager(create_app) |
|
manager.add_option('-m', '--cfgmode', dest='config_mode', default='Development') |
|
manager.add_option('-f', '--cfgfile', dest='config_file', type=path.abspath) |
|
manager.main = manager.run # Needed to do `manage <command>` from the cli |
|
|
|
|
|
@manager.option('-h', '--host', help='The server host') |
|
@manager.option('-p', '--port', help='The server port') |
|
@manager.option('-t', '--threaded', help='Run multiple threads', action='store_true') |
|
def runserver(**kwargs): |
|
"""Runs the flask development server |
|
|
|
Overrides the built-in `runserver` behavior |
|
""" |
|
with app.app_context(): |
|
if app.config.get('SERVER'): |
|
parsed = urlsplit(app.config['SERVER']) |
|
host, port = parsed.netloc, parsed.port or DEF_PORT |
|
else: |
|
host, port = app.config['HOST'], DEF_PORT |
|
|
|
kwargs.setdefault('host', host) |
|
kwargs.setdefault('port', port) |
|
|
|
server = Server(**kwargs) |
|
args = [ |
|
app, server.host, server.port, server.use_debugger, |
|
server.use_reloader, server.threaded, server.processes, |
|
server.passthrough_errors] |
|
|
|
server(*args) |
|
|
|
|
|
@manager.option('-h', '--host', help='The server host') |
|
@manager.option('-p', '--port', help='The server port') |
|
@manager.option('-t', '--threaded', help='Run multiple threads', action='store_true') |
|
def serve(**kwargs): |
|
"""Runs the flask development server |
|
|
|
Alias for `runserver` |
|
""" |
|
runserver(**kwargs) |
|
|
|
|
|
@manager.option('-w', '--where', help='Modules to check') |
|
def lint(where): |
|
"""Check style with flake8 linter""" |
|
def_where = ['src', 'manage.py', 'config.py'] |
|
extra = where.split(' ') if where else def_where |
|
|
|
try: |
|
check_call(['flake8'] + extra) |
|
except CalledProcessError as e: |
|
exit(e.returncode) |
|
|
|
|
|
@manager.option('-w', '--where', help='test path') |
|
@manager.option( |
|
'-x', '--stop', help='Stop after first error', action='store_true') |
|
@manager.option( |
|
'-f', '--failed', help='Run failed tests', action='store_true') |
|
@manager.option( |
|
'-c', '--cover', help='Add coverage report', action='store_true') |
|
@manager.option( |
|
'-v', '--verbose', help='Use detailed errors', action='store_true') |
|
@manager.option( |
|
'-p', '--parallel', help='Run tests in parallel in multiple processes', |
|
action='store_true') |
|
def test(where, stop, **kwargs): |
|
"""Run nose, tox, and script tests""" |
|
opts = '-x' if stop else '' |
|
opts += '-vv' if kwargs.get('verbose') else '' |
|
opts += '--with-coverage' if kwargs.get('cover') else '' |
|
opts += '--last-failed' if kwargs.get('failed') else '' |
|
opts += '--processes=-1' if kwargs.get('parallel') else '' |
|
opts += '-w %s' % where if where else '' |
|
|
|
try: |
|
check_call('python -m pytest src/tests {}'.format(opts).split(' ')) |
|
except CalledProcessError as e: |
|
exit(e.returncode) |
|
|
|
|
|
if __name__ == '__main__': |
|
manager.run() |