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

Diff of /eclass/python.eclass

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

Revision 1.95 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.95 2010/03/20 17:59:40 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
846 fi 1100 fi
847 cat << EOF >> "${file}" 1101 cat << EOF >> "${file}"
848 1102
849wrapper_script_path = os.path.realpath(sys.argv[0]) 1103wrapper_script_path = os.path.realpath(sys.argv[0])
850target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI) 1104target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
1105os.environ["GENTOO_PYTHON_PROCESS_NAME"] = os.path.basename(sys.argv[0])
851os.environ["GENTOO_PYTHON_WRAPPER_SCRIPT_PATH"] = sys.argv[0] 1106os.environ["GENTOO_PYTHON_WRAPPER_SCRIPT_PATH"] = sys.argv[0]
852os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH"] = target_executable_path 1107os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH"] = target_executable_path
853if not os.path.exists(target_executable_path): 1108if not os.path.exists(target_executable_path):
854 sys.stderr.write("'%s' does not exist\n" % target_executable_path) 1109 sys.stderr.write("'%s' does not exist\n" % target_executable_path)
855 sys.exit(1) 1110 sys.exit(1)
904# @DESCRIPTION: 1159# @DESCRIPTION:
905# Set specified version of CPython as active version of Python. 1160# Set specified version of CPython as active version of Python.
906python_set_active_version() { 1161python_set_active_version() {
907 if _python_package_supporting_installation_for_multiple_python_abis; then 1162 if _python_package_supporting_installation_for_multiple_python_abis; then
908 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"
1164 fi
1165
1166 if [[ -n "${PYTHON_PKG_SETUP_EXECUTED}" ]]; then
1167 die "${FUNCNAME}() should be called before python_pkg_setup()"
909 fi 1168 fi
910 1169
911 if [[ "$#" -ne 1 ]]; then 1170 if [[ "$#" -ne 1 ]]; then
912 die "${FUNCNAME}() requires 1 argument" 1171 die "${FUNCNAME}() requires 1 argument"
913 fi 1172 fi
1042 validate_PYTHON_ABIS 1301 validate_PYTHON_ABIS
1043 PYTHON_ABI="${PYTHON_ABIS##* }" 1302 PYTHON_ABI="${PYTHON_ABIS##* }"
1044 elif [[ "${python2}" == "1" ]]; then 1303 elif [[ "${python2}" == "1" ]]; then
1045 PYTHON_ABI="$(eselect python show --python2 --ABI)" 1304 PYTHON_ABI="$(eselect python show --python2 --ABI)"
1046 if [[ -z "${PYTHON_ABI}" ]]; then 1305 if [[ -z "${PYTHON_ABI}" ]]; then
1047 die "${FUNCNAME}(): Active Python 2 interpreter not set" 1306 die "${FUNCNAME}(): Active version of Python 2 not set"
1048 elif [[ "${PYTHON_ABI}" != "2."* ]]; then 1307 elif [[ "${PYTHON_ABI}" != "2."* ]]; then
1049 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`" 1308 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1050 fi 1309 fi
1051 elif [[ "${python3}" == "1" ]]; then 1310 elif [[ "${python3}" == "1" ]]; then
1052 PYTHON_ABI="$(eselect python show --python3 --ABI)" 1311 PYTHON_ABI="$(eselect python show --python3 --ABI)"
1053 if [[ -z "${PYTHON_ABI}" ]]; then 1312 if [[ -z "${PYTHON_ABI}" ]]; then
1054 die "${FUNCNAME}(): Active Python 3 interpreter not set" 1313 die "${FUNCNAME}(): Active version of Python 3 not set"
1055 elif [[ "${PYTHON_ABI}" != "3."* ]]; then 1314 elif [[ "${PYTHON_ABI}" != "3."* ]]; then
1056 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`" 1315 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1057 fi 1316 fi
1058 elif ! _python_package_supporting_installation_for_multiple_python_abis; then 1317 elif ! _python_package_supporting_installation_for_multiple_python_abis; then
1059 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
1060 elif [[ -z "${PYTHON_ABI}" ]]; then 1322 elif [[ -z "${PYTHON_ABI}" ]]; then
1061 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"
1062 fi 1324 fi
1063 elif [[ "$#" -eq 1 ]]; then 1325 elif [[ "$#" -eq 1 ]]; then
1064 if [[ "${final_ABI}" == "1" ]]; then 1326 if [[ "${final_ABI}" == "1" ]]; then
1395 "$(PYTHON ${PYTHON_ABI})" -c "${python_command}" 1657 "$(PYTHON ${PYTHON_ABI})" -c "${python_command}"
1396 fi 1658 fi
1397} 1659}
1398 1660
1399# ================================================================================================ 1661# ================================================================================================
1400# =================================== MISCELLANEOUS FUNCTIONS ====================================
1401# ================================================================================================
1402
1403_python_implementation() {
1404 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1405 return 0
1406 elif [[ "${CATEGORY}/${PN}" == "dev-java/jython" ]]; then
1407 return 0
1408 else
1409 return 1
1410 fi
1411}
1412
1413_python_package_supporting_installation_for_multiple_python_abis() {
1414 if [[ "${EBUILD_PHASE}" == "depend" ]]; then
1415 die "${FUNCNAME}() cannot be used in global scope"
1416 fi
1417
1418 if has "${EAPI:-0}" 0 1 2 3 4; then
1419 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1420 return 0
1421 else
1422 return 1
1423 fi
1424 else
1425 die "${FUNCNAME}(): Support for EAPI=\"${EAPI}\" not implemented"
1426 fi
1427}
1428
1429_python_initialize_prefix_variables() {
1430 if has "${EAPI:-0}" 0 1 2; then
1431 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
1432 EROOT="${ROOT%/}${EPREFIX}/"
1433 fi
1434 if [[ -n "${D}" && -z "${ED}" ]]; then
1435 ED="${D%/}${EPREFIX}/"
1436 fi
1437 fi
1438}
1439
1440unset PYTHON_SANITY_CHECKS
1441
1442_python_initial_sanity_checks() {
1443 if [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1444 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
1445 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
1446 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
1447 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1448 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
1449 fi
1450 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
1451 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
1452 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1453 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
1454 fi
1455 fi
1456}
1457
1458_python_final_sanity_checks() {
1459 if ! _python_implementation && [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1460 local PYTHON_ABI="${PYTHON_ABI}"
1461 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI}}; do
1462 # Ensure that appropriate version of Python is installed.
1463 if ! has_version "$(python_get_implementational_package)"; then
1464 die "$(python_get_implementational_package) is not installed"
1465 fi
1466
1467 # Ensure that EPYTHON variable is respected.
1468 if [[ "$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" != "${PYTHON_ABI}" ]]; then
1469 eerror "python: '$(type -p python)'"
1470 eerror "ABI: '${ABI}'"
1471 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
1472 eerror "EPYTHON: '$(PYTHON)'"
1473 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
1474 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")'"
1475 die "'python' does not respect EPYTHON variable"
1476 fi
1477 done
1478 fi
1479 PYTHON_SANITY_CHECKS="1"
1480}
1481
1482_python_set_color_variables() {
1483 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1484 _BOLD=$'\e[1m'
1485 _RED=$'\e[1;31m'
1486 _GREEN=$'\e[1;32m'
1487 _BLUE=$'\e[1;34m'
1488 _CYAN=$'\e[1;36m'
1489 _NORMAL=$'\e[0m'
1490 else
1491 _BOLD=
1492 _RED=
1493 _GREEN=
1494 _BLUE=
1495 _CYAN=
1496 _NORMAL=
1497 fi
1498}
1499
1500# @FUNCTION: python_convert_shebangs
1501# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1502# @DESCRIPTION:
1503# Convert shebangs in specified files. Directories can be specified only with --recursive option.
1504python_convert_shebangs() {
1505 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
1506
1507 while (($#)); do
1508 case "$1" in
1509 -r|--recursive)
1510 recursive="1"
1511 ;;
1512 -q|--quiet)
1513 quiet="1"
1514 ;;
1515 -x|--only-executables)
1516 only_executables="1"
1517 ;;
1518 --)
1519 shift
1520 break
1521 ;;
1522 -*)
1523 die "${FUNCNAME}(): Unrecognized option '$1'"
1524 ;;
1525 *)
1526 break
1527 ;;
1528 esac
1529 shift
1530 done
1531
1532 if [[ "$#" -eq 0 ]]; then
1533 die "${FUNCNAME}(): Missing Python version and files or directories"
1534 elif [[ "$#" -eq 1 ]]; then
1535 die "${FUNCNAME}(): Missing files or directories"
1536 fi
1537
1538 python_version="$1"
1539 shift
1540
1541 for argument in "$@"; do
1542 if [[ ! -e "${argument}" ]]; then
1543 die "${FUNCNAME}(): '${argument}' does not exist"
1544 elif [[ -f "${argument}" ]]; then
1545 files+=("${argument}")
1546 elif [[ -d "${argument}" ]]; then
1547 if [[ "${recursive}" == "1" ]]; then
1548 while read -d $'\0' -r file; do
1549 files+=("${file}")
1550 done < <(find "${argument}" $([[ "${only_executables}" == "1" ]] && echo -perm /111) -type f -print0)
1551 else
1552 die "${FUNCNAME}(): '${argument}' is not a regular file"
1553 fi
1554 else
1555 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
1556 fi
1557 done
1558
1559 for file in "${files[@]}"; do
1560 file="${file#./}"
1561 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1562
1563 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1564 [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue
1565
1566 if [[ "${quiet}" == "0" ]]; then
1567 einfo "Converting shebang in '${file}'"
1568 fi
1569
1570 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1571
1572 # Delete potential whitespace after "#!".
1573 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1574 fi
1575 done
1576}
1577
1578# @FUNCTION: python_clean_sitedirs
1579# @DESCRIPTION:
1580# Delete needless files in site-packages directories in ${ED}.
1581python_clean_sitedirs() {
1582 _python_initialize_prefix_variables
1583
1584 find "${ED}"usr/$(get_libdir)/python*/site-packages "(" -name "*.c" -o -name "*.h" -o -name "*.la" ")" -type f -print0 | xargs -0 rm -f
1585}
1586
1587# ================================================================================================
1588# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================ 1662# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1589# ================================================================================================ 1663# ================================================================================================
1590 1664
1591# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY 1665# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
1592# @DESCRIPTION: 1666# @DESCRIPTION:
1825# Tell Python not to automatically recompile modules to .pyc/.pyo 1899# Tell Python not to automatically recompile modules to .pyc/.pyo
1826# 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
1827# to protect sandbox. 1901# to protect sandbox.
1828python_disable_pyc() { 1902python_disable_pyc() {
1829 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
1830} 1991}
1831 1992
1832# @FUNCTION: python_mod_optimize 1993# @FUNCTION: python_mod_optimize
1833# @USAGE: [options] [directory|file] 1994# @USAGE: [options] [directory|file]
1834# @DESCRIPTION: 1995# @DESCRIPTION:
1846 2007
1847 # Check if phase is pkg_postinst(). 2008 # Check if phase is pkg_postinst().
1848 [[ "${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"
1849 2010
1850 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.
1851 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=()
1852 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
1853 # Strip trailing slash from ROOT. 2024 # Strip trailing slash from EROOT.
1854 root="${EROOT%/}" 2025 root="${EROOT%/}"
1855 2026
1856 # Respect ROOT and options passed to compileall.py. 2027 # Respect EROOT and options passed to compileall.py.
1857 while (($#)); do 2028 while (($#)); do
1858 case "$1" in 2029 case "$1" in
1859 -l|-f|-q) 2030 -l|-f|-q)
1860 options+=("$1") 2031 options+=("$1")
1861 ;; 2032 ;;
1865 ;; 2036 ;;
1866 -*) 2037 -*)
1867 ewarn "${FUNCNAME}(): Ignoring option '$1'" 2038 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1868 ;; 2039 ;;
1869 *) 2040 *)
1870 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
1871 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"
1872 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
1873 if [[ -d "${root}/$1" ]]; then 2047 if [[ -d "${root}$1" ]]; then
1874 other_dirs+=("${root}/$1") 2048 other_dirs+=("${root}$1")
1875 elif [[ -f "${root}/$1" ]]; then 2049 elif [[ -f "${root}$1" ]]; then
1876 other_files+=("${root}/$1") 2050 other_files+=("${root}$1")
1877 elif [[ -e "${root}/$1" ]]; then 2051 elif [[ -e "${root}$1" ]]; then
1878 ewarn "'${root}/$1' is not a file or a directory!" 2052 ewarn "'${root}$1' is not a file or a directory!"
1879 else 2053 else
1880 ewarn "'${root}/$1' does not exist!" 2054 ewarn "'${root}$1' does not exist!"
1881 fi 2055 fi
1882 else 2056 else
1883 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do 2057 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
1884 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then 2058 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
1885 site_packages_dirs+=("$1") 2059 site_packages_dirs+=("$1")
1886 break 2060 break
1887 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then 2061 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
1888 site_packages_files+=("$1") 2062 site_packages_files+=("$1")
1900 done 2074 done
1901 2075
1902 # Set additional options. 2076 # Set additional options.
1903 options+=("-q") 2077 options+=("-q")
1904 2078
1905 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do 2079 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
1906 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 2080 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
1907 return_code="0" 2081 return_code="0"
1908 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)"
1909 if ((${#site_packages_dirs[@]})); then 2083 if ((${#site_packages_dirs[@]})); then
1910 for dir in "${site_packages_dirs[@]}"; do 2084 for dir in "${site_packages_dirs[@]}"; do
1912 done 2086 done
1913 "$(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"
1914 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then 2088 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then
1915 "$(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"
1916 fi 2090 fi
2091 _python_clean_compiled_modules "${site_packages_absolute_dirs[@]}"
1917 fi 2092 fi
1918 if ((${#site_packages_files[@]})); then 2093 if ((${#site_packages_files[@]})); then
1919 for file in "${site_packages_files[@]}"; do 2094 for file in "${site_packages_files[@]}"; do
1920 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}") 2095 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
1921 done 2096 done
1922 "$(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"
1923 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then 2098 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then
1924 "$(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"
1925 fi 2100 fi
2101 _python_clean_compiled_modules "${site_packages_absolute_files[@]}"
1926 fi 2102 fi
1927 eend "${return_code}" 2103 eend "${return_code}"
1928 fi 2104 fi
1929 unset site_packages_absolute_dirs site_packages_absolute_files 2105 unset site_packages_absolute_dirs site_packages_absolute_files
1930 done 2106 done
1931 2107
2108 if _python_package_supporting_installation_for_multiple_python_abis; then
1932 # Restore previous value of PYTHON_ABI. 2109 # Restore previous value of PYTHON_ABI.
1933 if [[ -n "${previous_PYTHON_ABI}" ]]; then 2110 if [[ -n "${previous_PYTHON_ABI}" ]]; then
1934 PYTHON_ABI="${previous_PYTHON_ABI}" 2111 PYTHON_ABI="${previous_PYTHON_ABI}"
1935 else 2112 else
1936 unset PYTHON_ABI 2113 unset PYTHON_ABI
2114 fi
1937 fi 2115 fi
1938 2116
1939 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 2117 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1940 return_code="0" 2118 return_code="0"
1941 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)"
1942 if ((${#other_dirs[@]})); then 2120 if ((${#other_dirs[@]})); then
1943 "$(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"
1944 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then 2122 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
1945 "$(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"
1946 fi 2124 fi
2125 _python_clean_compiled_modules "${other_dirs[@]}"
1947 fi 2126 fi
1948 if ((${#other_files[@]})); then 2127 if ((${#other_files[@]})); then
1949 "$(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"
1950 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then 2129 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
1951 "$(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"
1952 fi 2131 fi
2132 _python_clean_compiled_modules "${other_dirs[@]}"
1953 fi 2133 fi
1954 eend "${return_code}" 2134 eend "${return_code}"
1955 fi 2135 fi
1956 else 2136 else
1957 local myroot mydirs=() myfiles=() myopts=() return_code="0" 2137 local myroot mydirs=() myfiles=() myopts=() return_code="0"
1958 2138
1959 # strip trailing slash 2139 # strip trailing slash
1960 myroot="${EROOT%/}" 2140 myroot="${EROOT%/}"
1961 2141
1962 # respect ROOT and options passed to compileall.py 2142 # respect EROOT and options passed to compileall.py
1963 while (($#)); do 2143 while (($#)); do
1964 case "$1" in 2144 case "$1" in
1965 -l|-f|-q) 2145 -l|-f|-q)
1966 myopts+=("$1") 2146 myopts+=("$1")
1967 ;; 2147 ;;
1971 ;; 2151 ;;
1972 -*) 2152 -*)
1973 ewarn "${FUNCNAME}(): Ignoring option '$1'" 2153 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1974 ;; 2154 ;;
1975 *) 2155 *)
1976 if [[ -d "${myroot}"/$1 ]]; then 2156 if [[ -d "${myroot}/${1#/}" ]]; then
1977 mydirs+=("${myroot}/$1") 2157 mydirs+=("${myroot}/${1#/}")
1978 elif [[ -f "${myroot}"/$1 ]]; then 2158 elif [[ -f "${myroot}/${1#/}" ]]; then
1979 # Files are passed to python_mod_compile which is ROOT-aware 2159 # Files are passed to python_mod_compile which is EROOT-aware
1980 myfiles+=("$1") 2160 myfiles+=("$1")
1981 elif [[ -e "${myroot}/$1" ]]; then 2161 elif [[ -e "${myroot}/${1#/}" ]]; then
1982 ewarn "${myroot}/$1 is not a file or directory!" 2162 ewarn "${myroot}/${1#/} is not a file or directory!"
1983 else 2163 else
1984 ewarn "${myroot}/$1 does not exist!" 2164 ewarn "${myroot}/${1#/} does not exist!"
1985 fi 2165 fi
1986 ;; 2166 ;;
1987 esac 2167 esac
1988 shift 2168 shift
1989 done 2169 done
1990 2170
1991 # set additional opts 2171 # set additional opts
1992 myopts+=(-q) 2172 myopts+=(-q)
1993 2173
2174 PYTHON_ABI="$(PYTHON --ABI)"
2175
1994 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)"
1995 if ((${#mydirs[@]})); then 2177 if ((${#mydirs[@]})); then
1996 "$(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"
1997 "$(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[@]}"
1998 fi 2181 fi
1999 2182
2000 if ((${#myfiles[@]})); then 2183 if ((${#myfiles[@]})); then
2001 python_mod_compile "${myfiles[@]}" 2184 python_mod_compile "${myfiles[@]}"
2002 fi 2185 fi
2016# 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.
2017# 2200#
2018# This function can be used only in pkg_postrm() phase. 2201# This function can be used only in pkg_postrm() phase.
2019python_mod_cleanup() { 2202python_mod_cleanup() {
2020 _python_initialize_prefix_variables 2203 _python_initialize_prefix_variables
2021 _python_set_color_variables
2022 2204
2023 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
2024 2206
2025 # Check if phase is pkg_postrm(). 2207 # Check if phase is pkg_postrm().
2026 [[ "${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"
2027 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
2028 # Strip trailing slash from ROOT. 2219 # Strip trailing slash from EROOT.
2029 root="${EROOT%/}" 2220 root="${EROOT%/}"
2030 2221
2031 if (($#)); then 2222 if (($#)); then
2032 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
2033 while (($#)); do 2224 while (($#)); do
2034 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
2035 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"
2036 elif [[ "$1" =~ ^/ ]]; then 2227 elif [[ "$1" =~ ^/ ]]; then
2037 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")
2038 else 2232 else
2039 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do 2233 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
2040 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 2234 search_paths+=("${root}$(python_get_sitedir)/$1")
2041 done 2235 done
2042 fi 2236 fi
2043 shift 2237 shift
2044 done 2238 done
2045 else 2239 else
2046 SEARCH_PATH=("${@#/}") 2240 search_paths=("${@#/}")
2047 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 2241 search_paths=("${search_paths[@]/#/${root}/}")
2048 fi 2242 fi
2049 else 2243 else
2050 local dir sitedir
2051 for dir in "${root}"/usr/lib*; do 2244 for dir in "${root}"/usr/lib*; do
2052 if [[ -d "${dir}" && ! -L "${dir}" ]]; then 2245 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
2053 for sitedir in "${dir}"/python*/site-packages; do 2246 for sitedir in "${dir}"/python*/site-packages; do
2054 if [[ -d "${sitedir}" ]]; then 2247 if [[ -d "${sitedir}" ]]; then
2055 SEARCH_PATH+=("${sitedir}") 2248 search_paths+=("${sitedir}")
2056 fi 2249 fi
2057 done 2250 done
2058 fi 2251 fi
2059 done 2252 done
2060 for sitedir in "${root}"/usr/share/jython-*/Lib/site-packages; do 2253 for sitedir in "${root}"/usr/share/jython-*/Lib/site-packages; do
2061 if [[ -d "${sitedir}" ]]; then 2254 if [[ -d "${sitedir}" ]]; then
2062 SEARCH_PATH+=("${sitedir}") 2255 search_paths+=("${sitedir}")
2063 fi 2256 fi
2064 done 2257 done
2065 fi 2258 fi
2066 2259
2067 for path in "${SEARCH_PATH[@]}"; do 2260 _python_clean_compiled_modules "${search_paths[@]}"
2068 if [[ -d "${path}" ]]; then
2069 find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0 | while read -rd ''; do
2070 if [[ "${REPLY}" == *[co] ]]; then
2071 py_file="${REPLY%[co]}"
2072 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
2073 echo "${_BLUE}<<< ${py_file}[co]${_NORMAL}"
2074 rm -f "${py_file}"[co]
2075 elif [[ "${REPLY}" == *\$py.class ]]; then
2076 py_file="${REPLY%\$py.class}.py"
2077 [[ -f "${py_file}" || ! -f "${py_file%.py}\$py.class" ]] && continue
2078 echo "${_BLUE}<<< ${py_file%.py}\$py.class${_NORMAL}"
2079 rm -f "${py_file%.py}\$py.class"
2080 fi
2081 done
2082
2083 # Attempt to delete directories, which may be empty.
2084 find "${path}" -type d | sort -r | while read -r dir; do
2085 rmdir "${dir}" 2>/dev/null && echo "${_CYAN}<<< ${dir}${_NORMAL}"
2086 done
2087 elif [[ "${path}" == *.py && ! -f "${path}" ]]; then
2088 if [[ (-f "${path}c" || -f "${path}o") ]]; then
2089 echo "${_BLUE}<<< ${path}[co]${_NORMAL}"
2090 rm -f "${path}"[co]
2091 fi
2092 if [[ -f "${path%.py}\$py.class" ]]; then
2093 echo "${_BLUE}<<< ${path%.py}\$py.class${_NORMAL}"
2094 rm -f "${path%.py}\$py.class"
2095 fi
2096 fi
2097 done
2098} 2261}
2099 2262
2100# ================================================================================================ 2263# ================================================================================================
2101# ===================================== DEPRECATED FUNCTIONS ===================================== 2264# ===================================== DEPRECATED FUNCTIONS =====================================
2102# ================================================================================================ 2265# ================================================================================================
2156 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
2157 eerror "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()." 2320 eerror "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()."
2158 die "${FUNCNAME}() cannot be used in this EAPI" 2321 die "${FUNCNAME}() cannot be used in this EAPI"
2159 fi 2322 fi
2160 2323
2324 _python_set_color_variables
2325
2161 echo 2326 echo
2162 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}"
2163 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}"
2164 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}"
2165 echo 2330 echo
2183python_tkinter_exists() { 2348python_tkinter_exists() {
2184 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
2185 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}()."
2186 die "${FUNCNAME}() cannot be used in this EAPI" 2351 die "${FUNCNAME}() cannot be used in this EAPI"
2187 fi 2352 fi
2353
2354 _python_set_color_variables
2188 2355
2189 if [[ "${FUNCNAME[1]}" != "distutils_python_tkinter" ]]; then 2356 if [[ "${FUNCNAME[1]}" != "distutils_python_tkinter" ]]; then
2190 echo 2357 echo
2191 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}"
2192 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}"
2235 [[ "${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"
2236 2403
2237 # strip trailing slash 2404 # strip trailing slash
2238 myroot="${EROOT%/}" 2405 myroot="${EROOT%/}"
2239 2406
2240 # respect ROOT 2407 # respect EROOT
2241 for f in "$@"; do 2408 for f in "$@"; do
2242 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") 2409 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
2243 done 2410 done
2411
2412 PYTHON_ABI="$(PYTHON --ABI)"
2244 2413
2245 if ((${#myfiles[@]})); then 2414 if ((${#myfiles[@]})); then
2246 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" 2415 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
2247 "$(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[@]}"
2248 else 2418 else
2249 ewarn "No files to compile!" 2419 ewarn "No files to compile!"
2250 fi 2420 fi
2251} 2421}

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

  ViewVC Help
Powered by ViewVC 1.1.20