util/ProcessLogger.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Thu, 28 Apr 2016 13:05:57 +0300
changeset 1507 d7f474d10210
parent 1506 b9b8978dbc9d
child 1516 9db0b436fbb3
permissions -rw-r--r--
fix issue with sometimes wrong return code of ProcessLogger


As a result of wrong return code Beremiz gives folowing traceback:
Traceback (most recent call last):
File "./Beremiz.py", line 850, in OnMenu
getattr(self.CTR, method)()
File "/home/developer/WorkData/PLC/beremiz/beremiz/ProjectController.py", line 925, in _Build
IECGenRes = self._Generate_SoftPLC()
File "/home/developer/WorkData/PLC/beremiz/beremiz/ProjectController.py", line 568, in _Generate_SoftPLC
return self._Compile_ST_to_SoftPLC()
File "/home/developer/WorkData/PLC/beremiz/beremiz/ProjectController.py", line 661, in _Compile_ST_to_SoftPLC
C_files.remove("POUS.c")
ValueError: list.remove(x): x not in list

The problem is that both threads (for reading stdout and stderr) call self.Proc.poll(),
that updates internal returncode field. This call is done without any locking and the first thread gets correct result,
but other gets 0 as retval. If 0 gets thread, that afterwards calls callback finish, then wrong return code is returned
to the parent. Now only the thread with a callback polls for the return code, other thread just checked local value.

Additionally function spin() waits now until all threads finish reading their pipes, so the results are always correct.
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
     1
#!/usr/bin/env python
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
     2
# -*- coding: utf-8 -*-
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
     3
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
     4
#This file is part of Beremiz, a Integrated Development Environment for
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
     5
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
     6
#
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
     8
#
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
     9
#See COPYING file for copyrights details.
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    10
#
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    11
#This library is free software; you can redistribute it and/or
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    12
#modify it under the terms of the GNU General Public
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    13
#License as published by the Free Software Foundation; either
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    15
#
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    16
#This library is distributed in the hope that it will be useful,
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    19
#General Public License for more details.
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    20
#
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    21
#You should have received a copy of the GNU General Public
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    22
#License along with this library; if not, write to the Free Software
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    24
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    25
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    26
import time
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    27
import wx
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    28
import subprocess, ctypes
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
    29
from threading import Timer, Lock, Thread, Semaphore
788
3cec473bef94 Fixing bug with filesystem encoding on Windows
laurent
parents: 726
diff changeset
    30
import os, sys
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
    31
if os.name == 'posix':
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
    32
    from signal import SIGTERM, SIGKILL
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    33
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
    34
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
    35
class outputThread(Thread):
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    36
    """
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    37
    Thread is used to print the output of a command to the stdout
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    38
    """
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    39
    def __init__(self, Proc, fd, callback=None, endcallback=None):
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
    40
        Thread.__init__(self)
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    41
        self.killed = False
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    42
        self.finished = False
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    43
        self.retval = None
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    44
        self.Proc = Proc
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    45
        self.callback = callback
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    46
        self.endcallback = endcallback
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    47
        self.fd = fd
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    48
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    49
    def run(self):
162
bf3eac08a96b Try to fix strange wxPopen behavior. Feedback appreciated.
etisserant
parents: 154
diff changeset
    50
        outchunk = None
149
fc7fe0de9143 Tried to fix Linux behaviour of wxPopen.py
etisserant
parents: 130
diff changeset
    51
        self.retval = None
fc7fe0de9143 Tried to fix Linux behaviour of wxPopen.py
etisserant
parents: 130
diff changeset
    52
        while self.retval is None and not self.killed :
1507
d7f474d10210 fix issue with sometimes wrong return code of ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1506
diff changeset
    53
            if self.endcallback:
d7f474d10210 fix issue with sometimes wrong return code of ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1506
diff changeset
    54
                self.retval = self.Proc.poll()
d7f474d10210 fix issue with sometimes wrong return code of ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1506
diff changeset
    55
            else:
