/usr/lib/python2.7/dist-packages/code_saturne/Base/CommandMgrDialogView.py is in code-saturne-data 4.2.0+repack-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- coding: utf-8 -*-
#
#-------------------------------------------------------------------------------
#
# This file is part of the Code_Saturne User Interface, element of the
# Code_Saturne CFD tool.
#
# Copyright (C) 1998-2010 EDF S.A., France
#
# contact: saturne-support@edf.fr
#
# The Code_Saturne User Interface is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# The Code_Saturne User Interface 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the Code_Saturne Kernel; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA
#
#-------------------------------------------------------------------------------
"""
Command Manager
===============
Generic Dialog window to handle execution of a list of external scripts.
"""
#-------------------------------------------------------------------------------
# Standard modules
#-------------------------------------------------------------------------------
import os, string, logging
import signal, subprocess
#-------------------------------------------------------------------------------
# Third-party modules
#-------------------------------------------------------------------------------
from PyQt4.QtCore import *
from PyQt4.QtGui import *
#-------------------------------------------------------------------------------
# Application modules
#-------------------------------------------------------------------------------
from CommandMgrDialogForm import Ui_CommandMgrDialogForm
from CommandMgrLinesDisplayedDialogForm import Ui_CommandMgrLinesDisplayedDialogForm
from QtPage import IntValidator, from_qvariant, to_text_string
#-------------------------------------------------------------------------------
# log config
#-------------------------------------------------------------------------------
logging.basicConfig()
log = logging.getLogger("CommandMgr")
log.setLevel(logging.NOTSET)
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
class CommandMgrLinesDisplayedDialogView(QDialog, Ui_CommandMgrLinesDisplayedDialogForm):
"""
Advanced dialog for the control of the number
of lines displayed in the QTextEdit.
"""
def __init__(self, parent, default):
"""
Constructor
"""
QDialog.__init__(self, parent)
Ui_CommandMgrLinesDisplayedDialogForm.__init__(self)
self.setupUi(self)
self.setWindowTitle(self.tr("Number of lines displayed"))
self.default = default
self.result = self.default.copy()
v = IntValidator(self.lineEditLines, min=0)
self.lineEditLines.setValidator(v)
# Previous values
self.lines = self.default['lines']
self.lineEditLines.setText(str(self.lines))
self.connect(self.lineEditLines,
SIGNAL("textChanged(const QString &)"),
self.__slotLines)
self.connect(self.pushButtonLines,
SIGNAL("clicked()"),
self.__slotUnlimited)
@pyqtSignature("const QString &")
def __slotLines(self, text):
"""
Private slot. Manage the number of lines allowed in the display zone.
"""
lines = from_qvariant(text, int)
if self.sender().validator().state == QValidator.Acceptable:
self.lines = lines
@pyqtSignature("")
def __slotUnlimited(self):
"""
Private slot. Set a unlimited number of lines in the display zone.
"""
self.lines = 0
self.lineEditLines.setText(str(self.lines))
def get_result(self):
"""
Method to get the result
"""
return self.result
def accept(self):
"""
Method called when user clicks 'OK'
"""
self.result['lines'] = self.lines
QDialog.accept(self)
def reject(self):
"""
Method called when user clicks 'Cancel'
"""
QDialog.reject(self)
#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
class CommandMgrDialogView(QDialog, Ui_CommandMgrDialogForm):
"""
Open a dialog to start external programs and display its output.
"""
def __init__(self, parent, title, cmd, start_directory="", obj_salome=""):
"""
Constructor. Must be overriden.
"""
QDialog.__init__(self, parent)
Ui_CommandMgrDialogForm.__init__(self)
self.setupUi(self)
self.setWindowTitle(title)
self.setWindowModality(Qt.NonModal)
self.setModal(False)
self.pushButtonOK.setEnabled(False)
self.log = "listing"
self.saveLog = "%ss (%s.*);;All files (*)" % (self.log, self.log)
self.proc = QProcess()
if start_directory != None and start_directory != "":
self.proc.setWorkingDirectory(start_directory)
self.objBr = obj_salome
self.connect(self.proc,
SIGNAL('readyReadStandardOutput()'),
self.slotReadFromStdout)
self.connect(self.proc,
SIGNAL('readyReadStandardError()'),
self.slotReadFromStderr)
self.connect(self.pushButtonLines,
SIGNAL('clicked()'),
self.__slotLines)
self.connect(self.pushButtonSaveAs,
SIGNAL('clicked()'),
self.__slotSaveAs)
self.connect(self.pushButtonKill,
SIGNAL('clicked()'),
self.__slotKill)
self.connect(self.proc,
SIGNAL('started()'),
self.slotStarted)
self.connect(self.proc,
SIGNAL('finished(int, QProcess::ExitStatus)'),
self.slotFinished)
self.cmd = cmd
cursor = QCursor(Qt.BusyCursor)
QApplication.setOverrideCursor(cursor)
@pyqtSignature("int, QProcess::ExitStatus")
def slotStarted(self):
"""
Public slot. Process is started.
"""
print("started: " + self.cmd)
@pyqtSignature("int, QProcess::ExitStatus")
def slotFinished(self, exitCode, exitStatus):
"""
Public slot. Enable the close button of the dialog window.
"""
if exitStatus != QProcess.NormalExit:
error = self.proc.error()
if error == QProcess.FailedToStart:
print("failed to start")
elif error == QProcess.Timedout:
print("timed out")
elif error == QProcess.WriteError:
print("error trying to write to process")
elif error == QProcess.ReadError:
print("error trying to read from process")
else:
print("crashed or killed")
else:
print("finished with exit code " + str(exitCode))
# if the GUI is launched through SALOME, update the object browser
# in order to display results
if self.objBr:
try:
import CFDSTUDYGUI_DataModel
r = CFDSTUDYGUI_DataModel.ScanChildren(self.objBr, "^RESU$")
CFDSTUDYGUI_DataModel.UpdateSubTree(r[0])
except:
pass
QApplication.restoreOverrideCursor()
self.pushButtonOK.setEnabled(True)
@pyqtSignature("")
def __slotLines(self):
"""
Private slot. Manage the number of lines allowed in the display zone.
"""
default = {}
default['lines'] = self.logText.document().maximumBlockCount()
dlg = CommandMgrLinesDisplayedDialogView(self, default)
if dlg.exec_():
result = dlg.get_result()
n = int(result['lines'])
if n != default['lines']:
self.logText.document().setMaximumBlockCount(n)
@pyqtSignature("")
def __slotKill(self):
"""
Private slot. Kill the subprocess.
"""
if self.proc.state() == QProcess.NotRunning:
QMessageBox.warning(self,
self.tr('Error'),
self.tr('The process is not running.'))
return
r = QMessageBox.question(self,
self.tr("Kill"),
self.tr("Kill the process "),
QMessageBox.Yes|QMessageBox.No)
if r == QMessageBox.Yes:
self.__killChildren()
self.proc.kill()
def __killChildren(self):
"""
Private slot. Find and kill all children of the spawned subprocess.
"""
cmd = "ps eo pid,ppid --sort=pid --no-headers"
psraw = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
universal_newlines=True).stdout.readlines()
psList = []
killList = []
for ps in psraw:
psList.append(list(map(int, ps.split())))
for ps in psList:
if int(self.proc.pid()) == ps[1]:
killList.append(ps[0])
for ps in psList:
if ps[1] in killList:
killList.append(ps[0])
if len(killList) <= 0:
return
cmd = "kill -9 %s" % " ".join(list(map(str, killList)))
subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
universal_newlines=True)
@pyqtSignature("")
def __slotSaveAs(self):
"""
Private slot. Save the contain of the display zone.
"""
if hasattr(self, 'suffix'):
l = self.log + "." + self.suffix
else:
l = self.log
f = os.path.join(self.case['resu_path'], l)
fileName = QFileDialog.getSaveFileName(self,
self.tr("Save log"),
f,
self.saveLog)
if not fileName:
return
try:
logFile = open(str(fileName), 'w')
except:
QMessageBox.warning(self, self.tr('Error'), self.tr('Could not open file for writing'))
return
logFile.write(self.logText.toPlainText().encode("utf-8"))
logFile.close()
@pyqtSignature("")
def slotReadFromStdout(self):
"""
Public slot. Handle the readyReadStandardOutput signal of the subprocess.
"""
if self.proc is None:
return
self.proc.setReadChannel(QProcess.StandardOutput)
while self.proc and self.proc.canReadLine():
ba = self.proc.readLine()
if ba.isNull(): return
s = (ba.data()).decode("utf-8")[:-1]
self.logText.append(s)
@pyqtSignature("")
def slotReadFromStderr(self):
"""
Public slot. Handle the readyReadStandardError signal of the subprocess.
"""
if self.proc is None:
return
self.proc.setReadChannel(QProcess.StandardError)
while self.proc and self.proc.canReadLine():
ba = self.proc.readLine()
if ba.isNull(): return
s = (ba.data()).decode("utf-8")[:-1]
self.logText.append('<font color="red">' + s + '</font>')
def closeEvent(self, event):
"""
Public Method. Close the Dialog window.
"""
self.__slotKill()
event.accept()
def tr(self, text):
"""
Public Method. Translation.
"""
return text
#-------------------------------------------------------------------------------
# End
#-------------------------------------------------------------------------------
|