11
22import logging
3- from sys import stdout , stderr , platform
3+ import os
4+ import re
5+ import sh
6+ from sys import stdout , stderr
7+ from math import log10
48from collections import defaultdict
59from colorama import Style as Colo_Style , Fore as Colo_Fore
610
11+
712class LevelDifferentiatingFormatter (logging .Formatter ):
813 def format (self , record ):
914 if record .levelno > 30 :
@@ -74,6 +79,19 @@ def info_notify(s):
7479 Err_Style .RESET_ALL ))
7580
7681
82+ def shorten_string (string , max_width ):
83+ ''' make limited length string in form:
84+ "the string is very lo...(and 15 more)"
85+ '''
86+ string_len = len (string )
87+ if string_len <= max_width :
88+ return string
89+ visible = max_width - 16 - int (log10 (string_len ))
90+ # expected suffix len "...(and XXXXX more)"
91+ return '' .join ((string [:visible ], '...(and ' , str (string_len - visible ),
92+ ' more)' ))
93+
94+
7795def shprint (command , * args , ** kwargs ):
7896 '''Runs the command (which should be an sh.Command instance), while
7997 logging the output.'''
@@ -114,22 +132,22 @@ def shprint(command, *args, **kwargs):
114132 '\t ' , ' ' ).replace (
115133 '\b ' , ' ' ).rstrip ()
116134 if msg :
117- sys . stdout .write (u'{}\r {}{:<{width}}' .format (
135+ stdout .write (u'{}\r {}{:<{width}}' .format (
118136 Err_Style .RESET_ALL , msg_hdr ,
119137 shorten_string (msg , msg_width ), width = msg_width ))
120- sys . stdout .flush ()
138+ stdout .flush ()
121139 need_closing_newline = True
122140 else :
123141 logger .debug ('' .join (['\t ' , line .rstrip ()]))
124142 if need_closing_newline :
125- sys . stdout .write ('{}\r {:>{width}}\r ' .format (
143+ stdout .write ('{}\r {:>{width}}\r ' .format (
126144 Err_Style .RESET_ALL , ' ' , width = (columns - 1 )))
127- sys . stdout .flush ()
145+ stdout .flush ()
128146 except sh .ErrorReturnCode as err :
129147 if need_closing_newline :
130- sys . stdout .write ('{}\r {:>{width}}\r ' .format (
148+ stdout .write ('{}\r {:>{width}}\r ' .format (
131149 Err_Style .RESET_ALL , ' ' , width = (columns - 1 )))
132- sys . stdout .flush ()
150+ stdout .flush ()
133151 if tail_n or filter_in or filter_out :
134152 def printtail (out , name , forecolor , tail_n = 0 ,
135153 re_filter_in = None , re_filter_out = None ):
@@ -144,7 +162,8 @@ def printtail(out, name, forecolor, tail_n=0,
144162 else :
145163 info ('{} (last {} lines of {}):\n {}\t {}{}' .format (
146164 name , tail_n , len (lines ),
147- forecolor , '\t \n ' .join (lines [- tail_n :]), Out_Fore .RESET ))
165+ forecolor , '\t \n ' .join (lines [- tail_n :]),
166+ Out_Fore .RESET ))
148167 printtail (err .stdout , 'STDOUT' , Out_Fore .YELLOW , tail_n ,
149168 re .compile (filter_in ) if filter_in else None ,
150169 re .compile (filter_out ) if filter_out else None )
@@ -156,7 +175,8 @@ def printtail(out, name, forecolor, tail_n=0,
156175 Err_Fore .YELLOW , Err_Fore .RESET , "\n " .join (
157176 "set {}={}" .format (n , v ) for n , v in env .items ())))
158177 info ("{}COMMAND:{}\n cd {} && {} {}\n " .format (
159- Err_Fore .YELLOW , Err_Fore .RESET , getcwd (), command , ' ' .join (args )))
178+ Err_Fore .YELLOW , Err_Fore .RESET , os .getcwd (), command ,
179+ ' ' .join (args )))
160180 warning ("{}ERROR: {} failed!{}" .format (
161181 Err_Fore .RED , command , Err_Fore .RESET ))
162182 exit (1 )
0 commit comments