Bundle-python » History » Version 12
Adrian Georgescu, 11/08/2014 05:38 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 | 7 | Adrian Georgescu | h2. Setting default Python interpreter on OSX for 32 bits |
6 | |||
7 | <pre> |
||
8 | defaults write com.apple.versioner.python Version 2.7 |
||
9 | export VERSIONER_PYTHON_PREFER_32_BIT=yes |
||
10 | </pre> |
||
11 | |||
12 | |||
13 | 1 | Saúl Ibarra Corretgé | h2. Building the Python Framework itself |
14 | |||
15 | * Download the desired Python version, at the time of this writing, 2.7.5 |
||
16 | |||
17 | <pre> |
||
18 | wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 |
||
19 | </pre> |
||
20 | |||
21 | * Uncompress and get ready to compile |
||
22 | |||
23 | <pre> |
||
24 | tar jxvf Python-2.7.5.tar.bz2 |
||
25 | cd Python-2.7.5 |
||
26 | </pre> |
||
27 | |||
28 | * Create a temporary directory for the build result |
||
29 | |||
30 | <pre> |
||
31 | mkdir -p /tmp/py |
||
32 | </pre> |
||
33 | |||
34 | * Compile Python (Framework build) in 32 bits mode and with compatibility for OSX >= 10.6 |
||
35 | |||
36 | <pre> |
||
37 | ./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" |
||
38 | make |
||
39 | make install |
||
40 | </pre> |
||
41 | |||
42 | The resulting framework will be located in /tmp/py |
||
43 | |||
44 | * Change the dynamic link target in the main binary file of the Python Framework |
||
45 | |||
46 | <pre> |
||
47 | cd /tmp/py/Python.framework/Versions/2.7 |
||
48 | chmod +w Python |
||
49 | install_name_tool -id @executable_path/../Frameworks/Python.framework/Versions/2.7/Python Python |
||
50 | chmod -w Python |
||
51 | </pre> |
||
52 | |||
53 | 3 | Saúl Ibarra Corretgé | The framework is almos ready for inclusion on the project. |
54 | 1 | Saúl Ibarra Corretgé | |
55 | 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. |
56 | |||
57 | 1 | Saúl Ibarra Corretgé | |
58 | * Reduce the size of the Python Framework: |
||
59 | |||
60 | 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: |
61 | 3 | Saúl Ibarra Corretgé | |
62 | <pre> |
||
63 | *.pyc |
||
64 | 1 | Saúl Ibarra Corretgé | *.pyo |
65 | 12 | Adrian Georgescu | Versions/Current/lib/python2.7/config/python.o |
66 | 3 | Saúl Ibarra Corretgé | Versions/Current/Mac |
67 | Versions/Current/bin |
||
68 | Versions/Current/share |
||
69 | Versions/Current/Resources/English* |
||
70 | Versions/Current/Resources/*.app |
||
71 | Versions/Current/lib/python2.7/test |
||
72 | Versions/Current/lib/python2.7/plat-* |
||
73 | Versions/Current/lib/python2.7/idlelib |
||
74 | Versions/Current/lib/python2.7/curses |
||
75 | Versions/Current/lib/python2.7/lib2to3 |
||
76 | Versions/Current/lib/python2.7/lib-tk |
||
77 | Versions/Current/lib/python2.7/bsddb |
||
78 | </pre> |
||
79 | 4 | Saúl Ibarra Corretgé | |
80 | * Prevent system paths from being used with this bundle |
||
81 | |||
82 | Edit @Versions/Current/lib/python2.7/site.py@ and add the following lines at the end on the @main()@ function body: |
||
83 | |||
84 | <pre> |
||
85 | # Remove system paths so that only things contained in this Framework are used |
||
86 | sys.path = [x for x in sys.path if not x.startswith(('/System', '/Library'))] |
||
87 | </pre> |
||
88 | 5 | Saúl Ibarra Corretgé | |
89 | h2. Compiling PyObjC |
||
90 | |||
91 | In order to get a PyObjC version that will work with the framework created above (Python 2.7, 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 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. |
||
92 | |||
93 | 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: |
||
94 | |||
95 | <pre> |
||
96 | pip install pyobjc-core |
||
97 | pip install pyobjc |
||
98 | </pre> |
||
99 | |||
100 | 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: |
||
101 | |||
102 | <pre> |
||
103 | AddressBook |
||
104 | AppKit |
||
105 | Cocoa |
||
106 | CoreFoundation |
||
107 | Foundation |
||
108 | LaunchServices |
||
109 | PyObjCTools |
||
110 | Quartz |
||
111 | QTKit |
||
112 | WebKit |
||
113 | objc |
||
114 | </pre> |
||
115 | |||
116 | *NOTE:* The _objc_ package is located inside a _PyObjC_ directory, just copy it from there, without the parent directory. |
||
117 | 6 | Saúl Ibarra Corretgé | |
118 | |||
119 | h2. Building lxml |
||
120 | |||
121 | Building lxml can be a huge pain. Here is the only way I managed to compile a version which works, not the latest though: |
||
122 | |||
123 | <pre> |
||
124 | wget https://pypi.python.org/packages/source/l/lxml/lxml-2.3.6.tar.gz |
||
125 | tar zxvf lxml-2.3.6.tar.gz |
||
126 | 8 | Adrian Georgescu | cd lxml-2.3.6 |
127 | 6 | Saúl Ibarra Corretgé | |
128 | export CC="gcc" |
||
129 | export ARCHFLAGS="-arch i386" |
||
130 | export CFLAGS="-arch i386" |
||
131 | |||
132 | python setup.py build --static-deps --libxml2-version=2.7.8 --libxslt-version=1.1.24 |
||
133 | python setup.py install |
||
134 | </pre> |
||
135 | |||
136 | |||
137 | h2. Building python-ldap |
||
138 | |||
139 | The only version known to work with OSX 10.6 and up is 2.3.13, so that one should be installed: |
||
140 | |||
141 | <pre> |
||
142 | pip install python-ldap==2.3.13 |
||
143 | </pre> |
||
144 | 9 | Adrian Georgescu | |
145 | |||
146 | h1. Creating a sandbox |
||
147 | |||
148 | <pre> |
||
149 | sudo easy_install pip |
||
150 | sudo pip install virtualenv virtualenvwrapper |
||
151 | </pre> |
||
152 | 10 | Adrian Georgescu | |
153 | Add to ~.bashrc |
||
154 | |||
155 | <pre> |
||
156 | # Virtualenv |
||
157 | export WORKON_HOME=$HOME/.virtualenvs |
||
158 | export PIP_VIRTUALENV_BASE=$WORKON_HOME |
||
159 | export PIP_RESPECT_VIRTUALENV=true |
||
160 | export VIRTUALENVWRAPPER_SCRIPT=/usr/local/bin/virtualenvwrapper.sh |
||
161 | [[ -f /usr/local/bin/virtualenvwrapper_lazy.sh ]] && source /usr/local/bin/virtualenvwrapper_lazy.sh |
||
162 | </pre> |
||
163 | 11 | Adrian Georgescu | |
164 | |||
165 | <pre> |
||
166 | mkvirtualenv sandbox |
||
167 | deactivate |
||
168 | workon sandbox |
||
169 | </pre> |