|
| 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' |
| 19 | +OLD_LDSHARED='LDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n' |
| 20 | +OLD_BLDSHARED='B' + OLD_LDSHARED |
| 21 | +NEW_LDSHARED='LDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n' |
| 22 | +NEW_BLDSHARED='B' + NEW_LDSHARED |
| 23 | + |
| 24 | +def 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 | + |
| 31 | +def 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 | + |
| 71 | +def 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 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
| 95 | + |
0 commit comments