forked from bethel-physics/WagonTestGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
executable file
·205 lines (158 loc) · 6.71 KB
/
__main__.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/TestingEnv/bin/python
# Including information about both Engine and Wagon GUIs
# Need to make the log file path before any imports
import os
from pathlib import Path
from PythonFiles.utils.helper import get_logging_path
guiLogPath = "{}".format(get_logging_path())
guiLogDir = "/".join(guiLogPath.split("/")[:-1])
print("Writing log file to {}".format(guiLogPath))
if not os.path.exists(guiLogDir):
os.makedirs(guiLogDir)
# Importing necessary modules
import multiprocessing as mp
import socket
# Imports the GUIWindow and Handlers
from PythonFiles.GUIWindow import GUIWindow
from PythonFiles.utils.SUBClient import SUBClient
from PythonFiles.update_config import update_config
from PythonFiles.utils.LocalHandler import LocalHandler
from PythonFiles.utils.SSHHandler import SSHHandler
import sys
import logging
import logging.handlers
import yaml
from pathlib import Path
# create logger with 'HGCALTestGUI'
logger = logging.getLogger('HGCALTestGUI')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.handlers.TimedRotatingFileHandler(guiLogPath, when="midnight", interval=1)
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
logger.info("Creating new instance of HGCALTestGUI")
# Creates a task of creating the GUIWindow
def task_GUI(conn, conn_trigger, queue, board_cfg, curpath):
# creates the main_window as an instantiation of GUIWindow
main_window = GUIWindow(conn, conn_trigger, queue, board_cfg, curpath)
# Creates a task of creating the SUBClient
def task_SUBClient(conn, queue, board_cfg, sub_pipe):
# Creates the SUBSCRIBE Socket Client
sub_client = SUBClient(conn, queue, board_cfg, sub_pipe)
# Function to create the handler of the type specified in the config file
def task_LocalHandler(gui_cfg, conn_trigger, local_pipe):
LocalHandler(gui_cfg, conn_trigger, local_pipe)
def task_SSHHandler(gui_cfg, host_cfg, conn_trigger, queue):
SSHHandler(gui_cfg, host_cfg, conn_trigger, queue)
def run(board_cfg, curpath, host_cfg):
# Creates a Pipe for the SUBClient to talk to the GUI Window
conn_SUB, conn_GUI = mp.Pipe()
# Create another pipe to trigger the tests when needed
if host_cfg["TestHandler"]["name"] != "ZMQ":
conn_trigger_GUI, conn_trigger_Handler = mp.Pipe()
# Creates a queue to send information to the testing window
queue = mp.Queue()
#logging.FileHandler(guiLogPath + "gui.log", mode='a')
# Turns creating the GUI and creating the SUBClient tasks into processes
if host_cfg["TestHandler"]["name"] == "Local":
# Creates a Queue to connect SUBClient and Handler
q = mp.Queue()
process_GUI = mp.Process(target = task_GUI, args=(conn_GUI, conn_trigger_GUI, queue, board_cfg, curpath))
process_Handler = mp.Process(target = task_LocalHandler, args=(board_cfg, conn_trigger_Handler, q))
process_SUBClient = mp.Process(target = task_SUBClient, args = (conn_SUB, queue, board_cfg, q))
elif host_cfg["TestHandler"]["name"] == "SSH":
q = mp.Queue()
process_GUI = mp.Process(target = task_GUI, args=(conn_GUI, conn_trigger_GUI, queue, board_cfg, curpath))
process_Handler = mp.Process(target = task_SSHHandler, args=(board_cfg, host_cfg, conn_trigger_Handler, q))
process_SUBClient = mp.Process(target = task_SUBClient, args = (conn_SUB, queue, board_cfg, q))
else:
process_GUI = mp.Process(target = task_GUI, args=(conn_GUI, None, queue, board_cfg, curpath))
process_SUBClient = mp.Process(target = task_SUBClient, args = (conn_SUB, queue, host_cfg, None))
# Starts the processes
process_GUI.start()
if host_cfg["TestHandler"]["name"] == "Local" or board_cfg['TestHandler']['name'] == 'SSH':
process_Handler.start()
process_SUBClient.start()
# holds the code at this line until the GUI process ends
process_GUI.join()
try:
#closes multiprocessing connections
conn_SUB.close()
conn_GUI.close()
conn_trigger_GUI.close()
conn_trigger_Handler.close()
except:
print("Pipe close is unnecessary.")
logger.debug("Pipe close is unnecessary.")
try:
# Cleans up the SUBClient process
process_SUBClient.terminate()
process_Handler.kill()
except:
print("Terminate is unnecessary.")
logger.debug("Terminate is unnecessary.")
pass
def import_yaml(config_path):
return yaml.safe_load(open(config_path,"r"))
def main(args):
pass
if __name__ == "__main__":
try:
if sys.argv[1] is not None:
config_path = sys.argv[1]
print(config_path)
logger.debug(config_path)
except:
config_path = None
try:
if sys.argv[2] is not None:
host_path = sys.argv[2]
except:
if config_path:
host_path = sys.argv[1]
else:
host_path = None
curpath = Path(__file__).parent.absolute()
print("Current path is: %s" % curpath)
logger.debug("Current path is: %s" % curpath)
node = socket.gethostname()
print(socket.gethostname())
logger.debug(socket.gethostname())
ld_wagon_computers = [
"cmsfactory4.cmsfactorynet",
"cmsfactory5.cmsfactorynet",
]
hd_wagon_computers = []
ld_engine_computers = [
"cmsfactory1.cmsfactorynet",
"cmsfactory2.cmsfactorynet",
]
hd_engine_computers = []
if config_path is not None:
board_cfg = import_yaml(config_path)
host_cfg = import_yaml(host_path)
run(board_cfg, curpath, host_cfg)
elif any((node in x for x in ld_wagon_computers)):
board_cfg = import_yaml(Path(__file__).parent / "Configs/LD_Wagon_cfg.yaml")
run(board_cfg, curpath, board_cfg)
elif any((node in x for x in hd_wagon_computers)):
board_cfg = import_yaml(Path(__file__).parent / "Configs/HD_Wagon_cfg.yaml")
run(board_cfg, curpath, board_cfg)
elif any((node in x for x in ld_engine_computers)):
board_cfg = import_yaml(Path(__file__).parent / "Configs/LD_Engine_cfg.yaml")
run(board_cfg, curpath, board_cfg)
elif any((node in x for x in hd_engine_computers)):
board_cfg = import_yaml(Path(__file__).parent / "Configs/HD_Engine_cfg.yaml")
run(board_cfg, curpath, board_cfg)
else:
board_cfg = import_yaml(Path(__file__).parent / "Configs/LD_Wagon_cfg.yaml")
run(board_cfg, curpath, board_cfg)