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