Bundle-python

Version 33 (Adrian Georgescu, 11/08/2016 03:13 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 13 Saúl Ibarra Corretgé
The following instructions only apply for 64bit builds, 32bit builds are no longer supported.
6 7 Adrian Georgescu
7 22 Adrian Georgescu
Blink dependencies must be installed under the following directory structure:
8 22 Adrian Georgescu
9 22 Adrian Georgescu
* Distribution/Frameworks/
10 22 Adrian Georgescu
* Distribution/Resources/lib
11 1 Saúl Ibarra Corretgé
 
12 7 Adrian Georgescu
h2. Building the Python Framework itself
13 1 Saúl Ibarra Corretgé
14 18 Saúl Ibarra Corretgé
* Install it using Homebrew
15 1 Saúl Ibarra Corretgé
16 1 Saúl Ibarra Corretgé
<pre>
17 18 Saúl Ibarra Corretgé
brew install python
18 1 Saúl Ibarra Corretgé
</pre>
19 1 Saúl Ibarra Corretgé
20 18 Saúl Ibarra Corretgé
The framework will be installed and linked with Homebrew supplied OpenSSL and SQLite versions. Those libraries will need to be copied too.
21 1 Saúl Ibarra Corretgé
22 18 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 -a@ instead.
23 22 Adrian Georgescu
24 22 Adrian Georgescu
The Python framework is found in
25 22 Adrian Georgescu
26 22 Adrian Georgescu
<pre>
27 27 Adrian Georgescu
cp -a /usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework ~/work/blink/Distribution/Frameworks/
28 22 Adrian Georgescu
</pre>
29 22 Adrian Georgescu
30 2 Saúl Ibarra Corretgé
* Reduce the size of the Python Framework:
31 2 Saúl Ibarra Corretgé
32 1 Saúl Ibarra Corretgé
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:
33 1 Saúl Ibarra Corretgé
34 1 Saúl Ibarra Corretgé
<pre>
35 27 Adrian Georgescu
cd ~/work/blink/Distribution/Frameworks//Python.framework
36 26 Adrian Georgescu
find . -name *.pyc -exec rm -r "{}" \; 
37 26 Adrian Georgescu
find . -name *.pyo -exec rm -r "{}" \; 
38 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/config/python.o
39 24 Adrian Georgescu
rm -r Versions/Current/Mac
40 24 Adrian Georgescu
rm -r Versions/Current/bin
41 24 Adrian Georgescu
rm -r Versions/Current/share
42 24 Adrian Georgescu
rm -r Versions/Current/Resources/*
43 24 Adrian Georgescu
rm -r Versions/Current/Resources/*.app
44 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/test
45 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/plat-*
46 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/idlelib
47 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/curses
48 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/lib2to3
49 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/lib-tk
50 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/bsddb
51 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/lib-dynload/gdbm.so
52 24 Adrian Georgescu
rm -r Versions/Current/lib/python2.7/lib-dynload/readline.so
53 1 Saúl Ibarra Corretgé
</pre>
54 3 Saúl Ibarra Corretgé
55 1 Saúl Ibarra Corretgé
Replace @Versions/Current/lib/python2.7/site.py@ with an empty file.
56 28 Adrian Georgescu
57 28 Adrian Georgescu
<pre>
58 28 Adrian Georgescu
rm Versions/Current/lib/python2.7/site.py
59 28 Adrian Georgescu
touch Versions/Current/lib/python2.7/site.py
60 28 Adrian Georgescu
</pre>
61 5 Saúl Ibarra Corretgé
62 5 Saúl Ibarra Corretgé
h2. Compiling PyObjC
63 5 Saúl Ibarra Corretgé
64 13 Saúl Ibarra Corretgé
In order to get a PyObjC version that will work with the framework created above (Python 2.7, 64bits) 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 64bit version. The MACOSX_DEPLOYMENT_TARGET must also be set to the appropriate value.
65 5 Saúl Ibarra Corretgé
66 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:
67 1 Saúl Ibarra Corretgé
68 5 Saúl Ibarra Corretgé
<pre>
69 5 Saúl Ibarra Corretgé
pip install pyobjc-core
70 5 Saúl Ibarra Corretgé
pip install pyobjc
71 23 Adrian Georgescu
pip install pycrypto
72 1 Saúl Ibarra Corretgé
</pre>
73 1 Saúl Ibarra Corretgé
74 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:
75 5 Saúl Ibarra Corretgé
76 5 Saúl Ibarra Corretgé
<pre>
77 5 Saúl Ibarra Corretgé
AddressBook
78 5 Saúl Ibarra Corretgé
AppKit
79 1 Saúl Ibarra Corretgé
Cocoa
80 5 Saúl Ibarra Corretgé
CoreFoundation
81 5 Saúl Ibarra Corretgé
Foundation
82 13 Saúl Ibarra Corretgé
JavaScriptCore
83 5 Saúl Ibarra Corretgé
LaunchServices
84 1 Saúl Ibarra Corretgé
PyObjCTools
85 1 Saúl Ibarra Corretgé
Quartz
86 13 Saúl Ibarra Corretgé
ScriptingBridge
87 13 Saúl Ibarra Corretgé
StoreKit
88 1 Saúl Ibarra Corretgé
WebKit
89 8 Adrian Georgescu
objc
90 6 Saúl Ibarra Corretgé
</pre>
91 6 Saúl Ibarra Corretgé
92 1 Saúl Ibarra Corretgé
93 21 Adrian Georgescu
For example this is the content of a Resources/lib bundled with Blink Cocoa as of November 3rd, 2016 (including sipsimple dependencies & all):
94 20 Adrian Georgescu
95 1 Saúl Ibarra Corretgé
<pre>
96 1 Saúl Ibarra Corretgé
AVFoundation
97 1 Saúl Ibarra Corretgé
AddressBook
98 1 Saúl Ibarra Corretgé
AppKit
99 1 Saúl Ibarra Corretgé
Cocoa
100 1 Saúl Ibarra Corretgé
CoreFoundation
101 1 Saúl Ibarra Corretgé
Crypto
102 1 Saúl Ibarra Corretgé
Foundation
103 1 Saúl Ibarra Corretgé
LaunchServices
104 1 Saúl Ibarra Corretgé
PyObjCTools
105 1 Saúl Ibarra Corretgé
Quartz
106 1 Saúl Ibarra Corretgé
ScriptingBridge
107 1 Saúl Ibarra Corretgé
WebKit
108 1 Saúl Ibarra Corretgé
_cffi_backend.so
109 1 Saúl Ibarra Corretgé
_ldap.so
110 1 Saúl Ibarra Corretgé
_markerlib
111 1 Saúl Ibarra Corretgé
application
112 1 Saúl Ibarra Corretgé
cffi
113 1 Saúl Ibarra Corretgé
cjson.so
114 1 Saúl Ibarra Corretgé
cryptography
115 1 Saúl Ibarra Corretgé
cryptography-1.5.1.dist-info
116 1 Saúl Ibarra Corretgé
dateutil
117 1 Saúl Ibarra Corretgé
dns
118 1 Saúl Ibarra Corretgé
dsml.py
119 1 Saúl Ibarra Corretgé
enum
120 1 Saúl Ibarra Corretgé
eventlib
121 1 Saúl Ibarra Corretgé
formencode
122 1 Saúl Ibarra Corretgé
gmpy2.so
123 1 Saúl Ibarra Corretgé
gnutls
124 1 Saúl Ibarra Corretgé
greenlet.so
125 1 Saúl Ibarra Corretgé
idna
126 1 Saúl Ibarra Corretgé
ipaddress.py
127 1 Saúl Ibarra Corretgé
ldap
128 1 Saúl Ibarra Corretgé
ldapurl.py
129 1 Saúl Ibarra Corretgé
ldif.py
130 1 Saúl Ibarra Corretgé
lxml
131 1 Saúl Ibarra Corretgé
msrplib
132 1 Saúl Ibarra Corretgé
objc
133 1 Saúl Ibarra Corretgé
otr
134 1 Saúl Ibarra Corretgé
pkg_resources
135 1 Saúl Ibarra Corretgé
pyasn1
136 1 Saúl Ibarra Corretgé
pycparser
137 1 Saúl Ibarra Corretgé
pydispatch
138 1 Saúl Ibarra Corretgé
pytz
139 1 Saúl Ibarra Corretgé
service_identity
140 1 Saúl Ibarra Corretgé
sipsimple
141 1 Saúl Ibarra Corretgé
six.py
142 1 Saúl Ibarra Corretgé
sqlobject
143 1 Saúl Ibarra Corretgé
twisted
144 1 Saúl Ibarra Corretgé
xcaplib
145 21 Adrian Georgescu
</pre>
146 21 Adrian Georgescu
147 21 Adrian Georgescu
148 21 Adrian Georgescu
*NOTE:* The _objc_ package is located inside a _PyObjC_ directory, just copy it from there, without the parent directory.
149 21 Adrian Georgescu
150 21 Adrian Georgescu
*NOTE:* _PyObjCTools_ is not a valid Python package, as it lacks a @__init__.py@ file, an empty one needs to be manually created with this content:
151 21 Adrian Georgescu
152 21 Adrian Georgescu
<pre>
153 21 Adrian Georgescu
__import__('pkg_resources').declare_namespace(__name__)
154 21 Adrian Georgescu
</pre>
155 21 Adrian Georgescu
156 25 Adrian Georgescu
h2. Fix library paths
157 25 Adrian Georgescu
158 25 Adrian Georgescu
All libraries must have their relative path change to the Framework path bundled within Blink.app
159 25 Adrian Georgescu
160 25 Adrian Georgescu
<pre>
161 25 Adrian Georgescu
#!/bin/sh
162 25 Adrian Georgescu
163 25 Adrian Georgescu
old_path="local/lib/\|local/Cellar/\|/usr/local/opt/libmpc/lib/\|/usr/local/opt/mpfr/lib/\|Frameworks/Frameworks/\|/Users/adigeo/work/ag-projects/video/local/lib/"
164 25 Adrian Georgescu
new_path="@executable_path/../Frameworks/"
165 25 Adrian Georgescu
166 25 Adrian Georgescu
for library in $@; do
167 25 Adrian Georgescu
  install_name_tool -id $new_path$library $library
168 25 Adrian Georgescu
  dependencies=$(otool -L $library | grep $old_path | awk '{print $1}')
169 25 Adrian Georgescu
  for dependency in $dependencies; do
170 25 Adrian Georgescu
      new_basename=$(basename $dependency)
171 25 Adrian Georgescu
      new_name="$new_path$new_basename"
172 25 Adrian Georgescu
      echo $dependency $new_name $library
173 25 Adrian Georgescu
      install_name_tool -change $dependency $new_name $library
174 25 Adrian Georgescu
  done
175 1 Saúl Ibarra Corretgé
done
176 1 Saúl Ibarra Corretgé
</pre>
177 1 Saúl Ibarra Corretgé
178 1 Saúl Ibarra Corretgé
A script is available in ./build_scripts/ directory
179 1 Saúl Ibarra Corretgé
180 1 Saúl Ibarra Corretgé
<pre>
181 1 Saúl Ibarra Corretgé
./build_scripts/change_lib_names.sh Distribution/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/*.so
182 1 Saúl Ibarra Corretgé
chmod +w Distribution/Frameworks/Python.framework/Versions/Current/Python
183 1 Saúl Ibarra Corretgé
./build_scripts/change_lib_names.sh Distribution/Frameworks/Python.framework/Versions/Current/Python
184 1 Saúl Ibarra Corretgé
</pre>
185 1 Saúl Ibarra Corretgé
186 33 Adrian Georgescu
*NOTE*: Python.framework as well as all other libraries must be signed using command line tools. Make sure when building Blink that "Code sign on copy" option is disabled for Python.framework. This script can be used to sign all libraries and frameworks
187 33 Adrian Georgescu
188 33 Adrian Georgescu
<pre>
189 33 Adrian Georgescu
sos=`find ./Resources/lib -name *.so`; for s in $sos; do codesign -f -s '3rd Party Mac Developer Application: AG Projects' $s; done
190 33 Adrian Georgescu
sos=`find ./Frameworks -name *.dylib`; for s in $sos; do codesign -f -s '3rd Party Mac Developer Application: AG Projects' $s; done
191 33 Adrian Georgescu
sos=`find ./Frameworks -name *.so`; for s in $sos; do codesign -f -s '3rd Party Mac Developer Application: AG Projects' $s; done
192 33 Adrian Georgescu
sos=`find ./Frameworks -name *.o`; for s in $sos; do codesign -f -s '3rd Party Mac Developer Application: AG Projects' $s; done
193 33 Adrian Georgescu
sos=`find ./Frameworks -name *.a`; for s in $sos; do codesign -f -s '3rd Party Mac Developer Application: AG Projects' $s; done
194 33 Adrian Georgescu
</pre>
195 33 Adrian Georgescu
196 33 Adrian Georgescu
A script is available in ./build_scripts/ directory
197 33 Adrian Georgescu
198 33 Adrian Georgescu
<pre>
199 33 Adrian Georgescu
./build_scripts/codesign.sh 
200 33 Adrian Georgescu
</pre>
201 29 Adrian Georgescu
202 21 Adrian Georgescu
h2. Module exceptions
203 21 Adrian Georgescu
204 21 Adrian Georgescu
When copying built Python modules into the distribution folder, care must be taken with the 2 following packages:
205 21 Adrian Georgescu
206 21 Adrian Georgescu
* zope: an empty @__init__.py@ file must be created in the @zope@ directory
207 21 Adrian Georgescu
* cryptography: the @*-dist.info@ must be copied too
208 21 Adrian Georgescu
209 21 Adrian Georgescu
h1. Creating a sandbox (Python virtualenv)
210 21 Adrian Georgescu
211 21 Adrian Georgescu
<pre>
212 21 Adrian Georgescu
sudo easy_install pip
213 21 Adrian Georgescu
sudo pip install virtualenv virtualenvwrapper
214 21 Adrian Georgescu
</pre>
215 21 Adrian Georgescu
216 21 Adrian Georgescu
Add to ~.bashrc
217 21 Adrian Georgescu
218 21 Adrian Georgescu
<pre>
219 21 Adrian Georgescu
# Virtualenv
220 21 Adrian Georgescu
export WORKON_HOME=$HOME/.virtualenvs
221 21 Adrian Georgescu
export PIP_VIRTUALENV_BASE=$WORKON_HOME
222 21 Adrian Georgescu
export PIP_RESPECT_VIRTUALENV=true
223 21 Adrian Georgescu
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
224 21 Adrian Georgescu
[[ -f /usr/local/bin/virtualenvwrapper_lazy.sh ]] && source /usr/local/bin/virtualenvwrapper_lazy.sh
225 21 Adrian Georgescu
</pre>
226 21 Adrian Georgescu
227 21 Adrian Georgescu
Creating a sandbox:
228 21 Adrian Georgescu
229 21 Adrian Georgescu
<pre>
230 21 Adrian Georgescu
mkvirtualenv -p $(which python2.7) sandbox
231 21 Adrian Georgescu
</pre>
232 21 Adrian Georgescu
233 21 Adrian Georgescu
Exiting the sandbox:
234 21 Adrian Georgescu
235 21 Adrian Georgescu
<pre>
236 21 Adrian Georgescu
deactivate
237 21 Adrian Georgescu
</pre>
238 21 Adrian Georgescu
239 21 Adrian Georgescu
Entering the sandbox:
240 21 Adrian Georgescu
241 21 Adrian Georgescu
<pre>
242 21 Adrian Georgescu
workon sandbox
243 19 Adrian Georgescu
</pre>