forked from michaeldavis-wf/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runserver.py
62 lines (52 loc) · 1.8 KB
/
runserver.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
import subprocess
import sys
import signal
import os
import settingslocal
manage_py_process = "python manage.py runserver"
if hasattr(settingslocal, 'RUNSERVER_PROCESS'):
manage_py_process = getattr(settingslocal, 'RUNSERVER_PROCESS')
class color:
PINK = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
class custom_color:
WARNING = color.YELLOW
DEBUG = color.BLUE
INFO = color.GREEN
ERROR = color.RED
def main():
process = subprocess.Popen("%s %s" % (manage_py_process, (' '.join(sys.argv[1:]))), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.stderr
def signal_handler(input_signal, frame):
os.kill(process.pid, signal.SIGTERM)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
blank_lines = 0
while output:
line = output.readline().strip('\n')
if line:
blank_lines = 0
split_location = str.find(line, ']') + 1
first_part = line[:split_location]
second_part = line[split_location:]
if line.startswith('WARNING'):
print custom_color.WARNING + first_part + color.ENDC + second_part
elif line.startswith('DEBUG'):
print custom_color.DEBUG + first_part + color.ENDC + second_part
elif line.startswith('INFO'):
print custom_color.INFO + first_part + color.ENDC + second_part
elif line.startswith('ERROR'):
print custom_color.ERROR + first_part + color.ENDC + second_part
else:
print line
else:
print line
blank_lines += 1
if blank_lines > 20:
sys.exit(0)
if __name__ == '__main__':
main()