Bundle-python

Version 3 (Saúl Ibarra Corretgé, 05/27/2013 02:20 pm) → Version 4/44 (Saúl Ibarra Corretgé, 05/27/2013 02:23 pm)

h1. Building a Python Framework to bundle inside Blink

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.

h2. Building the Python Framework itself

* Download the desired Python version, at the time of this writing, 2.7.5

<pre>
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
</pre>

* Uncompress and get ready to compile

<pre>
tar jxvf Python-2.7.5.tar.bz2
cd Python-2.7.5
</pre>

* Create a temporary directory for the build result

<pre>
mkdir -p /tmp/py
</pre>

* Compile Python (Framework build) in 32 bits mode and with compatibility for OSX >= 10.6

<pre>
./configure --prefix=/tmp/py --enable-framework=/tmp/py MACOSX_DEPLOYMENT_TARGET=10.6 ARCHFLAGS="-arch i386" CFLAGS="-arch i386" CXXFLAGS="-arch i386" LDFLAGS="-arch i386"
make
make install
</pre>

The resulting framework will be located in /tmp/py

* Change the dynamic link target in the main binary file of the Python Framework

<pre>
cd /tmp/py/Python.framework/Versions/2.7
chmod +w Python
install_name_tool -id @executable_path/../Frameworks/Python.framework/Versions/2.7/Python Python
chmod -w Python
</pre>

The framework is almos ready for inclusion on the project.

*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.

* Reduce the size of the Python Framework:

There are a number of things that can be removed from the framework directory to make it smaller in size:

<pre>
*.pyc
*.pyo
Versions/Current/Mac
Versions/Current/bin
Versions/Current/share
Versions/Current/Resources/English*
Versions/Current/Resources/*.app
Versions/Current/lib/python2.7/test
Versions/Current/lib/python2.7/plat-*
Versions/Current/lib/python2.7/idlelib
Versions/Current/lib/python2.7/curses
Versions/Current/lib/python2.7/lib2to3
Versions/Current/lib/python2.7/lib-tk
Versions/Current/lib/python2.7/bsddb
</pre>

* Prevent system paths from being used with this bundle

Edit @Versions/Current/lib/python2.7/site.py@ and add the following lines at the end on the @main()@ function body:

<pre>
# Remove system paths so that only things contained in this Framework are used
sys.path = [x for x in sys.path if not x.startswith(('/System', '/Library'))]

</pre>