blob: c2b1f4ebe92464cc863bd1a1403a0bf3c16b41d5 [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'
19OLD_LDSHARED='LDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n'
20OLD_BLDSHARED='B' + OLD_LDSHARED
Jack Jansen935ca102004-12-28 21:30:35 +000021NEW_LDSHARED='LDSHARED=\tenv MACOSX_DEPLOYMENT_TARGET=10.3 $(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
Jack Jansen6116f072004-12-26 23:02:05 +000022NEW_BLDSHARED='B' + NEW_LDSHARED
23
24def findline(lines, start):
25 """return line starting with given string or -1"""
26 for i in range(len(lines)):
27 if lines[i][:len(start)] == start:
28 return i
29 return -1
30
31def fix(makefile, do_apply):
32 """Fix the Makefile, if required."""
33 fixed = False
34 lines = open(makefile).readlines()
35
36 i = findline(lines, 'LDSHARED=')
37 if i < 0:
38 print 'fixapplepython23: Python installation not fixed (appears broken, no LDSHARED)'
39 return 2
40 if lines[i] == OLD_LDSHARED:
41 lines[i] = NEW_LDSHARED
42 fixed = True
43 elif lines[i] != NEW_LDSHARED:
44 print 'fixapplepython23: Python installation not fixed (appears modified, unexpected LDSHARED)'
45 return 2
46
47 i = findline(lines, 'BLDSHARED=')
48 if i < 0:
49 print 'fixapplepython23: Python installation not fixed (appears broken, no BLDSHARED)'
50 return 2
51 if lines[i] == OLD_BLDSHARED:
52 lines[i] = NEW_BLDSHARED
53 fixed = True
54 elif lines[i] != NEW_BLDSHARED:
55 print 'fixapplepython23: Python installation not fixed (appears modified, unexpected BLDSHARED)'
56 return 2
57
58 if fixed:
59 if do_apply:
60 print 'fixapplepython23: Fix to Apple-installed Python 2.3 applied'
61 os.rename(makefile, makefile + '~')
62 open(makefile, 'w').writelines(lines)
63 return 0
64 else:
65 print 'fixapplepython23: Fix to Apple-installed Python 2.3 should be applied'
66 return 1
67 else:
68 print 'fixapplepython23: No fix needed, appears to have been applied before'
69 return 0
70
71def main():
72 # Check for -n option
73 if len(sys.argv) > 1 and sys.argv[1] == '-n':
74 do_apply = False
75 else:
76 do_apply = True
77 # First check OS version
78 if gestalt.gestalt('sysv') < 0x1030:
79 print 'fixapplepython23: no fix needed on MacOSX < 10.3'
80 sys.exit(0)
81 # Test that a framework Python is indeed installed
82 if not os.path.exists(MAKEFILE):
83 print 'fixapplepython23: Python framework does not appear to be installed (?), nothing fixed'
84 sys.exit(0)
85 # Check that we can actually write the file
86 if do_apply and not os.access(MAKEFILE, os.W_OK):
87 print 'fixapplepython23: No write permission, please run with "sudo"'
88 sys.exit(2)
89 # And finally fix it
90 rv = fix(MAKEFILE, do_apply)
91 sys.exit(rv)
92
93if __name__ == '__main__':
94 main()
95