-
Notifications
You must be signed in to change notification settings - Fork 3
/
manage.py
executable file
·131 lines (108 loc) · 3.74 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
"""
manage
~~~~~~
Utilities for working with the Qcumber API app.
"""
import sys
from functools import wraps
def command(func, _funcs={}):
"""Decorate functions with this to register them as commands"""
# register the command
func_name = func.__name__.lower()
if func_name in _funcs:
raise Exception('Duplicate definition for command {}'.format(func_name))
_funcs[func_name] = func
# play nice and leave the command where it was in this script
@wraps(func)
def wrapped(*args):
return func(*args)
return wrapped
@command
def help():
"""Get usage information about this script"""
print('\nUsage: {} [command]\n'.format(sys.argv[0]))
print('Available commands:')
for name, func in command.__defaults__[0].items(): # _funcs={}
print(' * {:16s} {}'.format(name, func.__doc__ or ''))
raise SystemExit(1)
@command
def init():
"""Set the api up: clone the data repo, etc."""
import api
try:
api.repo.clone()
except api.repo.NotEmptyRepoError:
print('Not cloning into {} because it is not empty.'.format(api.config['DATA_LOCAL']))
except api.ConfigException as e:
print('There was a configuration error: {}'.format(e))
@command
def runserver(host="127.0.0.1", port="5000"):
"""Run a local development server"""
try:
port_num = int(port)
except ValueError:
print('The port number must be in integer (got "{}")'.format(port))
raise SystemExit(1)
try:
from werkzeug.serving import run_simple
except ImportError:
print('The werkzeug development server could not be imported :(\n'
'Have you installed requirements ($ pip install -r requirements'
'.txt) or perhaps forgotten to activate a virtualenv?')
raise SystemExit(1)
from api import app as app
run_simple(host, port_num, app, use_debugger=True, use_reloader=True)
@command
def clean():
"""Clean up __pycache__ folders (left behind from testing perhaps)"""
import os
import shutil
for root, subfolders, files in os.walk('.'):
if '__pycache__' in subfolders:
garbage = os.path.join(root, '__pycache__')
shutil.rmtree(garbage)
@command
def test(skip_lint=False, cleanup=True):
"""Run the app's test suite. Cleans up after by default."""
import unittest
suite = unittest.TestLoader().discover('test') # get all the tests
result = unittest.TextTestRunner(verbosity=2).run(suite)
if not result.wasSuccessful():
raise SystemExit(1)
if cleanup:
clean()
if not skip_lint:
lint()
@command
def lint():
"""Run pep8 linting to verify conformance with the style spec."""
import os
import pep8
import time
style = pep8.StyleGuide(max_line_length=119)
py_files = []
for root, dirnames, filenames in os.walk('.'):
py_files += [os.path.join(root, f) for f in filenames if f.endswith('.py')]
t0 = time.time()
result = style.check_files(py_files)
tf = time.time()
print('Linted {} files in {:.3f}s'.format(len(py_files), tf-t0))
if result.total_errors > 0:
print('FAILED (errors={})'.format(result.total_errors))
raise SystemExit(1)
else:
print('OK')
if __name__ == '__main__':
# Get the command, or run 'help' if no command is provided
if len(sys.argv) < 2:
cmd, args = 'help', []
else:
cmd, args = sys.argv[1].lower(), sys.argv[2:]
# Map the command to a function, falling back to 'help' if it's not found
funcs = command.__defaults__[0] # _funcs={}
if cmd not in funcs:
print('Command "{}" not found :('.format(cmd))
cmd, args = 'help', []
# do it!
funcs[cmd](*args)