Cleanup colouring functions.
authorMichael Hanselmann <hansmi@google.com>
Thu, 1 Nov 2007 14:31:56 +0000 (14:31 +0000)
committerMichael Hanselmann <hansmi@google.com>
Thu, 1 Nov 2007 14:31:56 +0000 (14:31 +0000)
Make the code somewhat smaller. Disable disk failure test for master for now.

Reviewed-by: schreiberal

qa/qa_daemon.py
qa/qa_instance.py
qa/qa_os.py
qa/qa_utils.py

index 8b40edf..413e5d7 100644 (file)
@@ -83,8 +83,8 @@ def PrintCronWarning():
 
   """
   print
-  qa_utils.PrintWarning("The following tests require the cron script for"
-                        " ganeti-watcher to be set up.")
+  print qa_utils.FormatWarning("The following tests require the cron script "
+                               "for ganeti-watcher to be set up.")
 
 
 def TestInstanceAutomaticRestart(node, instance):
index 39a5e4f..5d26017 100644 (file)
@@ -301,10 +301,10 @@ def _TestInstanceDiskFailure(instance, node, node2, onmaster):
 
 def TestInstanceMasterDiskFailure(instance, node, node2):
   """Testing disk failure on master node."""
-  qa_utils.PrintError("Disk failure on primary node cannot be "
-                      "tested due to potential crashes.")
+  print qa_utils.FormatError("Disk failure on primary node cannot be "
+                             "tested due to potential crashes.")
   # The following can cause crashes, thus it's disabled until fixed
-  return _TestInstanceDiskFailure(instance, node, node2, True)
+  #return _TestInstanceDiskFailure(instance, node, node2, True)
 
 
 def TestInstanceSecondaryDiskFailure(instance, node, node2):
index 5383d07..957b97d 100644 (file)
@@ -74,8 +74,9 @@ def _SetupTempOs(node, dir, valid):
 
   cmd = ' && '.join(parts)
 
-  qa_utils.PrintInfo("Setting up %s with %s OS definition" %
-                     (node["primary"], ["an invalid", "a valid"][int(valid)]))
+  print qa_utils.FormatInfo("Setting up %s with %s OS definition" %
+                            (node["primary"],
+                             ["an invalid", "a valid"][int(valid)]))
 
   AssertEqual(StartSSH(node['primary'], cmd).wait(), 0)
 
index f94aa6f..70650c1 100644 (file)
@@ -42,6 +42,10 @@ def _SetupColours():
   """
   global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ
 
+  # Don't use colours if stdout isn't a terminal
+  if not sys.stdout.isatty():
+    return
+
   try:
     import curses
   except ImportError:
@@ -201,29 +205,12 @@ def GetNodeInstances(node, secondaries=False):
   return instances
 
 
-def _PrintWithColor(text, seq):
-  f = sys.stdout
-
-  if not f.isatty():
-    seq = None
-
-  if seq:
-    f.write(seq)
-
-  f.write(text)
-  f.write("\n")
-
-  if seq:
-    f.write(_RESET_SEQ)
-
-
-def PrintWarning(text):
-  return _PrintWithColor(text, _WARNING_SEQ)
-
-
-def PrintError(text):
-  return _PrintWithColor(text, _ERROR_SEQ)
+def _FormatWithColor(text, seq):
+  if not seq:
+    return text
+  return "%s%s%s" % (seq, text, _RESET_SEQ)
 
 
-def PrintInfo(text):
-  return _PrintWithColor(text, _INFO_SEQ)
+FormatWarning = lambda text: _FormatWithColor(text, _WARNING_SEQ)
+FormatError = lambda text: _FormatWithColor(text, _ERROR_SEQ)
+FormatInfo = lambda text: _FormatWithColor(text, _INFO_SEQ)