One version of runProgram is enough.
[hgct.git] / ctcore.py
blob4ded37ba8f017c498c0ad33553e628f4d0fb4297
1 import sys
2 DEBUG = 0
4 # PyQt3 and python 2.4 isn't currently available together on
5 # Debian/testing we do therefore use the following quite ugly work
6 # around. The module 'mysubprocess' is just a copy of the 'subprocess'
7 # module from the Python 2.4 distribution.
8 ver = sys.version_info
9 if ver[0] < 2 or (ver[0] == 2 and ver[1] <= 3):
10 import mysubprocess
11 subprocess = mysubprocess
12 else:
13 import subprocess
15 class File:
16 pass
18 class ProgramError(Exception):
19 def __init__(self, program, err):
20 self.program = program
21 self.error = err
23 def runProgram(prog, input=None):
24 debug('runProgram prog: ' + str(prog) + " input: " + str(input))
25 if type(prog) is str:
26 progStr = prog
27 else:
28 progStr = ' '.join(prog)
30 try:
31 pop = subprocess.Popen(prog,
32 shell = type(prog) is str,
33 stderr=subprocess.STDOUT,
34 stdout=subprocess.PIPE,
35 stdin=subprocess.PIPE)
36 except OSError, e:
37 debug("strerror: " + e.strerror)
38 raise ProgramError(progStr, e.strerror)
40 if input != None:
41 pop.stdin.write(input)
42 pop.stdin.close()
44 out = pop.stdout.read()
45 code = pop.wait()
46 if code != 0:
47 debug("error output: " + out)
48 raise ProgramError(progStr, out)
49 debug("output: " + out.replace('\0', '\n'))
50 return out
52 def debug(str):
53 if DEBUG:
54 print str