forked from PyColorado/boulderpython.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
executable file
·103 lines (85 loc) · 2.4 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
97
98
99
100
101
102
103
#!/usr/bin/env python3.6
"""
Runs the boulderpython website
"""
# stdlib
import os
import sys
import argparse
import signal
import time
# 3rd
import livereload
from typing import Optional
import pytest
# local
from application import app, configure
PORT = os.environ.get('PORT') or '9999'
HOST = os.environ.get('HOST') or 'localhost'
def main(args: argparse.Namespace) -> int:
configure(os.getenv('FLASK_CONFIG') or 'default')
if args.test is True:
pid = os.fork()
if pid == 0:
run_server(mode='prod')
return True
else:
result = False
try:
result = pytest.main(['tests'])
except: # trust me this is ok
pass
os.kill(pid, signal.SIGINT)
return result
elif (args is not None) and (args.debug or app.debug):
run_server(mode='debug')
else:
run_server(mode='prod')
return True
def run_server(mode: Optional[str]='debug') -> None:
"""
Runs the server in either debug or prod mode
:param mode: mode to run in
:return: None
"""
if mode == 'debug':
# DEBUG (livereload) MODE
configure('testing')
app.jinja_env.auto_reload = True
app.debug = True
server = livereload.Server(app.wsgi_app)
server.watch('.', ignore=lambda x: ('log' in x or
'.idea' in x))
server.serve(
port=PORT,
host=HOST
)
elif mode == 'prod':
# Pseduo Prod Mode
pid = os.fork()
if pid == 0:
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
os.kill(pid, signal.SIGINT)
break
else:
os.system(f'gunicorn -b :{PORT} application:app')
def get_args():
"""
Get command line arguments
:return: arguments
"""
parser = argparse.ArgumentParser(
description='Run the Boulder Python Website'
)
parser.add_argument('-d', '--debug', default=False, action='store_true',
help='Run in DEBUG mode.')
parser.add_argument('-t', '--test', default=False, action='store_true',
help='Run in TEST mode.')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = get_args()
sys.exit(main(args))