patch_modpython.py

changeset 0
de9c2efb9d02
child 12
1d8dd9706f46
equal deleted inserted replaced
-1:000000000000 0:de9c2efb9d02
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2003-2009 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5 # This is a script to patch mod_python for eric4.
6
7 """
8 Script to patch mod_python for usage with the eric4 IDE.
9 """
10
11 import sys
12 import os
13 import shutil
14 import py_compile
15 import distutils.sysconfig
16
17 # Define the globals.
18 progName = None
19 modDir = None
20
21 def usage(rcode = 2):
22 """
23 Display a usage message and exit.
24
25 rcode is the return code passed back to the calling process.
26 """
27 global progName, modDir
28
29 print "Usage:"
30 print " %s [-h] [-d dir]" % (progName)
31 print "where:"
32 print " -h display this help message"
33 print " -d dir where Mod_python files are installed [default %s]" % \
34 (modDir)
35 print
36 print "This script patches the file apache.py of the Mod_python distribution"
37 print "so that it will work with the eric4 debugger instead of pdb."
38 print "Please see mod_python.html for more details."
39 print
40
41 sys.exit(rcode)
42
43
44 def initGlobals():
45 """
46 Sets the values of globals that need more than a simple assignment.
47 """
48 global modDir
49
50 modDir = os.path.join(distutils.sysconfig.get_python_lib(True), "mod_python")
51
52 def main(argv):
53 """The main function of the script.
54
55 argv is the list of command line arguments.
56 """
57 import getopt
58
59 # Parse the command line.
60 global progName, modDir
61 progName = os.path.basename(argv[0])
62
63 initGlobals()
64
65 try:
66 optlist, args = getopt.getopt(argv[1:],"hd:")
67 except getopt.GetoptError:
68 usage()
69
70 for opt, arg in optlist:
71 if opt == "-h":
72 usage(0)
73 elif opt == "-d":
74 global modDir
75 modDir = arg
76
77 try:
78 filename = os.path.join(modDir, "apache.py")
79 f = open(filename, "r")
80 except EnvironmentError:
81 print "The file %s does not exist. Aborting." % filename
82 sys.exit(1)
83
84 lines = f.readlines()
85 f.close()
86
87 pdbFound = False
88 ericFound = False
89
90 sn = "apache.py"
91 s = open(sn, "w")
92 for line in lines:
93 if not pdbFound and line.startswith("import pdb"):
94 s.write("import eric4.DebugClients.Python.eric4dbgstub as pdb\n")
95 pdbFound = True
96 else:
97 s.write(line)
98 if line.startswith("import eric4"):
99 ericFound = True
100
101 if not ericFound:
102 s.write("\n")
103 s.write('def initDebugger(name):\n')
104 s.write(' """\n')
105 s.write(' Initialize the debugger and set the script name to be reported \n')
106 s.write(' by the debugger. This is a patch for eric4.\n')
107 s.write(' """\n')
108 s.write(' if not pdb.initDebugger("standard"):\n')
109 s.write(' raise ImportError("Could not initialize debugger")\n')
110 s.write(' pdb.setScriptname(name)\n')
111 s.write("\n")
112 s.close()
113
114 if ericFound:
115 print "Mod_python is already patched for eric4."
116 os.remove(sn)
117 else:
118 try:
119 py_compile.compile(sn)
120 except py_compile.PyCompileError, e:
121 print "Error compiling %s. Aborting" % sn
122 print e
123 os.remove(sn)
124 sys.exit(1)
125 except SyntaxError, e:
126 print "Error compiling %s. Aborting" % sn
127 print e
128 os.remove(sn)
129 sys.exit(1)
130
131 shutil.copy(os.path.join(modDir, "apache.py"),
132 os.path.join(modDir, "apache.py.orig"))
133 shutil.copy(sn, modDir)
134 os.remove(sn)
135 if os.path.exists("%sc" % sn):
136 shutil.copy("%sc" % sn, modDir)
137 os.remove("%sc" % sn)
138 if os.path.exists("%so" % sn):
139 shutil.copy("%so" % sn, modDir)
140 os.remove("%so" % sn)
141
142 print "Mod_python patched successfully."
143 print "Unpatched file copied to %s." % os.path.join(modDir, "apache.py.orig")
144
145
146 if __name__ == "__main__":
147 try:
148 main(sys.argv)
149 except SystemExit:
150 raise
151 except:
152 print \
153 """An internal error occured. Please report all the output of the program,
154 including the following traceback, to eric-bugs@die-offenbachs.de.
155 """
156 raise
157

eric ide

mercurial