util/ProcessLogger.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 25 May 2022 09:18:37 +0200
changeset 3497 1b8906ca2a50
parent 3297 7e59bd180bc6
child 3483 e25f1cf69df9
permissions -rw-r--r--
IDE: fix saveAs dialog
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
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
     6
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
     8
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
     9
# See COPYING file for copyrights details.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    10
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    11
# This program is free software; you can redistribute it and/or
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    12
# modify it under the terms of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    13
# as published by the Free Software Foundation; either version 2
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    14
# of the License, or (at your option) any later version.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    15
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    16
# This program is distributed in the hope that it will be useful,
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    19
# GNU General Public License for more details.
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    20
#
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    21
# You should have received a copy of the GNU General Public License
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    22
# along with this program; if not, write to the Free Software
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1539
diff changeset
    23
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    24
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1878
diff changeset
    25
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1878
diff changeset
    26
from __future__ import absolute_import
1732
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    27
import os
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    28
import sys
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    29
import subprocess
94ffe74e6895 clean-up: fix PEP8 E401 multiple imports on one line
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
    30
import ctypes
2730
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
    31
import time
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
    32
from threading import Timer, Lock, Thread, Semaphore, Condition
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1883
diff changeset
    33
import signal
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    34
2671
30493ff3a23a Debug : when BEREMIZ_DEBUG file exist, display all commands issued in console.
Edouard Tisserant
parents: 1919
diff changeset
    35
_debug = os.path.exists("BEREMIZ_DEBUG")
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
    36
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
    37
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
    38
    """
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    39
    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
    40
    """
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    41
    def __init__(self, Proc, fd, callback=None, endcallback=None):
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
    42
        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
    43
        self.killed = False
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    44
        self.finished = False
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    45
        self.retval = None
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    46
        self.Proc = Proc
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    47
        self.callback = callback
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    48
        self.endcallback = endcallback
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    49
        self.fd = fd
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
    50
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    51
    def run(self):
162
bf3eac08a96b Try to fix strange wxPopen behavior. Feedback appreciated.
etisserant
parents: 154
diff changeset
    52
        outchunk = None
149
fc7fe0de9143 Tried to fix Linux behaviour of wxPopen.py
etisserant
parents: 130
diff changeset
    53
        self.retval = None
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    54
        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
    55
            if self.endcallback:
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.poll()
d7f474d10210 fix issue with sometimes wrong return code of ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1506
diff changeset
    57
            else:
d7f474d10210 fix issue with sometimes wrong return code of ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1506
diff changeset
    58
                self.retval = self.Proc.returncode
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
    59
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    60
            outchunk = self.fd.readline()
1756
08e4394ff4fb clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
    61
            if self.callback:
08e4394ff4fb clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
    62
                self.callback(outchunk)
1739
ec153828ded2 clean-up: fix PEP8 E203 whitespace before ':' and whitespace before ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
    63
        while outchunk != '' and not self.killed:
162
bf3eac08a96b Try to fix strange wxPopen behavior. Feedback appreciated.
etisserant
parents: 154
diff changeset
    64
            outchunk = self.fd.readline()
1756
08e4394ff4fb clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
    65
            if self.callback:
08e4394ff4fb clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
    66
                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
    67
        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
    68
            try:
232
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
    69
                err = self.Proc.wait()
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1759
diff changeset
    70
            except Exception:
149
fc7fe0de9143 Tried to fix Linux behaviour of wxPopen.py
etisserant
parents: 130
diff changeset
    71
                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
    72
            self.finished = True
421
c9ec111ad275 Bugs with logger and stand-alone PluginRoot fixed
laurent
parents: 361
diff changeset
    73
            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
    74
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1732
diff changeset
    75
1831
56b48961cc68 fix (old-style-class) Old-style class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
    76
class ProcessLogger(object):
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
    77
    def __init__(self, logger, Command, finish_callback=None,
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
    78
                 no_stdout=False, no_stderr=False, no_gui=True,
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
    79
                 timeout=None, outlimit=None, errlimit=None,
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1742
diff changeset
    80
                 endlog=None, keyword=None, kill_it=False, cwd=None,
1883
20ec80d6fd70 add possibility to specify subprocess stdout encoding
Surkov Sergey <surkovsv93@gmail.com>
parents: 1882
diff changeset
    81
                 encoding=None, output_encoding=None):
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
    82
        self.logger = logger
424
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    83
        if not isinstance(Command, list):
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    84
            self.Command_str = Command
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    85
            self.Command = []
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
    86
            for i, word in enumerate(Command.replace("'", '"').split('"')):
