Project

General

Profile

Bundle-python » History » Version 9

Adrian Georgescu, 07/26/2013 10:44 AM

1 1 Saúl Ibarra Corretgé
h1. Building a Python Framework to bundle inside Blink
2
3
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
5 7 Adrian Georgescu
h2. Setting default Python interpreter on OSX for 32 bits
6
7
<pre>
8
defaults write com.apple.versioner.python Version 2.7
9
export VERSIONER_PYTHON_PREFER_32_BIT=yes
10
</pre>
11
12
 
13 1 Saúl Ibarra Corretgé
h2. Building the Python Framework itself
14
15
* Download the desired Python version, at the time of this writing, 2.7.5
16
17
<pre>
18
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
19
</pre>
20
21
* Uncompress and get ready to compile
22
23
<pre>
24
tar jxvf Python-2.7.5.tar.bz2
25
cd Python-2.7.5
26
</pre>
27
28
* Create a temporary directory for the build result
29
30
<pre>
31
mkdir -p /tmp/py
32
</pre>
33
34
* Compile Python (Framework build) in 32 bits mode and with compatibility for OSX >= 10.6
35
36
<pre>
37
./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"
38
make
39
make install
40
</pre>
41
42
The resulting framework will be located in /tmp/py
43
44
* Change the dynamic link target in the main binary file of the Python Framework
45
46
<pre>
47
cd /tmp/py/Python.framework/Versions/2.7
48
chmod +w Python
49
install_name_tool -id @executable_path/../Frameworks/Python.framework/Versions/2.7/Python Python
50
chmod -w Python
51
</pre>
52
53 3 Saúl Ibarra Corretgé
The framework is almos ready for inclusion on the project.
54 1 Saúl Ibarra Corretgé
55 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.
56
57 1 Saúl Ibarra Corretgé
58
* Reduce the size of the Python Framework:
59
60 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:
61
62
<pre>
63
*.pyc
64
*.pyo
65
Versions/Current/Mac
66
Versions/Current/bin
67
Versions/Current/share
68
Versions/Current/Resources/English*
69
Versions/Current/Resources/*.app
70
Versions/Current/lib/python2.7/test
71
Versions/Current/lib/python2.7/plat-*
72
Versions/Current/lib/python2.7/idlelib
73
Versions/Current/lib/python2.7/curses
74
Versions/Current/lib/python2.7/lib2to3
75
Versions/Current/lib/python2.7/lib-tk
76
Versions/Current/lib/python2.7/bsddb
77
</pre>
78 4 Saúl Ibarra Corretgé
79
* Prevent system paths from being used with this bundle
80
81
Edit @Versions/Current/lib/python2.7/site.py@ and add the following lines at the end on the @main()@ function body:
82
83
<pre>
84
    # Remove system paths so that only things contained in this Framework are used
85
    sys.path = [x for x in sys.path if not x.startswith(('/System', '/Library'))]
86
</pre>
87 5 Saúl Ibarra Corretgé
88
h2. Compiling PyObjC
89
90
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.
91
92
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:
93
94
<pre>
95
pip install pyobjc-core
96
pip install pyobjc
97
</pre>
98
99
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:
100
101
<pre>
102
AddressBook
103
AppKit
104
Cocoa
105
CoreFoundation
106
Foundation
107
LaunchServices
108
PyObjCTools
109
Quartz
110
QTKit
111
WebKit
112
objc
113
</pre>
114
115
*NOTE:* The _objc_ package is located inside a _PyObjC_ directory, just copy it from there, without the parent directory.
116 6 Saúl Ibarra Corretgé
117
118
h2. Building lxml
119
120
Building lxml can be a huge pain. Here is the only way I managed to compile a version which works, not the latest though:
121
122
<pre>
123
wget https://pypi.python.org/packages/source/l/lxml/lxml-2.3.6.tar.gz
124
tar zxvf lxml-2.3.6.tar.gz
125 8 Adrian Georgescu
cd lxml-2.3.6
126 6 Saúl Ibarra Corretgé
127
export CC="gcc"
128
export ARCHFLAGS="-arch i386"
129
export CFLAGS="-arch i386"
130
131
python setup.py build --static-deps --libxml2-version=2.7.8 --libxslt-version=1.1.24
132
python setup.py install
133
</pre>
134
135
136
h2. Building python-ldap
137
138
The only version known to work with OSX 10.6 and up is 2.3.13, so that one should be installed:
139
140
<pre>
141
pip install python-ldap==2.3.13
142
</pre>
143 9 Adrian Georgescu
144
145
h1. Creating a sandbox
146
147
<pre>
148
sudo easy_install pip
149
sudo pip install virtualenv virtualenvwrapper
150
</pre>