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 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())
27 raise Exception, "".join(errors)
28 return (output, errors)
30 def popen3(command, verbose = False):
32 header(command, False)
33 (stdin, stdout, stderr) = os.popen3(command)
34 print >> logfile, "+ "+command
35 return (stdin, stdout, stderr)
37 def commands(command, fatal = True, verbose = False):
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)