This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.py
80 lines (65 loc) · 2.1 KB
/
server.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
import sys
import subprocess
import time
import os
# To load configurations
import yaml
# Change current working directory to project root folder
path = os.path.abspath(os.path.dirname(__file__))
os.chdir(path)
class InputError(Exception):
""" (None) -> None
Custom Exception to raise when input is wrong
"""
pass
def raise_input_error():
""" (None) -> None
Raise InputError wtih the proper usage of this script
"""
raise InputError("Usage: python server.py [run|stop]")
def configuration():
""" (None) -> dict
Returns a dictionary containing the micro settings from the
config.yaml file located in the directory containing this file
"""
config_yaml = open("config.yaml", 'r')
config = yaml.load(config_yaml)
config_yaml.close()
return config
def check_command(arg1):
if arg1.lower() == "run":
return "run"
elif arg1.lower() == "stop":
return "stop"
raise_input_error()
def run_server(ip_address, port):
""" (None) -> None
Gives installation shell script permission and executes
"""
os.chmod('./src/RunServer.sh', 0o700)
subprocess.call(['./src/RunServer.sh',
format('%s:%i' % (ip_address, port))])
#this script is not used anymore
os.chmod('./src/RunBackgroundProcess.sh', 0o700)
subprocess.call(['./src/RunBackgroundProcess.sh'])
def stop_server(port):
""" (None) -> None
Gives installation shell script permission and executes
"""
config = configuration()['server']
os.chmod('./src/StopServer.sh', 0o700)
subprocess.call(['./src/StopServer.sh', format('%i' % port)])
# os.chmod('./src/StopSchedule.sh', 0700)
# subprocess.call(['./src/StopSchedule.sh'])
if __name__ == "__main__":
if not len(sys.argv) == 2:
raise_input_error()
else:
command = check_command(sys.argv[1])
config = configuration()['server']
if command == "run":
run_server(config['ip_address'], config['port'])
print('Server Running')
else:
stop_server(config['port'])
print('Server Stopped')