2 """fixapplepython23 - Fix Apple-installed Python 2.3 (on Mac OS X 10.3)
4 Python 2.3 (and 2.3.X for X<5) have the problem that building an extension
5 for a framework installation may accidentally pick up the framework
6 of a newer Python, in stead of the one that was used to build the extension.
8 This script modifies the Makefile (in .../lib/python2.3/config) to use
9 the newer method of linking extensions with "-undefined dynamic_lookup"
10 which fixes this problem.
12 The script will first check all prerequisites, and return a zero exit
13 status also when nothing needs to be fixed.
19 MAKEFILE
='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/Makefile'
21 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n',
22 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
24 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n',
25 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
28 'CC=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc\n'
31 'CXX=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++\n'
34 GCC_SCRIPT
='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc'
35 GXX_SCRIPT
='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++'
37 export MACOSX_DEPLOYMENT_TARGET=10.3
41 def findline(lines
, start
):
42 """return line starting with given string or -1"""
43 for i
in range(len(lines
)):
44 if lines
[i
][:len(start
)] == start
:
48 def fix(makefile
, do_apply
):
49 """Fix the Makefile, if required."""
51 lines
= open(makefile
).readlines()
53 for old
, new
in CHANGES
:
54 i
= findline(lines
, new
)
58 i
= findline(lines
, old
)
60 print 'fixapplepython23: Python installation not fixed (appears broken)'
61 print 'fixapplepython23: missing line:', old
68 print 'fixapplepython23: Fix to Apple-installed Python 2.3 applied'
69 os
.rename(makefile
, makefile
+ '~')
70 open(makefile
, 'w').writelines(lines
)
73 print 'fixapplepython23: Fix to Apple-installed Python 2.3 should be applied'
76 print 'fixapplepython23: No fix needed, appears to have been applied before'
79 def makescript(filename
, compiler
):
80 """Create a wrapper script for a compiler"""
81 dirname
= os
.path
.split(filename
)[0]
82 if not os
.access(dirname
, os
.X_OK
):
83 os
.mkdir(dirname
, 0755)
84 fp
= open(filename
, 'w')
85 fp
.write(SCRIPT
% compiler
)
87 os
.chmod(filename
, 0755)
88 print 'fixapplepython23: Created', filename
92 if len(sys
.argv
) > 1 and sys
.argv
[1] == '-n':
96 # First check OS version
97 if sys
.byteorder
== 'little':
98 # All intel macs are fine
99 print "fixapplypython23: no fix is needed on MacOSX on Intel"
102 if gestalt
.gestalt('sysv') < 0x1030:
103 print 'fixapplepython23: no fix needed on MacOSX < 10.3'
106 if gestalt
.gestalt('sysv') >= 0x1040:
107 print 'fixapplepython23: no fix needed on MacOSX >= 10.4'
110 # Test that a framework Python is indeed installed
111 if not os
.path
.exists(MAKEFILE
):
112 print 'fixapplepython23: Python framework does not appear to be installed (?), nothing fixed'
114 # Check that we can actually write the file
115 if do_apply
and not os
.access(MAKEFILE
, os
.W_OK
):
116 print 'fixapplepython23: No write permission, please run with "sudo"'
118 # Create the shell scripts
120 if not os
.access(GCC_SCRIPT
, os
.X_OK
):
121 makescript(GCC_SCRIPT
, "gcc")
122 if not os
.access(GXX_SCRIPT
, os
.X_OK
):
123 makescript(GXX_SCRIPT
, "g++")
124 # Finally fix the makefile
125 rv
= fix(MAKEFILE
, do_apply
)
129 if __name__
== '__main__':