Bundle-python

Version 14 (Saúl Ibarra Corretgé, 02/13/2015 09:59 am)

1 1 Saúl Ibarra Corretgé
h1. Building a Python Framework to bundle inside Blink
2 1 Saúl Ibarra Corretgé
3 1 Saúl Ibarra Corretgé
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 1 Saúl Ibarra Corretgé
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 1 Saúl Ibarra Corretgé
<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 1 Saúl Ibarra Corretgé
16 1 Saúl Ibarra Corretgé
* Uncompress and get ready to compile
17 1 Saúl Ibarra Corretgé
18 1 Saúl Ibarra Corretgé
<pre>
19 13 Saúl Ibarra Corretgé
tar jxvf Python-2.7.9.tar.bz2
20 13 Saúl Ibarra Corretgé
cd Python-2.7.9
21 1 Saúl Ibarra Corretgé
</pre>
22 1 Saúl Ibarra Corretgé
23 1 Saúl Ibarra Corretgé
* Create a temporary directory for the build result
24 1 Saúl Ibarra Corretgé
25 1 Saúl Ibarra Corretgé
<pre>
26 1 Saúl Ibarra Corretgé
mkdir -p /tmp/py
27 1 Saúl Ibarra Corretgé
</pre>
28 1 Saúl Ibarra Corretgé
29 13 Saúl Ibarra Corretgé
* Compile Python (Framework build) with compatibility for OSX >= 10.9
30 1 Saúl Ibarra Corretgé
31 1 Saúl Ibarra Corretgé
<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 1 Saúl Ibarra Corretgé
make install
35 1 Saúl Ibarra Corretgé
</pre>
36 1 Saúl Ibarra Corretgé
37 1 Saúl Ibarra Corretgé
The resulting framework will be located in /tmp/py
38 1 Saúl Ibarra Corretgé
39 1 Saúl Ibarra Corretgé
* Change the dynamic link target in the main binary file of the Python Framework
40 1 Saúl Ibarra Corretgé
41 1 Saúl Ibarra Corretgé
<pre>
42 1 Saúl Ibarra Corretgé
cd /tmp/py/Python.framework/Versions/2.7
43 1 Saúl Ibarra Corretgé
chmod +w Python
44 1 Saúl Ibarra Corretgé
install_name_tool -id @executable_path/../Frameworks/Python.framework/Versions/2.7/Python Python
45 1 Saúl Ibarra Corretgé
chmod -w Python
46 1 Saúl Ibarra Corretgé
</pre>
47 1 Saúl Ibarra Corretgé
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 2 Saúl Ibarra Corretgé
52 1 Saúl Ibarra Corretgé
53 1 Saúl Ibarra Corretgé
* Reduce the size of the Python Framework:
54 1 Saúl Ibarra Corretgé
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 3 Saúl Ibarra Corretgé
<pre>
58 3 Saúl Ibarra Corretgé
*.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 3 Saúl Ibarra Corretgé
Versions/Current/bin
63 3 Saúl Ibarra Corretgé
Versions/Current/share
64 3 Saúl Ibarra Corretgé
Versions/Current/Resources/English*
65 3 Saúl Ibarra Corretgé
Versions/Current/Resources/*.app
66 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/test
67 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/plat-*
68 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/idlelib
69 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/curses
70 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/lib2to3
71 3 Saúl Ibarra Corretgé
Versions/Current/lib/python2.7/lib-tk
72 3 Saúl Ibarra Corretgé
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 4 Saúl Ibarra Corretgé
77 4 Saúl Ibarra Corretgé
Edit @Versions/Current/lib/python2.7/site.py@ and add the following lines at the end on the @main()@ function body:
78 4 Saúl Ibarra Corretgé
79 4 Saúl Ibarra Corretgé
<pre>
80 4 Saúl Ibarra Corretgé
    # Remove system paths so that only things contained in this Framework are used
81 4 Saúl Ibarra Corretgé
    sys.path = [x for x in sys.path if not x.startswith(('/System', '/Library'))]
82 4 Saúl Ibarra Corretgé
</pre>
83 5 Saúl Ibarra Corretgé
84 5 Saúl Ibarra Corretgé
h2. Compiling PyObjC
85 5 Saúl Ibarra Corretgé
86 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.
87 5 Saúl Ibarra Corretgé
88 5 Saúl Ibarra Corretgé
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:
89 1 Saúl Ibarra Corretgé
90 5 Saúl Ibarra Corretgé
<pre>
91 5 Saúl Ibarra Corretgé
pip install pyobjc-core
92 5 Saúl Ibarra Corretgé
pip install pyobjc
93 1 Saúl Ibarra Corretgé
</pre>
94 1 Saúl Ibarra Corretgé
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 5 Saúl Ibarra Corretgé
97 5 Saúl Ibarra Corretgé
<pre>
98 5 Saúl Ibarra Corretgé
AddressBook
99 5 Saúl Ibarra Corretgé
AppKit
100 1 Saúl Ibarra Corretgé
Cocoa
101 5 Saúl Ibarra Corretgé
CoreFoundation
102 5 Saúl Ibarra Corretgé
Foundation
103 13 Saúl Ibarra Corretgé
JavaScriptCore
104 5 Saúl Ibarra Corretgé
LaunchServices
105 1 Saúl Ibarra Corretgé
PyObjCTools
106 1 Saúl Ibarra Corretgé
Quartz
107 13 Saúl Ibarra Corretgé
ScriptingBridge
108 13 Saúl Ibarra Corretgé
StoreKit
109 1 Saúl Ibarra Corretgé
WebKit
110 8 Adrian Georgescu
objc
111 6 Saúl Ibarra Corretgé
</pre>
112 6 Saúl Ibarra Corretgé
113 6 Saúl Ibarra Corretgé
*NOTE:* The _objc_ package is located inside a _PyObjC_ directory, just copy it from there, without the parent directory.
114 6 Saúl Ibarra Corretgé
115 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.
116 6 Saúl Ibarra Corretgé
117 6 Saúl Ibarra Corretgé
118 14 Saúl Ibarra Corretgé
h1. Creating a sandbox (Python virtualenv)
119 6 Saúl Ibarra Corretgé
120 6 Saúl Ibarra Corretgé
<pre>
121 6 Saúl Ibarra Corretgé
sudo easy_install pip
122 6 Saúl Ibarra Corretgé
sudo pip install virtualenv virtualenvwrapper
123 6 Saúl Ibarra Corretgé
</pre>
124 6 Saúl Ibarra Corretgé
125 6 Saúl Ibarra Corretgé
Add to ~.bashrc
126 6 Saúl Ibarra Corretgé
127 9 Adrian Georgescu
<pre>
128 9 Adrian Georgescu
# Virtualenv
129 9 Adrian Georgescu
export WORKON_HOME=$HOME/.virtualenvs
130 9 Adrian Georgescu
export PIP_VIRTUALENV_BASE=$WORKON_HOME
131 9 Adrian Georgescu
export PIP_RESPECT_VIRTUALENV=true
132 9 Adrian Georgescu
export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh
133 9 Adrian Georgescu
[[ -f /usr/local/bin/virtualenvwrapper_lazy.sh ]] && source /usr/local/bin/virtualenvwrapper_lazy.sh
134 9 Adrian Georgescu
</pre>
135 10 Adrian Georgescu
136 13 Saúl Ibarra Corretgé
Creating a sandbox:
137 10 Adrian Georgescu
138 10 Adrian Georgescu
<pre>
139 13 Saúl Ibarra Corretgé
mkvirtualenv -p $(which python2.7) sandbox
140 13 Saúl Ibarra Corretgé
</pre>
141 13 Saúl Ibarra Corretgé
142 13 Saúl Ibarra Corretgé
Exiting the sandbox:
143 13 Saúl Ibarra Corretgé
144 13 Saúl Ibarra Corretgé
<pre>
145 10 Adrian Georgescu
deactivate
146 13 Saúl Ibarra Corretgé
</pre>
147 13 Saúl Ibarra Corretgé
148 13 Saúl Ibarra Corretgé
Entering the sandbox:
149 13 Saúl Ibarra Corretgé
150 13 Saúl Ibarra Corretgé
<pre>
151 11 Adrian Georgescu
workon sandbox
152 11 Adrian Georgescu
</pre>