-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
79 lines (66 loc) · 2.02 KB
/
app.py
File metadata and controls
79 lines (66 loc) · 2.02 KB
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
import os, sys
import click
from simple import create_app
from simple.models import db, User, Role, Post
# coverage的配置
COV = None
if os.environ.get('FLASK_COVERAGE'):
import coverage
COV = coverage.coverage(branch=True, include='simple/*')
COV.start()
# 创建simpleblog实例
app = create_app(os.getenv('FLASK_CONFIG') or 'development')
# shell上下文
@app.shell_context_processor
def make_shell_context():
return dict(app=app, db=db, User=User, Role=Role, Post=Post)
# 测试的命令
@app.cli.command()
@click.option(
'--coverage/--no-coverage',
default=False,
help='run tests under code coverage')
def test(coverage):
# 设置coverage的环境变量
if coverage and not os.environ.get('FLASK_COVERAGE'):
import subprocess
os.environ['FLASK_COVERAGE'] = '1'
sys.exit(subprocess.call(sys.argv))
# 启动单元测试
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
if COV:
COV.stop()
COV.save()
print('Coverage summary:')
COV.report()
basedir = os.path.abspath(os.path.dirname(__file__))
covdir = os.path.join(basedir, 'tmp/coverage')
COV.html_report(directory=covdir)
print('HTML version: file://%s/index.html' % covdir)
COV.erase()
# 分析源码运行时间
@app.cli.command()
@click.option(
'--length',
default=25,
help='Number of functions to include in the profiler report')
@click.option(
'--profile-dir',
default=None,
help='Directory where profiler data files are saved')
def profile(length, profile_dir):
from werkzeug.contrib.profiler import ProfilerMiddleware
app.wsgi_app = ProfilerMiddleware(
app.wsgi_app, restrictions=[length], profile_dir=profile_dir)
app.run()
# 升级程序的操作
@app.cli.command()
def deploy():
from flask_migrate import upgrade
from simple.models import Role
# 数据库迁移
upgrade()
# 创建角色
Role.insert_roles()