d7f474d10210 fix issue with sometimes wrong return code of ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1506
diff changeset
    56
                self.retval = self.Proc.returncode
d7f474d10210 fix issue with sometimes wrong return code of ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1506
diff changeset
    57
                
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    58
            outchunk = self.fd.readline()
162
bf3eac08a96b Try to fix strange wxPopen behavior. Feedback appreciated.
etisserant
parents: 154
diff changeset
    59
            if self.callback : self.callback(outchunk)
bf3eac08a96b Try to fix strange wxPopen behavior. Feedback appreciated.
etisserant
parents: 154
diff changeset
    60
        while outchunk != '' and not self.killed :
bf3eac08a96b Try to fix strange wxPopen behavior. Feedback appreciated.
etisserant
parents: 154
diff changeset
    61
            outchunk = self.fd.readline()
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
    62
            if self.callback : self.callback(outchunk)
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    63
        if self.endcallback:
130
9af34a1d33b7 fixed short process wainting bug. Seems wait() fail when process already finisshed... TO BE CONFIRMED.
greg
parents: 128
diff changeset
    64
            try:
232
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
    65
                err = self.Proc.wait()
130
9af34a1d33b7 fixed short process wainting bug. Seems wait() fail when process already finisshed... TO BE CONFIRMED.
greg
parents: 128
diff changeset
    66
            except:
149
fc7fe0de9143 Tried to fix Linux behaviour of wxPopen.py
etisserant
parents: 130
diff changeset
    67
                err = self.retval
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    68
            self.finished = True
421
c9ec111ad275 Bugs with logger and stand-alone PluginRoot fixed
laurent
parents: 361
diff changeset
    69
            self.endcallback(self.Proc.pid, err)
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
    70
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    71
class ProcessLogger:
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
    72
    def __init__(self, logger, Command, finish_callback = None,
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
    73
                 no_stdout = False, no_stderr = False, no_gui = True,
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
    74
                 timeout = None, outlimit = None, errlimit = None,
1415
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    75
                 endlog = None, keyword = None, kill_it = False, cwd = None,
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    76
                 encoding = None):
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    77
        self.logger = logger
424
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    78
        if not isinstance(Command, list):
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    79
            self.Command_str = Command
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    80
            self.Command = []
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    81
            for i,word in enumerate(Command.replace("'",'"').split('"')):
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    82
                if i % 2 == 0:
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    83
                    word = word.strip()
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    84
                    if len(word) > 0:
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    85
                        self.Command.extend(word.split())
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    86
                else:
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    87
                    self.Command.append(word)
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    88
        else:
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    89
            self.Command = Command
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    90
            self.Command_str = subprocess.list2cmdline(self.Command)
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
    91
1415
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    92
        fsencoding = sys.getfilesystemencoding()
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    93
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    94
        if encoding is None:
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    95
            encoding = fsencoding
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    96
        self.Command = [self.Command[0].encode(fsencoding)]+map(
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    97
            lambda x: x.encode(encoding), self.Command[1:])
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
    98
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    99
        self.finish_callback = finish_callback
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   100
        self.no_stdout = no_stdout
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   101
        self.no_stderr = no_stderr
111
e2e498333fbc fixed display/hide console when launch external programs
greg
parents: 110
diff changeset
   102
        self.startupinfo = None
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   103
        self.errlen = 0
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   104
        self.outlen = 0
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   105
        self.errlimit = errlimit
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   106
        self.outlimit = outlimit
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   107
        self.exitcode = None
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   108
        self.outdata = []
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   109
        self.errdata = []
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   110
        self.keyword = keyword
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   111
        self.kill_it = kill_it
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   112
        self.finishsem = Semaphore(0)
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   113
        self.endlock = Lock()
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   114
128
3db703a78e9c fixed subprocess launching on linux (avoid use of undefied self.startupinfo and use use Shell=True (bash will split arguments))
greg
parents: 112
diff changeset
   115
        popenargs= {
958
511bf048b8b7 Added CWD to ProcessLogger, and make sure local runtime have CWD setup correctly
Edouard Tisserant
parents: 788
diff changeset
   116
               "cwd":os.getcwd() if cwd is None else cwd,
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   117
               "stdin":subprocess.PIPE,
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   118
               "stdout":subprocess.PIPE,
128
3db703a78e9c fixed subprocess launching on linux (avoid use of undefied self.startupinfo and use use Shell=True (bash will split arguments))
greg
parents: 112
diff changeset
   119
               "stderr":subprocess.PIPE}
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   120
111
e2e498333fbc fixed display/hide console when launch external programs
greg
parents: 110
diff changeset
   121
        if no_gui == True and wx.Platform == '__WXMSW__':
