buildframework/helium/sf/python/pythoncore/lib/timeout_launcher.py
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    16 #
    16 #
    17 #Description:
    17 #Description:
    18 #===============================================================================
    18 #===============================================================================
    19 
    19 
    20 """ Application launcher supporting timeout. """
    20 """ Application launcher supporting timeout. """
       
    21 
    21 import os
    22 import os
    22 import sys
    23 import sys
    23 import re
    24 import re
    24 import subprocess
    25 import subprocess
    25 import logging
    26 import logging
    59         print "Empty command line."
    60         print "Empty command line."
    60         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"
    61         sys.exit(-1)
    62         sys.exit(-1)
    62     else:
    63     else:
    63         _logger.debug("Start command")
    64         _logger.debug("Start command")
       
    65         shell = True
       
    66         if _windows:
       
    67             shell = False
    64         if timeout != None:
    68         if timeout != None:
    65             finish = time.time() + timeout
    69             finish = time.time() + timeout
    66             timedout = False
    70             timedout = False
    67             shell = True
       
    68             if _windows:
       
    69                 shell = False
       
    70             p_file = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
    71             p_file = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
    71             while (p_file.poll() == None):
    72             while (p_file.poll() == None):
    72                 if time.time() > finish:
    73                 if time.time() > finish:
    73                     timedout = True
    74                     timedout = True
    74                     break
    75                     break
    82                         win32process.TerminateProcess(handle, -1)
    83                         win32process.TerminateProcess(handle, -1)
    83                         print "ERROR: Process killed..."
    84                         print "ERROR: Process killed..."
    84                     except Exception, exc:
    85                     except Exception, exc:
    85                         print "ERROR: %s" % exc
    86                         print "ERROR: %s" % exc
    86                 else:
    87                 else:
    87                     # pylint: disable-msg=E1101
    88                     os.kill(p_file.pid, 9) # pylint: disable=E1101
    88                     os.kill(p_file.pid, 9)
       
    89                 print "ERROR: exiting..."
    89                 print "ERROR: exiting..."
    90                 raise Exception("Timeout exception.")
    90                 raise Exception("Timeout exception.")
    91             else:
    91             else:
    92                 print p_file.communicate()[0]
    92                 print p_file.communicate()[0]
    93                 sys.exit(p_file.returncode)
    93                 sys.exit(p_file.returncode)
    94         else:
    94         else:
    95             p_file = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
    95             p_file = subprocess.Popen(' '.join(cmdline), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=shell)
    96             print p_file.communicate()[0]
    96             print p_file.communicate()[0]
    97             sys.exit(p_file.returncode)
    97             sys.exit(p_file.returncode)
    98 
    98 
    99 if __name__ == '__main__':
    99 if __name__ == '__main__':
   100     main()
   100     main()