Bundle-python

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