This repository was archived by the owner on Mar 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmanage.py
executable file
·186 lines (135 loc) · 5.04 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
from __future__ import (
absolute_import, division, print_function, with_statement,
unicode_literals)
import os.path as p
from subprocess import call, check_call
from pprint import pprint
from flask import current_app as app
from flask.ext.script import Server, Manager
from tabutils import fntools as ft
from app import create_app, db, models, utils
manager = Manager(create_app)
manager.add_option(
'-m', '--cfgmode', dest='config_mode', default='Development')
manager.add_option('-f', '--cfgfile', dest='config_file', type=p.abspath)
manager.main = manager.run # Needed to do `manage <command>` from the cli
@manager.option('-h', '--host', help='The server host', default=None)
@manager.option('-p', '--port', help='The server port', default=None)
@manager.option(
'-t', '--threaded', help='Run multiple threads', action='store_true')
def runserver(**kwargs):
# Overriding the built-in `runserver` behavior
"""Runs the flask development server"""
with app.app_context():
skwargs = {
'host': kwargs.get('host') or app.config['HOST'],
'port': kwargs.get('port') or app.config['PORT'],
'threaded': kwargs.get('threaded'),
}
server = Server(**skwargs)
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', default=None)
@manager.option('-p', '--port', help='The server port', default=None)
@manager.option(
'-t', '--threaded', help='Run multiple threads', action='store_true')
def serve(**kwargs):
# Alias for `runserver`
"""Runs the flask development server"""
runserver(**kwargs)
@manager.command
def checkstage():
"""Checks staged with git pre-commit hook"""
path = p.join(p.dirname(__file__), 'app', 'tests', 'test.sh')
cmd = "sh %s" % path
return call(cmd, shell=True)
@manager.option('-F', '--file', help='Lint file', default='')
def lint(file):
"""Check style with flake8"""
return call("flake8 %s" % file, shell=True)
@manager.option('-w', '--where', help='Requirement file', default=None)
@manager.option(
'-x', '--stop', help='Stop after first error', action='store_true')
def test(where, stop=False):
"""Run nose tests"""
nose = "python `which nosetests`"
opts = 'xv' if stop else 'v'
opts += 'w %s' % where if where else ''
return call("%s -%s" % (nose, opts), shell=True)
@manager.command
def deploy():
"""Deploy staging app"""
check_call('heroku keys:add ~/.ssh/id_rsa.pub', shell=True)
check_call('git push origin features', shell=True)
@manager.option('-r', '--requirement', help='Requirement file', default=None)
def pipme(requirement):
"""Install requirements.txt"""
prefix = '%s-' % requirement if requirement else ''
call('pippy -r %srequirements.txt' % prefix, shell=True)
@manager.command
def require():
"""Create requirements.txt"""
cmd = 'pip freeze -l | grep -vxFf dev-requirements.txt '
cmd += '| grep -vxFf prod-requirements.txt '
cmd += '> requirements.txt'
call(cmd, shell=True)
@manager.command
def createdb():
"""Creates database if it doesn't already exist"""
with app.app_context():
db.create_all()
print('Database created')
@manager.command
def cleardb():
"""Removes all content from database"""
with app.app_context():
db.drop_all()
print('Database cleared')
@manager.command
def setup():
"""Removes all content from database and creates new tables"""
with app.app_context():
cleardb()
createdb()
@manager.command
def populate():
"""Populates db with data"""
limit = 0
with app.app_context():
table_name = 'Data'
table = getattr(models, table_name)
row_limit = app.config['ROW_LIMIT']
chunk_size = min(row_limit or 'inf', app.config['CHUNK_SIZE'])
debug, test = app.config['DEBUG'], app.config['TESTING']
if test:
createdb()
data = utils.fetch_data(app.config)
del_count = table.query.delete(synchronize_session=False)
db.session.commit()
if debug:
print(
'Deleted %s records from the %s table...' % (
del_count, table_name))
for records in ft.chunk(data, chunk_size):
in_count = len(records)
pprint(records[0])
limit += in_count
if debug:
print(
'Inserting %s records into the %s table...' % (
in_count, table_name))
if test:
pprint(records)
db.engine.execute(table.__table__.insert(), records)
if row_limit and limit >= row_limit:
break
if debug:
print(
'Successfully inserted %s records into the %s table!' % (
limit, table_name))
if __name__ == '__main__':
manager.run()