1+ import logging
12import re
23import select
34import subprocess
@@ -29,43 +30,15 @@ def __init__(self, args=[]):
2930 threading .Thread .__init__ (self )
3031
3132 self .args = args
32- self .captured_stdout = ""
33- self .captured_stderr = ""
34- self .stdout_file = None
35- self .stderr_file = None
36- self .capture_stdout = True
37- self .capture_stderr = True
38- self .show_stdout = True
39- self .show_stderr = True
33+ self .captured_stdout = []
34+ self .captured_stderr = []
4035
4136 self .should_die = threading .Event ()
4237
43- def configure_stdout (self , file = None , capture = True , show = False ):
44- self .stdout_file = file
45- self .capture_stdout = capture
46- self .show_stdout = show
47-
48- def configure_stderr (self , file = None , capture = False , show = False ):
49- self .stderr_file = file
50- self .capture_stderr = capture
51- self .show_stderr = show
52-
5338 def run (self ):
54- stdout_handle = None
55- stderr_handle = None
56- try :
57- if self .stdout_file :
58- stdout_handle = open (self .stdout_file , "w" )
59- if self .stderr_file :
60- stderr_handle = open (self .stderr_file , "w" )
61- self .run_with_handles (stdout_handle , stderr_handle )
62- finally :
63- if stdout_handle :
64- stdout_handle .close ()
65- if stderr_handle :
66- stderr_handle .close ()
67-
68- def run_with_handles (self , stdout_handle , stderr_handle ):
39+ self .run_with_handles ()
40+
41+ def run_with_handles (self ):
6942 self .child = subprocess .Popen (
7043 self .args ,
7144 bufsize = 1 ,
@@ -78,35 +51,32 @@ def run_with_handles(self, stdout_handle, stderr_handle):
7851
7952 if self .child .stdout in rds :
8053 line = self .child .stdout .readline ()
81- if stdout_handle :
82- stdout_handle .write (line )
83- stdout_handle .flush ()
84- if self .capture_stdout :
85- self .captured_stdout += line
86- if self .show_stdout :
87- sys .stdout .write (line )
88- sys .stdout .flush ()
54+ self .captured_stdout .append (line )
8955
9056 if self .child .stderr in rds :
9157 line = self .child .stderr .readline ()
92- if stderr_handle :
93- stderr_handle .write (line )
94- stderr_handle .flush ()
95- if self .capture_stderr :
96- self .captured_stderr += line
97- if self .show_stderr :
98- sys .stderr .write (line )
99- sys .stderr .flush ()
58+ self .captured_stderr .append (line )
10059
10160 if self .should_die .is_set ():
10261 self .child .terminate ()
10362 alive = False
10463
105- if self .child .poll () is not None :
64+ poll_results = self .child .poll ()
65+ if poll_results is not None :
10666 if not alive :
10767 break
10868 else :
109- raise RuntimeError ("Subprocess has died. Aborting." )
69+ self .dump_logs ()
70+ raise RuntimeError ("Subprocess has died. Aborting. (args=%s)" % ' ' .join (str (x ) for x in self .args ))
71+
72+ def dump_logs (self ):
73+ logging .critical ('stderr' )
74+ for line in self .captured_stderr :
75+ logging .critical (line .rstrip ())
76+
77+ logging .critical ('stdout' )
78+ for line in self .captured_stdout :
79+ logging .critical (line .rstrip ())
11080
11181 def wait_for (self , pattern , timeout = 10 ):
11282 t1 = time .time ()
@@ -117,11 +87,13 @@ def wait_for(self, pattern, timeout=10):
11787 self .child .kill ()
11888 except :
11989 logging .exception ("Received exception when killing child process" )
90+ self .dump_logs ()
91+
12092 raise RuntimeError ("Waiting for %r timed out" % pattern )
12193
122- if re .search (pattern , self .captured_stdout , re .IGNORECASE ) is not None :
94+ if re .search (pattern , ' \n ' . join ( self .captured_stdout ) , re .IGNORECASE ) is not None :
12395 return
124- if re .search (pattern , self .captured_stderr , re .IGNORECASE ) is not None :
96+ if re .search (pattern , ' \n ' . join ( self .captured_stderr ) , re .IGNORECASE ) is not None :
12597 return
12698 time .sleep (0.1 )
12799
0 commit comments