| 1 | # Copyright 1999-2008 Gentoo Foundation |
1 | # Copyright 1999-2009 Gentoo Foundation |
| 2 | # Distributed under the terms of the GNU General Public License v2 |
2 | # Distributed under the terms of the GNU General Public License v2 |
| 3 | # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.54 2008/10/30 05:21:46 zmedico Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.58 2009/08/03 22:28:01 arfrever Exp $ |
| 4 | |
4 | |
| 5 | # @ECLASS: python.eclass |
5 | # @ECLASS: python.eclass |
| 6 | # @MAINTAINER: |
6 | # @MAINTAINER: |
| 7 | # python@gentoo.org |
7 | # python@gentoo.org |
| 8 | # |
8 | # |
| … | |
… | |
| 12 | # Some useful functions for dealing with python. |
12 | # Some useful functions for dealing with python. |
| 13 | inherit alternatives multilib |
13 | inherit alternatives multilib |
| 14 | |
14 | |
| 15 | |
15 | |
| 16 | if [[ -n "${NEED_PYTHON}" ]] ; then |
16 | if [[ -n "${NEED_PYTHON}" ]] ; then |
| 17 | DEPEND=">=dev-lang/python-${NEED_PYTHON}" |
17 | PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" |
|
|
18 | DEPEND="${PYTHON_ATOM}" |
| 18 | RDEPEND="${DEPEND}" |
19 | RDEPEND="${DEPEND}" |
|
|
20 | else |
|
|
21 | PYTHON_ATOM="dev-lang/python" |
|
|
22 | fi |
|
|
23 | |
|
|
24 | if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then |
|
|
25 | DEPEND="${DEPEND} >=app-admin/eselect-python-20090801" |
| 19 | fi |
26 | fi |
| 20 | |
27 | |
| 21 | __python_eclass_test() { |
28 | __python_eclass_test() { |
| 22 | __python_version_extract 2.3 |
29 | __python_version_extract 2.3 |
| 23 | echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" |
30 | echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" |
| … | |
… | |
| 57 | tmpstr="$(${python} -V 2>&1 )" |
64 | tmpstr="$(${python} -V 2>&1 )" |
| 58 | export PYVER_ALL="${tmpstr#Python }" |
65 | export PYVER_ALL="${tmpstr#Python }" |
| 59 | __python_version_extract $PYVER_ALL |
66 | __python_version_extract $PYVER_ALL |
| 60 | } |
67 | } |
| 61 | |
68 | |
|
|
69 | # @FUNCTION: get_python |
|
|
70 | # @USAGE: [-a|--absolute-path] <Python_ABI="${PYTHON_ABI}"> |
|
|
71 | # @DESCRIPTION: |
|
|
72 | # Get Python interpreter filename for specified Python ABI. If Python_ABI argument |
|
|
73 | # is ommitted, then PYTHON_ABI environment variable must be set and is used. |
|
|
74 | get_python() { |
|
|
75 | local absolute_path="0" slot= |
|
|
76 | |
|
|
77 | while (($#)); do |
|
|
78 | case "$1" in |
|
|
79 | -a|--absolute-path) |
|
|
80 | absolute_path="1" |
|
|
81 | ;; |
|
|
82 | -*) |
|
|
83 | die "${FUNCNAME}(): Unrecognized option $1" |
|
|
84 | ;; |
|
|
85 | *) |
|
|
86 | break |
|
|
87 | ;; |
|
|
88 | esac |
|
|
89 | shift |
|
|
90 | done |
|
|
91 | |
|
|
92 | if [[ "$#" -eq "0" ]]; then |
|
|
93 | if [[ -n "${PYTHON_ABI}" ]]; then |
|
|
94 | slot="${PYTHON_ABI}" |
|
|
95 | else |
|
|
96 | die "${FUNCNAME}(): Invalid usage" |
|
|
97 | fi |
|
|
98 | elif [[ "$#" -eq "1" ]]; then |
|
|
99 | slot="$1" |
|
|
100 | else |
|
|
101 | die "${FUNCNAME}(): Invalid usage" |
|
|
102 | fi |
|
|
103 | |
|
|
104 | if [[ "${absolute_path}" == "1" ]]; then |
|
|
105 | echo -n "/usr/bin/python${slot}" |
|
|
106 | else |
|
|
107 | echo -n "python${slot}" |
|
|
108 | fi |
|
|
109 | } |
|
|
110 | |
|
|
111 | # @FUNCTION: validate_PYTHON_ABIS |
|
|
112 | # @DESCRIPTION: |
|
|
113 | # Make sure PYTHON_ABIS variable has valid value. |
|
|
114 | validate_PYTHON_ABIS() { |
|
|
115 | # Ensure that /usr/bin/python and /usr/bin/python-config are scripts. |
|
|
116 | if [[ "$(</usr/bin/python)" != *"Gentoo Python wrapper script"* ]]; then |
|
|
117 | die "/usr/bin/python isn't valid script" |
|
|
118 | fi |
|
|
119 | if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then |
|
|
120 | die "/usr/bin/python-config isn't valid script" |
|
|
121 | fi |
|
|
122 | |
|
|
123 | # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. |
|
|
124 | if [[ -z "${PYTHON_ABIS}" ]] && has "${EAPI:-0}" 0 1 2 3; then |
|
|
125 | local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI |
|
|
126 | PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" |
|
|
127 | for ABI in ${USE_PYTHON}; do |
|
|
128 | if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then |
|
|
129 | ewarn "Ignoring unsupported Python ABI '${ABI}'" |
|
|
130 | continue |
|
|
131 | fi |
|
|
132 | support_ABI="1" |
|
|
133 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
|
|
134 | if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then |
|
|
135 | support_ABI="0" |
|
|
136 | break |
|
|
137 | fi |
|
|
138 | done |
|
|
139 | [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" |
|
|
140 | done |
|
|
141 | export PYTHON_ABIS="${supported_PYTHON_ABIS# }" |
|
|
142 | fi |
|
|
143 | |
|
|
144 | if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then |
|
|
145 | python_version |
|
|
146 | export PYTHON_ABIS="${PYVER}" |
|
|
147 | fi |
|
|
148 | } |
|
|
149 | |
|
|
150 | # @FUNCTION: python_copy_sources |
|
|
151 | # @USAGE: [directory] |
|
|
152 | # @DESCRIPTION: |
|
|
153 | # Copy unpacked sources of given package for each Python ABI. |
|
|
154 | python_copy_sources() { |
|
|
155 | local dir dirs=() PYTHON_ABI |
|
|
156 | |
|
|
157 | if [[ "$#" -eq "0" ]]; then |
|
|
158 | if [[ "${WORKDIR}" == "${S}" ]]; then |
|
|
159 | die "${FUNCNAME}() cannot be used" |
|
|
160 | fi |
|
|
161 | dirs="${S}" |
|
|
162 | else |
|
|
163 | dirs="$@" |
|
|
164 | fi |
|
|
165 | |
|
|
166 | validate_PYTHON_ABIS |
|
|
167 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
|
|
168 | for dir in "${dirs[@]}"; do |
|
|
169 | cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed" |
|
|
170 | done |
|
|
171 | done |
|
|
172 | } |
|
|
173 | |
|
|
174 | # @FUNCTION: python_set_build_dir_symlink |
|
|
175 | # @USAGE: [directory="build"] |
|
|
176 | # @DESCRIPTION: |
|
|
177 | # Create build directory symlink. |
|
|
178 | python_set_build_dir_symlink() { |
|
|
179 | local dir="$1" |
|
|
180 | |
|
|
181 | [[ -z "${PYTHON_ABIS}" ]] && die "PYTHON_ABIS variable not set" |
|
|
182 | [[ -z "${dir}" ]] && dir="build" |
|
|
183 | |
|
|
184 | # Don't delete preexistent directories. |
|
|
185 | rm -f "${dir}" || die "Deletion of '${dir}' failed" |
|
|
186 | ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed" |
|
|
187 | } |
|
|
188 | |
|
|
189 | # @FUNCTION: python_execute_function |
|
|
190 | # @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] <function> [arguments] |
|
|
191 | # @DESCRIPTION: |
|
|
192 | # Execute specified function for each value of PYTHON_ABIS, optionally passing |
|
|
193 | # additional arguments. The specified function can use PYTHON_ABI variable. |
|
|
194 | python_execute_function() { |
|
|
195 | local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" PYTHON_ABI quiet="0" separate_build_dirs="0" |
|
|
196 | |
|
|
197 | while (($#)); do |
|
|
198 | case "$1" in |
|
|
199 | --action-message) |
|
|
200 | action_message_template="$2" |
|
|
201 | shift |
|
|
202 | ;; |
|
|
203 | -d|--default-function) |
|
|
204 | default_function="1" |
|
|
205 | ;; |
|
|
206 | --failure-message) |
|
|
207 | failure_message_template="$2" |
|
|
208 | shift |
|
|
209 | ;; |
|
|
210 | --nonfatal) |
|
|
211 | nonfatal="1" |
|
|
212 | ;; |
|
|
213 | -q|--quiet) |
|
|
214 | quiet="1" |
|
|
215 | ;; |
|
|
216 | -s|--separate-build-dirs) |
|
|
217 | separate_build_dirs="1" |
|
|
218 | ;; |
|
|
219 | -*) |
|
|
220 | die "${FUNCNAME}(): Unrecognized option $1" |
|
|
221 | ;; |
|
|
222 | *) |
|
|
223 | break |
|
|
224 | ;; |
|
|
225 | esac |
|
|
226 | shift |
|
|
227 | done |
|
|
228 | |
|
|
229 | if [[ "${default_function}" == "0" ]]; then |
|
|
230 | if [[ "$#" -eq "0" ]]; then |
|
|
231 | die "${FUNCNAME}(): Missing function name" |
|
|
232 | fi |
|
|
233 | function="$1" |
|
|
234 | shift |
|
|
235 | else |
|
|
236 | if [[ "$#" -ne "0" ]]; then |
|
|
237 | die "${FUNCNAME}(): --default-function option and function name cannot be specified simultaneously" |
|
|
238 | fi |
|
|
239 | if has "${EAPI:-0}" 0 1; then |
|
|
240 | die "${FUNCNAME}(): --default-function option cannot be used in this EAPI" |
|
|
241 | fi |
|
|
242 | |
|
|
243 | if [[ "${EBUILD_PHASE}" == "configure" ]]; then |
|
|
244 | python_default_function() { |
|
|
245 | econf |
|
|
246 | } |
|
|
247 | elif [[ "${EBUILD_PHASE}" == "compile" ]]; then |
|
|
248 | python_default_function() { |
|
|
249 | emake |
|
|
250 | } |
|
|
251 | elif [[ "${EBUILD_PHASE}" == "test" ]]; then |
|
|
252 | python_default_function() { |
|
|
253 | if emake -j1 -n check &> /dev/null; then |
|
|
254 | emake -j1 check |
|
|
255 | elif emake -j1 -n test &> /dev/null; then |
|
|
256 | emake -j1 test |
|
|
257 | fi |
|
|
258 | } |
|
|
259 | elif [[ "${EBUILD_PHASE}" == "install" ]]; then |
|
|
260 | python_default_function() { |
|
|
261 | emake DESTDIR="${D}" install |
|
|
262 | } |
|
|
263 | else |
|
|
264 | die "${FUNCNAME}(): --default-function option cannot be used in this ebuild phase" |
|
|
265 | fi |
|
|
266 | function="python_default_function" |
|
|
267 | fi |
|
|
268 | |
|
|
269 | if [[ "${quiet}" == "0" ]]; then |
|
|
270 | [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up" |
|
|
271 | [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking" |
|
|
272 | [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation" |
|
|
273 | [[ "${EBUILD_PHASE}" == "configure" ]] && action="Configuration" |
|
|
274 | [[ "${EBUILD_PHASE}" == "compile" ]] && action="Building" |
|
|
275 | [[ "${EBUILD_PHASE}" == "test" ]] && action="Testing" |
|
|
276 | [[ "${EBUILD_PHASE}" == "install" ]] && action="Installation" |
|
|
277 | [[ "${EBUILD_PHASE}" == "preinst" ]] && action="Preinstallation" |
|
|
278 | [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation" |
|
|
279 | [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation" |
|
|
280 | [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation" |
|
|
281 | fi |
|
|
282 | |
|
|
283 | local RED GREEN BLUE NORMAL |
|
|
284 | if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then |
|
|
285 | RED=$'\e[1;31m' |
|
|
286 | GREEN=$'\e[1;32m' |
|
|
287 | BLUE=$'\e[1;34m' |
|
|
288 | NORMAL=$'\e[0m' |
|
|
289 | else |
|
|
290 | RED= |
|
|
291 | GREEN= |
|
|
292 | BLUE= |
|
|
293 | NORMAL= |
|
|
294 | fi |
|
|
295 | |
|
|
296 | validate_PYTHON_ABIS |
|
|
297 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
|
|
298 | if [[ "${quiet}" == "0" ]]; then |
|
|
299 | if [[ -n "${action_message_template}" ]]; then |
|
|
300 | action_message="$(eval echo -n "${action_message_template}")" |
|
|
301 | else |
|
|
302 | action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..." |
|
|
303 | fi |
|
|
304 | echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" |
|
|
305 | fi |
|
|
306 | if [[ "${separate_build_dirs}" == "1" ]]; then |
|
|
307 | pushd "${S}-${PYTHON_ABI}" > /dev/null || die "pushd failed" |
|
|
308 | fi |
|
|
309 | if ! EPYTHON="$(get_python)" "${function}" "$@"; then |
|
|
310 | if [[ -n "${failure_message_template}" ]]; then |
|
|
311 | failure_message="$(eval echo -n "${failure_message_template}")" |
|
|
312 | else |
|
|
313 | failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function" |
|
|
314 | fi |
|
|
315 | if [[ "${nonfatal}" == "1" ]] || has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then |
|
|
316 | local ABI enabled_PYTHON_ABIS |
|
|
317 | for ABI in ${PYTHON_ABIS}; do |
|
|
318 | [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}" |
|
|
319 | done |
|
|
320 | export PYTHON_ABIS="${enabled_PYTHON_ABIS# }" |
|
|
321 | if [[ "${quiet}" == "0" ]]; then |
|
|
322 | ewarn "${RED}${failure_message}${NORMAL}" |
|
|
323 | fi |
|
|
324 | else |
|
|
325 | die "${failure_message}" |
|
|
326 | fi |
|
|
327 | fi |
|
|
328 | if [[ "${separate_build_dirs}" == "1" ]]; then |
|
|
329 | popd > /dev/null || die "popd failed" |
|
|
330 | fi |
|
|
331 | done |
|
|
332 | |
|
|
333 | if [[ "${default_function}" == "1" ]]; then |
|
|
334 | unset -f python_default_function |
|
|
335 | fi |
|
|
336 | } |
|
|
337 | |
|
|
338 | |
|
|
339 | # @ECLASS-VARIABLE: PYTHON_USE_WITH |
|
|
340 | # @DESCRIPTION: |
|
|
341 | # Set this to a space separated list of use flags |
|
|
342 | # the python slot in use must be built with. |
|
|
343 | |
|
|
344 | # @ECLASS-VARIABLE: PYTHON_USE_WITH_OR |
|
|
345 | # @DESCRIPTION: |
|
|
346 | # Set this to a space separated list of use flags |
|
|
347 | # of which one must be turned on for the slot of |
|
|
348 | # in use. |
|
|
349 | |
|
|
350 | # @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT |
|
|
351 | # @DESCRIPTION: |
|
|
352 | # Set this if you need to make either PYTHON_USE_WITH or |
|
|
353 | # PYTHON_USE_WITH_OR atoms conditional under a use flag. |
|
|
354 | |
|
|
355 | # @FUNCTION: python_pkg_setup |
|
|
356 | # @DESCRIPTION: |
|
|
357 | # Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags |
|
|
358 | # are respected. Only exported if one of those variables is set. |
|
|
359 | if ! has ${EAPI} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then |
|
|
360 | python_pkg_setup_fail() { |
|
|
361 | eerror "${1}" |
|
|
362 | die "${1}" |
|
|
363 | } |
|
|
364 | |
|
|
365 | python_pkg_setup() { |
|
|
366 | [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return |
|
|
367 | |
|
|
368 | python_version |
|
|
369 | local failed |
|
|
370 | local pyatom="dev-lang/python:${PYVER}" |
|
|
371 | |
|
|
372 | for use in ${PYTHON_USE_WITH}; do |
|
|
373 | if ! has_version "${pyatom}[${use}]"; then |
|
|
374 | python_pkg_setup_fail \ |
|
|
375 | "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}" |
|
|
376 | fi |
|
|
377 | done |
|
|
378 | |
|
|
379 | for use in ${PYTHON_USE_WITH_OR}; do |
|
|
380 | if has_version "${pyatom}[${use}]"; then |
|
|
381 | return |
|
|
382 | fi |
|
|
383 | done |
|
|
384 | |
|
|
385 | if [[ ${PYTHON_USE_WITH_OR} ]]; then |
|
|
386 | python_pkg_setup_fail \ |
|
|
387 | "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}" |
|
|
388 | fi |
|
|
389 | } |
|
|
390 | |
|
|
391 | EXPORT_FUNCTIONS pkg_setup |
|
|
392 | |
|
|
393 | if [[ ${PYTHON_USE_WITH} ]]; then |
|
|
394 | PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" |
|
|
395 | elif [[ ${PYTHON_USE_WITH_OR} ]]; then |
|
|
396 | PYTHON_USE_WITH_ATOM="|| ( " |
|
|
397 | for use in ${PYTHON_USE_WITH_OR}; do |
|
|
398 | PYTHON_USE_WITH_ATOM=" |
|
|
399 | ${PYTHON_USE_WITH_ATOM} |
|
|
400 | ${PYTHON_ATOM}[${use}]" |
|
|
401 | done |
|
|
402 | PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )" |
|
|
403 | fi |
|
|
404 | if [[ ${PYTHON_USE_WITH_OPT} ]]; then |
|
|
405 | PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" |
|
|
406 | fi |
|
|
407 | DEPEND="${PYTHON_USE_WITH_ATOM}" |
|
|
408 | RDEPEND="${PYTHON_USE_WITH_ATOM}" |
|
|
409 | fi |
|
|
410 | |
| 62 | # @FUNCTION: python_disable_pyc |
411 | # @FUNCTION: python_disable_pyc |
| 63 | # @DESCRIPTION: |
412 | # @DESCRIPTION: |
| 64 | # Tells python not to automatically recompile modules to .pyc/.pyo |
413 | # Tells python not to automatically recompile modules to .pyc/.pyo |
| 65 | # even if the timestamps/version stamps don't match. This is done |
414 | # even if the timestamps/version stamps don't match. This is done |
| 66 | # to protect sandbox. |
415 | # to protect sandbox. |
| … | |
… | |
| 89 | python_need_rebuild() { |
438 | python_need_rebuild() { |
| 90 | python_version |
439 | python_version |
| 91 | export PYTHON_NEED_REBUILD=${PYVER} |
440 | export PYTHON_NEED_REBUILD=${PYVER} |
| 92 | } |
441 | } |
| 93 | |
442 | |
|
|
443 | # @FUNCTION: python_get_includedir |
|
|
444 | # @DESCRIPTION: |
|
|
445 | # Run without arguments, returns the Python include directory. |
|
|
446 | python_get_includedir() { |
|
|
447 | if [[ -n "${PYTHON_ABI}" ]]; then |
|
|
448 | echo "/usr/include/python${PYTHON_ABI}" |
|
|
449 | else |
|
|
450 | python_version |
|
|
451 | echo "/usr/include/python${PYVER}" |
|
|
452 | fi |
|
|
453 | } |
|
|
454 | |
| 94 | # @FUNCTION: python_get_libdir |
455 | # @FUNCTION: python_get_libdir |
| 95 | # @DESCRIPTION: |
456 | # @DESCRIPTION: |
| 96 | # Run without arguments, returns the python library dir |
457 | # Run without arguments, returns the Python library directory. |
| 97 | python_get_libdir() { |
458 | python_get_libdir() { |
|
|
459 | if [[ -n "${PYTHON_ABI}" ]]; then |
|
|
460 | echo "/usr/$(get_libdir)/python${PYTHON_ABI}" |
|
|
461 | else |
| 98 | python_version |
462 | python_version |
| 99 | echo "/usr/$(get_libdir)/python${PYVER}" |
463 | echo "/usr/$(get_libdir)/python${PYVER}" |
|
|
464 | fi |
| 100 | } |
465 | } |
| 101 | |
466 | |
| 102 | # @FUNCTION: python_get_sitedir |
467 | # @FUNCTION: python_get_sitedir |
| 103 | # @DESCRIPTION: |
468 | # @DESCRIPTION: |
| 104 | # Run without arguments, returns the python site-packages dir |
469 | # Run without arguments, returns the Python site-packages directory. |
| 105 | python_get_sitedir() { |
470 | python_get_sitedir() { |
| 106 | echo "$(python_get_libdir)/site-packages" |
471 | echo "$(python_get_libdir)/site-packages" |
| 107 | } |
472 | } |
| 108 | |
473 | |
| 109 | # @FUNCTION: python_makesym |
474 | # @FUNCTION: python_makesym |
| … | |
… | |
| 128 | die "missing tkinter support with installed python" |
493 | die "missing tkinter support with installed python" |
| 129 | fi |
494 | fi |
| 130 | } |
495 | } |
| 131 | |
496 | |
| 132 | # @FUNCTION: python_mod_exists |
497 | # @FUNCTION: python_mod_exists |
| 133 | # @USAGE: < module > |
498 | # @USAGE: <module> |
| 134 | # @DESCRIPTION: |
499 | # @DESCRIPTION: |
| 135 | # Run with the module name as an argument. it will check if a |
500 | # Run with the module name as an argument. it will check if a |
| 136 | # python module is installed and loadable. it will return |
501 | # python module is installed and loadable. it will return |
| 137 | # TRUE(0) if the module exists, and FALSE(1) if the module does |
502 | # TRUE(0) if the module exists, and FALSE(1) if the module does |
| 138 | # not exist. |
503 | # not exist. |
| … | |
… | |
| 141 | # if python_mod_exists gtk; then |
506 | # if python_mod_exists gtk; then |
| 142 | # echo "gtk support enabled" |
507 | # echo "gtk support enabled" |
| 143 | # fi |
508 | # fi |
| 144 | python_mod_exists() { |
509 | python_mod_exists() { |
| 145 | [[ "$1" ]] || die "${FUNCNAME} requires an argument!" |
510 | [[ "$1" ]] || die "${FUNCNAME} requires an argument!" |
| 146 | python -c "import $1" >/dev/null 2>&1 |
511 | python -c "import $1" &>/dev/null |
| 147 | } |
512 | } |
| 148 | |
513 | |
| 149 | # @FUNCTION: python_mod_compile |
514 | # @FUNCTION: python_mod_compile |
| 150 | # @USAGE: < file > [more files ...] |
515 | # @USAGE: <file> [more files ...] |
| 151 | # @DESCRIPTION: |
516 | # @DESCRIPTION: |
| 152 | # Given filenames, it will pre-compile the module's .pyc and .pyo. |
517 | # Given filenames, it will pre-compile the module's .pyc and .pyo. |
| 153 | # This function should only be run in pkg_postinst() |
518 | # This function should only be run in pkg_postinst() |
| 154 | # |
519 | # |
| 155 | # Example: |
520 | # Example: |
| 156 | # python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py |
521 | # python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py |
| 157 | # |
522 | # |
| 158 | python_mod_compile() { |
523 | python_mod_compile() { |
|
|
524 | if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then |
|
|
525 | die "${FUNCNAME}() cannot be used in this EAPI" |
|
|
526 | fi |
|
|
527 | |
| 159 | local f myroot myfiles=() |
528 | local f myroot myfiles=() |
| 160 | |
529 | |
| 161 | # Check if phase is pkg_postinst() |
530 | # Check if phase is pkg_postinst() |
| 162 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
531 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
| 163 | die "${FUNCNAME} should only be run in pkg_postinst()" |
532 | die "${FUNCNAME} should only be run in pkg_postinst()" |
| … | |
… | |
| 184 | ewarn "No files to compile!" |
553 | ewarn "No files to compile!" |
| 185 | fi |
554 | fi |
| 186 | } |
555 | } |
| 187 | |
556 | |
| 188 | # @FUNCTION: python_mod_optimize |
557 | # @FUNCTION: python_mod_optimize |
| 189 | # @USAGE: [ path ] |
558 | # @USAGE: [options] [directory|file] |
| 190 | # @DESCRIPTION: |
559 | # @DESCRIPTION: |
| 191 | # If no arguments supplied, it will recompile all modules under |
560 | # If no arguments supplied, it will recompile not recursively all modules |
| 192 | # sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..) |
561 | # under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages). |
| 193 | # no recursively |
|
|
| 194 | # |
562 | # |
| 195 | # If supplied with arguments, it will recompile all modules recursively |
563 | # If supplied with arguments, it will recompile all modules recursively |
| 196 | # in the supplied directory |
564 | # in the supplied directory. |
| 197 | # This function should only be run in pkg_postinst() |
565 | # This function should only be run in pkg_postinst(). |
| 198 | # |
566 | # |
| 199 | # Options passed to this function are passed to compileall.py |
567 | # Options passed to this function are passed to compileall.py. |
| 200 | # |
568 | # |
| 201 | # Example: |
569 | # Example: |
| 202 | # python_mod_optimize /usr/share/codegen |
570 | # python_mod_optimize ctypesgencore |
| 203 | python_mod_optimize() { |
571 | python_mod_optimize() { |
|
|
572 | # Check if phase is pkg_postinst(). |
|
|
573 | [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()" |
|
|
574 | |
|
|
575 | if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then |
|
|
576 | local dir file options=() other_dirs=() other_files=() PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=() |
|
|
577 | |
|
|
578 | # Strip trailing slash from ROOT. |
|
|
579 | root="${ROOT%/}" |
|
|
580 | |
|
|
581 | # Respect ROOT and options passed to compileall.py. |
|
|
582 | while (($#)); do |
|
|
583 | case "$1" in |
|
|
584 | -l|-f|-q) |
|
|
585 | options+=("$1") |
|
|
586 | ;; |
|
|
587 | -d|-x) |
|
|
588 | options+=("$1" "$2") |
|
|
589 | shift |
|
|
590 | ;; |
|
|
591 | -*) |
|
|
592 | ewarn "${FUNCNAME}: Ignoring compile option $1" |
|
|
593 | ;; |
|
|
594 | *) |
|
|
595 | if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then |
|
|
596 | die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" |
|
|
597 | elif [[ "$1" =~ ^/ ]]; then |
|
|
598 | if [[ -d "${root}/$1" ]]; then |
|
|
599 | other_dirs+=("${root}/$1") |
|
|
600 | elif [[ -f "${root}/$1" ]]; then |
|
|
601 | other_files+=("${root}/$1") |
|
|
602 | elif [[ -e "${root}/$1" ]]; then |
|
|
603 | ewarn "'${root}/$1' is not a file or a directory!" |
|
|
604 | else |
|
|
605 | ewarn "'${root}/$1' doesn't exist!" |
|
|
606 | fi |
|
|
607 | else |
|
|
608 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
|
|
609 | if [[ -d "${root}/$(python_get_sitedir)/$1" ]]; then |
|
|
610 | site_packages_dirs+=("$1") |
|
|
611 | break |
|
|
612 | elif [[ -f "${root}/$(python_get_sitedir)/$1" ]]; then |
|
|
613 | site_packages_files+=("$1") |
|
|
614 | break |
|
|
615 | elif [[ -e "${root}/$(python_get_sitedir)/$1" ]]; then |
|
|
616 | ewarn "'$1' is not a file or a directory!" |
|
|
617 | else |
|
|
618 | ewarn "'$1' doesn't exist!" |
|
|
619 | fi |
|
|
620 | done |
|
|
621 | fi |
|
|
622 | ;; |
|
|
623 | esac |
|
|
624 | shift |
|
|
625 | done |
|
|
626 | |
|
|
627 | # Set additional options. |
|
|
628 | options+=("-q") |
|
|
629 | |
|
|
630 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
|
|
631 | if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then |
|
|
632 | return_code="0" |
|
|
633 | ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" |
|
|
634 | if ((${#site_packages_dirs[@]})); then |
|
|
635 | for dir in "${site_packages_dirs[@]}"; do |
|
|
636 | site_packages_absolute_dirs+=("${root}/$(python_get_sitedir)/${dir}") |
|
|
637 | done |
|
|
638 | "$(get_python)" "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" |
|
|
639 | "$(get_python)" -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" |
|
|
640 | fi |
|
|
641 | if ((${#site_packages_files[@]})); then |
|
|
642 | for file in "${site_packages_files[@]}"; do |
|
|
643 | site_packages_absolute_files+=("${root}/$(python_get_sitedir)/${file}") |
|
|
644 | done |
|
|
645 | "$(get_python)" "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" |
|
|
646 | "$(get_python)" -O "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" |
|
|
647 | fi |
|
|
648 | eend "${return_code}" |
|
|
649 | fi |
|
|
650 | unset site_packages_absolute_dirs site_packages_absolute_files |
|
|
651 | done |
|
|
652 | |
|
|
653 | # Don't use PYTHON_ABI in next calls to python_get_libdir(). |
|
|
654 | unset PYTHON_ABI |
|
|
655 | |
|
|
656 | if ((${#other_dirs[@]})) || ((${#other_files[@]})); then |
|
|
657 | return_code="0" |
|
|
658 | ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." |
|
|
659 | if ((${#other_dirs[@]})); then |
|
|
660 | python${PYVER} "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" |
|
|
661 | python${PYVER} -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" |
|
|
662 | fi |
|
|
663 | if ((${#other_files[@]})); then |
|
|
664 | python${PYVER} "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" |
|
|
665 | python${PYVER} -O "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" |
|
|
666 | fi |
|
|
667 | eend "${return_code}" |
|
|
668 | fi |
|
|
669 | else |
| 204 | local myroot mydirs=() myfiles=() myopts=() |
670 | local myroot mydirs=() myfiles=() myopts=() |
| 205 | |
671 | |
| 206 | # Check if phase is pkg_postinst() |
|
|
| 207 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
|
|
| 208 | die "${FUNCNAME} should only be run in pkg_postinst()" |
|
|
| 209 | |
|
|
| 210 | # strip trailing slash |
672 | # strip trailing slash |
| 211 | myroot="${ROOT%/}" |
673 | myroot="${ROOT%/}" |
| 212 | |
674 | |
| 213 | # respect ROOT and options passed to compileall.py |
675 | # respect ROOT and options passed to compileall.py |
| 214 | while (($#)); do |
676 | while (($#)); do |
| 215 | case $1 in |
677 | case "$1" in |
| 216 | -l|-f|-q) |
678 | -l|-f|-q) |
| 217 | myopts+=("$1") |
679 | myopts+=("$1") |
| 218 | ;; |
680 | ;; |
| 219 | -d|-x) |
681 | -d|-x) |
| 220 | myopts+=("$1" "$2") |
682 | myopts+=("$1" "$2") |
|
|
683 | shift |
|
|
684 | ;; |
|
|
685 | -*) |
|
|
686 | ewarn "${FUNCNAME}: Ignoring compile option $1" |
|
|
687 | ;; |
|
|
688 | *) |
|
|
689 | if [[ -d "${myroot}"/$1 ]]; then |
|
|
690 | mydirs+=("${myroot}/$1") |
|
|
691 | elif [[ -f "${myroot}"/$1 ]]; then |
|
|
692 | # Files are passed to python_mod_compile which is ROOT-aware |
|
|
693 | myfiles+=("$1") |
|
|
694 | elif [[ -e "${myroot}/$1" ]]; then |
|
|
695 | ewarn "${myroot}/$1 is not a file or directory!" |
|
|
696 | else |
|
|
697 | ewarn "${myroot}/$1 doesn't exist!" |
|
|
698 | fi |
|
|
699 | ;; |
|
|
700 | esac |
|
|
701 | shift |
|
|
702 | done |
|
|
703 | |
|
|
704 | # allow compiling for older python versions |
|
|
705 | if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then |
|
|
706 | PYVER=${PYTHON_OVERRIDE_PYVER} |
|
|
707 | else |
|
|
708 | python_version |
|
|
709 | fi |
|
|
710 | |
|
|
711 | # set additional opts |
|
|
712 | myopts+=(-q) |
|
|
713 | |
|
|
714 | ebegin "Byte compiling python modules for python-${PYVER} .." |
|
|
715 | if ((${#mydirs[@]})); then |
|
|
716 | python${PYVER} \ |
|
|
717 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
|
|
718 | "${myopts[@]}" "${mydirs[@]}" |
|
|
719 | python${PYVER} -O \ |
|
|
720 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
|
|
721 | "${myopts[@]}" "${mydirs[@]}" |
|
|
722 | fi |
|
|
723 | |
|
|
724 | if ((${#myfiles[@]})); then |
|
|
725 | python_mod_compile "${myfiles[@]}" |
|
|
726 | fi |
|
|
727 | |
|
|
728 | eend $? |
|
|
729 | fi |
|
|
730 | } |
|
|
731 | |
|
|
732 | # @FUNCTION: python_mod_cleanup |
|
|
733 | # @USAGE: [directory] |
|
|
734 | # @DESCRIPTION: |
|
|
735 | # Run with optional arguments, where arguments are directories of |
|
|
736 | # python modules. If none given, it will look in /usr/lib/python[0-9].[0-9]. |
|
|
737 | # |
|
|
738 | # It will recursively scan all compiled Python modules in the directories and |
|
|
739 | # determine if they are orphaned (i.e. their corresponding .py files are missing.) |
|
|
740 | # If they are, then it will remove their corresponding .pyc and .pyo files. |
|
|
741 | # |
|
|
742 | # This function should only be run in pkg_postrm(). |
|
|
743 | python_mod_cleanup() { |
|
|
744 | local PYTHON_ABI SEARCH_PATH=() root src_py |
|
|
745 | |
|
|
746 | # Check if phase is pkg_postrm(). |
|
|
747 | [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" |
|
|
748 | |
|
|
749 | # Strip trailing slash from ROOT. |
|
|
750 | root="${ROOT%/}" |
|
|
751 | |
|
|
752 | if (($#)); then |
|
|
753 | if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then |
|
|
754 | while (($#)); do |
|
|
755 | if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then |
|
|
756 | die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" |
|
|
757 | elif [[ "$1" =~ ^/ ]]; then |
|
|
758 | SEARCH_PATH+=("${root}/${1#/}") |
|
|
759 | else |
|
|
760 | for PYTHON_ABI in ${PYTHON_ABIS}; do |
|
|
761 | SEARCH_PATH+=("${root}/$(python_get_sitedir)/$1") |
|
|
762 | done |
|
|
763 | fi |
| 221 | shift |
764 | shift |
| 222 | ;; |
|
|
| 223 | -*) |
|
|
| 224 | ewarn "${FUNCNAME}: Ignoring compile option $1" |
|
|
| 225 | ;; |
|
|
| 226 | *) |
|
|
| 227 | if [[ -d "${myroot}"/$1 ]]; then |
|
|
| 228 | mydirs+=("${myroot}/$1") |
|
|
| 229 | elif [[ -f "${myroot}"/$1 ]]; then |
|
|
| 230 | # Files are passed to python_mod_compile which is ROOT-aware |
|
|
| 231 | myfiles+=("$1") |
|
|
| 232 | elif [[ -e "${myroot}/$1" ]]; then |
|
|
| 233 | ewarn "${myroot}/$1 is not a file or directory!" |
|
|
| 234 | else |
|
|
| 235 | ewarn "${myroot}/$1 doesn't exist!" |
|
|
| 236 | fi |
|
|
| 237 | ;; |
|
|
| 238 | esac |
|
|
| 239 | shift |
|
|
| 240 | done |
765 | done |
| 241 | |
|
|
| 242 | # allow compiling for older python versions |
|
|
| 243 | if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then |
|
|
| 244 | PYVER=${PYTHON_OVERRIDE_PYVER} |
|
|
| 245 | else |
766 | else |
| 246 | python_version |
|
|
| 247 | fi |
|
|
| 248 | |
|
|
| 249 | # set additional opts |
|
|
| 250 | myopts+=(-q) |
|
|
| 251 | |
|
|
| 252 | ebegin "Byte compiling python modules for python-${PYVER} .." |
|
|
| 253 | if ((${#mydirs[@]})); then |
|
|
| 254 | python${PYVER} \ |
|
|
| 255 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
|
|
| 256 | "${myopts[@]}" "${mydirs[@]}" |
|
|
| 257 | python${PYVER} -O \ |
|
|
| 258 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
|
|
| 259 | "${myopts[@]}" "${mydirs[@]}" |
|
|
| 260 | fi |
|
|
| 261 | |
|
|
| 262 | if ((${#myfiles[@]})); then |
|
|
| 263 | python_mod_compile "${myfiles[@]}" |
|
|
| 264 | fi |
|
|
| 265 | |
|
|
| 266 | eend $? |
|
|
| 267 | } |
|
|
| 268 | |
|
|
| 269 | # @FUNCTION: python_mod_cleanup |
|
|
| 270 | # @USAGE: [ dir ] |
|
|
| 271 | # @DESCRIPTION: |
|
|
| 272 | # Run with optional arguments, where arguments are directories of |
|
|
| 273 | # python modules. if none given, it will look in /usr/lib/python[0-9].[0-9] |
|
|
| 274 | # |
|
|
| 275 | # It will recursively scan all compiled python modules in the directories |
|
|
| 276 | # and determine if they are orphaned (eg. their corresponding .py is missing.) |
|
|
| 277 | # if they are, then it will remove their corresponding .pyc and .pyo |
|
|
| 278 | # |
|
|
| 279 | # This function should only be run in pkg_postrm() |
|
|
| 280 | python_mod_cleanup() { |
|
|
| 281 | local SEARCH_PATH=() myroot src_py |
|
|
| 282 | |
|
|
| 283 | # Check if phase is pkg_postrm() |
|
|
| 284 | [[ ${EBUILD_PHASE} != postrm ]] &&\ |
|
|
| 285 | die "${FUNCNAME} should only be run in pkg_postrm()" |
|
|
| 286 | |
|
|
| 287 | # strip trailing slash |
|
|
| 288 | myroot="${ROOT%/}" |
|
|
| 289 | |
|
|
| 290 | if (($#)); then |
|
|
| 291 | SEARCH_PATH=("${@#/}") |
767 | SEARCH_PATH=("${@#/}") |
| 292 | SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}") |
768 | SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") |
|
|
769 | fi |
| 293 | else |
770 | else |
| 294 | SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages) |
771 | SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) |
| 295 | fi |
772 | fi |
| 296 | |
773 | |
| 297 | for path in "${SEARCH_PATH[@]}"; do |
774 | for path in "${SEARCH_PATH[@]}"; do |
| 298 | einfo "Cleaning orphaned Python bytecode from ${path} .." |
775 | einfo "Cleaning orphaned Python bytecode from ${path} .." |
| 299 | find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do |
776 | find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do |
| … | |
… | |
| 301 | [[ -f "${src_py}" ]] && continue |
778 | [[ -f "${src_py}" ]] && continue |
| 302 | einfo "Purging ${src_py}[co]" |
779 | einfo "Purging ${src_py}[co]" |
| 303 | rm -f "${src_py}"[co] |
780 | rm -f "${src_py}"[co] |
| 304 | done |
781 | done |
| 305 | |
782 | |
| 306 | # attempt to remove directories that maybe empty |
783 | # Attempt to remove directories that may be empty. |
| 307 | find "${path}" -type d | sort -r | while read -r dir; do |
784 | find "${path}" -type d | sort -r | while read -r dir; do |
| 308 | rmdir "${dir}" 2>/dev/null |
785 | rmdir "${dir}" 2>/dev/null |
| 309 | done |
786 | done |
| 310 | done |
787 | done |
| 311 | } |
788 | } |