| 1 | n/a | #!/usr/bin/python |
|---|
| 2 | n/a | """fixapplepython23 - Fix Apple-installed Python 2.3 (on Mac OS X 10.3) |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | Python 2.3 (and 2.3.X for X<5) have the problem that building an extension |
|---|
| 5 | n/a | for a framework installation may accidentally pick up the framework |
|---|
| 6 | n/a | of a newer Python, in stead of the one that was used to build the extension. |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | This script modifies the Makefile (in .../lib/python2.3/config) to use |
|---|
| 9 | n/a | the newer method of linking extensions with "-undefined dynamic_lookup" |
|---|
| 10 | n/a | which fixes this problem. |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | The script will first check all prerequisites, and return a zero exit |
|---|
| 13 | n/a | status also when nothing needs to be fixed. |
|---|
| 14 | n/a | """ |
|---|
| 15 | n/a | import sys |
|---|
| 16 | n/a | import os |
|---|
| 17 | n/a | import platform |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | MAKEFILE='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/Makefile' |
|---|
| 20 | n/a | CHANGES=(( |
|---|
| 21 | n/a | 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n', |
|---|
| 22 | n/a | 'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n' |
|---|
| 23 | n/a | ),( |
|---|
| 24 | n/a | 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n', |
|---|
| 25 | n/a | 'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n' |
|---|
| 26 | n/a | ),( |
|---|
| 27 | n/a | 'CC=\t\tgcc\n', |
|---|
| 28 | n/a | 'CC=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc\n' |
|---|
| 29 | n/a | ),( |
|---|
| 30 | n/a | 'CXX=\t\tc++\n', |
|---|
| 31 | n/a | 'CXX=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++\n' |
|---|
| 32 | n/a | )) |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | GCC_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc' |
|---|
| 35 | n/a | GXX_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++' |
|---|
| 36 | n/a | SCRIPT="""#!/bin/sh |
|---|
| 37 | n/a | export MACOSX_DEPLOYMENT_TARGET=10.3 |
|---|
| 38 | n/a | exec %s "${@}" |
|---|
| 39 | n/a | """ |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | def findline(lines, start): |
|---|
| 42 | n/a | """return line starting with given string or -1""" |
|---|
| 43 | n/a | for i in range(len(lines)): |
|---|
| 44 | n/a | if lines[i][:len(start)] == start: |
|---|
| 45 | n/a | return i |
|---|
| 46 | n/a | return -1 |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | def fix(makefile, do_apply): |
|---|
| 49 | n/a | """Fix the Makefile, if required.""" |
|---|
| 50 | n/a | fixed = False |
|---|
| 51 | n/a | lines = open(makefile).readlines() |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | for old, new in CHANGES: |
|---|
| 54 | n/a | i = findline(lines, new) |
|---|
| 55 | n/a | if i >= 0: |
|---|
| 56 | n/a | # Already fixed |
|---|
| 57 | n/a | continue |
|---|
| 58 | n/a | i = findline(lines, old) |
|---|
| 59 | n/a | if i < 0: |
|---|
| 60 | n/a | print('fixapplepython23: Python installation not fixed (appears broken)') |
|---|
| 61 | n/a | print('fixapplepython23: missing line:', old) |
|---|
| 62 | n/a | return 2 |
|---|
| 63 | n/a | lines[i] = new |
|---|
| 64 | n/a | fixed = True |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | if fixed: |
|---|
| 67 | n/a | if do_apply: |
|---|
| 68 | n/a | print('fixapplepython23: Fix to Apple-installed Python 2.3 applied') |
|---|
| 69 | n/a | os.rename(makefile, makefile + '~') |
|---|
| 70 | n/a | open(makefile, 'w').writelines(lines) |
|---|
| 71 | n/a | return 0 |
|---|
| 72 | n/a | else: |
|---|
| 73 | n/a | print('fixapplepython23: Fix to Apple-installed Python 2.3 should be applied') |
|---|
| 74 | n/a | return 1 |
|---|
| 75 | n/a | else: |
|---|
| 76 | n/a | print('fixapplepython23: No fix needed, appears to have been applied before') |
|---|
| 77 | n/a | return 0 |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def makescript(filename, compiler): |
|---|
| 80 | n/a | """Create a wrapper script for a compiler""" |
|---|
| 81 | n/a | dirname = os.path.split(filename)[0] |
|---|
| 82 | n/a | if not os.access(dirname, os.X_OK): |
|---|
| 83 | n/a | os.mkdir(dirname, 0o755) |
|---|
| 84 | n/a | fp = open(filename, 'w') |
|---|
| 85 | n/a | fp.write(SCRIPT % compiler) |
|---|
| 86 | n/a | fp.close() |
|---|
| 87 | n/a | os.chmod(filename, 0o755) |
|---|
| 88 | n/a | print('fixapplepython23: Created', filename) |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | def main(): |
|---|
| 91 | n/a | # Check for -n option |
|---|
| 92 | n/a | if len(sys.argv) > 1 and sys.argv[1] == '-n': |
|---|
| 93 | n/a | do_apply = False |
|---|
| 94 | n/a | else: |
|---|
| 95 | n/a | do_apply = True |
|---|
| 96 | n/a | # First check OS version |
|---|
| 97 | n/a | if sys.byteorder == 'little': |
|---|
| 98 | n/a | # All intel macs are fine |
|---|
| 99 | n/a | print("fixapplypython23: no fix is needed on MacOSX on Intel") |
|---|
| 100 | n/a | sys.exit(0) |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | osver = platform.mac_ver() |
|---|
| 103 | n/a | if osver != '10.3' and os.ver < '10.3.': |
|---|
| 104 | n/a | print('fixapplepython23: no fix needed on MacOSX < 10.3') |
|---|
| 105 | n/a | sys.exit(0) |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | if osver >= '10.4': |
|---|
| 108 | n/a | print('fixapplepython23: no fix needed on MacOSX >= 10.4') |
|---|
| 109 | n/a | sys.exit(0) |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | # Test that a framework Python is indeed installed |
|---|
| 112 | n/a | if not os.path.exists(MAKEFILE): |
|---|
| 113 | n/a | print('fixapplepython23: Python framework does not appear to be installed (?), nothing fixed') |
|---|
| 114 | n/a | sys.exit(0) |
|---|
| 115 | n/a | # Check that we can actually write the file |
|---|
| 116 | n/a | if do_apply and not os.access(MAKEFILE, os.W_OK): |
|---|
| 117 | n/a | print('fixapplepython23: No write permission, please run with "sudo"') |
|---|
| 118 | n/a | sys.exit(2) |
|---|
| 119 | n/a | # Create the shell scripts |
|---|
| 120 | n/a | if do_apply: |
|---|
| 121 | n/a | if not os.access(GCC_SCRIPT, os.X_OK): |
|---|
| 122 | n/a | makescript(GCC_SCRIPT, "gcc") |
|---|
| 123 | n/a | if not os.access(GXX_SCRIPT, os.X_OK): |
|---|
| 124 | n/a | makescript(GXX_SCRIPT, "g++") |
|---|
| 125 | n/a | # Finally fix the makefile |
|---|
| 126 | n/a | rv = fix(MAKEFILE, do_apply) |
|---|
| 127 | n/a | #sys.exit(rv) |
|---|
| 128 | n/a | sys.exit(0) |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | if __name__ == '__main__': |
|---|
| 131 | n/a | main() |
|---|