@@ -127,6 +127,8 @@ def shprint(command, *args, **kwargs):
127
127
kwargs ["_bg" ] = True
128
128
is_critical = kwargs .pop ('_critical' , False )
129
129
tail_n = kwargs .pop ('_tail' , 0 )
130
+ filter_in = kwargs .pop ('_filter' , None )
131
+ filter_out = kwargs .pop ('_filterout' , None )
130
132
if len (logger .handlers ) > 1 :
131
133
logger .removeHandler (logger .handlers [1 ])
132
134
try :
@@ -161,16 +163,24 @@ def shprint(command, *args, **kwargs):
161
163
if need_closing_newline : sys .stdout .write ('{}\r {:>{width}}\r ' .format (Style .RESET_ALL , ' ' , width = (columns - 1 )))
162
164
except sh .ErrorReturnCode , err :
163
165
if need_closing_newline : sys .stdout .write ('{}\r {:>{width}}\r ' .format (Style .RESET_ALL , ' ' , width = (columns - 1 )))
164
- if tail_n :
165
- def printtail (name , forecolor , tail_n , out ):
166
+ if tail_n or filter_in or filter_out :
167
+ def printtail (out , name , forecolor , tail_n = 0 , re_filter_in = None , re_filter_out = None ):
166
168
lines = out .splitlines ()
169
+ if re_filter_in is not None : lines = [l for l in lines if re_filter_in .search (l )]
170
+ if re_filter_out is not None : lines = [l for l in lines if not re_filter_out .search (l )]
167
171
if tail_n == 0 or len (lines ) <= tail_n :
168
172
info ('{}:\n {}\t {}{}' .format (name , forecolor , '\t \n ' .join (lines ), Fore .RESET ))
169
173
else :
170
174
info ('{} (last {} lines of {}):\n {}\t {}{}' .format (name , tail_n , len (lines ), forecolor , '\t \n ' .join (lines [- tail_n :]), Fore .RESET ))
171
- printtail ('STDOUT' , Fore .YELLOW , tail_n , err .stdout )
172
- printtail ('STDERR' , Fore .RED , 0 , err .stderr )
175
+ printtail (err .stdout , 'STDOUT' , Fore .YELLOW , tail_n ,
176
+ re .compile (filter_in ) if filter_in else None ,
177
+ re .compile (filter_out ) if filter_out else None )
178
+ printtail (err .stderr , 'STDERR' , Fore .RED )
173
179
if is_critical :
180
+ env = kwargs .get ("env" )
181
+ if env is not None :
182
+ info ("{}ENV:{}\n {}\n " .format (Fore .YELLOW , Fore .RESET , "\n " .join ("set {}={}" .format (n ,v ) for n ,v in env .items ())))
183
+ info ("{}COMMAND:{}\n cd {} && {} {}\n " .format (Fore .YELLOW , Fore .RESET , getcwd (), command , ' ' .join (args )))
174
184
warning ("{}ERROR: {} failed!{}" .format (Fore .RED , command , Fore .RESET ))
175
185
exit (1 )
176
186
else :
0 commit comments