Make the ProgramError class printable.
[hgct.git] / ctcore.py
blob01b55b7db564493ef85961e7f765c1e0dfc0f119
1 # Copyright (c) 2005 Fredrik Kuivinen <freku045@student.liu.se>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License version 2 as
5 # published by the Free Software Foundation.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 import sys
18 applicationName = 'Commit Tool'
19 shortName = 'ct'
20 version = 'v0.3'
22 _settings = None
24 def settings():
25 global _settings
26 if not _settings:
27 from settings import Settings
28 _settings = Settings()
29 return _settings
31 DEBUG = 0
33 # PyQt3 and python 2.4 isn't currently available together on
34 # Debian/testing we do therefore use the following quite ugly work
35 # around. The module 'mysubprocess' is just a copy of the 'subprocess'
36 # module from the Python 2.4 distribution.
37 ver = sys.version_info
38 if ver[0] < 2 or (ver[0] == 2 and ver[1] <= 3):
39 import mysubprocess
40 subprocess = mysubprocess
41 else:
42 import subprocess
44 class File:
45 def __init__(self):
46 self.listViewItem = None
48 class ProgramError(Exception):
49 def __init__(self, progStr, error):
50 self.progStr = progStr
51 self.error = error
53 def __str__(self):
54 return self.progStr + ': ' + self.error
56 def runProgram(prog, input=None, expectedexits=[0]):
57 debug('runProgram prog: ' + str(prog) + " input: " + str(input))
58 if type(prog) is str:
59 progStr = prog
60 else:
61 progStr = ' '.join(prog)
63 try:
64 pop = subprocess.Popen(prog,
65 shell = type(prog) is str,
66 stderr=subprocess.STDOUT,
67 stdout=subprocess.PIPE,
68 stdin=subprocess.PIPE)
69 except OSError, e:
70 debug("strerror: " + e.strerror)
71 raise ProgramError(progStr, e.strerror)
73 if input != None:
74 pop.stdin.write(input)
75 pop.stdin.close()
77 out = pop.stdout.read()
78 code = pop.wait()
79 if code not in expectedexits:
80 debug("error output: " + out)
81 raise ProgramError(progStr, out)
82 debug("output: " + out.replace('\0', '\n'))
83 return out
85 def debug(str):
86 if DEBUG:
87 print str