blob: 181181ea79fc10c719ac0c5a63b02c3b01bb04eb [file] [log] [blame]
Jack Jansen6116f072004-12-26 23:02:05 +00001"""fixapplepython23 - Fix Apple-installed Python 2.3 (on Mac OS X 10.3)
2
3Python 2.3 (and 2.3.X for X<5) have the problem that building an extension
4for a framework installation may accidentally pick up the framework
5of a newer Python, in stead of the one that was used to build the extension.
6
7This script modifies the Makefile (in .../lib/python2.3/config) to use
8the newer method of linking extensions with "-undefined dynamic_lookup"
9which fixes this problem.
10
11The script will first check all prerequisites, and return a zero exit
12status also when nothing needs to be fixed.
13"""
14import sys
15import os
16import gestalt
17
18MAKEFILE='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/Makefile'
Jack Jansen64585982005-01-01 22:33:36 +000019CHANGES=((
20 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n',
21 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
22 ),(
23 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n',
24 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
25 ),(
26 'CC=\t\tgcc\n',
27 'CC=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc\n'
28 ),(
29 'CXX=\t\tc++\n',
30 'CXX=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++\n'
31))
32
33GCC_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc'
34GXX_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++'
35SCRIPT="""#!/bin/sh
36export MACOSX_DEPLOYMENT_TARGET=10.3
37exec %s "${@}"
38"""
Jack Jansen6116f072004-12-26 23:02:05 +000039
40def findline(lines, start):
41 """return line starting with given string or -1"""
42 for i in range(len(lines)):
43 if lines[i][:len(start)] == start:
44 return i
45 return -1
Tim Peters5a9fb3c2005-01-07 16:01:32 +000046
Jack Jansen6116f072004-12-26 23:02:05 +000047def fix(makefile, do_apply):
48 """Fix the Makefile, if required."""
49 fixed = False
50 lines = open(makefile).readlines()
Tim Peters5a9fb3c2005-01-07 16:01:32 +000051
Jack Jansen64585982005-01-01 22:33:36 +000052 for old, new in CHANGES:
53 i = findline(lines, new)
54 if i >= 0:
55 # Already fixed
56 continue
57 i = findline(lines, old)
58 if i < 0:
59 print 'fixapplepython23: Python installation not fixed (appears broken)'
60 print 'fixapplepython23: missing line:', old
61 return 2
62 lines[i] = new
Jack Jansen6116f072004-12-26 23:02:05 +000063 fixed = True
Tim Peters5a9fb3c2005-01-07 16:01:32 +000064
Jack Jansen6116f072004-12-26 23:02:05 +000065 if fixed:
66 if do_apply:
67 print 'fixapplepython23: Fix to Apple-installed Python 2.3 applied'
68 os.rename(makefile, makefile + '~')
69 open(makefile, 'w').writelines(lines)
70 return 0
71 else:
72 print 'fixapplepython23: Fix to Apple-installed Python 2.3 should be applied'
73 return 1
74 else:
75 print 'fixapplepython23: No fix needed, appears to have been applied before'
76 return 0
Tim Peters5a9fb3c2005-01-07 16:01:32 +000077
Jack Jansen64585982005-01-01 22:33:36 +000078def makescript(filename, compiler):
79 """Create a wrapper script for a compiler"""
80 dirname = os.path.split(filename)[0]
81 if not os.access(dirname, os.X_OK):
82 os.mkdir(dirname, 0755)
83 fp = open(filename, 'w')
84 fp.write(SCRIPT % compiler)
85 fp.close()
86 os.chmod(filename, 0755)
87 print 'fixapplepython23: Created', filename
Tim Peters5a9fb3c2005-01-07 16:01:32 +000088
Jack Jansen6116f072004-12-26 23:02:05 +000089def main():
90 # Check for -n option
91 if len(sys.argv) > 1 and sys.argv[1] == '-n':
92 do_apply = False
93 else:
94 do_apply = True
95 # First check OS version
96 if gestalt.gestalt('sysv') < 0x1030:
97 print 'fixapplepython23: no fix needed on MacOSX < 10.3'
98 sys.exit(0)
99 # Test that a framework Python is indeed installed
100 if not os.path.exists(MAKEFILE):
101 print 'fixapplepython23: Python framework does not appear to be installed (?), nothing fixed'
102 sys.exit(0)
103 # Check that we can actually write the file
104 if do_apply and not os.access(MAKEFILE, os.W_OK):
105 print 'fixapplepython23: No write permission, please run with "sudo"'
106 sys.exit(2)
Jack Jansen64585982005-01-01 22:33:36 +0000107 # Create the shell scripts
108 if do_apply:
109 if not os.access(GCC_SCRIPT, os.X_OK):
110 makescript(GCC_SCRIPT, "gcc")
111 if not os.access(GXX_SCRIPT, os.X_OK):
112 makescript(GXX_SCRIPT, "g++")
113 # Finally fix the makefile
Jack Jansen6116f072004-12-26 23:02:05 +0000114 rv = fix(MAKEFILE, do_apply)
115 sys.exit(rv)
Tim Peters5a9fb3c2005-01-07 16:01:32 +0000116
Jack Jansen6116f072004-12-26 23:02:05 +0000117if __name__ == '__main__':
118 main()