-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_gui
executable file
·392 lines (332 loc) · 16.3 KB
/
bench_gui
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env python
import gi
import sys
import time
import os
import signal
from os import O_NONBLOCK, read
import subprocess
from fcntl import fcntl, F_GETFL, F_SETFL
gi.require_version("Gtk", "3.0")
from airtable import Airtable
sys.path.insert(1, os.path.join(sys.path[0], '/home/cmsdaq/AutoProcess/'))
import runDB
from gi.repository import Gtk, GLib
class GuiWindow(Gtk.Window):
def __init__(self, *args, **kwargs):
Gtk.Window.__init__(self, title="Array Bench GUI")
self.set_border_width(10)
self.processWindow = Gtk.Window(title="Output",can_focus=False,default_width=700,default_height=500)
self.processWindow.connect('delete-event', self.on_executeWindow_delete_event)
self.executeBox = Gtk.Box()
self.processWindow.add(self.executeBox)
self.executeTextView = Gtk.TextView(editable=False,cursor_visible=False)
self.executeTextScroll = Gtk.ScrolledWindow()
self.executeTextScroll.set_hexpand(True)
self.executeTextScroll.set_vexpand(True)
self.executeTextScroll.add(self.executeTextView)
self.executeBox.add(self.executeTextScroll)
self.mainBox = Gtk.Grid(row_spacing=8, column_spacing=2)
self.add(self.mainBox)
self.spinner = Gtk.Spinner()
self.mainBox.attach(self.spinner, 0, 0, 3, 1)
self.label = Gtk.Label()
self.mainBox.attach(self.label, 0, 1, 3, 1)
self.lscan = Gtk.CheckButton()
self.lscan.set_label("Longitudinal scan")
self.lscan.set_active(False)
self.mainBox.attach(self.lscan, 0, 2, 1, 1)
self.thscan = Gtk.CheckButton()
self.thscan.set_label("Threshold scan")
self.thscan.set_active(False)
self.mainBox.attach(self.thscan, 1, 2, 1, 1)
self.startDelay = Gtk.CheckButton()
self.startDelay.set_label("Start Delay")
self.startDelay.set_active(False)
self.mainBox.attach(self.startDelay, 2, 2, 1, 1)
self.tag_list = Gtk.ListStore(str)
self.populate_tag_list()
self.tag_label = Gtk.Label(label="Tag:")
self.mainBox.attach(self.tag_label, 0, 4, 1, 1)
self.tag_entry_combo = Gtk.ComboBox.new_with_model_and_entry(model = self.tag_list)
self.tag_entry_combo.set_entry_text_column(0)
self.tag_entry_combo.connect('changed', self.tag_combo_changed)
self.tag_completion = Gtk.EntryCompletion()
self.tag_completion.set_model(self.tag_list)
self.tag_completion.set_text_column(0)
self.tag_completion.set_match_func(self.tag_match_func)
self.tag_completion.connect ('match-selected', self.match_selected)
self.tag_entry = self.tag_entry_combo.get_child()
self.tag_entry.set_completion (self.tag_completion)
self.mainBox.attach(self.tag_entry_combo,1, 4, 1, 1)
self.array_list = Gtk.ListStore(str)
self.populate_array_list()
self.array1_label = Gtk.Label(label="IARR0 ID:")
self.mainBox.attach(self.array1_label, 0, 5, 1, 1)
self.array1_entry_combo = Gtk.ComboBox.new_with_model_and_entry(model = self.array_list)
self.array1_entry_combo.set_entry_text_column(0)
self.array1_entry_combo.connect('changed', self.array_combo_changed)
self.array1_completion = Gtk.EntryCompletion()
self.array1_completion.set_model(self.array_list)
self.array1_completion.set_text_column(0)
self.array1_completion.set_match_func(self.array1_match_func)
self.array1_completion.connect ('match-selected', self.match_selected)
self.array1_entry = self.array1_entry_combo.get_child()
self.array1_entry.set_completion (self.array1_completion)
self.mainBox.attach(self.array1_entry_combo,1, 5, 2, 1)
self.array2_label = Gtk.Label(label="IARR1 ID:")
self.mainBox.attach(self.array2_label, 0, 6, 1, 1)
self.array2_entry_combo = Gtk.ComboBox.new_with_model_and_entry(model = self.array_list)
self.array2_entry_combo.set_entry_text_column(0)
self.array2_entry_combo.connect('changed', self.array_combo_changed)
self.array2_completion = Gtk.EntryCompletion()
self.array2_completion.set_model(self.array_list)
self.array2_completion.set_text_column(0)
self.array2_completion.set_match_func(self.array2_match_func)
self.array2_completion.connect ('match-selected', self.match_selected)
self.array2_entry = self.array2_entry_combo.get_child()
self.array2_entry.set_completion (self.array2_completion)
self.mainBox.attach(self.array2_entry_combo,1, 6, 2, 1)
self.array3_label = Gtk.Label(label="IARR2 ID:")
self.mainBox.attach(self.array3_label, 0, 7, 1, 1)
self.array3_entry_combo = Gtk.ComboBox.new_with_model_and_entry(model = self.array_list)
self.array3_entry_combo.set_entry_text_column(0)
self.array3_entry_combo.connect('changed', self.array_combo_changed)
self.array3_completion = Gtk.EntryCompletion()
self.array3_completion.set_model(self.array_list)
self.array3_completion.set_text_column(0)
self.array3_completion.set_match_func(self.array3_match_func)
self.array3_completion.connect ('match-selected', self.match_selected)
self.array3_entry = self.array3_entry_combo.get_child()
self.array3_entry.set_completion (self.array3_completion)
self.mainBox.attach(self.array3_entry_combo,1, 7, 2, 1)
self.array4_label = Gtk.Label(label="IARR3 ID:")
self.mainBox.attach(self.array4_label, 0, 8, 1, 1)
self.array4_entry_combo = Gtk.ComboBox.new_with_model_and_entry(model = self.array_list)
self.array4_entry_combo.set_entry_text_column(0)
self.array4_entry_combo.connect('changed', self.array_combo_changed)
self.array4_completion = Gtk.EntryCompletion()
self.array4_completion.set_model(self.array_list)
self.array4_completion.set_text_column(0)
self.array4_completion.set_match_func(self.array4_match_func)
self.array4_completion.connect ('match-selected', self.match_selected)
self.array4_entry = self.array4_entry_combo.get_child()
self.array4_entry.set_completion (self.array4_completion)
self.mainBox.attach(self.array4_entry_combo,1, 8, 2, 1)
self.buttonStart = Gtk.Button(label="Start")
self.buttonStart.connect("clicked", self.on_buttonStart_clicked)
self.mainBox.attach(self.buttonStart, 1, 9, 2, 1)
self.buttonStop = Gtk.Button(label="Stop")
self.buttonStop.set_sensitive(False)
self.buttonStop.connect("clicked", self.on_buttonStop_clicked)
self.mainBox.attach(self.buttonStop, 0, 9, 1, 1)
self.timeout_id = None
self.io_stdout_id = None
self.io_stderr_id = None
self.connect("destroy", self.on_SpinnerWindow_destroy)
def array_combo_changed (self, combo):
_iter = combo.get_active_iter()
if _iter != None:
array = self.array_list[_iter][0]
print ('You selected array:', array)
def tag_combo_changed (self, combo):
_iter = combo.get_active_iter()
if _iter != None:
tag = self.tag_list[_iter][0]
print ('You selected tag:', tag)
def match_selected (self, completion, model, _iter):
print ('You selected:', model[_iter][0])
def tag_match_func (self, completion, string, _iter):
for word in string.split():
if word not in self.tag_list[_iter][0].lower(): #search is always lower case
return False
return True
def array1_match_func (self, completion, string, _iter):
for word in string.split():
if word not in self.array_list[_iter][0].lower(): #search is always lower case
return False
return True
def array2_match_func (self, completion, string, _iter):
for word in string.split():
if word not in self.array_list[_iter][0].lower(): #search is always lower case
return False
return True
def array3_match_func (self, completion, string, _iter):
for word in string.split():
if word not in self.array_list[_iter][0].lower(): #search is always lower case
return False
return True
def array4_match_func (self, completion, string, _iter):
for word in string.split():
if word not in self.array_list[_iter][0].lower(): #search is always lower case
return False
return True
def populate_tag_list(self):
self.tag_list.append(['REFDAILY'])
self.tag_list.append(['PREIRR'])
self.tag_list.append(['TEST'])
def populate_array_list(self):
records= runDB.airtables['Crystals'].get_all()
crystals=[ t['fields']['ID'] for t in records ]
crystals.sort(reverse=True)
for c in crystals:
self.array_list.append([str(int(c.replace("ARRAY","")))])
def on_buttonStart_clicked(self, widget, *args):
""" Handles "clicked" event of buttonStart. """
if (self.lscan.get_active() and self.thscan.get_active()):
self.show_confirmation_dialog("Cannot proceed. Both TH & L scan selected", "", self)
return
if self.show_confirmation_dialog("Starting the DAQ. Proceed?", "", self):
self.start_daq()
def on_buttonStop_clicked(self, widget, *args):
""" Handles "clicked" event of buttonStop. """
if self.show_confirmation_dialog("Stop the running process. Proceed?", "", self):
self.stop_daq("Run stopped")
self.processWindow.hide()
def on_SpinnerWindow_destroy(self, widget, *args):
""" Handles destroy event of main window. """
# ensure the timeout function is stopped
if self.timeout_id:
GLib.source_remove(self.timeout_id)
self.timeout_id = None
Gtk.main_quit()
def on_timeout(self, *args, **kwargs):
""" A timeout function.
Return True to stop it.
This is not a precise timer since next timeout
is recalculated based on the current time."""
self.counter += 1
# if self.counter <= 0:
# self.stop_daq("Run completed")
# return False
self.label.set_label("Running DAQ: " + str(int(self.counter / 4)))
sys.stdout.flush()
if (self.sub_proc and self.sub_proc.poll() is not None):
self.sub_proc = None
self.stop_daq("Run completed")
return True
def scroll_to_end(self,textview):
i = textview.props.buffer.get_end_iter()
mark = textview.props.buffer.get_insert()
textview.props.buffer.place_cursor(i)
textview.scroll_to_mark(mark, 0.0, True, 0.0, 1.0)
def non_block_read(self, output, condition):
if self.sub_proc is None:
return False
if condition == GLib.IO_IN:
op = output.read()
self.executeTextView.get_buffer().insert_at_cursor(op.decode('utf-8'))
self.scroll_to_end(self.executeTextView)
return True
def start_daq(self):
""" Stop the timer. """
if self.timeout_id:
GLib.source_remove(self.timeout_id)
self.timeout_id = None
""" Start the timer. """
self.buttonStart.set_sensitive(False)
self.buttonStop.set_sensitive(True)
self.lscan.set_sensitive(False)
self.tag_entry_combo.set_sensitive(False)
self.array1_entry_combo.set_sensitive(False)
self.array2_entry_combo.set_sensitive(False)
self.array3_entry_combo.set_sensitive(False)
self.array4_entry_combo.set_sensitive(False)
# time out will check every 250 miliseconds (1/4 of a second)
self.counter = 0
self.label.set_label("Running DAQ: " + str(int(self.counter / 4)))
runCommand="python run_DAQ_multiarrays.py -c list_config_main_array.txt -o /data/cmsdaq/LYSOMULTIARRAYNEWLAB4ARRAYS/ -n "
if (self.array1_entry.get_text()):
runCommand+='%s,'%self.array1_entry.get_text()
else:
runCommand+='-1,'
if (self.array2_entry.get_text()):
runCommand+='%s,'%self.array2_entry.get_text()
else:
runCommand+='-1,'
if (self.array3_entry.get_text()):
runCommand+='%s,'%self.array3_entry.get_text()
else:
runCommand+='-1,'
if (self.array4_entry.get_text()):
runCommand+='%s'%self.array4_entry.get_text()
else:
runCommand+='-1'
if (self.tag_entry.get_text()):
runCommand+=' -t %s'%self.tag_entry.get_text()
if (self.lscan.get_active()):
runCommand+=' -l'
if (self.thscan.get_active()):
runCommand+=' -s'
# runCommand="ping -c %d localhost"%int(self.array1_entry.get_text())
self.processWindow.set_title("%s (running)" % runCommand)
if (self.startDelay.get_active()):
runCommand='echo "Wait 25min to thermalize"; sleep 1500; '+runCommand
os.environ["PYTHONUNBUFFERED"] = "1"
runCommand="stdbuf -oL "+runCommand
self.sub_proc = subprocess.Popen(runCommand, stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True,bufsize=0,preexec_fn=os.setsid)
flags = fcntl(self.sub_proc.stdout, F_GETFL) # get current p.stdout flags
fcntl(self.sub_proc.stdout, F_SETFL, flags | O_NONBLOCK)
flags = fcntl(self.sub_proc.stderr, F_GETFL) # get current p.stdout flags
fcntl(self.sub_proc.stderr, F_SETFL, flags | O_NONBLOCK)
self.io_stdout_id=GLib.io_add_watch(self.sub_proc.stdout, # file descriptor
GLib.IO_IN, # condition
self.non_block_read ) # callback
self.io_stderr_id=GLib.io_add_watch(self.sub_proc.stderr, # file descriptor
GLib.IO_IN, # condition
self.non_block_read ) # callback
self.spinner.start()
self.executeTextView.get_buffer().set_text("")
self.processWindow.show_all()
self.timeout_id = GLib.timeout_add(250, self.on_timeout, None)
def stop_daq(self, alabeltext):
""" Stop the timer. """
if self.sub_proc is not None:
print("Killing process %d"%os.getpgid(self.sub_proc.pid))
os.killpg(os.getpgid(self.sub_proc.pid), signal.SIGKILL)
self.sub_proc=None
if self.timeout_id:
GLib.source_remove(self.timeout_id)
self.timeout_id = None
GLib.source_remove(self.io_stdout_id)
GLib.source_remove(self.io_stderr_id)
self.spinner.stop()
self.buttonStart.set_sensitive(True)
self.buttonStop.set_sensitive(False)
self.lscan.set_sensitive(True)
self.tag_entry_combo.set_sensitive(True)
self.array1_entry_combo.set_sensitive(True)
self.array2_entry_combo.set_sensitive(True)
self.array3_entry_combo.set_sensitive(True)
self.array4_entry_combo.set_sensitive(True)
self.label.set_label(alabeltext)
self.processWindow.set_title(alabeltext)
# self.processWindow.hide()
self.timeout_id = GLib.timeout_add(2000, self.reset_label, None)
def show_confirmation_dialog(self, query, title = "WARNING", parent = None):
if parent is None:
parent = self
dialog = Gtk.MessageDialog(parent, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO, title)
dialog.format_secondary_text(query)
rtn = dialog.run()
dialog.destroy()
return rtn == Gtk.ResponseType.YES
def on_executeWindow_delete_event(self, window, data):
if self.sub_proc is not None:
if self.show_confirmation_dialog("Closing window will stop the running process. Proceed?", "", self.processWindow):
self.stop_daq("Run stopped")
self.processWindow.hide()
else:
if self.show_confirmation_dialog("","Close window?", self.processWindow):
self.processWindow.hide()
return True
def reset_label(self, label):
""" Stop the timer. """
if self.timeout_id:
GLib.source_remove(self.timeout_id)
self.timeout_id = None
self.label.set_label("")
return True
win = GuiWindow()
win.show_all()
Gtk.main()