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