424
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    87
                if i % 2 == 0:
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    88
                    word = word.strip()
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    89
                    if len(word) > 0:
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    90
                        self.Command.extend(word.split())
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    91
                else:
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    92
                    self.Command.append(word)
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    93
        else:
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    94
            self.Command = Command
86a7c1d11bbd support for passing list command to ProcessLogger
greg
parents: 421
diff changeset
    95
            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
    96
1415
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    97
        fsencoding = sys.getfilesystemencoding()
1883
20ec80d6fd70 add possibility to specify subprocess stdout encoding
Surkov Sergey <surkovsv93@gmail.com>
parents: 1882
diff changeset
    98
        self.output_encoding = output_encoding
1415
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
    99
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
   100
        if encoding is None:
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
   101
            encoding = fsencoding
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
   102
        self.Command = [self.Command[0].encode(fsencoding)]+map(
c411fc7246eb Allow specification of command line arguments encoding in ProcessLogger
Edouard Tisserant
parents: 1407
diff changeset
   103
            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
   104
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   105
        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
   106
        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
   107
        self.no_stderr = no_stderr
111
e2e498333fbc fixed display/hide console when launch external programs
greg
parents: 110
diff changeset
   108
        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
   109
        self.errlen = 0
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   110
        self.outlen = 0
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   111
        self.errlimit = errlimit
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   112
        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
   113
        self.exitcode = None
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   114
        self.outdata = []
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   115
        self.errdata = []
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   116
        self.keyword = keyword
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   117
        self.kill_it = kill_it
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   118
        self.startsem = Semaphore(0)
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   119
        self.finishsem = Semaphore(0)
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   120
        self.endlock = Lock()
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   121
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1741
diff changeset
   122
        popenargs = {
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   123
            "cwd":    os.getcwd() if cwd is None else cwd,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   124
            "stdin":  subprocess.PIPE,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   125
            "stdout": subprocess.PIPE,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   126
            "stderr": subprocess.PIPE
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   127
        }
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   128
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1883
diff changeset
   129
        if no_gui and os.name in ("nt", "ce"):
111
e2e498333fbc fixed display/hide console when launch external programs
greg
parents: 110
diff changeset
   130
            self.startupinfo = subprocess.STARTUPINFO()
e2e498333fbc fixed display/hide console when launch external programs
greg
parents: 110
diff changeset
   131
            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
   132
            popenargs["startupinfo"] = self.startupinfo
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1883
diff changeset
   133
        elif os.name == 'posix':
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   134
            popenargs["shell"] = False
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   135
1506
b9b8978dbc9d Fix error about missing attribute 'timeout' that happens sometimes during compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1476
diff changeset
   136
        if timeout:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   137
            self.timeout = Timer(timeout, self.endlog)
1506
b9b8978dbc9d Fix error about missing attribute 'timeout' that happens sometimes during compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1476
diff changeset
   138
            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
   139
        else:
b9b8978dbc9d Fix error about missing attribute 'timeout' that happens sometimes during compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1476
diff changeset
   140
            self.timeout = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   141
3297
7e59bd180bc6 IDE: systematically log command when launching process (was not showing in case of LOCAL:// runtime being launched)
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2733
diff changeset
   142
        if _debug:
7e59bd180bc6 IDE: systematically log command when launching process (was not showing in case of LOCAL:// runtime being launched)
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2733
diff changeset
   143
            self.logger.write("(DEBUG) launching:\n" + self.Command_str + "\n")
7e59bd180bc6 IDE: systematically log command when launching process (was not showing in case of LOCAL:// runtime being launched)
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2733
diff changeset
   144
1746
45d6f5fba016 clean-up: fix PEP8 E202 whitespace before ')'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1744
diff changeset
   145
        self.Proc = subprocess.Popen(self.Command, **popenargs)
112
fa0eaeaa9012 Re-enabled stderr
etisserant
parents: 111
diff changeset
   146
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   147
        self.outt = outputThread(
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   148
            self.Proc,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   149
            self.Proc.stdout,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   150
            self.output,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   151
            self.finish)
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   152
        self.outt.start()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   153
112
fa0eaeaa9012 Re-enabled stderr
etisserant
parents: 111
diff changeset
   154
        self.errt = outputThread(
1878
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   155
            self.Proc,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   156
            self.Proc.stderr,
fb73a6b6622d fix pylint warning '(bad-continuation) Wrong hanging indentation before block'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1850
diff changeset
   157
            self.errors)
232
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   158
        self.errt.start()
1519
f7ac0d11118e fix race condition in ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1516
diff changeset
   159
        self.startsem.release()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   160
2730
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   161
        self.spinwakeuplock = Lock()
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   162
        self.spinwakeupcond = Condition(self.spinwakeuplock)
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   163
        self.spinwakeuptimer = None
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   164
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   165
    def output(self, v):
1883
20ec80d6fd70 add possibility to specify subprocess stdout encoding
Surkov Sergey <surkovsv93@gmail.com>
parents: 1882
diff changeset
   166
        if v and self.output_encoding:
20ec80d6fd70 add possibility to specify subprocess stdout encoding
Surkov Sergey <surkovsv93@gmail.com>
parents: 1882
diff changeset
   167
            v = v.decode(self.output_encoding)
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   168
        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
   169
        self.outlen += 1
2712
a00f41d097f3 Removed harmful assert in ProcessLogger.
Edouard Tisserant
parents: 2671
diff changeset
   170
        if self.logger and not self.no_stdout:
686
e4e1da75d411 More robust Logger, now resist to flooding.
Edouard Tisserant
parents: 424
diff changeset
   171
            self.logger.write(v)
1742
92932cd370a4 clean-up: fix PEP8 E225 missing whitespace around operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1741
diff changeset
   172
        if (self.keyword and v.find(self.keyword) != -1) or (self.outlimit and self.outlen > self.outlimit):
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   173
            self.endlog()
1407
cf3d2b53dd68 Organized controller's _Build sub-methods layout. Many (unwanted) white space changes.
Edouard Tisserant
parents: 958
diff changeset
   174
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   175
    def errors(self, v):
1883
20ec80d6fd70 add possibility to specify subprocess stdout encoding
Surkov Sergey <surkovsv93@gmail.com>
parents: 1882
diff changeset
   176
        if v and self.output_encoding:
20ec80d6fd70 add possibility to specify subprocess stdout encoding
Surkov Sergey <surkovsv93@gmail.com>
parents: 1882
diff changeset
   177
            v = v.decode(self.output_encoding)
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   178
        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
   179
        self.errlen += 1
2712
a00f41d097f3 Removed harmful assert in ProcessLogger.
Edouard Tisserant
parents: 2671
diff changeset
   180
        if self.logger and not self.no_stderr:
686
e4e1da75d411 More robust Logger, now resist to flooding.
Edouard Tisserant
parents: 424
diff changeset
   181
            self.logger.write_warning(v)
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   182
        if self.errlimit and self.errlen > self.errlimit:
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   183
            self.endlog()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   184
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   185
    def log_the_end(self, ecode, pid):
2671
30493ff3a23a Debug : when BEREMIZ_DEBUG file exist, display all commands issued in console.
Edouard Tisserant
parents: 1919
diff changeset
   186
        if self.logger is not None:
30493ff3a23a Debug : when BEREMIZ_DEBUG file exist, display all commands issued in console.
Edouard Tisserant
parents: 1919
diff changeset
   187
            self.logger.write(self.Command_str + "\n")
30493ff3a23a Debug : when BEREMIZ_DEBUG file exist, display all commands issued in console.
Edouard Tisserant
parents: 1919
diff changeset
   188
            self.logger.write_warning(_("exited with status {a1} (pid {a2})\n").format(a1=str(ecode), a2=str(pid)))
232
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   189
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   190
    def finish(self, pid, ecode):
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   191
        # avoid running function before start is finished
1519
f7ac0d11118e fix race condition in ProcessLogger
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1516
diff changeset
   192
        self.startsem.acquire()
1882
3707240447f8 fix deadlock if method kill() is called after process is already finished
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1881
diff changeset
   193
        self.startsem.release()
1476
49f1763a5613 fixes sometimes happened error during project compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com
parents: 1415
diff changeset
   194
        if self.timeout:
49f1763a5613 fixes sometimes happened error during project compilation
Andrey Skvortsov <andrej.skvortzov@gmail.com
parents: 1415
diff changeset
   195
            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
   196
        self.exitcode = ecode
3297
7e59bd180bc6 IDE: systematically log command when launching process (was not showing in case of LOCAL:// runtime being launched)
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2733
diff changeset
   197
        if self.exitcode != 0:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   198
            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
   199
        if self.finish_callback is not None:
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   200
            self.finish_callback(self, ecode, pid)
1516
9db0b436fbb3 fix bug: Beremiz IDE hangs during launching Beremiz_service
andrey@new-nest
parents: 1507
diff changeset
   201
        self.errt.join()
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   202
        self.finishsem.release()
2730
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   203
        self.spinwakeup()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   204
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
   205
    def kill(self, gently=True):
1539
8e5c03798abc fix syncronization issue during killing ProcessLoggers on IDE close
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1519
diff changeset
   206
        # avoid running kill before start is finished
8e5c03798abc fix syncronization issue during killing ProcessLoggers on IDE close
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1519
diff changeset
   207
        self.startsem.acquire()
8e5c03798abc fix syncronization issue during killing ProcessLoggers on IDE close
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1519
diff changeset
   208
        self.startsem.release()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1581
diff changeset
   209
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   210
        self.outt.killed = True
112
fa0eaeaa9012 Re-enabled stderr
etisserant
parents: 111
diff changeset
   211
        self.errt.killed = True
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1883
diff changeset
   212
        if os.name in ("nt", "ce"):
110
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   213
            PROCESS_TERMINATE = 1
a05e8b30c024 Fixed way apps are launched in parralel with single log window... Tested in win32 only.
etisserant
parents: 89
diff changeset
   214
            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
   215
            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
   216
            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
   217
        else:
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
   218
            if gently:
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1883
diff changeset
   219
                sig = signal.SIGTERM
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
   220
            else:
1919
ccea0fa6ea91 Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents: 1883
diff changeset
   221
                sig = signal.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
   222
            try:
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 232
diff changeset
   223
                os.kill(self.Proc.pid, sig)
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1759
diff changeset
   224
            except Exception:
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
   225
                pass
232
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   226
        self.outt.join()
83cc1a306560 add call to join method for stdout/stderr threads
greg
parents: 224
diff changeset
   227
        self.errt.join()
79
ae06c2da83f7 Some window related enhancements
etisserant
parents: 70
diff changeset
   228
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   229
    def endlog(self):
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   230
        if self.endlock.acquire(False):
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   231
            if not self.outt.finished and self.kill_it:
1757
0de89da92ee0 clean-up: fix PEP8 E111 indentation is not a multiple of four
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1756
diff changeset
   232
                self.kill()
1516
9db0b436fbb3 fix bug: Beremiz IDE hangs during launching Beremiz_service
andrey@new-nest
parents: 1507
diff changeset
   233
            self.finishsem.release()
2730
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   234
            self.spinwakeup()
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   235
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   236
    def spinwakeup(self):
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   237
        with self.spinwakeuplock:
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   238
            if self.spinwakeuptimer is not None:
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   239
                self.spinwakeuptimer.cancel()
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   240
            self.spinwakeuptimer = None
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   241
            self.spinwakeupcond.notify()
89
0ab2868c6aa6 Added right aligment of parameteres blocks
etisserant
parents: 79
diff changeset
   242
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   243
    def spin(self):
2730
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   244
        start = time.time()
2733
244598a6a0d6 Allow using spin() from ProcessLogger even with no logger given.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2730
diff changeset
   245
        if self.logger:
244598a6a0d6 Allow using spin() from ProcessLogger even with no logger given.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2730
diff changeset
   246
            while not self.finishsem.acquire(0):
244598a6a0d6 Allow using spin() from ProcessLogger even with no logger given.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2730
diff changeset
   247
                with self.spinwakeuplock:
244598a6a0d6 Allow using spin() from ProcessLogger even with no logger given.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2730
diff changeset
   248
                    self.spinwakeuptimer = Timer(0.1, self.spinwakeup)
244598a6a0d6 Allow using spin() from ProcessLogger even with no logger given.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2730
diff changeset
   249
                    self.spinwakeuptimer.start()
244598a6a0d6 Allow using spin() from ProcessLogger even with no logger given.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2730
diff changeset
   250
                    self.spinwakeupcond.wait()
244598a6a0d6 Allow using spin() from ProcessLogger even with no logger given.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2730
diff changeset
   251
                    self.logger.progress("%.3fs"%(time.time() - start))
244598a6a0d6 Allow using spin() from ProcessLogger even with no logger given.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2730
diff changeset
   252
        else:
244598a6a0d6 Allow using spin() from ProcessLogger even with no logger given.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2730
diff changeset
   253
            self.finishsem.acquire()
2730
ce21ce181fdb IDE: Process Logging : Add annotation in log so that user can see build is still alive and how long external process takes.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2712
diff changeset
   254
704
5993b16fe2d0 More stable ProcessLogger.spin()
Edouard Tisserant
parents: 686
diff changeset
   255
        return [self.exitcode, "".join(self.outdata), "".join(self.errdata)]