1
+ import logging
1
2
import re
2
3
import select
3
4
import subprocess
@@ -29,43 +30,15 @@ def __init__(self, args=[]):
29
30
threading .Thread .__init__ (self )
30
31
31
32
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 = []
40
35
41
36
self .should_die = threading .Event ()
42
37
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
-
53
38
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 ):
69
42
self .child = subprocess .Popen (
70
43
self .args ,
71
44
bufsize = 1 ,
@@ -78,35 +51,32 @@ def run_with_handles(self, stdout_handle, stderr_handle):
78
51
79
52
if self .child .stdout in rds :
80
53
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 )
89
55
90
56
if self .child .stderr in rds :
91
57
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 )
100
59
101
60
if self .should_die .is_set ():
102
61
self .child .terminate ()
103
62
alive = False
104
63
105
- if self .child .poll () is not None :
64
+ poll_results = self .child .poll ()
65
+ if poll_results is not None :
106
66
if not alive :
107
67
break
108
68
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 ())
110
80
111
81
def wait_for (self , pattern , timeout = 10 ):
112
82
t1 = time .time ()
@@ -117,11 +87,13 @@ def wait_for(self, pattern, timeout=10):
117
87
self .child .kill ()
118
88
except :
119
89
logging .exception ("Received exception when killing child process" )
90
+ self .dump_logs ()
91
+
120
92
raise RuntimeError ("Waiting for %r timed out" % pattern )
121
93
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 :
123
95
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 :
125
97
return
126
98
time .sleep (0.1 )
127
99
0 commit comments