Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 1 | """fixapplepython23 - Fix Apple-installed Python 2.3 (on Mac OS X 10.3) |
| 2 | |
| 3 | Python 2.3 (and 2.3.X for X<5) have the problem that building an extension |
| 4 | for a framework installation may accidentally pick up the framework |
| 5 | of a newer Python, in stead of the one that was used to build the extension. |
| 6 | |
| 7 | This script modifies the Makefile (in .../lib/python2.3/config) to use |
| 8 | the newer method of linking extensions with "-undefined dynamic_lookup" |
| 9 | which fixes this problem. |
| 10 | |
| 11 | The script will first check all prerequisites, and return a zero exit |
| 12 | status also when nothing needs to be fixed. |
| 13 | """ |
| 14 | import sys |
| 15 | import os |
| 16 | import gestalt |
| 17 | |
| 18 | MAKEFILE='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/Makefile' |
Jack Jansen | 6458598 | 2005-01-01 22:33:36 +0000 | [diff] [blame] | 19 | CHANGES=(( |
| 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 | |
| 33 | GCC_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc' |
| 34 | GXX_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++' |
| 35 | SCRIPT="""#!/bin/sh |
| 36 | export MACOSX_DEPLOYMENT_TARGET=10.3 |
| 37 | exec %s "${@}" |
| 38 | """ |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 39 | |
| 40 | def 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 Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 46 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 47 | def fix(makefile, do_apply): |
| 48 | """Fix the Makefile, if required.""" |
| 49 | fixed = False |
| 50 | lines = open(makefile).readlines() |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 51 | |
Jack Jansen | 6458598 | 2005-01-01 22:33:36 +0000 | [diff] [blame] | 52 | 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 Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 63 | fixed = True |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 64 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 65 | 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 Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 77 | |
Jack Jansen | 6458598 | 2005-01-01 22:33:36 +0000 | [diff] [blame] | 78 | def 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 Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 88 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 89 | def 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 Jansen | 6458598 | 2005-01-01 22:33:36 +0000 | [diff] [blame] | 107 | # 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 Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 114 | rv = fix(MAKEFILE, do_apply) |
| 115 | sys.exit(rv) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 116 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 117 | if __name__ == '__main__': |
| 118 | main() |