buildframework/helium/sf/python/pythoncore/lib/timeout_launcher.py
changeset 645 b8d81fa19e7d
parent 628 7c4a911dc066
equal deleted inserted replaced
643:27cf35f95864 645:b8d81fa19e7d
    59     if len(cmdline) == 0:
    59     if len(cmdline) == 0:
    60         print "Empty command line."
    60         print "Empty command line."
    61         print "e.g: timeout_launcher.py --timeout=1 -- cmd /c sleep 10"
    61         print "e.g: timeout_launcher.py --timeout=1 -- cmd /c sleep 10"
    62         sys.exit(-1)
    62         sys.exit(-1)
    63     else:
    63     else:
    64         _logger.debug("Start command")
    64         command = ' '.join(cmdline)
       
    65         _logger.debug("Start command: " + command)
    65         shell = True
    66         shell = True
    66         if _windows:
    67         if _windows:
    67             shell = False
    68             shell = False
    68         if timeout != None:
    69         if timeout != None:
    69             finish = time.time() + timeout
    70             finish = time.time() + timeout
    70             timedout = False
    71             timedout = False
    71             p_file = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
    72             p_file = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
    72             while (p_file.poll() == None):
    73             while (p_file.poll() == None):
    73                 if time.time() > finish:
    74                 if time.time() > finish:
    74                     timedout = True
    75                     timedout = True
    75                     break
    76                     break
    76                 time.sleep(1)
    77                 time.sleep(1)
    80                     try:
    81                     try:
    81                         print "ERROR: Trying to kill the process..."
    82                         print "ERROR: Trying to kill the process..."
    82                         handle = win32api.OpenProcess(True, win32con.PROCESS_TERMINATE, p_file.pid)
    83                         handle = win32api.OpenProcess(True, win32con.PROCESS_TERMINATE, p_file.pid)
    83                         win32process.TerminateProcess(handle, -1)
    84                         win32process.TerminateProcess(handle, -1)
    84                         print "ERROR: Process killed..."
    85                         print "ERROR: Process killed..."
    85                     except Exception, exc:
    86                     except Exception, exc: # pylint: disable=W0703
    86                         print "ERROR: %s" % exc
    87                         print "ERROR: %s" % exc
    87                 else:
    88                 else:
    88                     os.kill(p_file.pid, 9) # pylint: disable=E1101
    89                     os.kill(p_file.pid, 9) # pylint: disable=E1101
    89                 print "ERROR: exiting..."
    90                 print "ERROR: exiting..."
    90                 raise Exception("Timeout exception.")
    91                 raise IOError("Timeout exception.")
    91             else:
    92             else:
    92                 print p_file.communicate()[0]
    93                 print p_file.communicate()[0]
    93                 sys.exit(p_file.returncode)
    94                 sys.exit(p_file.returncode)
    94         else:
    95         else:
    95             p_file = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
    96             p_file = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)