buildframework/helium/sf/python/pythoncore/lib/timeout_launcher.py
author wbernard
Sun, 10 Oct 2010 15:22:15 +0300
changeset 645 b8d81fa19e7d
parent 628 7c4a911dc066
permissions -rw-r--r--
helium_12.0.0-63b64366f9cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
#============================================================================ 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
#Name        : timeout_launcher.py 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
#Part of     : Helium 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
#All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
#This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
#under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
#which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
#Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
#Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
#Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
#
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
#Description:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
#===============================================================================
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    20
""" Application launcher supporting timeout. """
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    21
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
import os
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import sys
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import re
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import subprocess
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import logging
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
import time
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
_logger = logging.getLogger('timeout_launcher')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
logging.basicConfig(level=logging.INFO)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
# Platform
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    34
_windows = False
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
if sys.platform == "win32":
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
    import win32process
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
    import win32con
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    import win32api
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    39
    _windows = True
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
def main():
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    42
    """main method """
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
    cmdarg = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
    cmdline = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
    timeout = None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
    for arg in sys.argv:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
        res = re.match("^--timeout=(\d+)$", arg)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
        if not cmdarg and res is not None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
            timeout = int(res.group(1))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
            _logger.debug("Set timeout to %s" % timeout)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
        elif not cmdarg and arg == '--':
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
            _logger.debug("Parsing command start")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
            cmdarg = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
        elif cmdarg:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
            _logger.debug("Adding arg: %s" % arg)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
            cmdline.append(arg)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
    if len(cmdline) == 0:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
        print "Empty command line."
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
        print "e.g: timeout_launcher.py --timeout=1 -- cmd /c sleep 10"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
        sys.exit(-1)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
    else:
645
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents: 628
diff changeset
    64
        command = ' '.join(cmdline)
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents: 628
diff changeset
    65
        _logger.debug("Start command: " + command)
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    66
        shell = True
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    67
        if _windows:
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    68
            shell = False
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
        if timeout != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
            finish = time.time() + timeout
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
            timedout = False
645
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents: 628
diff changeset
    72
            p_file = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    73
            while (p_file.poll() == None):
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
                if time.time() > finish:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
                    timedout = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
                    break
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
                time.sleep(1)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
            if timedout:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
                print "ERROR: Application has timed out (timeout=%s)." % timeout
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    80
                if _windows:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
                    try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
                        print "ERROR: Trying to kill the process..."
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    83
                        handle = win32api.OpenProcess(True, win32con.PROCESS_TERMINATE, p_file.pid)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
                        win32process.TerminateProcess(handle, -1)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
                        print "ERROR: Process killed..."
645
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents: 628
diff changeset
    86
                    except Exception, exc: # pylint: disable=W0703
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
                        print "ERROR: %s" % exc
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
                else:
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    89
                    os.kill(p_file.pid, 9) # pylint: disable=E1101
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
                print "ERROR: exiting..."
645
b8d81fa19e7d helium_12.0.0-63b64366f9cf
wbernard
parents: 628
diff changeset
    91
                raise IOError("Timeout exception.")
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
            else:
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    93
                print p_file.communicate()[0]
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    94
                sys.exit(p_file.returncode)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
        else:
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    96
            p_file = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    97
            print p_file.communicate()[0]
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
    98
            sys.exit(p_file.returncode)
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
if __name__ == '__main__':
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   101
    main()