Package node_manager_fkie :: Module supervised_popen
[frames] | no frames]

Source Code for Module node_manager_fkie.supervised_popen

 1  # Software License Agreement (BSD License) 
 2  # 
 3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
 4  # All rights reserved. 
 5  # 
 6  # Redistribution and use in source and binary forms, with or without 
 7  # modification, are permitted provided that the following conditions 
 8  # are met: 
 9  # 
10  #  * Redistributions of source code must retain the above copyright 
11  #    notice, this list of conditions and the following disclaimer. 
12  #  * Redistributions in binary form must reproduce the above 
13  #    copyright notice, this list of conditions and the following 
14  #    disclaimer in the documentation and/or other materials provided 
15  #    with the distribution. 
16  #  * Neither the name of Fraunhofer nor the names of its 
17  #    contributors may be used to endorse or promote products derived 
18  #    from this software without specific prior written permission. 
19  # 
20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
31  # POSSIBILITY OF SUCH DAMAGE. 
32   
33  import os 
34  import sys 
35  import subprocess 
36  import threading 
37  import roslib 
38   
39  from python_qt_binding import QtCore, QtGui 
40   
41  import node_manager_fkie as nm 
42  from detailed_msg_box import WarningMessageBox, DetailedError 
43   
44   
45 -class SupervisedPopen(QtCore.QObject, subprocess.Popen):
46 ''' 47 The class overrides the subprocess.Popen and waits in a thread for its finish. 48 If an error is printed out, it will be shown in a message dialog. 49 ''' 50 error = QtCore.Signal(str, str, str) 51 '''@ivar: the signal is emitted if error output was detected (id, decription, message)''' 52 53 finished = QtCore.Signal(str) 54 '''@ivar: the signal is emitted on exit (id)''' 55
56 - def __init__(self, args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=subprocess.PIPE, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, id='', description=''):
57 ''' 58 For arguments see https://docs.python.org/2/library/subprocess.html 59 Additional arguments: 60 :param id: the identification string of this object and title of the error message dialog 61 :type id: str 62 :param description: the description string used as addiotional information 63 in dialog if an error was occured 64 :type description: str 65 ''' 66 try: 67 subprocess.Popen.__init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags) 68 QtCore.QObject.__init__(self) 69 self._args = args 70 self._id = id 71 self._description = description 72 self.error.connect(self.on_error) 73 # wait for process to avoid 'defunct' processes 74 thread = threading.Thread(target=self.supervise) 75 thread.setDaemon(True) 76 thread.start() 77 except Exception as e: 78 raise
79 80 # def __del__(self): 81 # print "Deleted:", self._description 82
83 - def supervise(self):
84 # wait for process to avoid 'defunct' processes 85 self.wait() 86 result_err = '' 87 if not self.stderr is None: 88 result_err = self.stderr.read() 89 if result_err: 90 self.error.emit(self._id, self._description, result_err) 91 self.finished.emit(self._id)
92
93 - def on_error(self, id, descr, msg):
94 WarningMessageBox(QtGui.QMessageBox.Warning, id, '%s\n\n%s'%(descr, msg), ' '.join(self._args)).exec_()
95