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