1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, subprocess, ConfigParser, platform, os
# begin Autobuild/helpers/runcommand.py
import os
import re
import time
import sys
import signal
from subprocess import Popen
from threading import Thread
from subprocess import PIPE
import subprocess
import platform
def DebugN( l, m ):
pass
def windowskill( pid ):
""" replace os.kill on Windows, where it is not available"""
Cmd = 'taskkill /PID ' + str( int( pid ) ) + ' /T /F'
if os.system( Cmd ) == 0:
DebugN( 4, 'windowskill: process ' + str( pid ) + ' killed.' )
def kill( pid, signal ):
if 'Windows' in platform.platform():
windowskill( pid )
else:
os.kill( pid, signal )
class CommandRunner( Thread ):
def __init__ ( self, Cmd ):
Thread.__init__( self )
self.__started = None
self.mCmd = Cmd
self.mOutput = ()
self.mPid = -1
self.mReturnCode = -1
self.mError = ()
self.__combinedOutput = False
def setCombinedOutput(self, combine):
if combine: # make sure combine is usable as a boolean
self.__combinedOutput = True
else:
self.__combinedOutput = False
def getCombineOutput(self):
return self.__combinedOutput
def run( self ):
DebugN( 4, 'RunCommand: ' + str( self.mCmd ) )
stderrValue = subprocess.PIPE
if self.__combinedOutput:
stderrValue = subprocess.STDOUT
self.__started = True
#nokia-sdk: changed to shell=False, as shell=True requires manual quoting of args
p = Popen ( self.mCmd, shell = False, stdout=subprocess.PIPE, stderr=stderrValue )
self.mPid = p.pid
self.mOutput, self.mError = p.communicate()
self.mReturnCode = p.returncode
DebugN( 4, 'ReturnCode of RunCommand: ' + str( p.returncode ) )
def started(self):
return self.__started
def output( self ):
return self.mOutput, self.mError
def terminate( self ):
# FIXME logic?
if self.mPid != -1:
kill( self.mPid, signal.SIGTERM )
if self.mPid == True:
self.join( 5 )
kill( self.mPid, signal.SIGKILL )
self.mPid = -1
def RunCommand( Cmd, TimeOutSeconds=-1, CombineOutput = False ):
timeoutString = ' without a timeout'
if TimeOutSeconds > 0:
timeoutString = ' with timeout of ' + str( TimeOutSeconds )
combinedOutputString = ' and separate output for stdout and stderr'
if CombineOutput:
combinedOutputString = ' and combined stdout and stderr output'
DebugN ( 3, 'RunCommand: executing ' + str( Cmd ) + timeoutString + combinedOutputString )
runner = CommandRunner ( Cmd )
runner.setCombinedOutput( CombineOutput )
runner.start()
# poor man's mutex:
while not runner.started():
time.sleep( 0.1 )
# if "CYGWIN" in platform.platform() or 'Windows' in platform.platform():
# time.sleep( 1 )
if TimeOutSeconds == -1:
runner.join()
else:
runner.join( TimeOutSeconds )
if runner.isAlive():
runner.terminate()
# if "CYGWIN" in platform.platform() or 'Windows' in platform.platform():
# time.sleep(1)
runner.join( 5 )
DebugN( 3, 'RunCommand: command timed out, returncode is ' + str( runner.mReturnCode ) )
return ( runner.mReturnCode, runner.output(), True )
else:
DebugN( 3, 'RunCommand: command completed, returncode is ' + str( runner.mReturnCode ) )
return ( runner.mReturnCode, runner.output(), False )
# end Autobuild/helpers/runcommand.py
def printUsage():
print( "Usage: {0} <outputfile> <timeout> <program> [<args>*]".format( sys.argv[0] ) )
if len( sys.argv ) < 4:
printUsage()
sys.exit( 1 )
output = sys.argv[1]
timeout = int( sys.argv[2] )
cmd = sys.argv[3:]
start = time.clock()
exitCode, pout, timedOut = RunCommand( cmd, timeout )
end = time.clock()
if timedOut:
exitStatus = 'Timeout'
else:
exitStatus = 'Normal' #TODO: detect crash
config = ConfigParser.SafeConfigParser()
config.add_section( 'Result' )
config.set( 'Result', 'ExitCode', str( exitCode ) )
config.set( 'Result', 'ExitStatus', exitStatus )
config.set( 'Result', 'Filename', cmd[0] )
config.set( 'Result', 'Arguments', " ".join( cmd[1:] ) )
config.set( 'Result', 'Timeout', str( timeout ) )
config.set( 'Result', 'ExecutionTime', str( end - start ) )
config.set( 'Result', 'Stdout', pout[0] )
config.set( 'Result', 'Stderr', pout[1] )
config.add_section( 'Platform' )
config.set( 'Platform', 'system', platform.system() )
config.set( 'Platform', 'release', platform.release() )
config.set( 'Platform', 'version', platform.version() )
config.set( 'Platform', 'machine', platform.machine() )
with open( output, 'w' ) as configFile:
config.write(configFile )
sys.exit( exitCode )
|