1
1
2
2
import 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
4
8
from collections import defaultdict
5
9
from colorama import Style as Colo_Style , Fore as Colo_Fore
6
10
11
+
7
12
class LevelDifferentiatingFormatter (logging .Formatter ):
8
13
def format (self , record ):
9
14
if record .levelno > 30 :
@@ -74,6 +79,19 @@ def info_notify(s):
74
79
Err_Style .RESET_ALL ))
75
80
76
81
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
+
77
95
def shprint (command , * args , ** kwargs ):
78
96
'''Runs the command (which should be an sh.Command instance), while
79
97
logging the output.'''
@@ -114,22 +132,22 @@ def shprint(command, *args, **kwargs):
114
132
'\t ' , ' ' ).replace (
115
133
'\b ' , ' ' ).rstrip ()
116
134
if msg :
117
- sys . stdout .write (u'{}\r {}{:<{width}}' .format (
135
+ stdout .write (u'{}\r {}{:<{width}}' .format (
118
136
Err_Style .RESET_ALL , msg_hdr ,
119
137
shorten_string (msg , msg_width ), width = msg_width ))
120
- sys . stdout .flush ()
138
+ stdout .flush ()
121
139
need_closing_newline = True
122
140
else :
123
141
logger .debug ('' .join (['\t ' , line .rstrip ()]))
124
142
if need_closing_newline :
125
- sys . stdout .write ('{}\r {:>{width}}\r ' .format (
143
+ stdout .write ('{}\r {:>{width}}\r ' .format (
126
144
Err_Style .RESET_ALL , ' ' , width = (columns - 1 )))
127
- sys . stdout .flush ()
145
+ stdout .flush ()
128
146
except sh .ErrorReturnCode as err :
129
147
if need_closing_newline :
130
- sys . stdout .write ('{}\r {:>{width}}\r ' .format (
148
+ stdout .write ('{}\r {:>{width}}\r ' .format (
131
149
Err_Style .RESET_ALL , ' ' , width = (columns - 1 )))
132
- sys . stdout .flush ()
150
+ stdout .flush ()
133
151
if tail_n or filter_in or filter_out :
134
152
def printtail (out , name , forecolor , tail_n = 0 ,
135
153
re_filter_in = None , re_filter_out = None ):
@@ -144,7 +162,8 @@ def printtail(out, name, forecolor, tail_n=0,
144
162
else :
145
163
info ('{} (last {} lines of {}):\n {}\t {}{}' .format (
146
164
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 ))
148
167
printtail (err .stdout , 'STDOUT' , Out_Fore .YELLOW , tail_n ,
149
168
re .compile (filter_in ) if filter_in else None ,
150
169
re .compile (filter_out ) if filter_out else None )
@@ -156,7 +175,8 @@ def printtail(out, name, forecolor, tail_n=0,
156
175
Err_Fore .YELLOW , Err_Fore .RESET , "\n " .join (
157
176
"set {}={}" .format (n , v ) for n , v in env .items ())))
158
177
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 )))
160
180
warning ("{}ERROR: {} failed!{}" .format (
161
181
Err_Fore .RED , command , Err_Fore .RESET ))
162
182
exit (1 )
0 commit comments