File: patch_modpython.py

package info (click to toggle)
eric 3.6.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,308 kB
  • ctags: 8,668
  • sloc: python: 52,713; sh: 265; makefile: 47
file content (150 lines) | stat: -rw-r--r-- 4,059 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-

# Copyright (c) 2003-2005 Detlev Offenbach <detlev@die-offenbachs.de>
#
# This is a  script to patch Mod_python for eric3. 

"""
Script to patch mod_python for usage with the eric3 IDE.
"""

import sys
import os
import shutil
import py_compile
import distutils.sysconfig

# Define the globals.
progName = None
modDir = None

def usage(rcode = 2):
    """
    Display a usage message and exit.

    rcode is the return code passed back to the calling process.
    """
    global progName, modDir
    
    print "Usage:"
    print "    %s [-h] [-d dir]" % (progName)
    print "where:"
    print "    -h             display this help message"
    print "    -d dir         where Mod_python files are installed [default %s]" % (modDir)
    print
    print "This script patches the file apache.py of the Mod_python distribution"
    print "so that it will work with the eric3 debugger instead of pdb."
    print "Please see mod_python.html for more details."
    print

    sys.exit(rcode)


def initGlobals():
    """
    Sets the values of globals that need more than a simple assignment.
    """
    global modDir

    modDir = os.path.join(distutils.sysconfig.get_python_lib(), "mod_python")

def main(argv):
    """The main function of the script.

    argv is the list of command line arguments.
    """
    import getopt

    # Parse the command line.
    global progName, modDir
    progName = os.path.basename(argv[0])

    initGlobals()

    try:
        optlist, args = getopt.getopt(argv[1:],"hd:")
    except getopt.GetoptError:
        usage()

    for opt, arg in optlist:
        if opt == "-h":
            usage(0)
        elif opt == "-d":
            global modDir
            modDir = arg
            
    try:
        f = open(os.path.join(modDir, "apache.py"), "r")
    except:
        print "The file %s does not exist. Aborting."
        sys.exit(1)
        
    lines = f.readlines()
    f.close()
    
    pdbFound = 0
    ericFound = 0
    
    sn = "apache.py"
    s = open(sn, "w")
    for line in lines:
        if not pdbFound and line.startswith("import pdb"):
            s.write("import eric3.Debugger.eric3dbgstub as pdb\n")
            pdbFound = 1
        else:
            s.write(line)
            if line.startswith("import eric3"):
                ericFound = 1
                
    if not ericFound:
        s.write("\n")
        s.write('def initDebugger(name):\n')
        s.write('    """\n')
        s.write('    Initialize the debugger and set the script name to be reported \n')
        s.write('    by the debugger. This is a patch for eric3.\n')
        s.write('    """\n')
        s.write('    if not pdb.initDebugger("noqt"):\n')
        s.write('        raise ImportError("Could not initialize debugger")\n')
        s.write('    pdb.setScriptname(name)\n')
        s.write("\n")
    s.close()
    
    if ericFound:
        print "Mod_python is already patched for eric3."
        os.remove(sn)
    else:
        try:
            py_compile.compile(sn)
        except py_compile.PyCompileError, e:
            print "Error compiling %s. Aborting" % sn
            print e
            os.remove(sn)
            sys.exit(1)
            
        shutil.copy(os.path.join(modDir, "apache.py"),
                    os.path.join(modDir, "apache.py.orig"))
        shutil.copy(sn, modDir)
        os.remove(sn)
        if os.path.exists("%sc" % sn):
            shutil.copy("%sc" % sn, modDir)
            os.remove("%sc" % sn)
        if os.path.exists("%so" % sn):
            shutil.copy("%so" % sn, modDir)
            os.remove("%so" % sn)
            
        print "Mod_python patched successfully."
        print "Unpatched file copied to %s." % os.path.join(modDir, "apache.py.orig")
    
    
if __name__ == "__main__":
    try:
        main(sys.argv)
    except SystemExit:
        raise
    except:
        print \
"""An internal error occured.  Please report all the output of the program,
including the following traceback, to eric-bugs@die-offenbachs.de.
"""
        raise