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