ยปCore Development>Code coverage>Mac/Tools/fixapplepython23.py

Python code coverage for Mac/Tools/fixapplepython23.py

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