forked from jaheyns/CfdOF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCfdConsoleProcess.py
172 lines (158 loc) · 7.68 KB
/
CfdConsoleProcess.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
# ***************************************************************************
# * *
# * Copyright (c) 2017 Oliver Oxtoby (CSIR) <[email protected]> *
# * Copyright (c) 2017 Johan Heyns (CSIR) <[email protected]> *
# * Copyright (c) 2017 Alfred Bogaers (CSIR) <[email protected]> *
# * Copyright (c) 2019 Oliver Oxtoby <[email protected]> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
from __future__ import print_function
import platform
import os
from PySide import QtCore
from PySide.QtCore import QProcess, QTextStream
import FreeCAD
import CfdTools
class CfdConsoleProcess:
""" Class to run a console process asynchronously, printing output and
errors to the FreeCAD console and allowing clean termination in Linux
and Windows """
def __init__(self, finishedHook=None, stdoutHook=None, stderrHook=None):
self.process = QProcess()
self.finishedHook = finishedHook
self.stdoutHook = stdoutHook
self.stderrHook = stderrHook
self.process.finished.connect(self.finished)
self.process.readyReadStandardOutput.connect(self.readStdout)
self.process.readyReadStandardError.connect(self.readStderr)
self.print_next_error_lines = 0
self.print_next_error_file = False
def __del__(self):
self.terminate()
def start(self, cmd, env_vars=None, working_dir=None):
""" Start process and return immediately """
self.print_next_error_lines = 0
self.print_next_error_file = False
env = QtCore.QProcessEnvironment.systemEnvironment()
if env_vars:
for key in env_vars:
env.insert(key, env_vars[key])
CfdTools.removeAppimageEnvironment(env)
self.process.setProcessEnvironment(env)
if working_dir:
self.process.setWorkingDirectory(working_dir)
if platform.system() == "Windows":
# Run through a wrapper process to allow clean termination
cmd = [os.path.join(FreeCAD.getHomePath(), "bin", "python.exe"),
'-u', # Prevent python from buffering stdout
os.path.join(os.path.dirname(__file__), "WindowsRunWrapper.py")] + cmd
print("Raw command: ", cmd)
self.process.start(cmd[0], cmd[1:])
def terminate(self):
if self.process.state() != self.process.NotRunning:
if platform.system() == "Windows":
# terminate() doesn't operate and kill() doesn't allow cleanup and leaves mpi processes running
# Instead, instruct wrapper program to kill child process and itself cleanly with ctrl-break signal
self.process.write(b"terminate\n")
self.process.waitForBytesWritten() # 'flush'
else:
self.process.terminate()
self.process.waitForFinished()
def finished(self, exit_code):
if self.finishedHook:
self.finishedHook(exit_code)
def readStdout(self):
# Ensure only complete lines are passed on
text = ""
while self.process.canReadLine():
byteArr = self.process.readLine()
text += QTextStream(byteArr).readAll()
if text:
print(text, end='') # Avoid displaying on FreeCAD status bar
if self.stdoutHook:
self.stdoutHook(text)
# Must be at the end as it can cause re-entrance
if FreeCAD.GuiUp:
FreeCAD.Gui.updateGui()
def readStderr(self):
# Ensure only complete lines are passed on
# Print any error output to console
self.process.setReadChannel(QProcess.StandardError)
text = ""
while self.process.canReadLine():
byteArr = self.process.readLine()
text += QTextStream(byteArr).readAll()
self.process.setReadChannel(QProcess.StandardOutput)
if text:
if self.stderrHook:
self.stderrHook(text)
FreeCAD.Console.PrintError(text)
# Must be at the end as it can cause re-entrance
if FreeCAD.GuiUp:
FreeCAD.Gui.updateGui()
def state(self):
return self.process.state()
def waitForStarted(self):
return self.process.waitForStarted()
def waitForFinished(self):
# For some reason waitForFinished doesn't always return - so we resort to a failsafe timeout:
while True:
ret = self.process.waitForFinished(1000)
if self.process.error() != self.process.Timedout:
self.readStdout()
self.readStderr()
return ret
if self.process.state() == self.process.NotRunning:
self.readStdout()
self.readStderr()
return True
def exitCode(self):
return self.process.exitCode()
def processErrorOutput(self, err):
"""
Process standard error text output from OpenFOAM
:param err: Standard error output, single or multiple lines
:return: A message to be printed on console, or None
"""
ret = ""
errlines = err.split('\n')
for errline in errlines:
if len(errline) > 0: # Ignore blanks
if self.print_next_error_lines > 0:
ret += errline + "\n"
self.print_next_error_lines -= 1
if self.print_next_error_file and "file:" in errline:
ret += errline + "\n"
self.print_next_error_file = False
words = errline.split(' ', 1) # Split off first field for parallel
FATAL = "--> FOAM FATAL ERROR"
FATALIO = "--> FOAM FATAL IO ERROR"
if errline.startswith(FATAL) or (len(words) > 1 and words[1].startswith(FATAL)):
self.print_next_error_lines = 1
ret += "OpenFOAM fatal error:\n"
elif errline.startswith(FATALIO) or (len(words) > 1 and words[1].startswith(FATALIO)):
self.print_next_error_lines = 1
self.print_next_error_file = True
ret += "OpenFOAM IO error:\n"
elif errline.startswith("Fatal error:"):
ret += errline
if len(ret) > 0:
return ret
else:
return None