Diff exits with status code 2 if it is given binary files.
[hgct.git] / ctcore.py
blobd0d87b4a194cbe06862d42ccab0cc2ddd822eeac
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 executeMain = True
23 _settings = None
25 def settings():
26 global _settings
27 if not _settings:
28 from settings import Settings
29 _settings = Settings()
30 return _settings
32 DEBUG = 0
34 # PyQt3 and python 2.4 isn't currently available together on
35 # Debian/testing we do therefore use the following quite ugly work
36 # around. The module 'mysubprocess' is just a copy of the 'subprocess'
37 # module from the Python 2.4 distribution.
38 ver = sys.version_info
39 if ver[0] < 2 or (ver[0] == 2 and ver[1] <= 3):
40 import mysubprocess
41 subprocess = mysubprocess
42 else:
43 import subprocess
45 class File:
46 def __init__(self):
47 self.listViewItem = None
49 class ProgramError(Exception):
50 def __init__(self, progStr, error):
51 self.progStr = progStr
52 self.error = error
54 def __str__(self):
55 return self.progStr + ': ' + self.error
57 def runProgram(prog, input=None, expectedexits=[0]):
58 debug('runProgram prog: ' + str(prog) + " input: " + str(input))
59 if type(prog) is str:
60 progStr = prog
61 else:
62 progStr = ' '.join(prog)
64 try:
65 pop = subprocess.Popen(prog,
66 shell = type(prog) is str,
67 stderr=subprocess.STDOUT,
68 stdout=subprocess.PIPE,
69 stdin=subprocess.PIPE)
70 except OSError, e:
71 debug("strerror: " + e.strerror)
72 raise ProgramError(progStr, e.strerror)
74 if input != None:
75 pop.stdin.write(input)
76 pop.stdin.close()
78 out = pop.stdout.read()
79 code = pop.wait()
80 if code not in expectedexits:
81 debug("error output: " + out)
82 raise ProgramError(progStr, out)
83 debug("output: " + out.replace('\0', '\n'))
84 return out
86 def debug(str):
87 if DEBUG:
88 print str