e2e498333fbc fixed display/hide console when launch external programs
greg
parents: 110
diff changeset
   122
            self.startupinfo = subprocess.STARTUPINFO()
e2e498333fbc fixed display/hide console when launch external programs
greg
parents: 110
diff changeset
   123
            self.startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
128
3db703a78e9c fixed subprocess launching on linux (avoid use of undefied self.startupinfo and use use Shell=True (bash will split arguments))
greg
parents: 112
diff changeset
   124
            popenargs["startupinfo"] = self.startupinfo
3db703a78e9c fixed subprocess launching on linux (avoid use of undefied self.startupinfo and use use Shell=True (bash will split arguments))
greg
parents: 112
diff changeset
   125
        elif wx.Platform == '__WXGTK__':
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   126
            popenargs["shell"] = False
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   127
1506
b9b8978dbc9d Fix error about missing attribute 'timeout' that happens sometimes during compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1476
diff changeset
   128
        if timeout:
b9b8978dbc9d Fix error about missing attribute 'timeout' that happens sometimes during compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1476
diff changeset
   129
            self.timeout = Timer(timeout,self.endlog)
b9b8978dbc9d Fix error about missing attribute 'timeout' that happens sometimes during compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1476
diff changeset
   130
            self.timeout.start()
b9b8978dbc9d Fix error about missing attribute 'timeout' that happens sometimes during compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1476
diff changeset
   131
        else:
b9b8978dbc9d Fix error about missing attribute 'timeout' that happens sometimes during compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1476
diff changeset
   132
            self.timeout = None
b9b8978dbc9d Fix error about missing attribute 'timeout' that happens sometimes during compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1476
diff changeset
   133
            
128
3db703a78e9c fixed subprocess launching on linux (avoid use of undefied self.startupinfo and use use Shell=True (bash will split arguments))
greg
parents: 112
diff changeset
   134
        self.Proc = subprocess.Popen( self.Command, **popenargs )
112
fa0eaeaa9012 Re-enabled stderr
etisserant
parents: 111
diff changeset
   135
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   136
        self.outt = outputThread(
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   137
                      self.Proc,
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   138
                      self.Proc.stdout,
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   139
                      self.output,
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   140
                      self.finish)
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   141
        self.outt.start()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   142
112
fa0eaeaa9012 Re-enabled stderr
etisserant
parents: 111
diff changeset
   143
        self.errt = outputThread(
fa0eaeaa9012 Re-enabled stderr
etisserant
parents: 111
diff changeset
   144
                      self.Proc,
fa0eaeaa9012 Re-enabled stderr
etisserant
parents: 111
diff changeset
   145
                      self.Proc.stderr,
fa0eaeaa9012 Re-enabled stderr
etisserant
parents: 111
diff changeset
   146
                      self.errors)
232
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   147
        self.errt.start()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   148
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   149
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   150
    def output(self,v):
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   151
        self.outdata.append(v)
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   152
        self.outlen += 1
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   153
        if not self.no_stdout:
