buildframework/helium/sf/python/pythoncore/lib/timeout_launcher.py
author wbernard
Tue, 27 Apr 2010 08:33:08 +0300
changeset 587 85df38eb4012
child 588 c7c26511138f
permissions -rw-r--r--
helium_9.0-a7879c935424
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. """
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    21
import os
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    22
import sys
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import re
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
import subprocess
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    25
import logging
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    26
import time
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    27
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    28
_logger = logging.getLogger('timeout_launcher')
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    29
logging.basicConfig(level=logging.INFO)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    30
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    31
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    32
# Platform
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    33
windows = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    34
if sys.platform == "win32":
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    35
    import win32process
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    36
    import win32con
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    37
    import win32api
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    38
    windows = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    39
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    40
def main():
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    41
    cmdarg = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    42
    cmdline = []
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
    timeout = None
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
    for arg in sys.argv:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
        res = re.match("^--timeout=(\d+)$", arg)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
        if not cmdarg and res is not None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
            timeout = int(res.group(1))
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
            _logger.debug("Set timeout to %s" % timeout)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
        elif not cmdarg and arg == '--':
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
            _logger.debug("Parsing command start")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
            cmdarg = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
        elif cmdarg:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
            _logger.debug("Adding arg: %s" % arg)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
            cmdline.append(arg)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
    if len(cmdline) == 0:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
        print "Empty command line."
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
        print "e.g: timeout_launcher.py --timeout=1 -- cmd /c sleep 10"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
        sys.exit(-1)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    61
    else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
        _logger.debug("Start command")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    63
        if timeout != None:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    64
            finish = time.time() + timeout
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    65
            timedout = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    66
            shell = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    67
            if windows:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    68
                shell = False
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    69
            p = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    70
            while (p.poll() == None):
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    71
                if time.time() > finish:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    72
                    timedout = True
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    73
                    break
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    74
                time.sleep(1)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    75
            if timedout:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    76
                print "ERROR: Application has timed out (timeout=%s)." % timeout
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
                if windows:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    78
                    try:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    79
                        print "ERROR: Trying to kill the process..."
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    80
                        handle = win32api.OpenProcess(True, win32con.PROCESS_TERMINATE, p.pid)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    81
                        win32process.TerminateProcess(handle, -1)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    82
                        print "ERROR: Process killed..."
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    83
                    except Exception, exc:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    84
                        print "ERROR: %s" % exc
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    85
                else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    86
                    # pylint: disable-msg=E1101
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    87
                    os.kill(p.pid, 9)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    88
                print "ERROR: exiting..."
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    89
                raise Exception("Timeout exception.")
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    90
            else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    91
                print p.communicate()[0]
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    92
                sys.exit(p.returncode)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    93
        else:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    94
            p = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
            print p.communicate()[0]
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
            sys.exit(p.returncode)
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    98
if __name__ == '__main__':
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    99
    main()