Project

General

Profile

Bundle-python » History » Version 42

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