4 from commands import getstatusoutput
5 from logger import logfile
7 def header(message, log = True):
8 now=time.strftime("%H:%M:%S", time.localtime())
9 output = "*"+now+'--'+message
12 print >> logfile, output
14 def popen(command, fatal=True, verbose = False):
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)
28 raise Exception, "".join(errors)
29 return (output, errors)
31 def popen3(command, verbose = False):
33 header(command, False)
34 (stdin, stdout, stderr) = os.popen3(command)
35 print >> logfile, "+ "+command
36 return (stdin, stdout, stderr)
38 def commands(command, fatal = True, verbose = False):
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)