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

Diff of /eclass/python.eclass

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

Revision 1.84 Revision 1.85
1# Copyright 1999-2010 Gentoo Foundation 1# Copyright 1999-2010 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.84 2010/01/11 16:07:23 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.85 2010/01/14 19:23:02 arfrever Exp $
4 4
5# @ECLASS: python.eclass 5# @ECLASS: python.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# python@gentoo.org 7# python@gentoo.org
8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules. 8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules.
23 PYTHON_ATOM="dev-lang/python" 23 PYTHON_ATOM="dev-lang/python"
24fi 24fi
25 25
26DEPEND+=" >=app-admin/eselect-python-20090804" 26DEPEND+=" >=app-admin/eselect-python-20090804"
27 27
28__python_eclass_test() { 28# @ECLASS-VARIABLE: PYTHON_USE_WITH
29 __python_version_extract 2.3 29# @DESCRIPTION:
30 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 30# Set this to a space separated list of use flags
31 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" 31# the python slot in use must be built with.
32 __python_version_extract 2.3.4
33 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
34 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
35 __python_version_extract 2.3.5
36 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
37 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
38 __python_version_extract 2.4
39 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
40 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
41 __python_version_extract 2.5b3
42 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
43 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
44}
45 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
46# @FUNCTION: python_version 44# @FUNCTION: python_pkg_setup
47# @DESCRIPTION: 45# @DESCRIPTION:
48# Run without arguments and it will export the version of python 46# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
49# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 47# are respected. Only exported if one of those variables is set.
50__python_version_extract() { 48if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
51 local verstr=$1 49 python_pkg_setup() {
52 export PYVER_MAJOR=${verstr:0:1} 50 python_pkg_setup_fail() {
53 export PYVER_MINOR=${verstr:2:1} 51 eerror "${1}"
54 if [[ ${verstr:3:1} == . ]]; then 52 die "${1}"
55 export PYVER_MICRO=${verstr:4} 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
56 fi 86 fi
57 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}" 87 }
58}
59 88
60python_version() { 89 EXPORT_FUNCTIONS pkg_setup
61 [[ -n "${PYVER}" ]] && return 0
62 local tmpstr
63 python=${python:-/usr/bin/python}
64 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
65 export PYVER_ALL="${tmpstr#Python }"
66 __python_version_extract $PYVER_ALL
67}
68 90
69# @FUNCTION: PYTHON 91 if [[ -n "${PYTHON_USE_WITH}" ]]; then
70# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}"> 92 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
71# @DESCRIPTION: 93 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
72# Get Python interpreter filename for specified Python ABI. If Python_ABI argument 94 PYTHON_USE_WITH_ATOM="|| ( "
73# is ommitted, then PYTHON_ABI environment variable must be set and is used. 95 for use in ${PYTHON_USE_WITH_OR}; do
74# If -2 option is specified, then active version of Python 2 is used. 96 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
75# If -3 option is specified, then active version of Python 3 is used. 97 done
76# If --active option is specified, then active version of Python is used. 98 unset use
77# Active version of Python can be set by python_set_active_version(). 99 PYTHON_USE_WITH_ATOM+=" )"
78# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used. 100 fi
79# -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously. 101 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
80# If --ABI option is specified, then only specified Python ABI is printed instead of 102 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
81# Python interpreter filename. 103 fi
82# --ABI and --absolute-path options cannot be specified simultaneously. 104 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
83PYTHON() { 105 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
84 local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" python2="0" python3="0" slot= 106fi
85 107
86 while (($#)); do 108# ================================================================================================
87 case "$1" in 109# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ========
88 -2) 110# ================================================================================================
89 python2="1" 111
90 ;; 112# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
91 -3) 113# @DESCRIPTION:
92 python3="1" 114# Set this to define default functions for the following ebuild phases:
93 ;; 115# src_prepare, src_configure, src_compile, src_test, src_install.
94 --ABI) 116if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
95 ABI_output="1" 117 python_src_prepare() {
96 ;; 118 python_copy_sources
97 -A|--active) 119 }
98 active="1" 120
99 ;; 121 for python_default_function in src_configure src_compile src_test src_install; do
100 -a|--absolute-path) 122 eval "python_${python_default_function}() { python_execute_function -d -s; }"
101 absolute_path_output="1"
102 ;;
103 -f|--final-ABI)
104 final_ABI="1"
105 ;;
106 --)
107 break
108 ;;
109 -*)
110 die "${FUNCNAME}(): Unrecognized option '$1'"
111 ;;
112 *)
113 break
114 ;;
115 esac
116 shift
117 done 123 done
124 unset python_default_function
118 125
119 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then 126 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
120 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
121 fi 127fi
122
123 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then
124 die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously"
125 fi
126
127 if [[ "$#" -eq 0 ]]; then
128 if [[ "${active}" == "1" ]]; then
129 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
130 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
131 fi
132 slot="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
133 elif [[ "${final_ABI}" == "1" ]]; then
134 validate_PYTHON_ABIS
135 slot="${PYTHON_ABIS##* }"
136 elif [[ "${python2}" == "1" ]]; then
137 slot="$(eselect python show --python2)"
138 if [[ -z "${slot}" ]]; then
139 die "${FUNCNAME}(): Active Python 2 interpreter not set"
140 elif [[ "${slot}" != "python2."* ]]; then
141 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
142 fi
143 slot="${slot#python}"
144 elif [[ "${python3}" == "1" ]]; then
145 slot="$(eselect python show --python3)"
146 if [[ -z "${slot}" ]]; then
147 die "${FUNCNAME}(): Active Python 3 interpreter not set"
148 elif [[ "${slot}" != "python3."* ]]; then
149 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
150 fi
151 slot="${slot#python}"
152 elif [[ -n "${PYTHON_ABI}" ]]; then
153 slot="${PYTHON_ABI}"
154 else
155 die "${FUNCNAME}(): Invalid usage"
156 fi
157 elif [[ "$#" -eq 1 ]]; then
158 if [[ "${active}" == "1" ]]; then
159 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
160 fi
161 if [[ "${final_ABI}" == "1" ]]; then
162 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
163 fi
164 if [[ "${python2}" == "1" ]]; then
165 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
166 fi
167 if [[ "${python3}" == "1" ]]; then
168 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
169 fi
170 slot="$1"
171 else
172 die "${FUNCNAME}(): Invalid usage"
173 fi
174
175 if [[ "${ABI_output}" == "1" ]]; then
176 echo -n "${slot}"
177 return
178 elif [[ "${absolute_path_output}" == "1" ]]; then
179 echo -n "/usr/bin/python${slot}"
180 else
181 echo -n "python${slot}"
182 fi
183
184 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
185 echo -n "-${ABI}"
186 fi
187}
188
189_python_implementation() {
190 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
191 return 0
192 else
193 return 1
194 fi
195}
196
197# @FUNCTION: python_set_active_version
198# @USAGE: <Python_ABI|2|3>
199# @DESCRIPTION:
200# Set active version of Python.
201python_set_active_version() {
202 if [[ "$#" -ne "1" ]]; then
203 die "${FUNCNAME}() requires 1 argument"
204 fi
205
206 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
207 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
208 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
209 fi
210 export EPYTHON="$(PYTHON "$1")"
211 elif [[ "$1" == "2" ]]; then
212 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
213 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
214 fi
215 export EPYTHON="$(PYTHON -2)"
216 elif [[ "$1" == "3" ]]; then
217 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
218 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
219 fi
220 export EPYTHON="$(PYTHON -3)"
221 else
222 die "${FUNCNAME}(): Unrecognized argument '$1'"
223 fi
224
225 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
226 # so it does not need to be exported to subprocesses.
227 PYTHON_ABI="${EPYTHON#python}"
228 PYTHON_ABI="${PYTHON_ABI%%-*}"
229}
230 128
231unset PYTHON_ABIS 129unset PYTHON_ABIS
232unset PYTHON_ABIS_SANITY_CHECKS 130unset PYTHON_ABIS_SANITY_CHECKS
233 131
234# @FUNCTION: validate_PYTHON_ABIS 132# @FUNCTION: validate_PYTHON_ABIS
395 done 293 done
396 PYTHON_ABIS_SANITY_CHECKS="1" 294 PYTHON_ABIS_SANITY_CHECKS="1"
397 fi 295 fi
398} 296}
399 297
400# @FUNCTION: python_copy_sources
401# @USAGE: [--no-link] [--] [directory]
402# @DESCRIPTION:
403# Copy unpacked sources of given package for each Python ABI.
404python_copy_sources() {
405 local dir dirs=() no_link="0" PYTHON_ABI
406
407 while (($#)); do
408 case "$1" in
409 --no-link)
410 no_link="1"
411 ;;
412 --)
413 break
414 ;;
415 -*)
416 die "${FUNCNAME}(): Unrecognized option '$1'"
417 ;;
418 *)
419 break
420 ;;
421 esac
422 shift
423 done
424
425 if [[ "$#" -eq 0 ]]; then
426 if [[ "${WORKDIR}" == "${S}" ]]; then
427 die "${FUNCNAME}() cannot be used"
428 fi
429 dirs="${S}"
430 else
431 dirs="$@"
432 fi
433
434 validate_PYTHON_ABIS
435 for PYTHON_ABI in ${PYTHON_ABIS}; do
436 for dir in "${dirs[@]}"; do
437 if [[ "${no_link}" == "1" ]]; then
438 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
439 else
440 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
441 fi
442 done
443 done
444}
445
446# @FUNCTION: python_set_build_dir_symlink
447# @USAGE: [directory="build"]
448# @DESCRIPTION:
449# Create build directory symlink.
450python_set_build_dir_symlink() {
451 local dir="$1"
452
453 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
454 [[ -z "${dir}" ]] && dir="build"
455
456 # Do not delete preexistent directories.
457 rm -f "${dir}" || die "Deletion of '${dir}' failed"
458 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
459}
460
461# @FUNCTION: python_execute_function 298# @FUNCTION: python_execute_function
462# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments] 299# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
463# @DESCRIPTION: 300# @DESCRIPTION:
464# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 301# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
465# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 302# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
678 if [[ "${default_function}" == "1" ]]; then 515 if [[ "${default_function}" == "1" ]]; then
679 unset -f python_default_function 516 unset -f python_default_function
680 fi 517 fi
681} 518}
682 519
683# @FUNCTION: python_convert_shebangs 520# @FUNCTION: python_copy_sources
684# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories] 521# @USAGE: [--no-link] [--] [directory]
685# @DESCRIPTION: 522# @DESCRIPTION:
686# Convert shebangs in specified files. Directories can be specified only with --recursive option. 523# Copy unpacked sources of given package for each Python ABI.
687python_convert_shebangs() { 524python_copy_sources() {
688 local argument file files=() only_executables="0" python_version quiet="0" recursive="0" 525 local dir dirs=() no_link="0" PYTHON_ABI
689 526
690 while (($#)); do 527 while (($#)); do
691 case "$1" in 528 case "$1" in
692 -r|--recursive) 529 --no-link)
693 recursive="1" 530 no_link="1"
694 ;;
695 -q|--quiet)
696 quiet="1"
697 ;;
698 -x|--only-executables)
699 only_executables="1"
700 ;; 531 ;;
701 --) 532 --)
702 break 533 break
703 ;; 534 ;;
704 -*) 535 -*)
710 esac 541 esac
711 shift 542 shift
712 done 543 done
713 544
714 if [[ "$#" -eq 0 ]]; then 545 if [[ "$#" -eq 0 ]]; then
715 die "${FUNCNAME}(): Missing Python version and files or directories" 546 if [[ "${WORKDIR}" == "${S}" ]]; then
716 elif [[ "$#" -eq 1 ]]; then 547 die "${FUNCNAME}() cannot be used"
717 die "${FUNCNAME}(): Missing files or directories"
718 fi 548 fi
549 dirs="${S}"
550 else
551 dirs="$@"
552 fi
719 553
720 python_version="$1" 554 validate_PYTHON_ABIS
721 shift 555 for PYTHON_ABI in ${PYTHON_ABIS}; do
722 556 for dir in "${dirs[@]}"; do
723 for argument in "$@"; do
724 if [[ ! -e "${argument}" ]]; then
725 die "${FUNCNAME}(): '${argument}' does not exist"
726 elif [[ -f "${argument}" ]]; then
727 files+=("${argument}")
728 elif [[ -d "${argument}" ]]; then
729 if [[ "${recursive}" == "1" ]]; then 557 if [[ "${no_link}" == "1" ]]; then
730 if [[ "${only_executables}" == "1" ]]; then 558 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
731 files+=($(find "${argument}" -perm /111 -type f))
732 else
733 files+=($(find "${argument}" -type f))
734 fi
735 else 559 else
736 die "${FUNCNAME}(): '${argument}' is not a regular file" 560 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
737 fi
738 else
739 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
740 fi 561 fi
562 done
741 done 563 done
564}
742 565
743 for file in "${files[@]}"; do 566# @FUNCTION: python_set_build_dir_symlink
744 file="${file#./}" 567# @USAGE: [directory="build"]
745 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue 568# @DESCRIPTION:
569# Create build directory symlink.
570python_set_build_dir_symlink() {
571 local dir="$1"
746 572
747 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then 573 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
748 if [[ "${quiet}" == "0" ]]; then 574 [[ -z "${dir}" ]] && dir="build"
749 einfo "Converting shebang in '${file}'"
750 fi
751 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
752 575
753 # Delete potential whitespace after "#!". 576 # Do not delete preexistent directories.
754 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed" 577 rm -f "${dir}" || die "Deletion of '${dir}' failed"
755 fi 578 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
756 done
757} 579}
758 580
759# @FUNCTION: python_generate_wrapper_scripts 581# @FUNCTION: python_generate_wrapper_scripts
760# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files] 582# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
761# @DESCRIPTION: 583# @DESCRIPTION:
916 fi 738 fi
917 fperms +x "${file#${D%/}}" || die "fperms '${file}' failed" 739 fperms +x "${file#${D%/}}" || die "fperms '${file}' failed"
918 done 740 done
919} 741}
920 742
921# @ECLASS-VARIABLE: PYTHON_USE_WITH 743# ================================================================================================
922# @DESCRIPTION: 744# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======
923# Set this to a space separated list of use flags 745# ================================================================================================
924# the python slot in use must be built with.
925 746
926# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR 747# @FUNCTION: python_set_active_version
748# @USAGE: <Python_ABI|2|3>
927# @DESCRIPTION: 749# @DESCRIPTION:
928# Set this to a space separated list of use flags 750# Set active version of Python.
929# of which one must be turned on for the slot of 751python_set_active_version() {
930# in use. 752 if [[ "$#" -ne "1" ]]; then
753 die "${FUNCNAME}() requires 1 argument"
754 fi
931 755
932# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT 756 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
933# @DESCRIPTION: 757 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
934# Set this if you need to make either PYTHON_USE_WITH or 758 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
935# PYTHON_USE_WITH_OR atoms conditional under a use flag.
936
937# @FUNCTION: python_pkg_setup
938# @DESCRIPTION:
939# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
940# are respected. Only exported if one of those variables is set.
941if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
942 python_pkg_setup() {
943 python_pkg_setup_fail() {
944 eerror "${1}"
945 die "${1}"
946 }
947
948 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
949
950 python_pkg_setup_check_USE_flags() {
951 local pyatom use
952 if [[ -n "${PYTHON_ABI}" ]]; then
953 pyatom="dev-lang/python:${PYTHON_ABI}"
954 else
955 pyatom="dev-lang/python:$(PYTHON -A --ABI)"
956 fi 759 fi
957 760 export EPYTHON="$(PYTHON "$1")"
958 for use in ${PYTHON_USE_WITH}; do 761 elif [[ "$1" == "2" ]]; then
959 if ! has_version "${pyatom}[${use}]"; then 762 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
960 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}" 763 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
961 fi
962 done
963
964 for use in ${PYTHON_USE_WITH_OR}; do
965 if has_version "${pyatom}[${use}]"; then
966 return
967 fi
968 done
969
970 if [[ ${PYTHON_USE_WITH_OR} ]]; then
971 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
972 fi 764 fi
973 } 765 export EPYTHON="$(PYTHON -2)"
974 766 elif [[ "$1" == "3" ]]; then
975 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 767 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
976 python_execute_function -q python_pkg_setup_check_USE_flags 768 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
769 fi
770 export EPYTHON="$(PYTHON -3)"
977 else 771 else
978 python_pkg_setup_check_USE_flags 772 die "${FUNCNAME}(): Unrecognized argument '$1'"
979 fi 773 fi
980 }
981 774
982 EXPORT_FUNCTIONS pkg_setup 775 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
983 776 # so it does not need to be exported to subprocesses.
984 if [[ -n "${PYTHON_USE_WITH}" ]]; then 777 PYTHON_ABI="${EPYTHON#python}"
985 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 778 PYTHON_ABI="${PYTHON_ABI%%-*}"
986 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
987 PYTHON_USE_WITH_ATOM="|| ( "
988 for use in ${PYTHON_USE_WITH_OR}; do
989 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
990 done
991 unset use
992 PYTHON_USE_WITH_ATOM+=" )"
993 fi
994 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
995 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
996 fi
997 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
998 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
999fi
1000
1001# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
1002# @DESCRIPTION:
1003# Set this to define default functions for the following ebuild phases:
1004# src_prepare, src_configure, src_compile, src_test, src_install.
1005if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
1006 python_src_prepare() {
1007 python_copy_sources
1008 }
1009
1010 for python_default_function in src_configure src_compile src_test src_install; do
1011 eval "python_${python_default_function}() { python_execute_function -d -s; }"
1012 done
1013 unset python_default_function
1014
1015 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
1016fi
1017
1018# @FUNCTION: python_disable_pyc
1019# @DESCRIPTION:
1020# Tell Python not to automatically recompile modules to .pyc/.pyo
1021# even if the timestamps/version stamps do not match. This is done
1022# to protect sandbox.
1023python_disable_pyc() {
1024 export PYTHONDONTWRITEBYTECODE="1"
1025}
1026
1027# @FUNCTION: python_enable_pyc
1028# @DESCRIPTION:
1029# Tell Python to automatically recompile modules to .pyc/.pyo if the
1030# timestamps/version stamps have changed.
1031python_enable_pyc() {
1032 unset PYTHONDONTWRITEBYTECODE
1033} 779}
1034 780
1035# @FUNCTION: python_need_rebuild 781# @FUNCTION: python_need_rebuild
1036# @DESCRIPTION: Run without arguments, specifies that the package should be 782# @DESCRIPTION: Run without arguments, specifies that the package should be
1037# rebuilt after a python upgrade. 783# rebuilt after a python upgrade.
1039# for multiple versions of Python. 785# for multiple versions of Python.
1040python_need_rebuild() { 786python_need_rebuild() {
1041 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)" 787 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
1042} 788}
1043 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
1044# @FUNCTION: python_get_includedir 914# @FUNCTION: python_get_includedir
1045# @DESCRIPTION: 915# @DESCRIPTION:
1046# Run without arguments, returns the Python include directory. 916# Run without arguments, returns the Python include directory.
1047python_get_includedir() { 917python_get_includedir() {
1048 if [[ -n "${PYTHON_ABI}" ]]; then 918 if [[ -n "${PYTHON_ABI}" ]]; then
1068# Run without arguments, returns the Python site-packages directory. 938# Run without arguments, returns the Python site-packages directory.
1069python_get_sitedir() { 939python_get_sitedir() {
1070 echo "$(python_get_libdir)/site-packages" 940 echo "$(python_get_libdir)/site-packages"
1071} 941}
1072 942
1073# @FUNCTION: python_tkinter_exists 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]
1074# @DESCRIPTION: 957# @DESCRIPTION:
1075# Run without arguments, checks if python was compiled with Tkinter 958# Convert shebangs in specified files. Directories can be specified only with --recursive option.
1076# support. If not, prints an error message and dies. 959python_convert_shebangs() {
1077python_tkinter_exists() { 960 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
1078 if ! python -c "import Tkinter" >/dev/null 2>&1; then 961
1079 eerror "You need to recompile python with Tkinter support." 962 while (($#)); do
1080 eerror "Try adding: 'dev-lang/python tk'" 963 case "$1" in
1081 eerror "in to /etc/portage/package.use" 964 -r|--recursive)
1082 echo 965 recursive="1"
1083 die "missing tkinter support with installed python" 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"
1084 fi 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
1085} 1029}
1086 1030
1087# @FUNCTION: python_mod_exists 1031# @FUNCTION: python_mod_exists
1088# @USAGE: <module> 1032# @USAGE: <module>
1089# @DESCRIPTION: 1033# @DESCRIPTION:
1099python_mod_exists() { 1043python_mod_exists() {
1100 [[ "$1" ]] || die "${FUNCNAME} requires an argument!" 1044 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
1101 python -c "import $1" &>/dev/null 1045 python -c "import $1" &>/dev/null
1102} 1046}
1103 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
1104# @FUNCTION: python_mod_compile 1074# @FUNCTION: python_disable_pyc
1105# @USAGE: <file> [more files ...]
1106# @DESCRIPTION: 1075# @DESCRIPTION:
1107# Given filenames, it will pre-compile the module's .pyc and .pyo. 1076# Tell Python not to automatically recompile modules to .pyc/.pyo
1108# This function should only be run in pkg_postinst() 1077# even if the timestamps/version stamps do not match. This is done
1109# 1078# to protect sandbox.
1110# Example: 1079python_disable_pyc() {
1111# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 1080 export PYTHONDONTWRITEBYTECODE="1"
1112#
1113python_mod_compile() {
1114 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1115 die "${FUNCNAME}() cannot be used in this EAPI"
1116 fi
1117
1118 local f myroot myfiles=()
1119
1120 # Check if phase is pkg_postinst()
1121 [[ ${EBUILD_PHASE} != postinst ]] &&\
1122 die "${FUNCNAME} should only be run in pkg_postinst()"
1123
1124 # strip trailing slash
1125 myroot="${ROOT%/}"
1126
1127 # respect ROOT
1128 for f in "$@"; do
1129 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
1130 done
1131
1132 if ((${#myfiles[@]})); then
1133 "$(PYTHON -A)" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
1134 "$(PYTHON -A)" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
1135 else
1136 ewarn "No files to compile!"
1137 fi
1138} 1081}
1139 1082
1140# @FUNCTION: python_mod_optimize 1083# @FUNCTION: python_mod_optimize
1141# @USAGE: [options] [directory|file] 1084# @USAGE: [options] [directory|file]
1142# @DESCRIPTION: 1085# @DESCRIPTION:
1380 einfo "${BLUE}<<< ${path}[co]${NORMAL}" 1323 einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1381 rm -f "${path}"[co] 1324 rm -f "${path}"[co]
1382 fi 1325 fi
1383 done 1326 done
1384} 1327}
1328
1329# ================================================================================================
1330# ===================================== DEPRECATED FUNCTIONS =====================================
1331# ================================================================================================
1332
1333__python_eclass_test() {
1334 __python_version_extract 2.3
1335 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
1336 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
1337 __python_version_extract 2.3.4
1338 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
1339 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
1340 __python_version_extract 2.3.5
1341 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
1342 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
1343 __python_version_extract 2.4
1344 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
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"
1349}
1350
1351# @FUNCTION: python_version
1352# @DESCRIPTION:
1353# Run without arguments and it will export the version of python
1354# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
1355__python_version_extract() {
1356 local verstr=$1
1357 export PYVER_MAJOR=${verstr:0:1}
1358 export PYVER_MINOR=${verstr:2:1}
1359 if [[ ${verstr:3:1} == . ]]; then
1360 export PYVER_MICRO=${verstr:4}
1361 fi
1362 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
1363}
1364
1365python_version() {
1366 [[ -n "${PYVER}" ]] && return 0
1367 local tmpstr
1368 python=${python:-/usr/bin/python}
1369 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
1370 export PYVER_ALL="${tmpstr#Python }"
1371 __python_version_extract $PYVER_ALL
1372}
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()
1379#
1380# Example:
1381# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
1382#
1383python_mod_compile() {
1384 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1385 die "${FUNCNAME}() cannot be used in this EAPI"
1386 fi
1387
1388 local f myroot myfiles=()
1389
1390 # Check if phase is pkg_postinst()
1391 [[ ${EBUILD_PHASE} != postinst ]] &&\
1392 die "${FUNCNAME} should only be run in pkg_postinst()"
1393
1394 # strip trailing slash
1395 myroot="${ROOT%/}"
1396
1397 # respect ROOT
1398 for f in "$@"; do
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
1405 else
1406 ewarn "No files to compile!"
1407 fi
1408}

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

  ViewVC Help
Powered by ViewVC 1.1.20