Project

General

Profile

Bundle-python » History » Version 21

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