Bundle-python

Version 6 (Saúl Ibarra Corretgé, 05/27/2013 02:38 pm)

1 1 Saúl Ibarra Corretgé
h1. Building a Python Framework to bundle inside Blink
2 1 Saúl Ibarra Corretgé
3 1 Saúl Ibarra Corretgé
In order to avoid using the system Python a custom Framework build is needed. Using a bundled Python version will make the package bigger in size, but all package versions are controlled and not up to the environment. Also, we can use the latest Python version, with latest bugfixes and features, since Apple only updates the system Python version on every major OS release.
4 1 Saúl Ibarra Corretgé
5 1 Saúl Ibarra Corretgé
h2. Building the Python Framework itself
6 1 Saúl Ibarra Corretgé
7 1 Saúl Ibarra Corretgé
* Download the desired Python version, at the time of this writing, 2.7.5
8 1 Saúl Ibarra Corretgé
9 1 Saúl Ibarra Corretgé
<pre>
10 1 Saúl Ibarra Corretgé
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
11 1 Saúl Ibarra Corretgé
</pre>
12 1 Saúl Ibarra Corretgé
13 1 Saúl Ibarra Corretgé
* Uncompress and get ready to compile
14 1 Saúl Ibarra Corretgé
15 1 Saúl Ibarra Corretgé
<pre>
16 1 Saúl Ibarra Corretgé
tar jxvf Python-2.7.5.tar.bz2
17 1 Saúl Ibarra Corretgé
cd Python-2.7.5
18 1 Saúl Ibarra Corretgé
</pre>
19 1 Saúl Ibarra Corretgé
20 1 Saúl Ibarra Corretgé
* Create a temporary directory for the build result
21 1 Saúl Ibarra Corretgé
22 1 Saúl Ibarra Corretgé
<pre>
23 1 Saúl Ibarra Corretgé
mkdir -p /tmp/py
24 1 Saúl Ibarra Corretgé
</pre>
25 1 Saúl Ibarra Corretgé
26 1 Saúl Ibarra Corretgé
* Compile Python (Framework build) in 32 bits mode and with compatibility for OSX >= 10.6
27 1 Saúl Ibarra Corretgé
28 1 Saúl Ibarra Corretgé
<pre>
29 1 Saúl Ibarra Corretgé
./configure --prefix=/tmp/py --enable-framework=/tmp/py MACOSX_DEPLOYMENT_TARGET=10.6 ARCHFLAGS="-arch i386" CFLAGS="-arch i386" CXXFLAGS="-arch i386" LDFLAGS="-arch i386"
30 1 Saúl Ibarra Corretgé
make
31 1 Saúl Ibarra Corretgé
make install
32 1 Saúl Ibarra Corretgé
</pre>
33 1 Saúl Ibarra Corretgé
34 1 Saúl Ibarra Corretgé
The resulting framework will be located in /tmp/py
35 1 Saúl Ibarra Corretgé
36 1 Saúl Ibarra Corretgé
* Change the dynamic link target in the main binary file of the Python Framework
37 1 Saúl Ibarra Corretgé
38 1 Saúl Ibarra Corretgé
<pre>
39 1 Saúl Ibarra Corretgé
cd /tmp/py/Python.framework/Versions/2.7
40 1 Saúl Ibarra Corretgé
chmod +w Python
41 1 Saúl Ibarra Corretgé
install_name_tool -id @executable_path/../Frameworks/Python.framework/Versions/2.7/Python Python
42 1 Saúl Ibarra Corretgé
chmod -w Python
43 1 Saúl Ibarra Corretgé
</pre>
44 1 Saúl Ibarra Corretgé
45 3 Saúl Ibarra Corretgé
The framework is almos ready for inclusion on the project.
46 1 Saúl Ibarra Corretgé
47 2 Saúl Ibarra Corretgé
*NOTE*: Be careful when copying the framework around, it contains symlinks and if @cp -r@ is used the size will we doubled, use @cp -RH@ instead.
48 2 Saúl Ibarra Corretgé
49 1 Saúl Ibarra Corretgé
50 1 Saúl Ibarra Corretgé
* Reduce the size of the Python Framework:
51 1 Saúl Ibarra Corretgé
52 3 Saúl Ibarra Corretgé
There are a number of things that can be removed from the framework directory to make it smaller in size:
53 3 Saúl Ibarra Corretgé
54 3 Saúl Ibarra Corretgé
<pre>
55 3 Saúl Ibarra Corretgé
*.pyc
56 3 Saúl Ibarra Corretgé
*.pyo
57 3 Saúl Ibarra Corretgé
Versions/Current/Mac
58 3 Saúl Ibarra Corretgé
Versions/Current/bin
59 3 Saúl Ibarra Corretgé
Versions/Current/share
60 3 Saúl Ibarra Corretgé
Versions/Current/Resources/English*
61 3 Saúl Ibarra Corretgé
Versions/Current/Resources/*.app
62 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/test
63 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/plat-*
64 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/idlelib
65 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/curses
66 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/lib2to3
67 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/lib-tk
68 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/bsddb
69 3 Saúl Ibarra Corretgé
</pre>
70 4 Saúl Ibarra Corretgé
71 4 Saúl Ibarra Corretgé
* Prevent system paths from being used with this bundle
72 4 Saúl Ibarra Corretgé
73 4 Saúl Ibarra Corretgé
Edit @Versions/Current/lib/python2.7/site.py@ and add the following lines at the end on the @main()@ function body:
74 4 Saúl Ibarra Corretgé
75 4 Saúl Ibarra Corretgé
<pre>
76 4 Saúl Ibarra Corretgé
    # Remove system paths so that only things contained in this Framework are used
77 4 Saúl Ibarra Corretgé
    sys.path = [x for x in sys.path if not x.startswith(('/System', '/Library'))]
78 4 Saúl Ibarra Corretgé
</pre>
79 5 Saúl Ibarra Corretgé
80 5 Saúl Ibarra Corretgé
h2. Compiling PyObjC
81 5 Saúl Ibarra Corretgé
82 5 Saúl Ibarra Corretgé
In order to get a PyObjC version that will work with the framework created above (Python 2.7, 32 bits) an equivalent Python must be used to compile it. That is, if has to be a Python 2.7 version (it doesn't have to be the exact version) and it has to be a 32 bit version or the ARCHFLAGS env variable must be set to @"-arch i386"@. The MACOSX_DEPLOYMENT_TARGET must also be set to the appropriate value.
83 5 Saúl Ibarra Corretgé
84 5 Saúl Ibarra Corretgé
PyObjcC can be installed with easy_install or pip. We install it in 2 steps to save some compilation time due to a bug in the build system:
85 5 Saúl Ibarra Corretgé
86 5 Saúl Ibarra Corretgé
<pre>
87 5 Saúl Ibarra Corretgé
pip install pyobjc-core
88 5 Saúl Ibarra Corretgé
pip install pyobjc
89 5 Saúl Ibarra Corretgé
</pre>
90 5 Saúl Ibarra Corretgé
91 5 Saúl Ibarra Corretgé
When compiling PyObjC a Python package will be created for every system framework, but not all of them are needed (at the moment), so just pick the ones we use:
92 5 Saúl Ibarra Corretgé
93 5 Saúl Ibarra Corretgé
<pre>
94 5 Saúl Ibarra Corretgé
AddressBook
95 5 Saúl Ibarra Corretgé
AppKit
96 5 Saúl Ibarra Corretgé
Cocoa
97 5 Saúl Ibarra Corretgé
CoreFoundation
98 5 Saúl Ibarra Corretgé
Foundation
99 5 Saúl Ibarra Corretgé
LaunchServices
100 5 Saúl Ibarra Corretgé
PyObjCTools
101 5 Saúl Ibarra Corretgé
Quartz
102 5 Saúl Ibarra Corretgé
QTKit
103 5 Saúl Ibarra Corretgé
WebKit
104 5 Saúl Ibarra Corretgé
objc
105 5 Saúl Ibarra Corretgé
</pre>
106 5 Saúl Ibarra Corretgé
107 5 Saúl Ibarra Corretgé
*NOTE:* The _objc_ package is located inside a _PyObjC_ directory, just copy it from there, without the parent directory.
108 6 Saúl Ibarra Corretgé
109 6 Saúl Ibarra Corretgé
110 6 Saúl Ibarra Corretgé
h2. Building lxml
111 6 Saúl Ibarra Corretgé
112 6 Saúl Ibarra Corretgé
Building lxml can be a huge pain. Here is the only way I managed to compile a version which works, not the latest though:
113 6 Saúl Ibarra Corretgé
114 6 Saúl Ibarra Corretgé
<pre>
115 6 Saúl Ibarra Corretgé
wget https://pypi.python.org/packages/source/l/lxml/lxml-2.3.6.tar.gz
116 6 Saúl Ibarra Corretgé
tar zxvf lxml-2.3.6.tar.gz
117 6 Saúl Ibarra Corretgé
lxml-2.3.6
118 6 Saúl Ibarra Corretgé
119 6 Saúl Ibarra Corretgé
export CC="gcc"
120 6 Saúl Ibarra Corretgé
export ARCHFLAGS="-arch i386"
121 6 Saúl Ibarra Corretgé
export CFLAGS="-arch i386"
122 6 Saúl Ibarra Corretgé
123 6 Saúl Ibarra Corretgé
python setup.py build --static-deps --libxml2-version=2.7.8 --libxslt-version=1.1.24
124 6 Saúl Ibarra Corretgé
python setup.py install
125 6 Saúl Ibarra Corretgé
</pre>
126 6 Saúl Ibarra Corretgé
127 6 Saúl Ibarra Corretgé
128 6 Saúl Ibarra Corretgé
h2. Building python-ldap
129 6 Saúl Ibarra Corretgé
130 6 Saúl Ibarra Corretgé
The only version known to work with OSX 10.6 and up is 2.3.13, so that one should be installed:
131 6 Saúl Ibarra Corretgé
132 6 Saúl Ibarra Corretgé
<pre>
133 6 Saúl Ibarra Corretgé
pip install python-ldap==2.3.13
134 6 Saúl Ibarra Corretgé
</pre>