686
e4e1da75d411 More robust Logger, now resist to flooding.
Edouard Tisserant
parents: 424
diff changeset
   154
            self.logger.write(v)
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   155
        if (self.keyword and v.find(self.keyword)!=-1) or (self.outlimit and self.outlen > self.outlimit):
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   156
            self.endlog()
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   157
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   158
    def errors(self,v):
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   159
        self.errdata.append(v)
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   160
        self.errlen += 1
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   161
        if not self.no_stderr:
686
e4e1da75d411 More robust Logger, now resist to flooding.
Edouard Tisserant
parents: 424
diff changeset
   162
            self.logger.write_warning(v)
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   163
        if self.errlimit and self.errlen > self.errlimit:
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   164
            self.endlog()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   165
232
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   166
    def log_the_end(self,ecode,pid):
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   167
        self.logger.write(self.Command_str + "\n")
361
331d698e1118 Adding support for internationalization
laurent
parents: 235
diff changeset
   168
        self.logger.write_warning(_("exited with status %s (pid %s)\n")%(str(ecode),str(pid)))
232
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   169
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   170
    def finish(self, pid,ecode):
1476
49f1763a5613 fixes sometimes happened error during project compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com
parents: 1415
diff changeset
   171
        if self.timeout:
49f1763a5613 fixes sometimes happened error during project compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com
parents: 1415
diff changeset
   172
            self.timeout.cancel()
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   173
        self.exitcode = ecode
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   174
        if self.exitcode != 0:
686
e4e1da75d411 More robust Logger, now resist to flooding.
Edouard Tisserant
parents: 424
diff changeset
   175
            self.log_the_end(ecode,pid)
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   176
        if self.finish_callback is not None:
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   177
            self.finish_callback(self,ecode,pid)
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   178
        self.finishsem.release()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   179
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
   180
    def kill(self,gently=True):
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   181
        self.outt.killed = True
112
fa0eaeaa9012 Re-enabled stderr
etisserant
parents: 111
diff changeset
   182
        self.errt.killed = True
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   183
        if wx.Platform == '__WXMSW__':
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   184
            PROCESS_TERMINATE = 1
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   185
            handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, self.Proc.pid)
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   186
            ctypes.windll.kernel32.TerminateProcess(handle, -1)
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   187
            ctypes.windll.kernel32.CloseHandle(handle)
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   188
        else:
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
   189
            if gently:
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
   190
                sig=SIGTERM
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
   191
            else:
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
   192
                sig=SIGKILL
154
f3134b2c6d92 Fixed killing app on Linux in wxPopen. Do not use shell anymore. Command line is splitted into args, taking care of double and simple cotes. To be tested on win32.
etisserant
parents: 151
diff changeset
   193
            try:
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
   194
                os.kill(self.Proc.pid, sig)
154
f3134b2c6d92 Fixed killing app on Linux in wxPopen. Do not use shell anymore. Command line is splitted into args, taking care of double and simple cotes. To be tested on win32.
etisserant
parents: 151
diff changeset
   195
            except:
f3134b2c6d92 Fixed killing app on Linux in wxPopen. Do not use shell anymore. Command line is splitted into args, taking care of double and simple cotes. To be tested on win32.
etisserant
parents: 151
diff changeset
   196
                pass
232
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   197
        self.outt.join()
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   198
        self.errt.join()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   199
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   200
    def endlog(self):
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   201
        if self.endlock.acquire(False):
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   202
            self.finishsem.release()
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   203
            if not self.outt.finished and self.kill_it:
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   204
               self.kill()
89
0ab2868c6aa6 Added right aligment of parameteres blocks
etisserant
parents: 79
diff changeset
   205
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   206
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   207
    def spin(self):
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   208
        self.finishsem.acquire()
1507
d7f474d10210 fix issue with sometimes wrong return code of ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1506
diff changeset
   209
        self.outt.join()
d7f474d10210 fix issue with sometimes wrong return code of ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1506
diff changeset
   210
        self.errt.join()
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   211
        return [self.exitcode, "".join(self.outdata), "".join(self.errdata)]
89
0ab2868c6aa6 Added right aligment of parameteres blocks
etisserant
parents: 79
diff changeset
   212