Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 2 | """fixapplepython23 - Fix Apple-installed Python 2.3 (on Mac OS X 10.3) |
| 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. |
| 7 | |
| 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. |
| 11 | |
| 12 | The script will first check all prerequisites, and return a zero exit |
| 13 | status also when nothing needs to be fixed. |
| 14 | """ |
| 15 | import sys |
| 16 | import os |
Benjamin Peterson | de9c869 | 2008-07-01 18:23:09 +0000 | [diff] [blame] | 17 | import gestalt as _gestalt |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 18 | |
| 19 | 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] | 20 | CHANGES=(( |
| 21 | 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n', |
| 22 | 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n' |
| 23 | ),( |
| 24 | 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n', |
| 25 | 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n' |
| 26 | ),( |
| 27 | 'CC=\t\tgcc\n', |
| 28 | 'CC=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc\n' |
| 29 | ),( |
| 30 | 'CXX=\t\tc++\n', |
| 31 | 'CXX=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++\n' |
| 32 | )) |
| 33 | |
| 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++' |
| 36 | SCRIPT="""#!/bin/sh |
| 37 | export MACOSX_DEPLOYMENT_TARGET=10.3 |
| 38 | exec %s "${@}" |
| 39 | """ |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 40 | |
| 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: |
| 45 | return i |
| 46 | return -1 |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 47 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 48 | def fix(makefile, do_apply): |
| 49 | """Fix the Makefile, if required.""" |
| 50 | fixed = False |
| 51 | lines = open(makefile).readlines() |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 52 | |
Jack Jansen | 6458598 | 2005-01-01 22:33:36 +0000 | [diff] [blame] | 53 | for old, new in CHANGES: |
| 54 | i = findline(lines, new) |
| 55 | if i >= 0: |
| 56 | # Already fixed |
| 57 | continue |
| 58 | i = findline(lines, old) |
| 59 | if i < 0: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 60 | print('fixapplepython23: Python installation not fixed (appears broken)') |
| 61 | print('fixapplepython23: missing line:', old) |
Jack Jansen | 6458598 | 2005-01-01 22:33:36 +0000 | [diff] [blame] | 62 | return 2 |
| 63 | lines[i] = new |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 64 | fixed = True |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 65 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 66 | if fixed: |
| 67 | if do_apply: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 68 | print('fixapplepython23: Fix to Apple-installed Python 2.3 applied') |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 69 | os.rename(makefile, makefile + '~') |
| 70 | open(makefile, 'w').writelines(lines) |
| 71 | return 0 |
| 72 | else: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 73 | print('fixapplepython23: Fix to Apple-installed Python 2.3 should be applied') |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 74 | return 1 |
| 75 | else: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 76 | print('fixapplepython23: No fix needed, appears to have been applied before') |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 77 | return 0 |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 78 | |
Jack Jansen | 6458598 | 2005-01-01 22:33:36 +0000 | [diff] [blame] | 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): |
Georg Brandl | c76473d | 2007-09-03 07:27:49 +0000 | [diff] [blame] | 83 | os.mkdir(dirname, 0o755) |
Jack Jansen | 6458598 | 2005-01-01 22:33:36 +0000 | [diff] [blame] | 84 | fp = open(filename, 'w') |
| 85 | fp.write(SCRIPT % compiler) |
| 86 | fp.close() |
Georg Brandl | c76473d | 2007-09-03 07:27:49 +0000 | [diff] [blame] | 87 | os.chmod(filename, 0o755) |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 88 | print('fixapplepython23: Created', filename) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 89 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 90 | def main(): |
| 91 | # Check for -n option |
| 92 | if len(sys.argv) > 1 and sys.argv[1] == '-n': |
| 93 | do_apply = False |
| 94 | else: |
| 95 | do_apply = True |
| 96 | # First check OS version |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 97 | if sys.byteorder == 'little': |
| 98 | # All intel macs are fine |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 99 | print("fixapplypython23: no fix is needed on MacOSX on Intel") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 100 | sys.exit(0) |
| 101 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 102 | if gestalt.gestalt('sysv') < 0x1030: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 103 | print('fixapplepython23: no fix needed on MacOSX < 10.3') |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 104 | sys.exit(0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 105 | |
| 106 | if gestalt.gestalt('sysv') >= 0x1040: |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 107 | print('fixapplepython23: no fix needed on MacOSX >= 10.4') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 108 | sys.exit(0) |
| 109 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 110 | # Test that a framework Python is indeed installed |
| 111 | if not os.path.exists(MAKEFILE): |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 112 | print('fixapplepython23: Python framework does not appear to be installed (?), nothing fixed') |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 113 | sys.exit(0) |
| 114 | # Check that we can actually write the file |
| 115 | if do_apply and not os.access(MAKEFILE, os.W_OK): |
Collin Winter | e7bf59f | 2007-08-30 18:39:28 +0000 | [diff] [blame] | 116 | print('fixapplepython23: No write permission, please run with "sudo"') |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 117 | sys.exit(2) |
Jack Jansen | 6458598 | 2005-01-01 22:33:36 +0000 | [diff] [blame] | 118 | # Create the shell scripts |
| 119 | if do_apply: |
| 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 |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 125 | rv = fix(MAKEFILE, do_apply) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 126 | #sys.exit(rv) |
| 127 | sys.exit(0) |
Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 128 | |
Jack Jansen | 6116f07 | 2004-12-26 23:02:05 +0000 | [diff] [blame] | 129 | if __name__ == '__main__': |
| 130 | main() |