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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
import os
import sys
from lib.singleton import Singleton
# Use it to print messages on the screen and to the worker's log.
color_stdout = None # = Colorer(); below the class definition
def color_log(*args, **kwargs):
""" Print the message only to log file, not on the screen. The intention is
use this function only for regular, non-error output that appears every run
and mostly not needed for a user (but useful when investigating occured
problem). Don't hide errors and backtraces (or any other details of an
exceptional circumstances) from the screen, because such details especially
useful with CI bots.
"""
kwargs['log_only'] = True
color_stdout(*args, **kwargs)
class CSchema(object):
objects = {}
def __init__(self):
self.main_objects = {
'diff_mark': {},
'diff_in': {},
'diff_out': {},
'test_pass': {},
'test_fail': {},
'test_new': {},
'test_skip': {},
'test_disa': {},
'error': {},
'lerror': {},
'tail': {},
'ts_text': {},
'path': {},
'info': {},
'separator': {},
't_name': {},
'serv_text': {},
'version': {},
'tr_text': {},
'log': {},
}
self.main_objects.update(self.objects)
class SchemaAscetic(CSchema):
objects = {
'diff_mark': {'fgcolor': 'magenta'},
'diff_in': {'fgcolor': 'green'},
'diff_out': {'fgcolor': 'red'},
'test_pass': {'fgcolor': 'green'},
'test_fail': {'fgcolor': 'red'},
'test_new': {'fgcolor': 'lblue'},
'test_skip': {'fgcolor': 'grey'},
'test_disa': {'fgcolor': 'grey'},
'error': {'fgcolor': 'red'},
'test_var': {'fgcolor': 'yellow'},
}
class SchemaPretty(CSchema):
objects = {
'diff_mark': {'fgcolor': 'magenta'},
'diff_in': {'fgcolor': 'blue'},
'diff_out': {'fgcolor': 'red'},
'test_pass': {'fgcolor': 'green'},
'test_fail': {'fgcolor': 'red'},
'test_new': {'fgcolor': 'lblue'},
'test_skip': {'fgcolor': 'grey'},
'test_disa': {'fgcolor': 'grey'},
'error': {'fgcolor': 'red'},
'lerror': {'fgcolor': 'lred'},
'tail': {'fgcolor': 'lblue'},
'ts_text': {'fgcolor': 'lmagenta'},
'path': {'fgcolor': 'green', 'bold': True},
'info': {'fgcolor': 'yellow', 'bold': True},
'separator': {'fgcolor': 'blue'},
't_name': {'fgcolor': 'lblue'},
'serv_text': {'fgcolor': 'lmagenta'},
'version': {'fgcolor': 'yellow', 'bold': True},
'tr_text': {'fgcolor': 'green'},
'log': {'fgcolor': 'grey'},
'test_var': {'fgcolor': 'yellow'},
}
class Colorer(object):
"""
Colorer/Styler based on VT220+ specifications (Not full). Based on:
1. ftp://ftp.cs.utk.edu/pub/shuford/terminal/dec_vt220_codes.txt
2. http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
"""
__metaclass__ = Singleton
fgcolor = {
"black": '0;30',
"red": '0;31',
"green": '0;32',
"brown": '0;33',
"blue": '0;34',
"magenta": '0;35',
"cyan": '0;36',
"grey": '0;37',
"lgrey": '1;30',
"lred": '1;31',
"lgreen": '1;32',
"yellow": '1;33',
"lblue": '1;34',
"lmagenta": '1;35',
"lcyan": '1;36',
"white": '1;37',
}
bgcolor = {
"black": '0;40',
"red": '0;41',
"green": '0;42',
"brown": '0;43',
"blue": '0;44',
"magenta": '0;45',
"cyan": '0;46',
"grey": '0;47',
"lgrey": '1;40',
"lred": '1;41',
"lgreen": '1;42',
"yellow": '1;43',
"lblue": '1;44',
"lmagenta": '1;45',
"lcyan": '1;46',
"white": '1;47',
}
attributes = {
"bold": '1',
"underline": '4',
"blinking": '5',
"negative": '7',
"invisible": '8',
}
begin = "\033["
end = "m"
disable = begin+'0'+end
def __init__(self):
# These two fields can be filled later. It's for passing output from
# workers via result queue. When worker initializes, it set these
# fields and just use Colorer as before having multiplexed output.
self.queue_msg_wrapper = None
self.queue = None
self.stdout = sys.stdout
self.is_term = self.stdout.isatty()
self.colors = None
if self.is_term:
try:
p = os.popen('tput colors 2>/dev/null')
self.colors = int(p.read())
except: # noqa: E722
pass
finally:
p.close()
schema = os.getenv('TT_SCHEMA', 'ascetic')
if schema == 'ascetic':
self.schema = SchemaAscetic()
elif schema == 'pretty':
self.schema = SchemaPretty()
else:
self.schema = CSchema()
self.schema = self.schema.main_objects
def set_stdout(self):
sys.stdout = self
def ret_stdout(self):
sys.stdout = self.stdout
def _write(self, obj, log_only):
if self.queue:
if self.queue_msg_wrapper:
obj = self.queue_msg_wrapper(obj, log_only)
self.queue.put(obj)
elif not log_only:
self.stdout.write(obj)
def _flush(self):
if not self.queue:
self.stdout.flush()
def write(self, *args, **kwargs):
flags = []
if 'schema' in kwargs:
kwargs.update(self.schema[kwargs['schema']])
for i in self.attributes:
if i in kwargs and kwargs[i] is True:
flags.append(self.attributes[i])
flags.append(self.fgcolor[kwargs['fgcolor']]) \
if 'fgcolor' in kwargs else None
flags.append(self.bgcolor[kwargs['bgcolor']]) \
if 'bgcolor' in kwargs else None
data = ''
if self.is_term and flags:
data += self.begin + (';'.join(flags)) + self.end
for i in args:
data += str(i)
if self.is_term:
# write 'color disable' before newline to better work with parallel
# processes writing signle stdout/stderr
if data.endswith('\n'):
data = data[:-1] + self.disable + '\n'
else:
data += self.disable
if data:
self._write(data, kwargs.get('log_only', False))
self._flush()
def __call__(self, *args, **kwargs):
self.write(*args, **kwargs)
def writeout_unidiff(self, diff):
for i in diff:
if not i.endswith('\n'):
i += "\n\\ No newline\n"
if i.startswith('+'):
self.write(i, schema='diff_in')
elif i.startswith('-'):
self.write(i, schema='diff_out')
elif i.startswith('@'):
self.write(i, schema='diff_mark')
else:
self.write(i)
def flush(self):
return self.stdout.flush()
def fileno(self):
return self.stdout.fileno()
def isatty(self):
return self.is_term
# Globals
#########
color_stdout = Colorer()
|