-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmanage.py
More file actions
executable file
·126 lines (105 loc) · 4.2 KB
/
manage.py
File metadata and controls
executable file
·126 lines (105 loc) · 4.2 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from flask import current_app
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand
from app import create_app, db
from app.models.blog import BlogPost, BlogCategory, BlogTag
from app.models.movie import MoviePost, MovieStill, MovieCategory, MovieTag
from app.models.photo import PhotoPost, PhotoImage, PhotoCategory, PhotoTag
"""
Development Usage:
----------------------------------------------------------------
manage.py runserver --passthrough-errors --no-reload
manage.py test/profile/deploy
"""
"""
Import Environment Variables from '.pysite_env'
----------------------------------------------------------------
"""
environment_file = os.path.join(os.path.abspath(os.path.dirname(__file__)), u'.pysite_env')
if os.path.exists(environment_file):
print u'Importing environment variables from \'%s\' ...' % environment_file
for line in open(environment_file):
if line[0] == u'#': # comments
continue
kv = line.split(u'=', 1)
if len(kv) == 2:
os.environ[kv[0].strip()] = kv[1].split(u'#')[0].strip()
else:
print u'Can not find environment file (%s)!' % environment_file
exit()
COV = None
if os.environ.get(u'FLASK_COVERAGE'):
import coverage
COV = coverage.coverage(branch=True, include=u'app/*')
COV.start()
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db,
BlogPost=BlogPost, BlogCategory=BlogCategory, BlogTag=BlogTag,
MoviePost=MoviePost, MovieStill=MovieStill, MovieCategory=MovieCategory, MovieTag=MovieTag,
PhotoPost=PhotoPost, PhotoImage=PhotoImage, PhotoCategory=PhotoCategory, PhotoTag=PhotoTag)
manager.add_command(u"shell", Shell(make_context=make_shell_context))
manager.add_command(u'db', MigrateCommand)
@manager.command
def test(coverage=False):
"""
Runs the unit tests.
"""
if coverage and not os.environ.get(u'FLASK_COVERAGE'):
import sys
os.environ[u'FLASK_COVERAGE'] = u'1'
os.execvp(sys.executable, [sys.executable] + sys.argv)
import unittest
tests = unittest.TestLoader().discover(u'tests')
unittest.TextTestRunner(verbosity=2).run(tests)
if COV:
COV.stop()
COV.save()
print(u'Coverage Summary:')
COV.report()
basedir = os.path.abspath(os.path.dirname(__file__))
covdir = os.path.join(basedir, u'tmp/coverage')
COV.html_report(directory=covdir)
print(u'HTML version: file://%s/index.html' % covdir)
COV.erase()
@manager.command
def profile(length=25, profile_dir=None):
"""
Starts the application under the code profiler.
"""
from werkzeug.contrib.profiler import ProfilerMiddleware
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[length], profile_dir=profile_dir)
app.run()
@manager.command
def deploy():
"""
Runs deployment tasks.
"""
data_root = current_app.data_path
if os.path.exists(data_root):
print u'SKIP: Target directory \'%s\' already exists, skip deploy.' % data_root
return
print u'DEPLOY: Creating necessary directories for application data storage ...'
data_dirs = [u'', u'blog', u'music', u'movie', u'photo', u'files', u'files/public']
for dd in data_dirs:
dir_new = os.path.join(data_root, dd)
os.mkdir(dir_new)
print u' * Creating %s' % dir_new
print u'DEPLOY: Preparing database ...'
db.create_all()
from app.models import user, blog, movie, photo
db.session.add(user.User(email=current_app.config[u'MAIL_USERNAME'],
username=u'ROOT_ADMIN', password=u'12345678',
permissions=user.Role.ROOT, confirmed=True))
db.session.add(blog.BlogCategory(id=0, name=u'未分类'))
db.session.add(movie.MovieCategory(id=0, name=u'未分类'))
db.session.add(photo.PhotoCategory(id=0, name=u'未分类'))
print u'DEPLOY: Successfully finished!'
print u'(Now you can start this project by running \'./manage.py runserver -t HOST -p PORT\')'
if __name__ == u'__main__':
manager.run()