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

Diff of /eclass/python.eclass

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

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

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.85

  ViewVC Help
Powered by ViewVC 1.1.20