-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrun.py
executable file
·96 lines (70 loc) · 2.46 KB
/
run.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
#!/usr/bin/env python
import os
import sys
__all__ = {'server', 'makemessages', 'compilemessages', 'tests'}
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
PARENT_DIR = os.path.dirname(ROOT_DIR)
def server():
"""
Runs developer server on 0.0.0.0 with Dev config.
"""
os.environ['SITE_CONFIG'] = 'website.config.Dev'
from website import site
site.run(host='0.0.0.0')
def tests():
"""
Runs tests and reports coverage.
"""
os.environ['SITE_CONFIG'] = 'website.config.Testing'
os.system('coverage run -m unittest discover -t {} -s {} '
'&& coverage report'
.format(PARENT_DIR, ROOT_DIR))
def makemessages():
"""
Makes translation messages.
"""
pot = os.path.join(ROOT_DIR, 'messages.pot')
translations = os.path.join(ROOT_DIR, 'translations')
babel = os.path.join(ROOT_DIR, 'babel.cfg')
# Creates translations
os.system('pybabel -q extract '
'-s --omit-header --no-location --sort-by-file '
'-k lazy_gettext -F {babel} -o {pot} {root} '
'&& pybabel update -i {pot} -d {translations} '
'2>/dev/null'
.format(root=ROOT_DIR, babel=babel, pot=pot,
translations=translations))
# Removes pot file
os.unlink(pot)
# Removes meta data
for root, dirs, files in os.walk(translations):
pos = (f for f in files if f.endswith('.po'))
for po in pos:
path = os.path.join(root, po)
with open(path, 'r') as trans:
clean = (
s for s in trans.readlines()
if not s.startswith(('"', '#'))
)
with open(path, 'w+') as trans:
trans.write(''.join(clean).strip())
def compilemessages():
"""
Compiles translation messages.
"""
translations = os.path.join(ROOT_DIR, 'translations')
os.system('pybabel -q compile -d ' + translations)
if __name__ == '__main__':
# Resolves relative imports
sys.path.append(PARENT_DIR)
# Caches for help output
scope = globals()
# Returns help if command not on the list
args = sys.argv[1:]
if not args or args[0] not in __all__:
ljust = len(max(__all__, key=len))
descr = ('{} - {}'.format(fn.ljust(ljust), scope[fn].__doc__.strip())
for fn in __all__)
sys.exit('Available commands:\n' + '\n'.join(descr))
# Runs command
scope[args[0]]()