0e5a5a686ac12b44f4d249e1e3a82ad477720339
[tests.git] / qaapi / qa / utils.py
1 # $Id$
2 import time
3 import os
4 from commands import getstatusoutput
5 from logger import logfile
6
7 def header(message):
8     now=time.strftime("%H:%M:%S", time.localtime())
9     output = "*"+now+'--'+message       
10     print output
11     print >> logfile, output    
12
13
14 def popen(command, fatal=True):
15     (stdin, stdout, stderr) = os.popen3(command)
16     output = stdout.readlines()
17     print >> logfile, "+ "+command
18     print >> logfile, "".join(output).strip() 
19     # filter output generated by set x
20     remove_set_x = lambda line: not line.startswith("+")         
21     errors = filter(remove_set_x, stderr.readlines())
22     
23     if fatal and errors:
24         raise Exception, "".join(errors)        
25     return (output, errors)    
26
27 def popen3(command):
28     (stdin, stdout, stderr) = os.popen3(command)
29     print >> logfile, "+ "+command
30     return (stdin, stdout, stderr)                      
31     
32 def commands(command, fatal = True):
33     (status, output) = getstatusoutput(command)
34     print >> logfile, "+ "+command
35     print >> logfile, output.strip()            
36     if fatal and not status == 0:
37         raise Exception, "%(command)s Failed:\n%(output)s" % locals()
38     return (status, output)                      
39