/[gentoo-x86]/eclass/python.eclass
Gentoo

Diff of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.96 Revision 1.97
1# Copyright 1999-2010 Gentoo Foundation 1# Copyright 1999-2010 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.96 2010/03/26 15:23:17 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.97 2010/05/17 18:01:59 arfrever Exp $
4 4
5# @ECLASS: python.eclass 5# @ECLASS: python.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# Gentoo Python Project <python@gentoo.org> 7# Gentoo Python Project <python@gentoo.org>
8# @BLURB: Eclass for Python packages 8# @BLURB: Eclass for Python packages
182# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT 182# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
183# @DESCRIPTION: 183# @DESCRIPTION:
184# Set this to a name of a USE flag if you need to make either PYTHON_USE_WITH or 184# Set this to a name of a USE flag if you need to make either PYTHON_USE_WITH or
185# PYTHON_USE_WITH_OR atoms conditional under a USE flag. 185# PYTHON_USE_WITH_OR atoms conditional under a USE flag.
186 186
187# @FUNCTION: python_pkg_setup
188# @DESCRIPTION:
189# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
190# are respected. Only exported if one of those variables is set.
191if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 187if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
192 python_pkg_setup() {
193 # Check if phase is pkg_setup().
194 [[ "${EBUILD_PHASE}" != "setup" ]] && die "${FUNCNAME}() can be used only in pkg_setup() phase"
195
196 python_pkg_setup_fail() {
197 eerror "${1}"
198 die "${1}"
199 }
200
201 if [[ "${PYTHON_USE_WITH_OPT}" ]]; then
202 if [[ "${PYTHON_USE_WITH_OPT}" == !* ]]; then
203 use ${PYTHON_USE_WITH_OPT#!} && return
204 else
205 use !${PYTHON_USE_WITH_OPT} && return
206 fi
207 fi
208
209 python_pkg_setup_check_USE_flags() {
210 local pyatom use
211 pyatom="$(python_get_implementational_package)"
212
213 for use in ${PYTHON_USE_WITH}; do
214 if ! has_version "${pyatom}[${use}]"; then
215 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
216 fi
217 done
218
219 for use in ${PYTHON_USE_WITH_OR}; do
220 if has_version "${pyatom}[${use}]"; then
221 return
222 fi
223 done
224
225 if [[ ${PYTHON_USE_WITH_OR} ]]; then
226 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
227 fi
228 }
229
230 if _python_package_supporting_installation_for_multiple_python_abis; then
231 python_execute_function -q python_pkg_setup_check_USE_flags
232 else
233 python_pkg_setup_check_USE_flags
234 fi
235
236 unset -f python_pkg_setup_check_USE_flags python_pkg_setup_fail
237 }
238
239 EXPORT_FUNCTIONS pkg_setup
240
241 _PYTHON_USE_WITH_ATOMS_ARRAY=() 188 _PYTHON_USE_WITH_ATOMS_ARRAY=()
242 if [[ -n "${PYTHON_USE_WITH}" ]]; then 189 if [[ -n "${PYTHON_USE_WITH}" ]]; then
243 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do 190 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
244 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]") 191 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${PYTHON_USE_WITH// /,}]")
245 done 192 done
246 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then 193 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
247 for _USE_flag in ${PYTHON_USE_WITH_OR}; do 194 for _USE_flag in ${PYTHON_USE_WITH_OR}; do
248 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do 195 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
249 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${_USE_flag}]") 196 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${_USE_flag}]")
265fi 212fi
266 213
267unset _PYTHON_ATOMS 214unset _PYTHON_ATOMS
268 215
269# ================================================================================================ 216# ================================================================================================
217# =================================== MISCELLANEOUS FUNCTIONS ====================================
218# ================================================================================================
219
220_python_implementation() {
221 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
222 return 0
223 elif [[ "${CATEGORY}/${PN}" == "dev-java/jython" ]]; then
224 return 0
225 else
226 return 1
227 fi
228}
229
230_python_package_supporting_installation_for_multiple_python_abis() {
231 if [[ "${EBUILD_PHASE}" == "depend" ]]; then
232 die "${FUNCNAME}() cannot be used in global scope"
233 fi
234
235 if has "${EAPI:-0}" 0 1 2 3 4; then
236 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
237 return 0
238 else
239 return 1
240 fi
241 else
242 die "${FUNCNAME}(): Support for EAPI=\"${EAPI}\" not implemented"
243 fi
244}
245
246_python_initialize_prefix_variables() {
247 if has "${EAPI:-0}" 0 1 2; then
248 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
249 EROOT="${ROOT%/}${EPREFIX}/"
250 fi
251 if [[ -n "${D}" && -z "${ED}" ]]; then
252 ED="${D%/}${EPREFIX}/"
253 fi
254 fi
255}
256
257unset PYTHON_SANITY_CHECKS
258
259_python_initial_sanity_checks() {
260 if [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
261 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
262 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
263 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
264 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
265 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
266 fi
267 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
268 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
269 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
270 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
271 fi
272 fi
273}
274
275_python_final_sanity_checks() {
276 if ! _python_implementation && [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
277 local PYTHON_ABI="${PYTHON_ABI}"
278 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI}}; do
279 # Ensure that appropriate version of Python is installed.
280 if ! has_version "$(python_get_implementational_package)"; then
281 die "$(python_get_implementational_package) is not installed"
282 fi
283
284 # Ensure that EPYTHON variable is respected.
285 if [[ "$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" != "${PYTHON_ABI}" ]]; then
286 eerror "Path to 'python': '$(type -p python)'"
287 eerror "ABI: '${ABI}'"
288 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
289 eerror "EPYTHON: '$(PYTHON)'"
290 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
291 eerror "Locally active version of Python: '$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")'"
292 die "'python' does not respect EPYTHON variable"
293 fi
294 done
295 fi
296 PYTHON_SANITY_CHECKS="1"
297}
298
299_python_set_color_variables() {
300 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
301 _BOLD=$'\e[1m'
302 _RED=$'\e[1;31m'
303 _GREEN=$'\e[1;32m'
304 _BLUE=$'\e[1;34m'
305 _CYAN=$'\e[1;36m'
306 _NORMAL=$'\e[0m'
307 else
308 _BOLD=
309 _RED=
310 _GREEN=
311 _BLUE=
312 _CYAN=
313 _NORMAL=
314 fi
315}
316
317unset PYTHON_PKG_SETUP_EXECUTED
318
319# @FUNCTION: python_pkg_setup
320# @DESCRIPTION:
321# Perform sanity checks and initialize environment.
322#
323# This function is exported in EAPI 2 and 3 when PYTHON_USE_WITH or PYTHON_USE_WITH_OR variable
324# is set and always in EAPI >=4. Calling of this function is mandatory in EAPI >=4.
325#
326# This function can be used only in pkg_setup() phase.
327python_pkg_setup() {
328 # Check if phase is pkg_setup().
329 [[ "${EBUILD_PHASE}" != "setup" ]] && die "${FUNCNAME}() can be used only in pkg_setup() phase"
330
331 if _python_package_supporting_installation_for_multiple_python_abis; then
332 validate_PYTHON_ABIS
333 export EPYTHON="$(PYTHON -f)"
334 else
335 PYTHON_ABI="$(PYTHON --ABI)"
336 fi
337
338 if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; then
339 if [[ "${PYTHON_USE_WITH_OPT}" ]]; then
340 if [[ "${PYTHON_USE_WITH_OPT}" == !* ]]; then
341 use ${PYTHON_USE_WITH_OPT#!} && return
342 else
343 use !${PYTHON_USE_WITH_OPT} && return
344 fi
345 fi
346
347 python_pkg_setup_check_USE_flags() {
348 local python_atom USE_flag
349 python_atom="$(python_get_implementational_package)"
350
351 for USE_flag in ${PYTHON_USE_WITH}; do
352 if ! has_version "${python_atom}[${USE_flag}]"; then
353 eerror "Please rebuild ${python_atom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
354 die "Please rebuild ${python_atom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
355 fi
356 done
357
358 for USE_flag in ${PYTHON_USE_WITH_OR}; do
359 if has_version "${python_atom}[${USE_flag}]"; then
360 return
361 fi
362 done
363
364 if [[ ${PYTHON_USE_WITH_OR} ]]; then
365 eerror "Please rebuild ${python_atom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
366 die "Please rebuild ${python_atom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
367 fi
368 }
369
370 if _python_package_supporting_installation_for_multiple_python_abis; then
371 python_execute_function -q python_pkg_setup_check_USE_flags
372 else
373 python_pkg_setup_check_USE_flags
374 fi
375
376 unset -f python_pkg_setup_check_USE_flags
377 fi
378
379 PYTHON_PKG_SETUP_EXECUTED="1"
380}
381
382if ! has "${EAPI:-0}" 0 1 2 3 || has "${EAPI:-0}" 2 3 && [[ -n "${PYTHON_USE_WITH}" || -n "${PYTHON_USE_WITH_OR}" ]]; then
383 EXPORT_FUNCTIONS pkg_setup
384fi
385
386# @FUNCTION: python_convert_shebangs
387# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
388# @DESCRIPTION:
389# Convert shebangs in specified files. Directories can be specified only with --recursive option.
390python_convert_shebangs() {
391 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
392
393 while (($#)); do
394 case "$1" in
395 -r|--recursive)
396 recursive="1"
397 ;;
398 -q|--quiet)
399 quiet="1"
400 ;;
401 -x|--only-executables)
402 only_executables="1"
403 ;;
404 --)
405 shift
406 break
407 ;;
408 -*)
409 die "${FUNCNAME}(): Unrecognized option '$1'"
410 ;;
411 *)
412 break
413 ;;
414 esac
415 shift
416 done
417
418 if [[ "$#" -eq 0 ]]; then
419 die "${FUNCNAME}(): Missing Python version and files or directories"
420 elif [[ "$#" -eq 1 ]]; then
421 die "${FUNCNAME}(): Missing files or directories"
422 fi
423
424 python_version="$1"
425 shift
426
427 for argument in "$@"; do
428 if [[ ! -e "${argument}" ]]; then
429 die "${FUNCNAME}(): '${argument}' does not exist"
430 elif [[ -f "${argument}" ]]; then
431 files+=("${argument}")
432 elif [[ -d "${argument}" ]]; then
433 if [[ "${recursive}" == "1" ]]; then
434 while read -d $'\0' -r file; do
435 files+=("${file}")
436 done < <(find "${argument}" $([[ "${only_executables}" == "1" ]] && echo -perm /111) -type f -print0)
437 else
438 die "${FUNCNAME}(): '${argument}' is not a regular file"
439 fi
440 else
441 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
442 fi
443 done
444
445 for file in "${files[@]}"; do
446 file="${file#./}"
447 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
448
449 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
450 [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue
451
452 if [[ "${quiet}" == "0" ]]; then
453 einfo "Converting shebang in '${file}'"
454 fi
455
456 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
457
458 # Delete potential whitespace after "#!".
459 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
460 fi
461 done
462}
463
464# @FUNCTION: python_clean_installation_image
465# @USAGE: [-q|--quiet]
466# @DESCRIPTION:
467# Delete needless files in installation image.
468python_clean_installation_image() {
469 _python_initialize_prefix_variables
470
471 local file files=() quiet="0"
472
473 # Check if phase is src_install().
474 [[ "${EBUILD_PHASE}" != "install" ]] && die "${FUNCNAME}() can be used only in src_install() phase"
475
476 while (($#)); do
477 case "$1" in
478 -q|--quiet)
479 quiet="1"
480 ;;
481 -*)
482 die "${FUNCNAME}(): Unrecognized option '$1'"
483 ;;
484 *)
485 die "${FUNCNAME}(): Invalid usage"
486 ;;
487 esac
488 shift
489 done
490
491 while read -d $'\0' -r file; do
492 files+=("${file}")
493 done < <(find "${ED}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -type f -print0)
494
495 if [[ "${#files[@]}" -gt 0 ]]; then
496 if [[ "${quiet}" == "0" ]]; then
497 ewarn "Deleting byte-compiled Python modules needlessly generated by build system:"
498 fi
499 for file in "${files[@]}"; do
500 if [[ "${quiet}" == "0" ]]; then
501 ewarn " ${file}"
502 fi
503 rm -f "${file}"
504 done
505 fi
506
507 python_clean_sitedirs() {
508 find "${ED}$(python_get_sitedir)" "(" -name "*.c" -o -name "*.h" -o -name "*.la" ")" -type f -print0 | xargs -0 rm -f
509 }
510 if _python_package_supporting_installation_for_multiple_python_abis; then
511 python_execute_function -q python_clean_sitedirs
512 else
513 python_clean_sitedirs
514 fi
515
516 unset -f python_clean_sitedirs
517}
518
519# ================================================================================================
270# =========== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ============ 520# =========== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE PYTHON ABIS ============
271# ================================================================================================ 521# ================================================================================================
272 522
273# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS 523# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
274# @DESCRIPTION: 524# @DESCRIPTION:
292 eval "python_${python_default_function}() { 542 eval "python_${python_default_function}() {
293 if ! _python_package_supporting_installation_for_multiple_python_abis; then 543 if ! _python_package_supporting_installation_for_multiple_python_abis; then
294 die \"\${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs\" 544 die \"\${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs\"
295 fi 545 fi
296 546
297 python_execute_function -d -s \"\$@\" 547 python_execute_function -d -s -- \"\$@\"
298 }" 548 }"
299 done 549 done
300 unset python_default_function 550 unset python_default_function
301 551
302 if [[ -n "${PYTHON_EXPORT_PHASE_FUNCTIONS}" ]]; then 552 if [[ -n "${PYTHON_EXPORT_PHASE_FUNCTIONS}" ]]; then
355 if [[ "${cpython_enabled}" == "0" ]]; then 605 if [[ "${cpython_enabled}" == "0" ]]; then
356 die "USE_PYTHON variable does not enable any CPython ABI" 606 die "USE_PYTHON variable does not enable any CPython ABI"
357 fi 607 fi
358 else 608 else
359 local python_version python2_version= python3_version= support_python_major_version 609 local python_version python2_version= python3_version= support_python_major_version
610
611 if ! has_version "dev-lang/python"; then
612 die "${FUNCNAME}(): 'dev-lang/python' is not installed"
613 fi
360 614
361 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')" 615 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
362 616
363 if has_version "=dev-lang/python-2*"; then 617 if has_version "=dev-lang/python-2*"; then
364 if [[ "$(readlink "${EPREFIX}/usr/bin/python2")" != "python2."* ]]; then 618 if [[ "$(readlink "${EPREFIX}/usr/bin/python2")" != "python2."* ]]; then
673 927
674 local dir dirs=() PYTHON_ABI 928 local dir dirs=() PYTHON_ABI
675 929
676 if [[ "$#" -eq 0 ]]; then 930 if [[ "$#" -eq 0 ]]; then
677 if [[ "${WORKDIR}" == "${S}" ]]; then 931 if [[ "${WORKDIR}" == "${S}" ]]; then
678 die "${FUNCNAME}() cannot be used" 932 die "${FUNCNAME}() cannot be used with current value of S variable"
679 fi 933 fi
680 dirs=("${S%/}") 934 dirs=("${S%/}")
681 else 935 else
682 dirs=("$@") 936 dirs=("$@")
683 fi 937 fi
907python_set_active_version() { 1161python_set_active_version() {
908 if _python_package_supporting_installation_for_multiple_python_abis; then 1162 if _python_package_supporting_installation_for_multiple_python_abis; then
909 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs" 1163 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
910 fi 1164 fi
911 1165
1166 if [[ -n "${PYTHON_PKG_SETUP_EXECUTED}" ]]; then
1167 die "${FUNCNAME}() should be called before python_pkg_setup()"
1168 fi
1169
912 if [[ "$#" -ne 1 ]]; then 1170 if [[ "$#" -ne 1 ]]; then
913 die "${FUNCNAME}() requires 1 argument" 1171 die "${FUNCNAME}() requires 1 argument"
914 fi 1172 fi
915 1173
916 _python_initial_sanity_checks 1174 _python_initial_sanity_checks
1043 validate_PYTHON_ABIS 1301 validate_PYTHON_ABIS
1044 PYTHON_ABI="${PYTHON_ABIS##* }" 1302 PYTHON_ABI="${PYTHON_ABIS##* }"
1045 elif [[ "${python2}" == "1" ]]; then 1303 elif [[ "${python2}" == "1" ]]; then
1046 PYTHON_ABI="$(eselect python show --python2 --ABI)" 1304 PYTHON_ABI="$(eselect python show --python2 --ABI)"
1047 if [[ -z "${PYTHON_ABI}" ]]; then 1305 if [[ -z "${PYTHON_ABI}" ]]; then
1048 die "${FUNCNAME}(): Active Python 2 interpreter not set" 1306 die "${FUNCNAME}(): Active version of Python 2 not set"
1049 elif [[ "${PYTHON_ABI}" != "2."* ]]; then 1307 elif [[ "${PYTHON_ABI}" != "2."* ]]; then
1050 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`" 1308 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1051 fi 1309 fi
1052 elif [[ "${python3}" == "1" ]]; then 1310 elif [[ "${python3}" == "1" ]]; then
1053 PYTHON_ABI="$(eselect python show --python3 --ABI)" 1311 PYTHON_ABI="$(eselect python show --python3 --ABI)"
1054 if [[ -z "${PYTHON_ABI}" ]]; then 1312 if [[ -z "${PYTHON_ABI}" ]]; then
1055 die "${FUNCNAME}(): Active Python 3 interpreter not set" 1313 die "${FUNCNAME}(): Active version of Python 3 not set"
1056 elif [[ "${PYTHON_ABI}" != "3."* ]]; then 1314 elif [[ "${PYTHON_ABI}" != "3."* ]]; then
1057 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`" 1315 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1058 fi 1316 fi
1059 elif ! _python_package_supporting_installation_for_multiple_python_abis; then 1317 elif ! _python_package_supporting_installation_for_multiple_python_abis; then
1060 PYTHON_ABI="$("${EPREFIX}/usr/bin/python" -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" 1318 PYTHON_ABI="$("${EPREFIX}/usr/bin/python" -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")"
1319 if [[ -z "${PYTHON_ABI}" ]]; then
1320 die "${FUNCNAME}(): Main active version of Python not set"
1321 fi
1061 elif [[ -z "${PYTHON_ABI}" ]]; then 1322 elif [[ -z "${PYTHON_ABI}" ]]; then
1062 die "${FUNCNAME}(): Invalid usage: ${FUNCNAME}() should be used in ABI-specific local scope" 1323 die "${FUNCNAME}(): Invalid usage: ${FUNCNAME}() should be used in ABI-specific local scope"
1063 fi 1324 fi
1064 elif [[ "$#" -eq 1 ]]; then 1325 elif [[ "$#" -eq 1 ]]; then
1065 if [[ "${final_ABI}" == "1" ]]; then 1326 if [[ "${final_ABI}" == "1" ]]; then
1396 "$(PYTHON ${PYTHON_ABI})" -c "${python_command}" 1657 "$(PYTHON ${PYTHON_ABI})" -c "${python_command}"
1397 fi 1658 fi
1398} 1659}
1399 1660
1400# ================================================================================================ 1661# ================================================================================================
1401# =================================== MISCELLANEOUS FUNCTIONS ====================================
1402# ================================================================================================
1403
1404_python_implementation() {
1405 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1406 return 0
1407 elif [[ "${CATEGORY}/${PN}" == "dev-java/jython" ]]; then
1408 return 0
1409 else
1410 return 1
1411 fi
1412}
1413
1414_python_package_supporting_installation_for_multiple_python_abis() {
1415 if [[ "${EBUILD_PHASE}" == "depend" ]]; then
1416 die "${FUNCNAME}() cannot be used in global scope"
1417 fi
1418
1419 if has "${EAPI:-0}" 0 1 2 3 4; then
1420 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1421 return 0
1422 else
1423 return 1
1424 fi
1425 else
1426 die "${FUNCNAME}(): Support for EAPI=\"${EAPI}\" not implemented"
1427 fi
1428}
1429
1430_python_initialize_prefix_variables() {
1431 if has "${EAPI:-0}" 0 1 2; then
1432 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
1433 EROOT="${ROOT%/}${EPREFIX}/"
1434 fi
1435 if [[ -n "${D}" && -z "${ED}" ]]; then
1436 ED="${D%/}${EPREFIX}/"
1437 fi
1438 fi
1439}
1440
1441unset PYTHON_SANITY_CHECKS
1442
1443_python_initial_sanity_checks() {
1444 if [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1445 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
1446 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
1447 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
1448 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1449 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
1450 fi
1451 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
1452 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
1453 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1454 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
1455 fi
1456 fi
1457}
1458
1459_python_final_sanity_checks() {
1460 if ! _python_implementation && [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1461 local PYTHON_ABI="${PYTHON_ABI}"
1462 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI}}; do
1463 # Ensure that appropriate version of Python is installed.
1464 if ! has_version "$(python_get_implementational_package)"; then
1465 die "$(python_get_implementational_package) is not installed"
1466 fi
1467
1468 # Ensure that EPYTHON variable is respected.
1469 if [[ "$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" != "${PYTHON_ABI}" ]]; then
1470 eerror "python: '$(type -p python)'"
1471 eerror "ABI: '${ABI}'"
1472 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
1473 eerror "EPYTHON: '$(PYTHON)'"
1474 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
1475 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")'"
1476 die "'python' does not respect EPYTHON variable"
1477 fi
1478 done
1479 fi
1480 PYTHON_SANITY_CHECKS="1"
1481}
1482
1483_python_set_color_variables() {
1484 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1485 _BOLD=$'\e[1m'
1486 _RED=$'\e[1;31m'
1487 _GREEN=$'\e[1;32m'
1488 _BLUE=$'\e[1;34m'
1489 _CYAN=$'\e[1;36m'
1490 _NORMAL=$'\e[0m'
1491 else
1492 _BOLD=
1493 _RED=
1494 _GREEN=
1495 _BLUE=
1496 _CYAN=
1497 _NORMAL=
1498 fi
1499}
1500
1501# @FUNCTION: python_convert_shebangs
1502# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1503# @DESCRIPTION:
1504# Convert shebangs in specified files. Directories can be specified only with --recursive option.
1505python_convert_shebangs() {
1506 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
1507
1508 while (($#)); do
1509 case "$1" in
1510 -r|--recursive)
1511 recursive="1"
1512 ;;
1513 -q|--quiet)
1514 quiet="1"
1515 ;;
1516 -x|--only-executables)
1517 only_executables="1"
1518 ;;
1519 --)
1520 shift
1521 break
1522 ;;
1523 -*)
1524 die "${FUNCNAME}(): Unrecognized option '$1'"
1525 ;;
1526 *)
1527 break
1528 ;;
1529 esac
1530 shift
1531 done
1532
1533 if [[ "$#" -eq 0 ]]; then
1534 die "${FUNCNAME}(): Missing Python version and files or directories"
1535 elif [[ "$#" -eq 1 ]]; then
1536 die "${FUNCNAME}(): Missing files or directories"
1537 fi
1538
1539 python_version="$1"
1540 shift
1541
1542 for argument in "$@"; do
1543 if [[ ! -e "${argument}" ]]; then
1544 die "${FUNCNAME}(): '${argument}' does not exist"
1545 elif [[ -f "${argument}" ]]; then
1546 files+=("${argument}")
1547 elif [[ -d "${argument}" ]]; then
1548 if [[ "${recursive}" == "1" ]]; then
1549 while read -d $'\0' -r file; do
1550 files+=("${file}")
1551 done < <(find "${argument}" $([[ "${only_executables}" == "1" ]] && echo -perm /111) -type f -print0)
1552 else
1553 die "${FUNCNAME}(): '${argument}' is not a regular file"
1554 fi
1555 else
1556 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
1557 fi
1558 done
1559
1560 for file in "${files[@]}"; do
1561 file="${file#./}"
1562 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1563
1564 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1565 [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue
1566
1567 if [[ "${quiet}" == "0" ]]; then
1568 einfo "Converting shebang in '${file}'"
1569 fi
1570
1571 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1572
1573 # Delete potential whitespace after "#!".
1574 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1575 fi
1576 done
1577}
1578
1579# @FUNCTION: python_clean_sitedirs
1580# @DESCRIPTION:
1581# Delete needless files in site-packages directories in ${ED}.
1582python_clean_sitedirs() {
1583 _python_initialize_prefix_variables
1584
1585 find "${ED}"usr/$(get_libdir)/python*/site-packages "(" -name "*.c" -o -name "*.h" -o -name "*.la" ")" -type f -print0 | xargs -0 rm -f
1586}
1587
1588# ================================================================================================
1589# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================ 1662# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1590# ================================================================================================ 1663# ================================================================================================
1591 1664
1592# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY 1665# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
1593# @DESCRIPTION: 1666# @DESCRIPTION:
1826# Tell Python not to automatically recompile modules to .pyc/.pyo 1899# Tell Python not to automatically recompile modules to .pyc/.pyo
1827# even if the timestamps/version stamps do not match. This is done 1900# even if the timestamps/version stamps do not match. This is done
1828# to protect sandbox. 1901# to protect sandbox.
1829python_disable_pyc() { 1902python_disable_pyc() {
1830 export PYTHONDONTWRITEBYTECODE="1" 1903 export PYTHONDONTWRITEBYTECODE="1"
1904}
1905
1906_python_clean_compiled_modules() {
1907 _python_initialize_prefix_variables
1908 _python_set_color_variables
1909
1910 [[ "${FUNCNAME[1]}" =~ ^(python_mod_optimize|python_mod_compile|python_mod_cleanup)$ ]] || die "${FUNCNAME}(): Invalid usage"
1911
1912 local base_module_name compiled_file compiled_files=() dir path py_file root
1913
1914 # Strip trailing slash from EROOT.
1915 root="${EROOT%/}"
1916
1917 for path in "$@"; do
1918 compiled_files=()
1919 if [[ -d "${path}" ]]; then
1920 while read -d $'\0' -r compiled_file; do
1921 compiled_files+=("${compiled_file}")
1922 done < <(find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0)
1923
1924 if [[ "${EBUILD_PHASE}" == "postrm" ]]; then
1925 # Delete empty child directories.
1926 find "${path}" -type d | sort -r | while read -r dir; do
1927 rmdir "${dir}" 2> /dev/null && echo "${_CYAN}<<< ${dir}${_NORMAL}"
1928 done
1929 fi
1930 elif [[ "${path}" == *.py ]]; then
1931 base_module_name="${path##*/}"
1932 base_module_name="${base_module_name%.py}"
1933 if [[ -d "${path%/*}/__pycache__" ]]; then
1934 while read -d $'\0' -r compiled_file; do
1935 compiled_files+=("${compiled_file}")
1936 done < <(find "${path%/*}/__pycache__" "(" -name "${base_module_name}.*.py[co]" -o -name "${base_module_name}\$py.class" ")" -print0)
1937 fi
1938 compiled_files+=("${path}c" "${path}o" "${path%.py}\$py.class")
1939 fi
1940
1941 for compiled_file in "${compiled_files[@]}"; do
1942 [[ ! -f "${compiled_file}" ]] && continue
1943 dir="${compiled_file%/*}"
1944 dir="${dir##*/}"
1945 if [[ "${compiled_file}" == *.py[co] ]]; then
1946 if [[ "${dir}" == "__pycache__" ]]; then
1947 base_module_name="${compiled_file##*/}"
1948 base_module_name="${base_module_name%%.*py[co]}"
1949 py_file="${compiled_file%__pycache__/*}${base_module_name}.py"
1950 else
1951 py_file="${compiled_file%[co]}"
1952 fi
1953 if [[ "${EBUILD_PHASE}" == "postinst" ]]; then
1954 [[ -f "${py_file}" && "${compiled_file}" -nt "${py_file}" ]] && continue
1955 else
1956 [[ -f "${py_file}" ]] && continue
1957 fi
1958 echo "${_BLUE}<<< ${compiled_file%[co]}[co]${_NORMAL}"
1959 rm -f "${compiled_file%[co]}"[co]
1960 elif [[ "${compiled_file}" == *\$py.class ]]; then
1961 if [[ "${dir}" == "__pycache__" ]]; then
1962 base_module_name="${compiled_file##*/}"
1963 base_module_name="${base_module_name%\$py.class}"
1964 py_file="${compiled_file%__pycache__/*}${base_module_name}.py"
1965 else
1966 py_file="${compiled_file%\$py.class}"
1967 fi
1968 if [[ "${EBUILD_PHASE}" == "postinst" ]]; then
1969 [[ -f "${py_file}" && "${compiled_file}" -nt "${py_file}" ]] && continue
1970 else
1971 [[ -f "${py_file}" ]] && continue
1972 fi
1973 echo "${_BLUE}<<< ${compiled_file}${_NORMAL}"
1974 rm -f "${compiled_file}"
1975 else
1976 die "${FUNCNAME}(): Unrecognized file type: '${compiled_file}'"
1977 fi
1978
1979 # Delete empty parent directories.
1980 dir="${compiled_file%/*}"
1981 while [[ "${dir}" != "${root}" ]]; do
1982 if rmdir "${compiled_file%/*}" 2> /dev/null; then
1983 echo "${_CYAN}<<< ${compiled_file%/*}${_NORMAL}"
1984 else
1985 break
1986 fi
1987 dir="${dir%/*}"
1988 done
1989 done
1990 done
1831} 1991}
1832 1992
1833# @FUNCTION: python_mod_optimize 1993# @FUNCTION: python_mod_optimize
1834# @USAGE: [options] [directory|file] 1994# @USAGE: [options] [directory|file]
1835# @DESCRIPTION: 1995# @DESCRIPTION:
1847 2007
1848 # Check if phase is pkg_postinst(). 2008 # Check if phase is pkg_postinst().
1849 [[ "${EBUILD_PHASE}" != "postinst" ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase" 2009 [[ "${EBUILD_PHASE}" != "postinst" ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase"
1850 2010
1851 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then 2011 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
2012 # PYTHON_ABI variable cannot be local in packages not supporting installation for multiple Python ABIs.
1852 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI="${PYTHON_ABI}" return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=() 2013 local dir file iterated_PYTHON_ABIS options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
1853 2014
2015 if _python_package_supporting_installation_for_multiple_python_abis; then
2016 if [[ -z "${PYTHON_ABIS}" ]]; then
2017 die "${FUNCNAME}(): Environment not initialized"
2018 fi
2019 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
2020 else
2021 iterated_PYTHON_ABIS="${PYTHON_ABI:=$(PYTHON --ABI)}"
2022 fi
2023
1854 # Strip trailing slash from ROOT. 2024 # Strip trailing slash from EROOT.
1855 root="${EROOT%/}" 2025 root="${EROOT%/}"
1856 2026
1857 # Respect ROOT and options passed to compileall.py. 2027 # Respect EROOT and options passed to compileall.py.
1858 while (($#)); do 2028 while (($#)); do
1859 case "$1" in 2029 case "$1" in
1860 -l|-f|-q) 2030 -l|-f|-q)
1861 options+=("$1") 2031 options+=("$1")
1862 ;; 2032 ;;
1866 ;; 2036 ;;
1867 -*) 2037 -*)
1868 ewarn "${FUNCNAME}(): Ignoring option '$1'" 2038 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1869 ;; 2039 ;;
1870 *) 2040 *)
1871 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 2041 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1872 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories" 2042 die "${FUNCNAME}(): Paths of directories / files in site-packages directories must be relative to site-packages directories"
1873 elif [[ "$1" =~ ^/ ]]; then 2043 elif [[ "$1" =~ ^/ ]]; then
2044 if _python_package_supporting_installation_for_multiple_python_abis; then
2045 die "${FUNCNAME}(): Absolute paths cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
2046 fi
1874 if [[ -d "${root}/$1" ]]; then 2047 if [[ -d "${root}$1" ]]; then
1875 other_dirs+=("${root}/$1") 2048 other_dirs+=("${root}$1")
1876 elif [[ -f "${root}/$1" ]]; then 2049 elif [[ -f "${root}$1" ]]; then
1877 other_files+=("${root}/$1") 2050 other_files+=("${root}$1")
1878 elif [[ -e "${root}/$1" ]]; then 2051 elif [[ -e "${root}$1" ]]; then
1879 ewarn "'${root}/$1' is not a file or a directory!" 2052 ewarn "'${root}$1' is not a file or a directory!"
1880 else 2053 else
1881 ewarn "'${root}/$1' does not exist!" 2054 ewarn "'${root}$1' does not exist!"
1882 fi 2055 fi
1883 else 2056 else
1884 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do 2057 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
1885 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then 2058 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
1886 site_packages_dirs+=("$1") 2059 site_packages_dirs+=("$1")
1887 break 2060 break
1888 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then 2061 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
1889 site_packages_files+=("$1") 2062 site_packages_files+=("$1")
1901 done 2074 done
1902 2075
1903 # Set additional options. 2076 # Set additional options.
1904 options+=("-q") 2077 options+=("-q")
1905 2078
1906 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do 2079 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
1907 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 2080 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
1908 return_code="0" 2081 return_code="0"
1909 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)" 2082 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
1910 if ((${#site_packages_dirs[@]})); then 2083 if ((${#site_packages_dirs[@]})); then
1911 for dir in "${site_packages_dirs[@]}"; do 2084 for dir in "${site_packages_dirs[@]}"; do
1913 done 2086 done
1914 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" 2087 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
1915 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then 2088 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then
1916 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1" 2089 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
1917 fi 2090 fi
2091 _python_clean_compiled_modules "${site_packages_absolute_dirs[@]}"
1918 fi 2092 fi
1919 if ((${#site_packages_files[@]})); then 2093 if ((${#site_packages_files[@]})); then
1920 for file in "${site_packages_files[@]}"; do 2094 for file in "${site_packages_files[@]}"; do
1921 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}") 2095 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
1922 done 2096 done
1923 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" 2097 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
1924 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then 2098 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then
1925 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1" 2099 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
1926 fi 2100 fi
2101 _python_clean_compiled_modules "${site_packages_absolute_files[@]}"
1927 fi 2102 fi
1928 eend "${return_code}" 2103 eend "${return_code}"
1929 fi 2104 fi
1930 unset site_packages_absolute_dirs site_packages_absolute_files 2105 unset site_packages_absolute_dirs site_packages_absolute_files
1931 done 2106 done
1932 2107
2108 if _python_package_supporting_installation_for_multiple_python_abis; then
1933 # Restore previous value of PYTHON_ABI. 2109 # Restore previous value of PYTHON_ABI.
1934 if [[ -n "${previous_PYTHON_ABI}" ]]; then 2110 if [[ -n "${previous_PYTHON_ABI}" ]]; then
1935 PYTHON_ABI="${previous_PYTHON_ABI}" 2111 PYTHON_ABI="${previous_PYTHON_ABI}"
1936 else 2112 else
1937 unset PYTHON_ABI 2113 unset PYTHON_ABI
2114 fi
1938 fi 2115 fi
1939 2116
1940 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 2117 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1941 return_code="0" 2118 return_code="0"
1942 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation) $(python_get_version)" 2119 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation) $(python_get_version)"
1943 if ((${#other_dirs[@]})); then 2120 if ((${#other_dirs[@]})); then
1944 "$(PYTHON ${PYTHON_ABI})" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 2121 "$(PYTHON ${PYTHON_ABI})" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
1945 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then 2122 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
1946 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" 2123 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
1947 fi 2124 fi
2125 _python_clean_compiled_modules "${other_dirs[@]}"
1948 fi 2126 fi
1949 if ((${#other_files[@]})); then 2127 if ((${#other_files[@]})); then
1950 "$(PYTHON ${PYTHON_ABI})" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 2128 "$(PYTHON ${PYTHON_ABI})" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
1951 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then 2129 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
1952 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1" 2130 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
1953 fi 2131 fi
2132 _python_clean_compiled_modules "${other_dirs[@]}"
1954 fi 2133 fi
1955 eend "${return_code}" 2134 eend "${return_code}"
1956 fi 2135 fi
1957 else 2136 else
1958 local myroot mydirs=() myfiles=() myopts=() return_code="0" 2137 local myroot mydirs=() myfiles=() myopts=() return_code="0"
1959 2138
1960 # strip trailing slash 2139 # strip trailing slash
1961 myroot="${EROOT%/}" 2140 myroot="${EROOT%/}"
1962 2141
1963 # respect ROOT and options passed to compileall.py 2142 # respect EROOT and options passed to compileall.py
1964 while (($#)); do 2143 while (($#)); do
1965 case "$1" in 2144 case "$1" in
1966 -l|-f|-q) 2145 -l|-f|-q)
1967 myopts+=("$1") 2146 myopts+=("$1")
1968 ;; 2147 ;;
1972 ;; 2151 ;;
1973 -*) 2152 -*)
1974 ewarn "${FUNCNAME}(): Ignoring option '$1'" 2153 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1975 ;; 2154 ;;
1976 *) 2155 *)
1977 if [[ -d "${myroot}"/$1 ]]; then 2156 if [[ -d "${myroot}/${1#/}" ]]; then
1978 mydirs+=("${myroot}/$1") 2157 mydirs+=("${myroot}/${1#/}")
1979 elif [[ -f "${myroot}"/$1 ]]; then 2158 elif [[ -f "${myroot}/${1#/}" ]]; then
1980 # Files are passed to python_mod_compile which is ROOT-aware 2159 # Files are passed to python_mod_compile which is EROOT-aware
1981 myfiles+=("$1") 2160 myfiles+=("$1")
1982 elif [[ -e "${myroot}/$1" ]]; then 2161 elif [[ -e "${myroot}/${1#/}" ]]; then
1983 ewarn "${myroot}/$1 is not a file or directory!" 2162 ewarn "${myroot}/${1#/} is not a file or directory!"
1984 else 2163 else
1985 ewarn "${myroot}/$1 does not exist!" 2164 ewarn "${myroot}/${1#/} does not exist!"
1986 fi 2165 fi
1987 ;; 2166 ;;
1988 esac 2167 esac
1989 shift 2168 shift
1990 done 2169 done
1991 2170
1992 # set additional opts 2171 # set additional opts
1993 myopts+=(-q) 2172 myopts+=(-q)
1994 2173
2174 PYTHON_ABI="$(PYTHON --ABI)"
2175
1995 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)" 2176 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
1996 if ((${#mydirs[@]})); then 2177 if ((${#mydirs[@]})); then
1997 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1" 2178 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
1998 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1" 2179 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
2180 _python_clean_compiled_modules "${mydirs[@]}"
1999 fi 2181 fi
2000 2182
2001 if ((${#myfiles[@]})); then 2183 if ((${#myfiles[@]})); then
2002 python_mod_compile "${myfiles[@]}" 2184 python_mod_compile "${myfiles[@]}"
2003 fi 2185 fi
2017# If they are, then it will remove their corresponding .pyc and .pyo files. 2199# If they are, then it will remove their corresponding .pyc and .pyo files.
2018# 2200#
2019# This function can be used only in pkg_postrm() phase. 2201# This function can be used only in pkg_postrm() phase.
2020python_mod_cleanup() { 2202python_mod_cleanup() {
2021 _python_initialize_prefix_variables 2203 _python_initialize_prefix_variables
2022 _python_set_color_variables
2023 2204
2024 local path py_file PYTHON_ABI="${PYTHON_ABI}" SEARCH_PATH=() root 2205 local dir iterated_PYTHON_ABIS PYTHON_ABI="${PYTHON_ABI}" root search_paths=() sitedir
2025 2206
2026 # Check if phase is pkg_postrm(). 2207 # Check if phase is pkg_postrm().
2027 [[ "${EBUILD_PHASE}" != "postrm" ]] && die "${FUNCNAME}() can be used only in pkg_postrm() phase" 2208 [[ "${EBUILD_PHASE}" != "postrm" ]] && die "${FUNCNAME}() can be used only in pkg_postrm() phase"
2028 2209
2210 if _python_package_supporting_installation_for_multiple_python_abis; then
2211 if [[ -z "${PYTHON_ABIS}" ]]; then
2212 die "${FUNCNAME}(): Environment not initialized"
2213 fi
2214 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
2215 else
2216 iterated_PYTHON_ABIS="${PYTHON_ABI:-$(PYTHON --ABI)}"
2217 fi
2218
2029 # Strip trailing slash from ROOT. 2219 # Strip trailing slash from EROOT.
2030 root="${EROOT%/}" 2220 root="${EROOT%/}"
2031 2221
2032 if (($#)); then 2222 if (($#)); then
2033 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then 2223 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
2034 while (($#)); do 2224 while (($#)); do
2035 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 2225 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
2036 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories" 2226 die "${FUNCNAME}(): Paths of directories / files in site-packages directories must be relative to site-packages directories"
2037 elif [[ "$1" =~ ^/ ]]; then 2227 elif [[ "$1" =~ ^/ ]]; then
2038 SEARCH_PATH+=("${root}/${1#/}") 2228 if _python_package_supporting_installation_for_multiple_python_abis; then
2229 die "${FUNCNAME}(): Absolute paths cannot be used in ebuilds of packages supporting installation for multiple Python ABIs"
2230 fi
2231 search_paths+=("${root}$1")
2039 else 2232 else
2040 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do 2233 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
2041 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 2234 search_paths+=("${root}$(python_get_sitedir)/$1")
2042 done 2235 done
2043 fi 2236 fi
2044 shift 2237 shift
2045 done 2238 done
2046 else 2239 else
2047 SEARCH_PATH=("${@#/}") 2240 search_paths=("${@#/}")
2048 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 2241 search_paths=("${search_paths[@]/#/${root}/}")
2049 fi 2242 fi
2050 else 2243 else
2051 local dir sitedir
2052 for dir in "${root}"/usr/lib*; do 2244 for dir in "${root}"/usr/lib*; do
2053 if [[ -d "${dir}" && ! -L "${dir}" ]]; then 2245 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
2054 for sitedir in "${dir}"/python*/site-packages; do 2246 for sitedir in "${dir}"/python*/site-packages; do
2055 if [[ -d "${sitedir}" ]]; then 2247 if [[ -d "${sitedir}" ]]; then
2056 SEARCH_PATH+=("${sitedir}") 2248 search_paths+=("${sitedir}")
2057 fi 2249 fi
2058 done 2250 done
2059 fi 2251 fi
2060 done 2252 done
2061 for sitedir in "${root}"/usr/share/jython-*/Lib/site-packages; do 2253 for sitedir in "${root}"/usr/share/jython-*/Lib/site-packages; do
2062 if [[ -d "${sitedir}" ]]; then 2254 if [[ -d "${sitedir}" ]]; then
2063 SEARCH_PATH+=("${sitedir}") 2255 search_paths+=("${sitedir}")
2064 fi 2256 fi
2065 done 2257 done
2066 fi 2258 fi
2067 2259
2068 for path in "${SEARCH_PATH[@]}"; do 2260 _python_clean_compiled_modules "${search_paths[@]}"
2069 if [[ -d "${path}" ]]; then
2070 find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0 | while read -rd ''; do
2071 if [[ "${REPLY}" == *[co] ]]; then
2072 py_file="${REPLY%[co]}"
2073 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
2074 echo "${_BLUE}<<< ${py_file}[co]${_NORMAL}"
2075 rm -f "${py_file}"[co]
2076 elif [[ "${REPLY}" == *\$py.class ]]; then
2077 py_file="${REPLY%\$py.class}.py"
2078 [[ -f "${py_file}" || ! -f "${py_file%.py}\$py.class" ]] && continue
2079 echo "${_BLUE}<<< ${py_file%.py}\$py.class${_NORMAL}"
2080 rm -f "${py_file%.py}\$py.class"
2081 fi
2082 done
2083
2084 # Attempt to delete directories, which may be empty.
2085 find "${path}" -type d | sort -r | while read -r dir; do
2086 rmdir "${dir}" 2>/dev/null && echo "${_CYAN}<<< ${dir}${_NORMAL}"
2087 done
2088 elif [[ "${path}" == *.py && ! -f "${path}" ]]; then
2089 if [[ (-f "${path}c" || -f "${path}o") ]]; then
2090 echo "${_BLUE}<<< ${path}[co]${_NORMAL}"
2091 rm -f "${path}"[co]
2092 fi
2093 if [[ -f "${path%.py}\$py.class" ]]; then
2094 echo "${_BLUE}<<< ${path%.py}\$py.class${_NORMAL}"
2095 rm -f "${path%.py}\$py.class"
2096 fi
2097 fi
2098 done
2099} 2261}
2100 2262
2101# ================================================================================================ 2263# ================================================================================================
2102# ===================================== DEPRECATED FUNCTIONS ===================================== 2264# ===================================== DEPRECATED FUNCTIONS =====================================
2103# ================================================================================================ 2265# ================================================================================================
2157 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then 2319 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
2158 eerror "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()." 2320 eerror "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()."
2159 die "${FUNCNAME}() cannot be used in this EAPI" 2321 die "${FUNCNAME}() cannot be used in this EAPI"
2160 fi 2322 fi
2161 2323
2324 _python_set_color_variables
2325
2162 echo 2326 echo
2163 echo " ${_RED}*${_NORMAL} ${_RED}Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01.${_NORMAL}" 2327 echo " ${_RED}*${_NORMAL} ${_RED}Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01.${_NORMAL}"
2164 echo " ${_RED}*${_NORMAL} ${_RED}Use USE dependencies and/or has_version() instead of ${FUNCNAME}().${_NORMAL}" 2328 echo " ${_RED}*${_NORMAL} ${_RED}Use USE dependencies and/or has_version() instead of ${FUNCNAME}().${_NORMAL}"
2165 echo " ${_RED}*${_NORMAL} ${_RED}The ebuild needs to be fixed. Please report a bug, if it has not been already reported.${_NORMAL}" 2329 echo " ${_RED}*${_NORMAL} ${_RED}The ebuild needs to be fixed. Please report a bug, if it has not been already reported.${_NORMAL}"
2166 echo 2330 echo
2184python_tkinter_exists() { 2348python_tkinter_exists() {
2185 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then 2349 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
2186 eerror "Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}()." 2350 eerror "Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}()."
2187 die "${FUNCNAME}() cannot be used in this EAPI" 2351 die "${FUNCNAME}() cannot be used in this EAPI"
2188 fi 2352 fi
2353
2354 _python_set_color_variables
2189 2355
2190 if [[ "${FUNCNAME[1]}" != "distutils_python_tkinter" ]]; then 2356 if [[ "${FUNCNAME[1]}" != "distutils_python_tkinter" ]]; then
2191 echo 2357 echo
2192 echo " ${_RED}*${_NORMAL} ${_RED}Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01.${_NORMAL}" 2358 echo " ${_RED}*${_NORMAL} ${_RED}Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01.${_NORMAL}"
2193 echo " ${_RED}*${_NORMAL} ${_RED}Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}().${_NORMAL}" 2359 echo " ${_RED}*${_NORMAL} ${_RED}Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}().${_NORMAL}"
2236 [[ "${EBUILD_PHASE}" != "postinst" ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase" 2402 [[ "${EBUILD_PHASE}" != "postinst" ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase"
2237 2403
2238 # strip trailing slash 2404 # strip trailing slash
2239 myroot="${EROOT%/}" 2405 myroot="${EROOT%/}"
2240 2406
2241 # respect ROOT 2407 # respect EROOT
2242 for f in "$@"; do 2408 for f in "$@"; do
2243 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") 2409 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
2244 done 2410 done
2411
2412 PYTHON_ABI="$(PYTHON --ABI)"
2245 2413
2246 if ((${#myfiles[@]})); then 2414 if ((${#myfiles[@]})); then
2247 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" 2415 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
2248 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null 2416 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
2417 _python_clean_compiled_modules "${myfiles[@]}"
2249 else 2418 else
2250 ewarn "No files to compile!" 2419 ewarn "No files to compile!"
2251 fi 2420 fi
2252} 2421}

Legend:
Removed from v.1.96  
changed lines
  Added in v.1.97

  ViewVC Help
Powered by ViewVC 1.1.20