2013-09-26 13:43:02 +04:00
import time , os , sys , logging
2013-01-31 04:39:40 +04:00
from subprocess import Popen , PIPE , STDOUT
2013-09-26 13:43:02 +04:00
TRACK_PROCESS_SPAWNS = True if ( os . getenv ( ' EM_BUILD_VERBOSE ' ) and int ( os . getenv ( ' EM_BUILD_VERBOSE ' ) ) > = 3 ) else False
2013-11-20 06:13:28 +04:00
def timeout_run ( proc , timeout = None , note = ' unnamed process ' , full_output = False ) :
2013-01-31 04:39:40 +04:00
start = time . time ( )
if timeout is not None :
while time . time ( ) - start < timeout and proc . poll ( ) is None :
time . sleep ( 0.1 )
if proc . poll ( ) is None :
proc . kill ( ) # XXX bug: killing emscripten.py does not kill it's child process!
raise Exception ( " Timed out: " + note )
out = proc . communicate ( )
2013-08-29 22:30:11 +04:00
out = map ( lambda o : ' ' if o is None else o , out )
2013-09-26 13:43:02 +04:00
if TRACK_PROCESS_SPAWNS :
2014-05-06 00:32:20 +04:00
logging . info ( ' Process ' + str ( proc . pid ) + ' finished after ' + str ( time . time ( ) - start ) + ' seconds. Exit code: ' + str ( proc . returncode ) )
2013-01-31 04:39:40 +04:00
return ' \n ' . join ( out ) if full_output else out [ 0 ]
2014-05-06 00:32:20 +04:00
def run_js ( filename , engine = None , args = [ ] , check_timeout = False , stdin = None , stdout = PIPE , stderr = None , cwd = None , full_output = False , assert_returncode = None ) :
2013-01-31 04:39:40 +04:00
if type ( engine ) is not list :
engine = [ engine ]
2013-08-20 02:17:09 +04:00
command = engine + [ filename ] + ( [ ' -- ' ] if ' d8 ' in engine [ 0 ] or ' jsc ' in engine [ 0 ] else [ ] ) + args
2013-09-26 13:43:02 +04:00
proc = Popen (
2013-01-31 04:39:40 +04:00
command ,
2013-08-28 10:43:56 +04:00
stdin = stdin ,
2013-01-31 04:39:40 +04:00
stdout = stdout ,
stderr = stderr ,
2013-09-26 13:43:02 +04:00
cwd = cwd )
timeout = 15 * 60 if check_timeout else None
if TRACK_PROCESS_SPAWNS :
logging . info ( ' Blocking on process ' + str ( proc . pid ) + ' : ' + str ( command ) + ( ' for ' + str ( timeout ) + ' seconds ' if timeout else ' until it finishes. ' ) )
2014-05-06 00:32:20 +04:00
ret = timeout_run (
2013-09-26 13:43:02 +04:00
proc ,
timeout ,
2013-01-31 04:39:40 +04:00
' Execution ' ,
full_output = full_output )
2014-05-06 00:32:20 +04:00
if assert_returncode is not None and proc . returncode is not assert_returncode :
raise Exception ( ' Expected the command ' + str ( command ) + ' to finish with return code ' + str ( assert_returncode ) + ' , but it returned with code ' + str ( proc . returncode ) + ' instead! Output: ' + str ( ret ) )
return ret