@@ -38,6 +38,7 @@ def format(self, record):
38
38
warning = logger .warning
39
39
error = logger .error
40
40
41
+
41
42
class colorama_shim (object ):
42
43
43
44
def __init__ (self ):
@@ -61,3 +62,105 @@ def __getattr__(self, key):
61
62
else :
62
63
Err_Style = Null_Style
63
64
Err_Fore = Null_Fore
65
+
66
+
67
+ def info_main (* args ):
68
+ logger .info ('' .join ([Err_Style .BRIGHT , Err_Fore .GREEN ] + list (args ) +
69
+ [Err_Style .RESET_ALL , Err_Fore .RESET ]))
70
+
71
+
72
+ def info_notify (s ):
73
+ info ('{}{}{}{}' .format (Err_Style .BRIGHT , Err_Fore .LIGHTBLUE_EX , s ,
74
+ Err_Style .RESET_ALL ))
75
+
76
+
77
+ def shprint (command , * args , ** kwargs ):
78
+ '''Runs the command (which should be an sh.Command instance), while
79
+ logging the output.'''
80
+ kwargs ["_iter" ] = True
81
+ kwargs ["_out_bufsize" ] = 1
82
+ kwargs ["_err_to_out" ] = True
83
+ kwargs ["_bg" ] = True
84
+ is_critical = kwargs .pop ('_critical' , False )
85
+ tail_n = kwargs .pop ('_tail' , 0 )
86
+ filter_in = kwargs .pop ('_filter' , None )
87
+ filter_out = kwargs .pop ('_filterout' , None )
88
+ if len (logger .handlers ) > 1 :
89
+ logger .removeHandler (logger .handlers [1 ])
90
+ try :
91
+ columns = max (25 , int (os .popen ('stty size' , 'r' ).read ().split ()[1 ]))
92
+ except :
93
+ columns = 100
94
+ command_path = str (command ).split ('/' )
95
+ command_string = command_path [- 1 ]
96
+ string = ' ' .join (['running' , command_string ] + list (args ))
97
+
98
+ # If logging is not in DEBUG mode, trim the command if necessary
99
+ if logger .level > logging .DEBUG :
100
+ logger .info ('{}{}' .format (shorten_string (string , columns - 12 ),
101
+ Err_Style .RESET_ALL ))
102
+ else :
103
+ logger .debug ('{}{}' .format (string , Err_Style .RESET_ALL ))
104
+
105
+ need_closing_newline = False
106
+ try :
107
+ msg_hdr = ' working: '
108
+ msg_width = columns - len (msg_hdr ) - 1
109
+ output = command (* args , ** kwargs )
110
+ for line in output :
111
+ if logger .level > logging .DEBUG :
112
+ msg = line .replace (
113
+ '\n ' , ' ' ).replace (
114
+ '\t ' , ' ' ).replace (
115
+ '\b ' , ' ' ).rstrip ()
116
+ if msg :
117
+ sys .stdout .write (u'{}\r {}{:<{width}}' .format (
118
+ Err_Style .RESET_ALL , msg_hdr ,
119
+ shorten_string (msg , msg_width ), width = msg_width ))
120
+ sys .stdout .flush ()
121
+ need_closing_newline = True
122
+ else :
123
+ logger .debug ('' .join (['\t ' , line .rstrip ()]))
124
+ if need_closing_newline :
125
+ sys .stdout .write ('{}\r {:>{width}}\r ' .format (
126
+ Err_Style .RESET_ALL , ' ' , width = (columns - 1 )))
127
+ sys .stdout .flush ()
128
+ except sh .ErrorReturnCode as err :
129
+ if need_closing_newline :
130
+ sys .stdout .write ('{}\r {:>{width}}\r ' .format (
131
+ Err_Style .RESET_ALL , ' ' , width = (columns - 1 )))
132
+ sys .stdout .flush ()
133
+ if tail_n or filter_in or filter_out :
134
+ def printtail (out , name , forecolor , tail_n = 0 ,
135
+ re_filter_in = None , re_filter_out = None ):
136
+ lines = out .splitlines ()
137
+ if re_filter_in is not None :
138
+ lines = [l for l in lines if re_filter_in .search (l )]
139
+ if re_filter_out is not None :
140
+ lines = [l for l in lines if not re_filter_out .search (l )]
141
+ if tail_n == 0 or len (lines ) <= tail_n :
142
+ info ('{}:\n {}\t {}{}' .format (
143
+ name , forecolor , '\t \n ' .join (lines ), Out_Fore .RESET ))
144
+ else :
145
+ info ('{} (last {} lines of {}):\n {}\t {}{}' .format (
146
+ name , tail_n , len (lines ),
147
+ forecolor , '\t \n ' .join (lines [- tail_n :]), Out_Fore .RESET ))
148
+ printtail (err .stdout , 'STDOUT' , Out_Fore .YELLOW , tail_n ,
149
+ re .compile (filter_in ) if filter_in else None ,
150
+ re .compile (filter_out ) if filter_out else None )
151
+ printtail (err .stderr , 'STDERR' , Err_Fore .RED )
152
+ if is_critical :
153
+ env = kwargs .get ("env" )
154
+ if env is not None :
155
+ info ("{}ENV:{}\n {}\n " .format (
156
+ Err_Fore .YELLOW , Err_Fore .RESET , "\n " .join (
157
+ "set {}={}" .format (n , v ) for n , v in env .items ())))
158
+ info ("{}COMMAND:{}\n cd {} && {} {}\n " .format (
159
+ Err_Fore .YELLOW , Err_Fore .RESET , getcwd (), command , ' ' .join (args )))
160
+ warning ("{}ERROR: {} failed!{}" .format (
161
+ Err_Fore .RED , command , Err_Fore .RESET ))
162
+ exit (1 )
163
+ else :
164
+ raise
165
+
166
+ return output
0 commit comments