-fix is_ready(). -Now check if NodeManager has started and vuseradd has stopped
[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, log = True):
8     now=time.strftime("%H:%M:%S", time.localtime())
9     output = "*"+now+'--'+message       
10     print output
11     if log:     
12         print >> logfile, output        
13
14 def popen(command, fatal=True, verbose = False):
15     header(command)     
16     if verbose:
17         header(command, False)  
18     (stdin, stdout, stderr) = os.popen3(command)
19     output = stdout.readlines()
20     errors = stderr.readlines()         
21     print >> logfile, "+ "+command
22     print >> logfile, "".join(output).strip() 
23     # filter output generated by set x
24     remove_set_x = lambda line: not line.startswith("+")         
25     errors = filter(remove_set_x, errors)
26     
27     if fatal and errors:
28         raise Exception, "".join(errors)        
29     return (output, errors)    
30
31 def popen3(command, verbose = False):
32     if verbose:
33         header(command, False)
34     (stdin, stdout, stderr) = os.popen3(command)
35     print >> logfile, "+ "+command
36     return (stdin, stdout, stderr)                      
37     
38 def commands(command, fatal = True, verbose = False):
39     if verbose:
40         header(command, False)  
41     (status, output) = getstatusoutput(command)
42     print >> logfile, "+ "+command
43     print >> logfile, output.strip()            
44     if fatal and not status == 0:
45         raise Exception, "%(command)s Failed:\n%(output)s" % locals()
46     return (status, output)                      
47