Project

General

Profile

Bundle-python » History » Version 22

Adrian Georgescu, 11/03/2016 10:13 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
/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/
28
</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 12 Adrian Georgescu
*.pyc
47 3 Saúl Ibarra Corretgé
*.pyo
48
Versions/Current/lib/python2.7/config/python.o
49 1 Saúl Ibarra Corretgé
Versions/Current/Mac
50 12 Adrian Georgescu
Versions/Current/bin
51 3 Saúl Ibarra Corretgé
Versions/Current/share
52 18 Saúl Ibarra Corretgé
Versions/Current/Resources/*
53 3 Saúl Ibarra Corretgé
Versions/Current/Resources/*.app
54
Versions/Current/lib/python2.7/test
55
Versions/Current/lib/python2.7/plat-*
56
Versions/Current/lib/python2.7/idlelib
57
Versions/Current/lib/python2.7/curses
58
Versions/Current/lib/python2.7/lib2to3
59
Versions/Current/lib/python2.7/lib-tk
60
Versions/Current/lib/python2.7/bsddb
61 18 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/lib-dynload/gdbm.so
62
Versions/Current/lib/python2.7/lib-dynload/readline.so
63 1 Saúl Ibarra Corretgé
</pre>
64 3 Saúl Ibarra Corretgé
65 4 Saúl Ibarra Corretgé
* Prevent system paths from being used with this bundle
66
67 17 Saúl Ibarra Corretgé
Replace @Versions/Current/lib/python2.7/site.py@ with an empty file.
68 5 Saúl Ibarra Corretgé
69
h2. Compiling PyObjC
70
71 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.
72 5 Saúl Ibarra Corretgé
73
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:
74 1 Saúl Ibarra Corretgé
75 5 Saúl Ibarra Corretgé
<pre>
76
pip install pyobjc-core
77
pip install pyobjc
78 1 Saúl Ibarra Corretgé
</pre>
79
80 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:
81
82
<pre>
83
AddressBook
84
AppKit
85 1 Saúl Ibarra Corretgé
Cocoa
86 5 Saúl Ibarra Corretgé
CoreFoundation
87
Foundation
88 13 Saúl Ibarra Corretgé
JavaScriptCore
89 5 Saúl Ibarra Corretgé
LaunchServices
90 1 Saúl Ibarra Corretgé
PyObjCTools
91
Quartz
92 13 Saúl Ibarra Corretgé
ScriptingBridge
93
StoreKit
94 1 Saúl Ibarra Corretgé
WebKit
95 8 Adrian Georgescu
objc
96 6 Saúl Ibarra Corretgé
</pre>
97
98 1 Saúl Ibarra Corretgé
99 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):
100 20 Adrian Georgescu
101 1 Saúl Ibarra Corretgé
<pre>
102
AVFoundation
103
AddressBook
104
AppKit
105
Cocoa
106
CoreFoundation
107
Crypto
108
Foundation
109
LaunchServices
110
PyObjCTools
111
Quartz
112
ScriptingBridge
113
WebKit
114
_cffi_backend.so
115
_ldap.so
116
_markerlib
117
application
118
cffi
119
cjson.so
120
cryptography
121
cryptography-1.5.1.dist-info
122
dateutil
123
dns
124
dsml.py
125
enum
126
eventlib
127
formencode
128
gmpy2.so
129
gnutls
130
greenlet.so
131
idna
132
ipaddress.py
133
ldap
134
ldapurl.py
135
ldif.py
136
lxml
137
msrplib
138
objc
139
otr
140
pkg_resources
141
pyasn1
142
pycparser
143
pydispatch
144
pytz
145
service_identity
146
sipsimple
147
six.py
148
sqlobject
149
twisted
150
xcaplib
151 21 Adrian Georgescu
</pre>
152
153
154
*NOTE:* The _objc_ package is located inside a _PyObjC_ directory, just copy it from there, without the parent directory.
155
156
*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:
157
158
<pre>
159
__import__('pkg_resources').declare_namespace(__name__)
160
</pre>
161
162
h2. Module exceptions
163
164
When copying built Python modules into the distribution folder, care must be taken with the 2 following packages:
165
166
* zope: an empty @__init__.py@ file must be created in the @zope@ directory
167
* cryptography: the @*-dist.info@ must be copied too
168
169
h1. Creating a sandbox (Python virtualenv)
170
171
<pre>
172
sudo easy_install pip
173
sudo pip install virtualenv virtualenvwrapper
174
</pre>
175
176
Add to ~.bashrc
177
178
<pre>
179
# Virtualenv
180
export WORKON_HOME=$HOME/.virtualenvs
181
export PIP_VIRTUALENV_BASE=$WORKON_HOME
182
export PIP_RESPECT_VIRTUALENV=true
183
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
184
[[ -f /usr/local/bin/virtualenvwrapper_lazy.sh ]] && source /usr/local/bin/virtualenvwrapper_lazy.sh
185
</pre>
186
187
Creating a sandbox:
188
189
<pre>
190
mkvirtualenv -p $(which python2.7) sandbox
191
</pre>
192
193
Exiting the sandbox:
194
195
<pre>
196
deactivate
197
</pre>
198
199
Entering the sandbox:
200
201
<pre>
202
workon sandbox
203 19 Adrian Georgescu
</pre>