-added methods to help scp files to / from Remotes
[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     print >> logfile, "+ "+command
21     print >> logfile, "".join(output).strip() 
22     # filter output generated by set x
23     remove_set_x = lambda line: not line.startswith("+")         
24     errors = filter(remove_set_x, stderr.readlines())
25     
26     if fatal and errors:
27         raise Exception, "".join(errors)        
28     return (output, errors)    
29
30 def popen3(command, verbose = False):
31     if verbose:
32         header(command, False)
33     (stdin, stdout, stderr) = os.popen3(command)
34     print >> logfile, "+ "+command
35     return (stdin, stdout, stderr)                      
36     
37 def commands(command, fatal = True, verbose = False):
38     if verbose:
39         header(command, False)  
40     (status, output) = getstatusoutput(command)
41     print >> logfile, "+ "+command
42     print >> logfile, output.strip()            
43     if fatal and not status == 0:
44         raise Exception, "%(command)s Failed:\n%(output)s" % locals()
45     return (status, output)                      
46