Project

General

Profile

Bundle-python » History » Revision 13

Revision 12 (Adrian Georgescu, 11/08/2014 05:38 PM) → Revision 13/44 (Saúl Ibarra Corretgé, 02/13/2015 09:58 AM)

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. 

 The following instructions only apply h2. Setting default Python interpreter on OSX for 64bit builds, 32bit builds are no longer supported. 32 bits 

 <pre> 
 defaults write com.apple.versioner.python Version 2.7 
 export VERSIONER_PYTHON_PREFER_32_BIT=yes 
 </pre> 

 
 h2. Building the Python Framework itself 

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

 <pre> 
 wget http://python.org/ftp/python/2.7.9/Python-2.7.9.tar.bz2 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.9.tar.bz2 Python-2.7.5.tar.bz2 
 cd Python-2.7.9 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.9 10.6 

 <pre> 
 ./configure --prefix=/tmp/py --enable-framework=/tmp/py MACOSX_DEPLOYMENT_TARGET=10.9 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 almost 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 (and must when submitting a sandbox app to Mac App Store) be removed from the framework directory to make it smaller in size: 

 <pre> 
 *.pyc 
 *.pyo 
 Versions/Current/lib/python2.7/config/python.o 
 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> 

 h2. Compiling PyObjC 

 In order to get a PyObjC version that will work with the framework created above (Python 2.7, 64bits) 32 bits) 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. 32 bit version or the ARCHFLAGS env variable must be set to @"-arch i386"@. The MACOSX_DEPLOYMENT_TARGET must also be set to the appropriate value. 

 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: 

 <pre> 
 pip install pyobjc-core 
 pip install pyobjc 
 </pre> 

 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: 

 <pre> 
 AddressBook 
 AppKit 
 Cocoa 
 CoreFoundation 
 Foundation 
 JavaScriptCore 
 LaunchServices 
 PyObjCTools 
 Quartz 
 ScriptingBridge QTKit 
 StoreKit 
 WebKit 
 objc 
 </pre> 

 *NOTE:* The _objc_ package is located inside a _PyObjC_ directory, just copy it from there, without the parent directory. 


 h2. Building lxml 

 *NOTE:* _PyObjCTools_ Building lxml can be a huge pain. Here is the only way I managed to compile a version which works, not a valid Python package, as it lacks a @__init__.py@ file, an empty the latest though: 

 <pre> 
 wget https://pypi.python.org/packages/source/l/lxml/lxml-2.3.6.tar.gz 
 tar zxvf lxml-2.3.6.tar.gz 
 cd lxml-2.3.6 

 export CC="gcc" 
 export ARCHFLAGS="-arch i386" 
 export CFLAGS="-arch i386" 

 python setup.py build --static-deps --libxml2-version=2.7.8 --libxslt-version=1.1.24 
 python setup.py install 
 </pre> 


 h2. Building python-ldap 

 The only version known to work with OSX 10.6 and up is 2.3.13, so that one needs to should be manually created. installed: 

 <pre> 
 pip install python-ldap==2.3.13 
 </pre> 


 h1. Creating a sandbox 

 <pre> 
 sudo easy_install pip 
 sudo pip install virtualenv virtualenvwrapper 
 </pre> 

 Add to ~.bashrc 

 <pre> 
 # Virtualenv 
 export WORKON_HOME=$HOME/.virtualenvs 
 export PIP_VIRTUALENV_BASE=$WORKON_HOME 
 export PIP_RESPECT_VIRTUALENV=true 
 export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh 
 [[ -f /usr/local/bin/virtualenvwrapper_lazy.sh ]] && source /usr/local/bin/virtualenvwrapper_lazy.sh 
 </pre> 

 Creating a sandbox: 

 


 <pre> 
 mkvirtualenv -p $(which python2.7) sandbox 
 </pre> 

 Exiting the sandbox: 

 <pre> 
 deactivate 
 </pre> 

 Entering the sandbox: 

 <pre> 
 workon sandbox 
 </pre>