Project

General

Profile

Bundle-python » History » Version 28

Adrian Georgescu, 11/08/2016 03:01 PM

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