PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/Mac/Tools/fixapplepython23.py

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