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

Diff of /eclass/python.eclass

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

Revision 1.16 Revision 1.86
1# Copyright 1999-2004 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.16 2005/04/20 15:43:37 liquidx Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.86 2010/01/15 14:46:20 arfrever Exp $
4
5# @ECLASS: python.eclass
6# @MAINTAINER:
7# python@gentoo.org
8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules.
9# @DESCRIPTION:
10# Some useful functions for dealing with Python.
11
12inherit multilib
13
14if ! has "${EAPI:-0}" 0 1 2; then
15 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
16fi
17
18if [[ -n "${NEED_PYTHON}" ]]; then
19 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
20 DEPEND="${PYTHON_ATOM}"
21 RDEPEND="${DEPEND}"
22else
23 PYTHON_ATOM="dev-lang/python"
24fi
25
26DEPEND+=" >=app-admin/eselect-python-20090804"
27
28# @ECLASS-VARIABLE: PYTHON_USE_WITH
29# @DESCRIPTION:
30# Set this to a space separated list of use flags
31# the python slot in use must be built with.
32
33# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
34# @DESCRIPTION:
35# Set this to a space separated list of use flags
36# of which one must be turned on for the slot of
37# in use.
38
39# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
40# @DESCRIPTION:
41# Set this if you need to make either PYTHON_USE_WITH or
42# PYTHON_USE_WITH_OR atoms conditional under a use flag.
43
44# @FUNCTION: python_pkg_setup
45# @DESCRIPTION:
46# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
47# are respected. Only exported if one of those variables is set.
48if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
49 python_pkg_setup() {
50 python_pkg_setup_fail() {
51 eerror "${1}"
52 die "${1}"
53 }
54
55 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
56
57 python_pkg_setup_check_USE_flags() {
58 local pyatom use
59 if [[ -n "${PYTHON_ABI}" ]]; then
60 pyatom="dev-lang/python:${PYTHON_ABI}"
61 else
62 pyatom="dev-lang/python:$(PYTHON -A --ABI)"
63 fi
64
65 for use in ${PYTHON_USE_WITH}; do
66 if ! has_version "${pyatom}[${use}]"; then
67 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
68 fi
69 done
70
71 for use in ${PYTHON_USE_WITH_OR}; do
72 if has_version "${pyatom}[${use}]"; then
73 return
74 fi
75 done
76
77 if [[ ${PYTHON_USE_WITH_OR} ]]; then
78 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
79 fi
80 }
81
82 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
83 python_execute_function -q python_pkg_setup_check_USE_flags
84 else
85 python_pkg_setup_check_USE_flags
86 fi
87 }
88
89 EXPORT_FUNCTIONS pkg_setup
90
91 if [[ -n "${PYTHON_USE_WITH}" ]]; then
92 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
93 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
94 PYTHON_USE_WITH_ATOM="|| ( "
95 for use in ${PYTHON_USE_WITH_OR}; do
96 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
97 done
98 unset use
99 PYTHON_USE_WITH_ATOM+=" )"
100 fi
101 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
102 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
103 fi
104 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
105 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
106fi
107
108# ================================================================================================
109# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ========
110# ================================================================================================
111
112# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
113# @DESCRIPTION:
114# Set this in EAPI <= 4 to indicate that given package supports installation for
115# multiple versions of Python.
116
117# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
118# @DESCRIPTION:
119# Set this to define default functions for the following ebuild phases:
120# src_prepare, src_configure, src_compile, src_test, src_install.
121if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
122 python_src_prepare() {
123 python_copy_sources
124 }
125
126 for python_default_function in src_configure src_compile src_test src_install; do
127 eval "python_${python_default_function}() { python_execute_function -d -s; }"
128 done
129 unset python_default_function
130
131 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
132fi
133
134unset PYTHON_ABIS
135unset PYTHON_ABIS_SANITY_CHECKS
136
137# @FUNCTION: validate_PYTHON_ABIS
138# @DESCRIPTION:
139# Ensure that PYTHON_ABIS variable has valid value.
140# This function usually should not be directly called in ebuilds.
141validate_PYTHON_ABIS() {
142 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable.
143 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
144 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
145 fi
146
147 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
148 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then
149 eerror "'/usr/bin/python' is not valid symlink."
150 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
151 die "'/usr/bin/python' is not valid symlink"
152 fi
153 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
154 eerror "'/usr/bin/python-config' is not valid script"
155 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
156 die "'/usr/bin/python-config' is not valid script"
157 fi
158
159 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
160 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
161 local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS=
162 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
163 python2_supported_versions="2.4 2.5 2.6 2.7"
164 python3_supported_versions="3.0 3.1 3.2"
165
166 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
167 local python2_enabled="0" python3_enabled="0"
168
169 if [[ -z "${USE_PYTHON}" ]]; then
170 die "USE_PYTHON variable is empty"
171 fi
172
173 for PYTHON_ABI in ${USE_PYTHON}; do
174 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
175 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
176 fi
177
178 if has "${PYTHON_ABI}" ${python2_supported_versions}; then
179 python2_enabled="1"
180 fi
181 if has "${PYTHON_ABI}" ${python3_supported_versions}; then
182 python3_enabled="1"
183 fi
184
185 support_ABI="1"
186 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
187 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
188 support_ABI="0"
189 break
190 fi
191 done
192 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
193 done
194
195 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
196 die "USE_PYTHON variable does not enable any version of Python supported by ${CATEGORY}/${PF}"
197 fi
198
199 if [[ "${python2_enabled}" == "0" ]]; then
200 ewarn "USE_PYTHON variable does not enable any version of Python 2. This configuration is unsupported."
201 fi
202 if [[ "${python3_enabled}" == "0" ]]; then
203 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported."
204 fi
205 else
206 local python_version python2_version= python3_version= support_python_major_version
207
208 python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
209
210 if has_version "=dev-lang/python-2*"; then
211 if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then
212 die "'/usr/bin/python2' is not valid symlink"
213 fi
214
215 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
216
217 for PYTHON_ABI in ${python2_supported_versions}; do
218 support_python_major_version="1"
219 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
220 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
221 support_python_major_version="0"
222 fi
223 done
224 [[ "${support_python_major_version}" == "1" ]] && break
225 done
226 if [[ "${support_python_major_version}" == "1" ]]; then
227 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
228 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
229 die "Active version of Python 2 is not supported by ${CATEGORY}/${PF}"
230 fi
231 done
232 else
233 python2_version=""
234 fi
235 fi
236
237 if has_version "=dev-lang/python-3*"; then
238 if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then
239 die "'/usr/bin/python3' is not valid symlink"
240 fi
241
242 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
243
244 for PYTHON_ABI in ${python3_supported_versions}; do
245 support_python_major_version="1"
246 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
247 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
248 support_python_major_version="0"
249 fi
250 done
251 [[ "${support_python_major_version}" == "1" ]] && break
252 done
253 if [[ "${support_python_major_version}" == "1" ]]; then
254 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
255 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
256 die "Active version of Python 3 is not supported by ${CATEGORY}/${PF}"
257 fi
258 done
259 else
260 python3_version=""
261 fi
262 fi
263
264 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
265 eerror "Python wrapper is configured incorrectly or /usr/bin/python2 symlink"
266 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
267 die "Incorrect configuration of Python"
268 fi
269 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
270 eerror "Python wrapper is configured incorrectly or /usr/bin/python3 symlink"
271 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
272 die "Incorrect configuration of Python"
273 fi
274
275 PYTHON_ABIS="${python2_version} ${python3_version}"
276 PYTHON_ABIS="${PYTHON_ABIS# }"
277 export PYTHON_ABIS="${PYTHON_ABIS% }"
278 fi
279 fi
280
281 if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
282 local PYTHON_ABI
283 for PYTHON_ABI in ${PYTHON_ABIS}; do
284 # Ensure that appropriate version of Python is installed.
285 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
286 die "dev-lang/python:${PYTHON_ABI} is not installed"
287 fi
288
289 # Ensure that EPYTHON variable is respected.
290 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
291 eerror "python: '$(type -p python)'"
292 eerror "ABI: '${ABI}'"
293 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
294 eerror "EPYTHON: '$(PYTHON)'"
295 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
296 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')'"
297 die "'python' does not respect EPYTHON variable"
298 fi
299 done
300 PYTHON_ABIS_SANITY_CHECKS="1"
301 fi
302}
303
304# @FUNCTION: python_execute_function
305# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
306# @DESCRIPTION:
307# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
308# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
309python_execute_function() {
310 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
311
312 while (($#)); do
313 case "$1" in
314 --action-message)
315 action_message_template="$2"
316 shift
317 ;;
318 -d|--default-function)
319 default_function="1"
320 ;;
321 --failure-message)
322 failure_message_template="$2"
323 shift
324 ;;
325 --nonfatal)
326 nonfatal="1"
327 ;;
328 -q|--quiet)
329 quiet="1"
330 ;;
331 -s|--separate-build-dirs)
332 separate_build_dirs="1"
333 ;;
334 --source-dir)
335 source_dir="$2"
336 shift
337 ;;
338 --)
339 break
340 ;;
341 -*)
342 die "${FUNCNAME}(): Unrecognized option '$1'"
343 ;;
344 *)
345 break
346 ;;
347 esac
348 shift
349 done
350
351 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
352 die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
353 fi
354
355 if [[ "${default_function}" == "0" ]]; then
356 if [[ "$#" -eq 0 ]]; then
357 die "${FUNCNAME}(): Missing function name"
358 fi
359 function="$1"
360 shift
361
362 if [[ -z "$(type -t "${function}")" ]]; then
363 die "${FUNCNAME}(): '${function}' function is not defined"
364 fi
365 else
366 if [[ "$#" -ne "0" ]]; then
367 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
368 fi
369 if has "${EAPI:-0}" 0 1; then
370 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
371 fi
372
373 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
374 if has "${EAPI}" 2 3; then
375 python_default_function() {
376 econf
377 }
378 else
379 python_default_function() {
380 nonfatal econf
381 }
382 fi
383 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
384 python_default_function() {
385 emake
386 }
387 elif [[ "${EBUILD_PHASE}" == "test" ]]; then
388 python_default_function() {
389 if emake -j1 -n check &> /dev/null; then
390 emake -j1 check
391 elif emake -j1 -n test &> /dev/null; then
392 emake -j1 test
393 fi
394 }
395 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
396 python_default_function() {
397 emake DESTDIR="${D}" install
398 }
399 else
400 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
401 fi
402 function="python_default_function"
403 fi
404
405 if [[ "${quiet}" == "0" ]]; then
406 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
407 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
408 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
409 [[ "${EBUILD_PHASE}" == "configure" ]] && action="Configuration"
410 [[ "${EBUILD_PHASE}" == "compile" ]] && action="Building"
411 [[ "${EBUILD_PHASE}" == "test" ]] && action="Testing"
412 [[ "${EBUILD_PHASE}" == "install" ]] && action="Installation"
413 [[ "${EBUILD_PHASE}" == "preinst" ]] && action="Preinstallation"
414 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
415 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
416 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
417 fi
418
419 local RED GREEN BLUE NORMAL
420 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
421 RED=$'\e[1;31m'
422 GREEN=$'\e[1;32m'
423 BLUE=$'\e[1;34m'
424 NORMAL=$'\e[0m'
425 else
426 RED=
427 GREEN=
428 BLUE=
429 NORMAL=
430 fi
431
432 validate_PYTHON_ABIS
433 for PYTHON_ABI in ${PYTHON_ABIS}; do
434 if [[ "${quiet}" == "0" ]]; then
435 if [[ -n "${action_message_template}" ]]; then
436 action_message="$(eval echo -n "${action_message_template}")"
437 else
438 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..."
439 fi
440 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
441 fi
442
443 if [[ "${separate_build_dirs}" == "1" ]]; then
444 if [[ -n "${source_dir}" ]]; then
445 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
446 else
447 export BUILDDIR="${S}-${PYTHON_ABI}"
448 fi
449 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
450 else
451 export BUILDDIR="${S}"
452 fi
453
454 previous_directory="$(pwd)"
455 previous_directory_stack="$(dirs -p)"
456 previous_directory_stack_length="$(dirs -p | wc -l)"
457
458 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
459 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
460 else
461 EPYTHON="$(PYTHON)" "${function}" "$@"
462 fi
463
464 if [[ "$?" != "0" ]]; then
465 if [[ -n "${failure_message_template}" ]]; then
466 failure_message="$(eval echo -n "${failure_message_template}")"
467 else
468 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
469 fi
470
471 if [[ "${nonfatal}" == "1" ]]; then
472 if [[ "${quiet}" == "0" ]]; then
473 ewarn "${RED}${failure_message}${NORMAL}"
474 fi
475 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
476 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
477 local enabled_PYTHON_ABIS= other_PYTHON_ABI
478 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
479 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
480 done
481 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
482 fi
483 if [[ "${quiet}" == "0" ]]; then
484 ewarn "${RED}${failure_message}${NORMAL}"
485 fi
486 if [[ -z "${PYTHON_ABIS}" ]]; then
487 die "${function}() function failed with all enabled versions of Python"
488 fi
489 else
490 die "${failure_message}"
491 fi
492 fi
493
494 # Ensure that directory stack has not been decreased.
495 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
496 die "Directory stack decreased illegally"
497 fi
498
499 # Avoid side effects of earlier returning from the specified function.
500 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
501 popd > /dev/null || die "popd failed"
502 done
503
504 # Ensure that the bottom part of directory stack has not been changed. Restore
505 # previous directory (from before running of the specified function) before
506 # comparison of directory stacks to avoid mismatch of directory stacks after
507 # potential using of 'cd' to change current directory. Restoration of previous
508 # directory allows to safely use 'cd' to change current directory in the
509 # specified function without changing it back to original directory.
510 cd "${previous_directory}"
511 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
512 die "Directory stack changed illegally"
513 fi
514
515 if [[ "${separate_build_dirs}" == "1" ]]; then
516 popd > /dev/null || die "popd failed"
517 fi
518 unset BUILDDIR
519 done
520
521 if [[ "${default_function}" == "1" ]]; then
522 unset -f python_default_function
523 fi
524}
525
526# @FUNCTION: python_copy_sources
527# @USAGE: [--no-link] [--] [directory]
528# @DESCRIPTION:
529# Copy unpacked sources of given package for each Python ABI.
530python_copy_sources() {
531 local dir dirs=() no_link="0" PYTHON_ABI
532
533 while (($#)); do
534 case "$1" in
535 --no-link)
536 no_link="1"
537 ;;
538 --)
539 break
540 ;;
541 -*)
542 die "${FUNCNAME}(): Unrecognized option '$1'"
543 ;;
544 *)
545 break
546 ;;
547 esac
548 shift
549 done
550
551 if [[ "$#" -eq 0 ]]; then
552 if [[ "${WORKDIR}" == "${S}" ]]; then
553 die "${FUNCNAME}() cannot be used"
554 fi
555 dirs="${S}"
556 else
557 dirs="$@"
558 fi
559
560 validate_PYTHON_ABIS
561 for PYTHON_ABI in ${PYTHON_ABIS}; do
562 for dir in "${dirs[@]}"; do
563 if [[ "${no_link}" == "1" ]]; then
564 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
565 else
566 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
567 fi
568 done
569 done
570}
571
572# @FUNCTION: python_set_build_dir_symlink
573# @USAGE: [directory="build"]
574# @DESCRIPTION:
575# Create build directory symlink.
576python_set_build_dir_symlink() {
577 local dir="$1"
578
579 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
580 [[ -z "${dir}" ]] && dir="build"
581
582 # Do not delete preexistent directories.
583 rm -f "${dir}" || die "Deletion of '${dir}' failed"
584 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
585}
586
587# @FUNCTION: python_generate_wrapper_scripts
588# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
589# @DESCRIPTION:
590# Generate wrapper scripts. Existing files are overwritten only with --force option.
591# If --respect-EPYTHON option is specified, then generated wrapper scripts will
592# respect EPYTHON variable at run time.
593python_generate_wrapper_scripts() {
594 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python2_supported_versions python3_enabled="0" python3_supported_versions respect_EPYTHON="0"
595 python2_supported_versions="2.4 2.5 2.6 2.7"
596 python3_supported_versions="3.0 3.1 3.2"
597
598 while (($#)); do
599 case "$1" in
600 -E|--respect-EPYTHON)
601 respect_EPYTHON="1"
602 ;;
603 -f|--force)
604 force="1"
605 ;;
606 -q|--quiet)
607 quiet="1"
608 ;;
609 --)
610 break
611 ;;
612 -*)
613 die "${FUNCNAME}(): Unrecognized option '$1'"
614 ;;
615 *)
616 break
617 ;;
618 esac
619 shift
620 done
621
622 if [[ "$#" -eq 0 ]]; then
623 die "${FUNCNAME}(): Missing arguments"
624 fi
625
626 validate_PYTHON_ABIS
627 for PYTHON_ABI in ${python2_supported_versions}; do
628 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
629 python2_enabled="1"
630 fi
631 done
632 for PYTHON_ABI in ${python3_supported_versions}; do
633 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
634 python3_enabled="1"
635 fi
636 done
637
638 if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
639 eselect_python_option=
640 elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
641 eselect_python_option="--python2"
642 elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
643 eselect_python_option="--python3"
644 else
645 die "${FUNCNAME}(): Unsupported environment"
646 fi
647
648 for file in "$@"; do
649 if [[ -f "${file}" && "${force}" == "0" ]]; then
650 die "${FUNCNAME}(): '$1' already exists"
651 fi
652
653 if [[ "${quiet}" == "0" ]]; then
654 einfo "Generating '${file#${D%/}}' wrapper script"
655 fi
656
657 cat << EOF > "${file}"
658#!/usr/bin/env python
659# Gentoo '${file##*/}' wrapper script
660
661import os
662import re
663import subprocess
664import sys
665
666EPYTHON_re = re.compile(r"^python(\d+\.\d+)$")
667
668EOF
669 if [[ "$?" != "0" ]]; then
670 die "${FUNCNAME}(): Generation of '$1' failed"
671 fi
672 if [[ "${respect_EPYTHON}" == "1" ]]; then
673 cat << EOF >> "${file}"
674EPYTHON = os.environ.get("EPYTHON")
675if EPYTHON:
676 EPYTHON_matched = EPYTHON_re.match(EPYTHON)
677 if EPYTHON_matched:
678 PYTHON_ABI = EPYTHON_matched.group(1)
679 else:
680 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
681 sys.exit(1)
682else:
683 try:
684 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
685 if eselect_process.wait() != 0:
686 raise ValueError
687 except (OSError, ValueError):
688 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
689 sys.exit(1)
690
691 eselect_output = eselect_process.stdout.read()
692 if not isinstance(eselect_output, str):
693 # Python 3
694 eselect_output = eselect_output.decode()
695
696 EPYTHON_matched = EPYTHON_re.match(eselect_output)
697 if EPYTHON_matched:
698 PYTHON_ABI = EPYTHON_matched.group(1)
699 else:
700 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
701 sys.exit(1)
702EOF
703 if [[ "$?" != "0" ]]; then
704 die "${FUNCNAME}(): Generation of '$1' failed"
705 fi
706 else
707 cat << EOF >> "${file}"
708try:
709 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
710 if eselect_process.wait() != 0:
711 raise ValueError
712except (OSError, ValueError):
713 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
714 sys.exit(1)
715
716eselect_output = eselect_process.stdout.read()
717if not isinstance(eselect_output, str):
718 # Python 3
719 eselect_output = eselect_output.decode()
720
721EPYTHON_matched = EPYTHON_re.match(eselect_output)
722if EPYTHON_matched:
723 PYTHON_ABI = EPYTHON_matched.group(1)
724else:
725 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
726 sys.exit(1)
727EOF
728 if [[ "$?" != "0" ]]; then
729 die "${FUNCNAME}(): Generation of '$1' failed"
730 fi
731 fi
732 cat << EOF >> "${file}"
733
734os.environ["PYTHON_PROCESS_NAME"] = sys.argv[0]
735target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI)
736if not os.path.exists(target_executable):
737 sys.stderr.write("'%s' does not exist\n" % target_executable)
738 sys.exit(1)
739
740os.execv(target_executable, sys.argv)
741EOF
742 if [[ "$?" != "0" ]]; then
743 die "${FUNCNAME}(): Generation of '$1' failed"
744 fi
745 fperms +x "${file#${D%/}}" || die "fperms '${file}' failed"
746 done
747}
748
749# ================================================================================================
750# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======
751# ================================================================================================
752
753# @FUNCTION: python_set_active_version
754# @USAGE: <Python_ABI|2|3>
755# @DESCRIPTION:
756# Set active version of Python.
757python_set_active_version() {
758 if [[ "$#" -ne "1" ]]; then
759 die "${FUNCNAME}() requires 1 argument"
760 fi
761
762 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
763 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
764 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
765 fi
766 export EPYTHON="$(PYTHON "$1")"
767 elif [[ "$1" == "2" ]]; then
768 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
769 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
770 fi
771 export EPYTHON="$(PYTHON -2)"
772 elif [[ "$1" == "3" ]]; then
773 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
774 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
775 fi
776 export EPYTHON="$(PYTHON -3)"
777 else
778 die "${FUNCNAME}(): Unrecognized argument '$1'"
779 fi
780
781 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
782 # so it does not need to be exported to subprocesses.
783 PYTHON_ABI="${EPYTHON#python}"
784 PYTHON_ABI="${PYTHON_ABI%%-*}"
785}
786
787# @FUNCTION: python_need_rebuild
788# @DESCRIPTION: Run without arguments, specifies that the package should be
789# rebuilt after a python upgrade.
790# Do not use this function in ebuilds of packages supporting installation
791# for multiple versions of Python.
792python_need_rebuild() {
793 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
794}
795
796# ================================================================================================
797# ======================================= GETTER FUNCTIONS =======================================
798# ================================================================================================
799
800# @FUNCTION: PYTHON
801# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
802# @DESCRIPTION:
803# Get Python interpreter filename for specified Python ABI. If Python_ABI argument
804# is ommitted, then PYTHON_ABI environment variable must be set and is used.
805# If -2 option is specified, then active version of Python 2 is used.
806# If -3 option is specified, then active version of Python 3 is used.
807# If --active option is specified, then active version of Python is used.
808# Active version of Python can be set by python_set_active_version().
809# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
810# -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
811# If --ABI option is specified, then only specified Python ABI is printed instead of
812# Python interpreter filename.
813# --ABI and --absolute-path options cannot be specified simultaneously.
814PYTHON() {
815 local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" python2="0" python3="0" slot=
816
817 while (($#)); do
818 case "$1" in
819 -2)
820 python2="1"
821 ;;
822 -3)
823 python3="1"
824 ;;
825 --ABI)
826 ABI_output="1"
827 ;;
828 -A|--active)
829 active="1"
830 ;;
831 -a|--absolute-path)
832 absolute_path_output="1"
833 ;;
834 -f|--final-ABI)
835 final_ABI="1"
836 ;;
837 --)
838 break
839 ;;
840 -*)
841 die "${FUNCNAME}(): Unrecognized option '$1'"
842 ;;
843 *)
844 break
845 ;;
846 esac
847 shift
848 done
849
850 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
851 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
852 fi
853
854 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then
855 die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously"
856 fi
857
858 if [[ "$#" -eq 0 ]]; then
859 if [[ "${active}" == "1" ]]; then
860 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
861 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
862 fi
863 slot="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
864 elif [[ "${final_ABI}" == "1" ]]; then
865 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
866 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python"
867 fi
868 validate_PYTHON_ABIS
869 slot="${PYTHON_ABIS##* }"
870 elif [[ "${python2}" == "1" ]]; then
871 slot="$(eselect python show --python2)"
872 if [[ -z "${slot}" ]]; then
873 die "${FUNCNAME}(): Active Python 2 interpreter not set"
874 elif [[ "${slot}" != "python2."* ]]; then
875 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
876 fi
877 slot="${slot#python}"
878 elif [[ "${python3}" == "1" ]]; then
879 slot="$(eselect python show --python3)"
880 if [[ -z "${slot}" ]]; then
881 die "${FUNCNAME}(): Active Python 3 interpreter not set"
882 elif [[ "${slot}" != "python3."* ]]; then
883 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
884 fi
885 slot="${slot#python}"
886 elif [[ -n "${PYTHON_ABI}" ]]; then
887 slot="${PYTHON_ABI}"
888 else
889 die "${FUNCNAME}(): Invalid usage"
890 fi
891 elif [[ "$#" -eq 1 ]]; then
892 if [[ "${active}" == "1" ]]; then
893 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
894 fi
895 if [[ "${final_ABI}" == "1" ]]; then
896 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
897 fi
898 if [[ "${python2}" == "1" ]]; then
899 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
900 fi
901 if [[ "${python3}" == "1" ]]; then
902 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
903 fi
904 slot="$1"
905 else
906 die "${FUNCNAME}(): Invalid usage"
907 fi
908
909 if [[ "${ABI_output}" == "1" ]]; then
910 echo -n "${slot}"
911 return
912 elif [[ "${absolute_path_output}" == "1" ]]; then
913 echo -n "/usr/bin/python${slot}"
914 else
915 echo -n "python${slot}"
916 fi
917
918 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
919 echo -n "-${ABI}"
920 fi
921}
922
923# @FUNCTION: python_get_includedir
924# @USAGE: [-f|--final-ABI]
925# @DESCRIPTION:
926# Print Python include directory.
927# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
928python_get_includedir() {
929 local final_ABI="0"
930
931 while (($#)); do
932 case "$1" in
933 -f|--final-ABI)
934 final_ABI="1"
935 ;;
936 -*)
937 die "${FUNCNAME}(): Unrecognized option '$1'"
938 ;;
939 *)
940 die "${FUNCNAME}(): Invalid usage"
941 ;;
942 esac
943 shift
944 done
945
946 if [[ "${final_ABI}" == "1" ]]; then
947 echo "/usr/include/python$(PYTHON -f --ABI)"
948 elif [[ -n "${PYTHON_ABI}" ]]; then
949 echo "/usr/include/python${PYTHON_ABI}"
950 else
951 echo "/usr/include/python$(PYTHON -A --ABI)"
952 fi
953}
954
955# @FUNCTION: python_get_libdir
956# @USAGE: [-f|--final-ABI]
957# @DESCRIPTION:
958# Print Python library directory.
959# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
960python_get_libdir() {
961 local final_ABI="0"
962
963 while (($#)); do
964 case "$1" in
965 -f|--final-ABI)
966 final_ABI="1"
967 ;;
968 -*)
969 die "${FUNCNAME}(): Unrecognized option '$1'"
970 ;;
971 *)
972 die "${FUNCNAME}(): Invalid usage"
973 ;;
974 esac
975 shift
976 done
977
978 if [[ "${final_ABI}" == "1" ]]; then
979 echo "/usr/$(get_libdir)/python$(PYTHON -f --ABI)"
980 elif [[ -n "${PYTHON_ABI}" ]]; then
981 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
982 else
983 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)"
984 fi
985}
986
987# @FUNCTION: python_get_sitedir
988# @USAGE: [-f|--final-ABI]
989# @DESCRIPTION:
990# Print Python site-packages directory.
991# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
992python_get_sitedir() {
993 local options=()
994
995 while (($#)); do
996 case "$1" in
997 -f|--final-ABI)
998 options+=("$1")
999 ;;
1000 -*)
1001 die "${FUNCNAME}(): Unrecognized option '$1'"
1002 ;;
1003 *)
1004 die "${FUNCNAME}(): Invalid usage"
1005 ;;
1006 esac
1007 shift
1008 done
1009
1010 echo "$(python_get_libdir "${options[@]}")/site-packages"
1011}
1012
1013# ================================================================================================
1014# =================================== MISCELLANEOUS FUNCTIONS ====================================
1015# ================================================================================================
1016
1017_python_implementation() {
1018 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1019 return 0
1020 else
1021 return 1
1022 fi
1023}
1024
1025# @FUNCTION: python_convert_shebangs
1026# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1027# @DESCRIPTION:
1028# Convert shebangs in specified files. Directories can be specified only with --recursive option.
1029python_convert_shebangs() {
1030 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
1031
1032 while (($#)); do
1033 case "$1" in
1034 -r|--recursive)
1035 recursive="1"
1036 ;;
1037 -q|--quiet)
1038 quiet="1"
1039 ;;
1040 -x|--only-executables)
1041 only_executables="1"
1042 ;;
1043 --)
1044 break
1045 ;;
1046 -*)
1047 die "${FUNCNAME}(): Unrecognized option '$1'"
1048 ;;
1049 *)
1050 break
1051 ;;
1052 esac
1053 shift
1054 done
1055
1056 if [[ "$#" -eq 0 ]]; then
1057 die "${FUNCNAME}(): Missing Python version and files or directories"
1058 elif [[ "$#" -eq 1 ]]; then
1059 die "${FUNCNAME}(): Missing files or directories"
1060 fi
1061
1062 python_version="$1"
1063 shift
1064
1065 for argument in "$@"; do
1066 if [[ ! -e "${argument}" ]]; then
1067 die "${FUNCNAME}(): '${argument}' does not exist"
1068 elif [[ -f "${argument}" ]]; then
1069 files+=("${argument}")
1070 elif [[ -d "${argument}" ]]; then
1071 if [[ "${recursive}" == "1" ]]; then
1072 if [[ "${only_executables}" == "1" ]]; then
1073 files+=($(find "${argument}" -perm /111 -type f))
1074 else
1075 files+=($(find "${argument}" -type f))
1076 fi
1077 else
1078 die "${FUNCNAME}(): '${argument}' is not a regular file"
1079 fi
1080 else
1081 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
1082 fi
1083 done
1084
1085 for file in "${files[@]}"; do
1086 file="${file#./}"
1087 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1088
1089 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1090 if [[ "${quiet}" == "0" ]]; then
1091 einfo "Converting shebang in '${file}'"
1092 fi
1093 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1094
1095 # Delete potential whitespace after "#!".
1096 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1097 fi
1098 done
1099}
1100
1101# @FUNCTION: python_mod_exists
1102# @USAGE: <module>
1103# @DESCRIPTION:
1104# Run with the module name as an argument. it will check if a
1105# python module is installed and loadable. it will return
1106# TRUE(0) if the module exists, and FALSE(1) if the module does
1107# not exist.
4# 1108#
5# Author: Alastair Tse <liquidx@gentoo.org> 1109# Example:
6# 1110# if python_mod_exists gtk; then
7# A Utility Eclass that should be inherited by anything that deals with 1111# echo "gtk support enabled"
8# Python or Python modules. 1112# fi
9# 1113python_mod_exists() {
10# - Features: 1114 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
11# python_version() - sets PYVER/PYVER_MAJOR/PYVER_MINOR 1115 python -c "import $1" &>/dev/null
12# python_tkinter_exists() - Checks for tkinter support in python
13# python_mod_exists() - Checks if a python module exists
14# python_mod_compile() - Compiles a .py file to a .pyc/.pyo
15# python_mod_optimize() - Generates .pyc/.pyo precompiled scripts
16# python_mod_cleanup() - Goes through /usr/lib*/python* to remove
17# orphaned *.pyc *.pyo
18# python_makesym() - Makes /usr/bin/python symlinks
19
20inherit alternatives
21
22ECLASS="python"
23INHERITED="$INHERITED $ECLASS"
24
25#
26# name: python_disable/enable_pyc
27# desc: tells python not to automatically recompile modules to .pyc/.pyo
28# even if the timestamps/version stamps don't match. this is
29# done to protect sandbox.
30#
31# note: supported by >=dev-lang/python-2.2.3-r3 only.
32#
33python_disable_pyc() {
34 export PYTHON_DONTCOMPILE=1
35} 1116}
36 1117
37python_enable_pyc() {
38 unset PYTHON_DONTCOMPILE
39}
40
41python_disable_pyc
42
43#
44# name: python_version
45# desc: run without arguments and it will export the version of python
46# currently in use as $PYVER
47#
48python_version() {
49 local tmpstr
50 python=${python:-/usr/bin/python}
51 tmpstr="$(${python} -V 2>&1 )"
52 export PYVER_ALL="${tmpstr#Python }"
53
54 export PYVER="${PYVER_ALL%.*}"
55 export PYVER_MAJOR="${PYVER%.*}"
56 export PYVER_MINOR="${PYVER##*.}"
57 export PYVER_MICRO="${PYVER_ALL##*.}"
58}
59
60#
61# name: python_makesym
62# desc: run without arguments, it will create the /usr/bin/python symlinks
63# to the latest installed version
64#
65python_makesym() {
66 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
67 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
68}
69
70#
71# name: python_tkinter_exists 1118# @FUNCTION: python_tkinter_exists
1119# @DESCRIPTION:
72# desc: run without arguments, checks if python was compiled with Tkinter 1120# Run without arguments, checks if python was compiled with Tkinter
73# support. If not, prints an error message and dies. 1121# support. If not, prints an error message and dies.
74#
75python_tkinter_exists() { 1122python_tkinter_exists() {
76 if ! python -c "import Tkinter" >/dev/null 2>&1; then 1123 if ! python -c "import Tkinter" >/dev/null 2>&1; then
77 eerror "You need to recompile python with Tkinter support." 1124 eerror "You need to recompile python with Tkinter support."
78 eerror "That means: USE='tcltk' emerge python" 1125 eerror "Try adding: 'dev-lang/python tk'"
1126 eerror "in to /etc/portage/package.use"
79 echo 1127 echo
80 die "missing tkinter support with installed python" 1128 die "missing tkinter support with installed python"
81 fi 1129 fi
82} 1130}
83 1131
1132# ================================================================================================
1133# ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
1134# ================================================================================================
1135
1136# @FUNCTION: python_enable_pyc
1137# @DESCRIPTION:
1138# Tell Python to automatically recompile modules to .pyc/.pyo if the
1139# timestamps/version stamps have changed.
1140python_enable_pyc() {
1141 unset PYTHONDONTWRITEBYTECODE
1142}
1143
1144# @FUNCTION: python_disable_pyc
1145# @DESCRIPTION:
1146# Tell Python not to automatically recompile modules to .pyc/.pyo
1147# even if the timestamps/version stamps do not match. This is done
1148# to protect sandbox.
1149python_disable_pyc() {
1150 export PYTHONDONTWRITEBYTECODE="1"
1151}
1152
1153# @FUNCTION: python_mod_optimize
1154# @USAGE: [options] [directory|file]
1155# @DESCRIPTION:
1156# If no arguments supplied, it will recompile not recursively all modules
1157# under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages).
84# 1158#
85# name: python_mod_exists 1159# If supplied with arguments, it will recompile all modules recursively
86# desc: run with the module name as an argument. it will check if a 1160# in the supplied directory.
87# python module is installed and loadable. it will return 1161# This function should only be run in pkg_postinst().
88# TRUE(0) if the module exists, and FALSE(1) if the module does
89# not exist.
90# exam:
91# if python_mod_exists gtk; then
92# echo "gtk support enabled
93# fi
94# 1162#
95python_mod_exists() { 1163# Options passed to this function are passed to compileall.py.
96 [ -z "$1" ] && die "${FUNCTION} requires an argument!"
97 if ! python -c "import $1" >/dev/null 2>&1; then
98 return 1
99 fi
100 return 0
101}
102
103# 1164#
104# name: python_mod_compile 1165# Example:
1166# python_mod_optimize ctypesgencore
1167python_mod_optimize() {
1168 # Check if phase is pkg_postinst().
1169 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()"
1170
1171 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1172 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
1173
1174 # Strip trailing slash from ROOT.
1175 root="${ROOT%/}"
1176
1177 # Respect ROOT and options passed to compileall.py.
1178 while (($#)); do
1179 case "$1" in
1180 -l|-f|-q)
1181 options+=("$1")
1182 ;;
1183 -d|-x)
1184 options+=("$1" "$2")
1185 shift
1186 ;;
1187 -*)
1188 ewarn "${FUNCNAME}: Ignoring option '$1'"
1189 ;;
1190 *)
1191 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1192 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories"
1193 elif [[ "$1" =~ ^/ ]]; then
1194 if [[ -d "${root}/$1" ]]; then
1195 other_dirs+=("${root}/$1")
1196 elif [[ -f "${root}/$1" ]]; then
1197 other_files+=("${root}/$1")
1198 elif [[ -e "${root}/$1" ]]; then
1199 ewarn "'${root}/$1' is not a file or a directory!"
1200 else
1201 ewarn "'${root}/$1' does not exist!"
1202 fi
1203 else
1204 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1205 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
1206 site_packages_dirs+=("$1")
1207 break
1208 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
1209 site_packages_files+=("$1")
1210 break
1211 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
1212 ewarn "'$1' is not a file or a directory!"
1213 else
1214 ewarn "'$1' does not exist!"
1215 fi
1216 done
1217 fi
1218 ;;
1219 esac
1220 shift
1221 done
1222
1223 # Set additional options.
1224 options+=("-q")
1225
1226 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1227 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
1228 return_code="0"
1229 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
1230 if ((${#site_packages_dirs[@]})); then
1231 for dir in "${site_packages_dirs[@]}"; do
1232 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
1233 done
1234 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
1235 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
1236 fi
1237 if ((${#site_packages_files[@]})); then
1238 for file in "${site_packages_files[@]}"; do
1239 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
1240 done
1241 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
1242 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
1243 fi
1244 eend "${return_code}"
1245 fi
1246 unset site_packages_absolute_dirs site_packages_absolute_files
1247 done
1248
1249 # Restore previous value of PYTHON_ABI.
1250 if [[ -n "${previous_PYTHON_ABI}" ]]; then
1251 PYTHON_ABI="${previous_PYTHON_ABI}"
1252 else
1253 unset PYTHON_ABI
1254 fi
1255
1256 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1257 return_code="0"
1258 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)"
1259 if ((${#other_dirs[@]})); then
1260 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
1261 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
1262 fi
1263 if ((${#other_files[@]})); then
1264 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
1265 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
1266 fi
1267 eend "${return_code}"
1268 fi
1269 else
1270 local myroot mydirs=() myfiles=() myopts=() return_code="0"
1271
1272 # strip trailing slash
1273 myroot="${ROOT%/}"
1274
1275 # respect ROOT and options passed to compileall.py
1276 while (($#)); do
1277 case "$1" in
1278 -l|-f|-q)
1279 myopts+=("$1")
1280 ;;
1281 -d|-x)
1282 myopts+=("$1" "$2")
1283 shift
1284 ;;
1285 -*)
1286 ewarn "${FUNCNAME}: Ignoring option '$1'"
1287 ;;
1288 *)
1289 if [[ -d "${myroot}"/$1 ]]; then
1290 mydirs+=("${myroot}/$1")
1291 elif [[ -f "${myroot}"/$1 ]]; then
1292 # Files are passed to python_mod_compile which is ROOT-aware
1293 myfiles+=("$1")
1294 elif [[ -e "${myroot}/$1" ]]; then
1295 ewarn "${myroot}/$1 is not a file or directory!"
1296 else
1297 ewarn "${myroot}/$1 does not exist!"
1298 fi
1299 ;;
1300 esac
1301 shift
1302 done
1303
1304 # set additional opts
1305 myopts+=(-q)
1306
1307 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)"
1308 if ((${#mydirs[@]})); then
1309 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
1310 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
1311 fi
1312
1313 if ((${#myfiles[@]})); then
1314 python_mod_compile "${myfiles[@]}"
1315 fi
1316
1317 eend "${return_code}"
1318 fi
1319}
1320
1321# @FUNCTION: python_mod_cleanup
1322# @USAGE: [directory|file]
1323# @DESCRIPTION:
1324# Run with optional arguments, where arguments are Python modules. If none given,
1325# it will look in /usr/lib/python[0-9].[0-9].
1326#
1327# It will recursively scan all compiled Python modules in the directories and
1328# determine if they are orphaned (i.e. their corresponding .py files are missing.)
1329# If they are, then it will remove their corresponding .pyc and .pyo files.
1330#
1331# This function should only be run in pkg_postrm().
1332python_mod_cleanup() {
1333 local path py_file PYTHON_ABI SEARCH_PATH=() root
1334
1335 # Check if phase is pkg_postrm().
1336 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
1337
1338 # Strip trailing slash from ROOT.
1339 root="${ROOT%/}"
1340
1341 if (($#)); then
1342 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1343 while (($#)); do
1344 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1345 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories"
1346 elif [[ "$1" =~ ^/ ]]; then
1347 SEARCH_PATH+=("${root}/${1#/}")
1348 else
1349 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1350 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
1351 done
1352 fi
1353 shift
1354 done
1355 else
1356 SEARCH_PATH=("${@#/}")
1357 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
1358 fi
1359 else
1360 local dir sitedir
1361 for dir in "${root}"/usr/lib*; do
1362 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
1363 for sitedir in "${dir}"/python*/site-packages; do
1364 if [[ -d "${sitedir}" ]]; then
1365 SEARCH_PATH+=("${sitedir}")
1366 fi
1367 done
1368 fi
1369 done
1370 fi
1371
1372 local BLUE CYAN NORMAL
1373 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1374 BLUE=$'\e[1;34m'
1375 CYAN=$'\e[1;36m'
1376 NORMAL=$'\e[0m'
1377 else
1378 BLUE=
1379 CYAN=
1380 NORMAL=
1381 fi
1382
1383 for path in "${SEARCH_PATH[@]}"; do
1384 if [[ -d "${path}" ]]; then
1385 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
1386 py_file="${REPLY%[co]}"
1387 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
1388 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
1389 rm -f "${py_file}"[co]
1390 done
1391
1392 # Attempt to delete directories, which may be empty.
1393 find "${path}" -type d | sort -r | while read -r dir; do
1394 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
1395 done
1396 elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then
1397 einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1398 rm -f "${path}"[co]
1399 fi
1400 done
1401}
1402
1403# ================================================================================================
1404# ===================================== DEPRECATED FUNCTIONS =====================================
1405# ================================================================================================
1406
1407# @FUNCTION: python_version
1408# @DESCRIPTION:
1409# Run without arguments and it will export the version of python
1410# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
1411python_version() {
1412 [[ -n "${PYVER}" ]] && return 0
1413 local tmpstr
1414 python="${python:-/usr/bin/python}"
1415 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
1416 export PYVER_ALL="${tmpstr#Python }"
1417 export PYVER_MAJOR="${PYVER_ALL:0:1}"
1418 export PYVER_MINOR="${PYVER_ALL:2:1}"
1419 if [[ "${PYVER_ALL:3:1}" == "." ]]; then
1420 export PYVER_MICRO="${PYVER_ALL:4}"
1421 fi
1422 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
1423}
1424
1425# @FUNCTION: python_mod_compile
1426# @USAGE: <file> [more files ...]
1427# @DESCRIPTION:
105# desc: given a filename, it will pre-compile the module's .pyc and .pyo. 1428# Given filenames, it will pre-compile the module's .pyc and .pyo.
106# should only be run in pkg_postinst() 1429# This function should only be run in pkg_postinst()
107# exam: 1430#
1431# Example:
108# python_mod_compile ${ROOT}usr/lib/python2.3/site-packages/pygoogle.py 1432# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
109# 1433#
110python_mod_compile() { 1434python_mod_compile() {
111 # allow compiling for older python versions 1435 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
112 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 1436 die "${FUNCNAME}() cannot be used in this EAPI"
113 PYVER=${PYTHON_OVERRIDE_PYVER}
114 else
115 python_version
116 fi 1437 fi
117 1438
118 if [ -f "$1" ]; then 1439 local f myroot myfiles=()
119 python${PYVER} -c "import py_compile; py_compile.compile('${1}')" || \
120 ewarn "Failed to compile ${1}"
121 python${PYVER} -O -c "import py_compile; py_compile.compile('${1}')" || \
122 ewarn "Failed to compile ${1}"
123 else
124 ewarn "Unable to find ${1}"
125 fi
126}
127 1440
128# 1441 # Check if phase is pkg_postinst()
129# name: python_mod_optimize 1442 [[ ${EBUILD_PHASE} != postinst ]] &&\
130# desc: if no arguments supplied, it will recompile all modules under 1443 die "${FUNCNAME} should only be run in pkg_postinst()"
131# sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..) 1444
132# no recursively
133#
134# if supplied with arguments, it will recompile all modules recursively
135# in the supplied directory
136# exam:
137# python_mod_optimize ${ROOT}usr/share/codegen
138#
139python_mod_optimize() {
140 local myroot
141 # strip trailing slash 1445 # strip trailing slash
142 myroot="${ROOT%/}" 1446 myroot="${ROOT%/}"
143 1447
144 # allow compiling for older python versions 1448 # respect ROOT
145 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 1449 for f in "$@"; do
146 PYVER=${PYTHON_OVERRIDE_PYVER} 1450 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
1451 done
1452
1453 if ((${#myfiles[@]})); then
1454 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
1455 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
147 else 1456 else
148 python_version 1457 ewarn "No files to compile!"
149 fi
150
151 # set opts
152 if [ "${PYVER}" = "2.2" ]; then
153 compileopts=""
154 else
155 compileopts="-q"
156 fi
157
158 ebegin "Byte compiling python modules for python-${PYVER} .."
159 python${PYVER} ${myroot}/usr/lib/python${PYVER}/compileall.py ${compileopts} $@
160 python${PYVER} -O ${myroot}/usr/lib/python${PYVER}/compileall.py ${compileopts} $@
161 eend $?
162}
163
164#
165# name: python_mod_cleanup
166# desc: run with optional arguments, where arguments are directories of
167# python modules. if none given, it will look in /usr/lib/python[0-9].[0-9]
168#
169# it will recursively scan all compiled python modules in the directories
170# and determine if they are orphaned (eg. their corresponding .py is missing.)
171# if they are, then it will remove their corresponding .pyc and .pyo
172#
173python_mod_cleanup() {
174 local SEARCH_PATH myroot
175
176 # strip trailing slash
177 myroot="${ROOT%/}"
178
179 if [ $# -gt 0 ]; then
180 for path in $@; do
181 SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}"
182 done
183 else
184 for path in ${myroot}/usr/lib*/python*/site-packages; do
185 SEARCH_PATH="${SEARCH_PATH} ${path}"
186 done
187 fi
188
189 for path in ${SEARCH_PATH}; do
190 einfo "Cleaning orphaned Python bytecode from ${path} .."
191 for obj in $(find ${path} -name *.pyc); do
192 src_py="${obj%c}"
193 if [ ! -f "${src_py}" ]; then
194 einfo "Purging ${src_py}[co]"
195 rm -f ${src_py}[co]
196 fi 1458 fi
197 done
198 # attempt to remove directories that maybe empty
199 for dir in $(find ${path} -type d | sort -r); do
200 rmdir ${dir} 2>/dev/null
201 done
202 done
203} 1459}

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.86

  ViewVC Help
Powered by ViewVC 1.1.20