-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_ansible_output.py
executable file
·159 lines (135 loc) · 3.91 KB
/
color_ansible_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
import re
import subprocess
import sys
def get_terminal_max_lines():
try:
return int( subprocess.check_output( ['tput', 'lines'] ).strip() )
except:
pass
try:
return int( subprocess.check_output( ['stty', 'size'] ).split()[0] )
except:
pass
try:
out = subprocess.check_output( ['stty', '-a' ] )
for line in out.split( "\n" ):
fields = line.split( ";" )
for field in fields:
words = field.split()
if len( words ) == 2 and words[0] == 'rows': return int( words[1] )
except:
pass
return 50
def green():
sys.stdout.write( '\033[0;32m' )
def grey():
sys.stdout.write( '\033[1;30m' )
def normal():
sys.stdout.write( '\033[0m' )
def white():
sys.stdout.write( '\033[1;37m')
def yellow():
sys.stdout.write( '\033[1;33m' )
def red():
sys.stdout.write( '\033[0;31m' )
def blue():
sys.stdout.write( '\033[0;34m' )
def cyan():
sys.stdout.write( '\033[0;36m' )
def find_space( s, start ):
n = len( s )
while start < n:
if s[start].isspace():
return start
else:
start += 1
return start
def find_non_space( s, start ):
n = len( s )
while start < n:
if not s[start].isspace():
return start
else:
start += 1
return start
def color_play_recap( line ):
index = line.find( ':' )
if index > 0:
yellow()
sys.stdout.write( line[0:index] )
index += 1
normal()
sys.stdout.write( ':' )
end = find_non_space( line, index )
end = find_space( line, end )
green()
sys.stdout.write( line[index:end] )
index = end + 1
end = find_non_space( line, index )
end = find_space( line, end )
yellow()
sys.stdout.write( line[index:end] )
normal()
sys.stdout.write( line[ end + 1:] )
class LineReader:
def __init__( self, filename ):
self.filename = filename
self.fobj = None
def __enter__( self ):
if self.filename == None:
return sys.stdin
else:
self.fobj = open( self.filename )
return self.fobj
def __exit__( self, *args ):
if self.fobj is not None:
self.fobj.close()
def read_line( filename, line_proc_func ):
if filename is not None:
with open( filename ) as fp:
for line in fp:
line_proc_func( line )
else:
for line in sys.stdin:
line_proc_func( line )
def color_output_file( filename ):
line_colors = {
r"changed:": yellow,
r"ok": green,
r"TASK": normal,
r"PLAY": normal,
r"ansible-playbook \d": blue,
r"ansible-playbook": normal,
r"<": blue,
r"META:": blue,
r"fatal:": red,
r"task path:": grey,
r"skipping:": cyan,
r"...ignoring": cyan,
r"\d+ plays": green
}
playRecap = False
lines = 0
max_lines_in_terminal = get_terminal_max_lines()
with LineReader( filename ) as fp:
for line in fp:
lines += 1
if lines % max_lines_in_terminal == 0:
if filename is not None: raw_input()
color_found = False
for k, v in line_colors.iteritems():
if re.match( k, line ):
color_found = True
v()
if playRecap:
color_play_recap( line )
else:
sys.stdout.write( line )
playRecap = line.startswith( "PLAY RECAP" )
normal()
if __name__ == "__main__":
try:
color_output_file( sys.argv[1] if len( sys.argv ) > 1 else None )
except:
normal()