forked from ColinTalbert/sahm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobMoniterApp.py
179 lines (141 loc) · 6.48 KB
/
JobMoniterApp.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
'''Original from:
http://stackoverflow.com/questions/8786136/pyqt-how-to-detect-and-close-ui-if-its-already-running
'''
import os, sys
from PyQt4 import QtGui, QtCore, QtNetwork
class SingleApplication(QtGui.QApplication):
def __init__(self, argv, key):
QtGui.QApplication.__init__(self, argv)
self._memory = QtCore.QSharedMemory(self)
self._memory.setKey(key)
if self._memory.attach():
self._running = True
else:
self._running = False
if not self._memory.create(1):
raise RuntimeError(
self._memory.errorString().toLocal8Bit().data())
def isRunning(self):
return self._running
class Window(QtGui.QWidget):
def __init__(self, workspace):
QtGui.QWidget.__init__(self)
try:
#Try to set the window icon. This might not be cross-platform
vistrailsdir = os.path.dirname(os.path.dirname(sys.executable))
app_icon = QtGui.QIcon(vistrailsdir +
'/gui/resources/images/vistrails_icon_small.png')
self.setWindowIcon(app_icon)
except:
pass
self.sessionDir = workspace
self.treeView = QtGui.QTreeWidget()
self.treeView.setAlternatingRowColors(True)
self.treeView.setColumnCount(2)
self.treeView.headerItem().setText(0, "Model")
self.treeView.setColumnWidth(0,150)
self.treeView.headerItem().setText(1, "Status")
self.treeView.setColumnWidth(1, 550)
self.data = self.loadData()
#add a timer so that we can update the contents every 5 sec
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updateContents)
self.timer.start(500)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.treeView)
self.setLayout(layout)
def addItems(self, parent, elements):
for text, children in elements:
item = QtGui.QStandardItem(text)
parent.appendRow(item)
if children:
self.addItems(item, children)
def loadData(self):
subFolders = [x[0] for x in os.walk(self.sessionDir)]
models = ["brt", "rf_", "mar", "glm", "Max", "App"]
self.data = []
expandedNodes = []
for index in range(self.treeView.topLevelItemCount()):
if self.treeView.topLevelItem(index).isExpanded():
expandedNodes.append(index)
self.treeView.clear()
for subFolder in subFolders:
if os.path.split(subFolder)[1][:3] in models:
itemName = (os.path.split(subFolder)[1])
result = self.checkIfModelFinished(subFolder)
errorText = self.getText(subFolder, "stdErr.txt").strip()
warningCount = errorText.count("Warning")
if warningCount == 0:
resultText = result
else:
resultText = result + " (with " + str(warningCount) + " warnings)"
child_item = QtGui.QTreeWidgetItem([itemName, resultText])
if result.startswith("Completed successfully"):
child_item.setBackgroundColor(1, QtGui.QColor(188,220, 157))
elif result == "Error in model":
child_item.setBackgroundColor(1, QtGui.QColor(223, 131, 125))
errorText = self.getText(subFolder, "stdErr.txt").strip()
errorText += "\n" + self.getText(subFolder, "stdErr_R.txt").strip()
if itemName.startswith("Maxent"):
errorText += "\n" + self.getText(subFolder, "stdErr_max.txt").strip()
error_item = QtGui.QTreeWidgetItem(["stdError", errorText])
error_item.setTextAlignment(0, QtCore.Qt.AlignJustify)
error_item.setTextAlignment(1, QtCore.Qt.AlignJustify)
# out_item = QTreeWidgetItem(["stdOut", self.getText(subFolder, "stdOut.txt")])
# out_item.setTextAlignment(Qt.AlignTop, Qt.AlignCenter)
child_item.addChild(error_item)
# child_item.addChild(out_item)
self.treeView.addTopLevelItem(child_item)
for index in expandedNodes:
self.treeView.topLevelItem(index).setExpanded(True)
def getText(self, subfolder, fname):
try:
fullName = os.path.join(subfolder, fname)
return "".join(open(fullName, "r").readlines())
except IOError:
return ""
def checkIfModelFinished(self, model_dir):
try:
for err_fname in ['stdErr.txt', 'stdErr_R.txt', 'stdErr_max.txt']:
out_err = os.path.join(model_dir, err_fname)
stdErrLines = "\n".join(open(out_err, "r").readlines())
if "Error" in stdErrLines:
return "Error in model"
except:
pass
try:
outText = self.find_file(model_dir, "_output.txt")
except RuntimeError:
return "Starting ..."
model_text = os.path.join(model_dir, outText)
try:
lastLine = open(model_text, 'r').readlines()[-2]
except IndexError:
return "Running ..."
if lastLine.startswith("Total time"):
return "Completed successfully in " + lastLine[lastLine.find(" = ")+3:]
elif lastLine.startswith("Model Failed"):
return "Error in model"
else:
return "Running ..."
def find_file(self, model_dir, suffix):
try:
return [file_name for file_name in os.listdir(model_dir)
if file_name.endswith(suffix)][0]
except IndexError:
raise RuntimeError('The expected model output '
+ suffix + ' was not found in the model output directory')
def updateContents(self,):
self.loadData()
if __name__ == '__main__':
import sys
key = 'SAHM_JobMonitorApplication'
app = SingleApplication(sys.argv, key)
if app.isRunning():
print('SAHM_JobMonitorApplication is already running')
sys.exit(1)
window = Window(sys.argv[1])
window.setWindowTitle("Asynchronous Model Run Monitor")
window.resize(750, 800)
window.show()
sys.exit(app.exec_())