move verbose check here
[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 def popen(command, fatal=True, verbose = False):
14     if verbose:
15         header(command) 
16     (stdin, stdout, stderr) = os.popen3(command)
17     output = stdout.readlines()
18     print >> logfile, "+ "+command
19     print >> logfile, "".join(output).strip() 
20     # filter output generated by set x
21     remove_set_x = lambda line: not line.startswith("+")         
22     errors = filter(remove_set_x, stderr.readlines())
23     
24     if fatal and errors:
25         raise Exception, "".join(errors)        
26     return (output, errors)    
27
28 def popen3(command, verbose = False):
29     if verbose:
30         header(command)
31     (stdin, stdout, stderr) = os.popen3(command)
32     print >> logfile, "+ "+command
33     return (stdin, stdout, stderr)                      
34     
35 def commands(command, fatal = True, verbose = False):
36     if verbose:
37         header(command) 
38     (status, output) = getstatusoutput(command)
39     print >> logfile, "+ "+command
40     print >> logfile, output.strip()            
41     if fatal and not status == 0:
42         raise Exception, "%(command)s Failed:\n%(output)s" % locals()
43     return (status, output)                      
44