Revision d5a9b556

b/qa/colors.py
1
#!/usr/bin/python -u
2
#
3

  
4
# Copyright (C) 2013 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for adding colorized output to Ganeti.
23

  
24
Colors are enabled only if the standard output is a proper terminal.
25

  
26
"""
27

  
28
import os
29
import sys
30

  
31
DEFAULT = '\033[0m'
32
RED = '\033[91m'
33
GREEN = '\033[92m'
34
BLUE = '\033[94m'
35
CYAN = '\033[96m'
36
WHITE = '\033[97m'
37
YELLOW = '\033[93m'
38
MAGENTA = '\033[95m'
39
GREY = '\033[90m'
40
BLACK = '\033[90m'
41

  
42
_enabled = sys.stdout.isatty()
43

  
44

  
45
def colorize(line, color=None):
46
  if _enabled and color is not None:
47
    return color + line + DEFAULT
48
  else:
49
    return line
b/qa/ganeti-qa.py
31 31
import optparse
32 32
import sys
33 33

  
34
import colors
34 35
import qa_cluster
35 36
import qa_config
36 37
import qa_daemon
......
57 58
from ganeti.rapi.client import UsesRapiClient
58 59

  
59 60

  
60
def _FormatHeader(line, end=72):
61
def _FormatHeader(line, end=72, mark="-", color=None):
61 62
  """Fill a line up to the end column.
62 63

  
63 64
  """
64
  line = "---- " + line + " "
65
  line = (mark * 4) + " " + line + " "
65 66
  line += "-" * (end - len(line))
66 67
  line = line.rstrip()
68
  line = colors.colorize(line, color)
67 69
  return line
68 70

  
69 71

  
......
92 94
  desc = _DescriptionOf(fn)
93 95

  
94 96
  print
95
  print _FormatHeader("%s start %s" % (tstart, desc))
97
  print _FormatHeader("%s start %s" % (tstart, desc),
98
                      color=colors.YELLOW, mark="<")
96 99

  
97 100
  try:
98 101
    retval = fn(*args, **kwargs)
102
    print _FormatHeader("PASSED %s" % (desc, ), color=colors.GREEN)
99 103
    return retval
104
  except Exception, e:
105
    print _FormatHeader("FAILED %s: %s" % (desc, e), color=colors.RED)
106
    raise
100 107
  finally:
101 108
    tstop = datetime.datetime.now()
102 109
    tdelta = tstop - tstart
103
    print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc))
110
    print _FormatHeader("%s time=%s %s" % (tstop, tdelta, desc),
111
                        color=colors.MAGENTA, mark=">")
104 112

  
105 113

  
106 114
def RunTestIf(testnames, fn, *args, **kwargs):
......
117 125
    desc = _DescriptionOf(fn)
118 126
    # TODO: Formatting test names when non-string names are involved
119 127
    print _FormatHeader("%s skipping %s, test(s) %s disabled" %
120
                        (tstart, desc, testnames))
128
                        (tstart, desc, testnames),
129
                        color=colors.BLUE, mark="*")
121 130

  
122 131

  
123 132
def RunEnvTests():
b/qa/qa_utils.py
45 45
from ganeti import pathutils
46 46
from ganeti import vcluster
47 47

  
48
import colors
48 49
import qa_config
49 50
import qa_error
50 51

  
......
285 286
      pcmd = [i for i in cmd if not i.startswith("-")]
286 287
    else:
287 288
      pcmd = cmd
288
    print "Command: %s" % utils.ShellQuoteArgs(pcmd)
289
    print "%s %s" % (colors.colorize("Command:", colors.CYAN),
290
                     utils.ShellQuoteArgs(pcmd))
289 291
  return subprocess.Popen(cmd, shell=False, **kwargs)
290 292

  
291 293

  

Also available in: Unified diff