Project

General

Profile

Bundle-python » History » Version 17

Saúl Ibarra Corretgé, 02/22/2016 05:25 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 13 Saúl Ibarra Corretgé
* Download the desired Python version, at the time of this writing, 2.7.9
11 1 Saúl Ibarra Corretgé
12
<pre>
13 13 Saúl Ibarra Corretgé
wget http://python.org/ftp/python/2.7.9/Python-2.7.9.tar.bz2
14 1 Saúl Ibarra Corretgé
</pre>
15
16
* Uncompress and get ready to compile
17
18
<pre>
19 13 Saúl Ibarra Corretgé
tar jxvf Python-2.7.9.tar.bz2
20
cd Python-2.7.9
21 1 Saúl Ibarra Corretgé
</pre>
22
23
* Create a temporary directory for the build result
24
25
<pre>
26
mkdir -p /tmp/py
27
</pre>
28
29 13 Saúl Ibarra Corretgé
* Compile Python (Framework build) with compatibility for OSX >= 10.9
30 1 Saúl Ibarra Corretgé
31
<pre>
32 13 Saúl Ibarra Corretgé
./configure --prefix=/tmp/py --enable-framework=/tmp/py MACOSX_DEPLOYMENT_TARGET=10.9
33 1 Saúl Ibarra Corretgé
make
34
make install
35
</pre>
36
37
The resulting framework will be located in /tmp/py
38
39
* Change the dynamic link target in the main binary file of the Python Framework
40
41
<pre>
42
cd /tmp/py/Python.framework/Versions/2.7
43
chmod +w Python
44
install_name_tool -id @executable_path/../Frameworks/Python.framework/Versions/2.7/Python Python
45
chmod -w Python
46
</pre>
47
48 13 Saúl Ibarra Corretgé
The framework is almost ready for inclusion on the project.
49 1 Saúl Ibarra Corretgé
50 2 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 -RH@ instead.
51
52 1 Saúl Ibarra Corretgé
53
* Reduce the size of the Python Framework:
54
55 12 Adrian Georgescu
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:
56 3 Saúl Ibarra Corretgé
57
<pre>
58
*.pyc
59 1 Saúl Ibarra Corretgé
*.pyo
60 12 Adrian Georgescu
Versions/Current/lib/python2.7/config/python.o
61 3 Saúl Ibarra Corretgé
Versions/Current/Mac
62
Versions/Current/bin
63
Versions/Current/share
64
Versions/Current/Resources/English*
65
Versions/Current/Resources/*.app
66
Versions/Current/lib/python2.7/test
67
Versions/Current/lib/python2.7/plat-*
68
Versions/Current/lib/python2.7/idlelib
69
Versions/Current/lib/python2.7/curses
70
Versions/Current/lib/python2.7/lib2to3
71
Versions/Current/lib/python2.7/lib-tk
72
Versions/Current/lib/python2.7/bsddb
73 1 Saúl Ibarra Corretgé
</pre>
74 3 Saúl Ibarra Corretgé
75 4 Saúl Ibarra Corretgé
* Prevent system paths from being used with this bundle
76
77 17 Saúl Ibarra Corretgé
Replace @Versions/Current/lib/python2.7/site.py@ with an empty file.
78 5 Saúl Ibarra Corretgé
79
h2. Compiling PyObjC
80
81 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.
82 5 Saúl Ibarra Corretgé
83
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:
84 1 Saúl Ibarra Corretgé
85 5 Saúl Ibarra Corretgé
<pre>
86
pip install pyobjc-core
87
pip install pyobjc
88 1 Saúl Ibarra Corretgé
</pre>
89
90 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:
91
92
<pre>
93
AddressBook
94
AppKit
95 1 Saúl Ibarra Corretgé
Cocoa
96 5 Saúl Ibarra Corretgé
CoreFoundation
97
Foundation
98 13 Saúl Ibarra Corretgé
JavaScriptCore
99 5 Saúl Ibarra Corretgé
LaunchServices
100 1 Saúl Ibarra Corretgé
PyObjCTools
101
Quartz
102 13 Saúl Ibarra Corretgé
ScriptingBridge
103
StoreKit
104 1 Saúl Ibarra Corretgé
WebKit
105 8 Adrian Georgescu
objc
106 6 Saúl Ibarra Corretgé
</pre>
107
108
*NOTE:* The _objc_ package is located inside a _PyObjC_ directory, just copy it from there, without the parent directory.
109
110 13 Saúl Ibarra Corretgé
*NOTE:* _PyObjCTools_ is not a valid Python package, as it lacks a @__init__.py@ file, an empty one needs to be manually created.
111 6 Saúl Ibarra Corretgé
112 16 Saúl Ibarra Corretgé
h2. Module exceptions
113
114
When copying built Python modules into the distribution folder, care must be taken with the 2 following packages:
115
116
* zope: an empty @__init__.py@ file must be created in the @zope@ directory
117
* cryptography: the @*-dist.info@ must be copied too
118 6 Saúl Ibarra Corretgé
119 14 Saúl Ibarra Corretgé
h1. Creating a sandbox (Python virtualenv)
120 6 Saúl Ibarra Corretgé
121
<pre>
122
sudo easy_install pip
123
sudo pip install virtualenv virtualenvwrapper
124
</pre>
125
126
Add to ~.bashrc
127
128 9 Adrian Georgescu
<pre>
129
# Virtualenv
130
export WORKON_HOME=$HOME/.virtualenvs
131
export PIP_VIRTUALENV_BASE=$WORKON_HOME
132
export PIP_RESPECT_VIRTUALENV=true
133
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
134
[[ -f /usr/local/bin/virtualenvwrapper_lazy.sh ]] && source /usr/local/bin/virtualenvwrapper_lazy.sh
135
</pre>
136 10 Adrian Georgescu
137 13 Saúl Ibarra Corretgé
Creating a sandbox:
138 10 Adrian Georgescu
139
<pre>
140 13 Saúl Ibarra Corretgé
mkvirtualenv -p $(which python2.7) sandbox
141
</pre>
142
143
Exiting the sandbox:
144
145
<pre>
146 10 Adrian Georgescu
deactivate
147 13 Saúl Ibarra Corretgé
</pre>
148
149
Entering the sandbox:
150
151
<pre>
152 11 Adrian Georgescu
workon sandbox
153
</pre>