add date to log's filename by default
[tests.git] / qaapi / qa / utils.py
index 38132bf..40eb086 100644 (file)
@@ -1,21 +1,46 @@
 # $Id$
 import time
 import os
+from commands import getstatusoutput
+from logger import logfile
 
-def header(message):
+def header(message, log = True):
     now=time.strftime("%H:%M:%S", time.localtime())
-    print "*",now,'--',message
+    output = "*"+now+'--'+message      
+    print output
+    if log:    
+        print >> logfile, output       
 
-
-def popen(command, fatal=True):
+def popen(command, fatal=True, verbose = False):
+    header(command)    
+    if verbose:
+       header(command, False)  
     (stdin, stdout, stderr) = os.popen3(command)
     output = stdout.readlines()
-    
+    print >> logfile, "+ "+command
+    print >> logfile, "".join(output).strip() 
     # filter output generated by set x
     remove_set_x = lambda line: not line.startswith("+")        
     errors = filter(remove_set_x, stderr.readlines())
     
     if fatal and errors:
-       raise Exception, "\n".join(errors)      
+       raise Exception, "".join(errors)        
     return (output, errors)    
-     
+
+def popen3(command, verbose = False):
+    if verbose:
+       header(command, False)
+    (stdin, stdout, stderr) = os.popen3(command)
+    print >> logfile, "+ "+command
+    return (stdin, stdout, stderr)                     
+    
+def commands(command, fatal = True, verbose = False):
+    if verbose:
+       header(command, False)  
+    (status, output) = getstatusoutput(command)
+    print >> logfile, "+ "+command
+    print >> logfile, output.strip()           
+    if fatal and not status == 0:
+       raise Exception, "%(command)s Failed:\n%(output)s" % locals()
+    return (status, output)                     
+