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