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

Diff of /eclass/python.eclass

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

Revision 1.60 Revision 1.94
1# Copyright 1999-2009 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.60 2009/08/05 18:31:30 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.94 2010/03/13 13:46:20 arfrever Exp $
4 4
5# @ECLASS: python.eclass 5# @ECLASS: python.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# python@gentoo.org 7# Gentoo Python Project <python@gentoo.org>
8# 8# @BLURB: Eclass for Python packages
9# original author: Alastair Tse <liquidx@gentoo.org>
10# @BLURB: A Utility Eclass that should be inherited by anything that deals with Python or Python modules.
11# @DESCRIPTION: 9# @DESCRIPTION:
12# Some useful functions for dealing with python. 10# The python eclass contains miscellaneous, useful functions for Python packages.
13inherit alternatives multilib
14 11
12inherit multilib
15 13
16if [[ -n "${NEED_PYTHON}" ]] ; then 14if ! has "${EAPI:-0}" 0 1 2 3; then
17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" 15 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
18 DEPEND="${PYTHON_ATOM}" 16fi
17
18_CPYTHON2_SUPPORTED_ABIS=(2.4 2.5 2.6 2.7)
19_CPYTHON3_SUPPORTED_ABIS=(3.0 3.1 3.2)
20_JYTHON_SUPPORTED_ABIS=(2.5-jython)
21
22# @ECLASS-VARIABLE: PYTHON_DEPEND
23# @DESCRIPTION:
24# Specification of dependency on dev-lang/python.
25# Syntax:
26# PYTHON_DEPEND: [[!]USE_flag? ]<version_components_group>[ version_components_group]
27# version_components_group: <major_version[:[minimal_version][:maximal_version]]>
28# major_version: <2|3|*>
29# minimal_version: <minimal_major_version.minimal_minor_version>
30# maximal_version: <maximal_major_version.maximal_minor_version>
31
32_parse_PYTHON_DEPEND() {
33 local major_version maximal_version minimal_version python_all="0" python_maximal_version python_minimal_version python_versions=() python2="0" python2_maximal_version python2_minimal_version python3="0" python3_maximal_version python3_minimal_version USE_flag= version_components_group version_components_group_regex version_components_groups
34
35 version_components_group_regex="(2|3|\*)(:([[:digit:]]+\.[[:digit:]]+)?(:([[:digit:]]+\.[[:digit:]]+)?)?)?"
36 version_components_groups="${PYTHON_DEPEND}"
37
38 if [[ "${version_components_groups}" =~ ^((\!)?[[:alnum:]_-]+\?\ )?${version_components_group_regex}(\ ${version_components_group_regex})?$ ]]; then
39 if [[ "${version_components_groups}" =~ ^(\!)?[[:alnum:]_-]+\? ]]; then
40 USE_flag="${version_components_groups%\? *}"
41 version_components_groups="${version_components_groups#* }"
42 fi
43 if [[ "${version_components_groups}" =~ ("*".*" "|" *"|^2.*\ (2|\*)|^3.*\ (3|\*)) ]]; then
44 die "Invalid syntax of PYTHON_DEPEND: Incorrectly specified groups of versions"
45 fi
46
47 version_components_groups="${version_components_groups// /$'\n'}"
48 while read version_components_group; do
49 major_version="${version_components_group:0:1}"
50 minimal_version="${version_components_group:2}"
51 minimal_version="${minimal_version%:*}"
52 maximal_version="${version_components_group:$((3 + ${#minimal_version}))}"
53
54 if [[ "${major_version}" =~ ^(2|3)$ ]]; then
55 if [[ -n "${minimal_version}" && "${major_version}" != "${minimal_version:0:1}" ]]; then
56 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' not in specified group of versions"
57 fi
58 if [[ -n "${maximal_version}" && "${major_version}" != "${maximal_version:0:1}" ]]; then
59 die "Invalid syntax of PYTHON_DEPEND: Maximal version '${maximal_version}' not in specified group of versions"
60 fi
61 fi
62
63 if [[ "${major_version}" == "2" ]]; then
64 python2="1"
65 python_versions=("${_CPYTHON2_SUPPORTED_ABIS[@]}")
66 python2_minimal_version="${minimal_version}"
67 python2_maximal_version="${maximal_version}"
68 elif [[ "${major_version}" == "3" ]]; then
69 python3="1"
70 python_versions=("${_CPYTHON3_SUPPORTED_ABIS[@]}")
71 python3_minimal_version="${minimal_version}"
72 python3_maximal_version="${maximal_version}"
73 else
74 python_all="1"
75 python_versions=("${_CPYTHON2_SUPPORTED_ABIS[@]}" "${_CPYTHON3_SUPPORTED_ABIS[@]}")
76 python_minimal_version="${minimal_version}"
77 python_maximal_version="${maximal_version}"
78 fi
79
80 if [[ -n "${minimal_version}" ]] && ! has "${minimal_version}" "${python_versions[@]}"; then
81 die "Invalid syntax of PYTHON_DEPEND: Unrecognized minimal version '${minimal_version}'"
82 fi
83 if [[ -n "${maximal_version}" ]] && ! has "${maximal_version}" "${python_versions[@]}"; then
84 die "Invalid syntax of PYTHON_DEPEND: Unrecognized maximal version '${maximal_version}'"
85 fi
86
87 if [[ -n "${minimal_version}" && -n "${maximal_version}" && "${minimal_version}" > "${maximal_version}" ]]; then
88 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' greater than maximal version '${maximal_version}'"
89 fi
90 done <<< "${version_components_groups}"
91
92 _PYTHON_ATOMS=()
93
94 _append_accepted_versions_range() {
95 local accepted_version="0" i
96 for ((i = "${#python_versions[@]}"; i >= 0; i--)); do
97 if [[ "${python_versions[${i}]}" == "${python_maximal_version}" ]]; then
98 accepted_version="1"
99 fi
100 if [[ "${accepted_version}" == "1" ]]; then
101 _PYTHON_ATOMS+=("=dev-lang/python-${python_versions[${i}]}*")
102 fi
103 if [[ "${python_versions[${i}]}" == "${python_minimal_version}" ]]; then
104 accepted_version="0"
105 fi
106 done
107 }
108
109 if [[ "${python_all}" == "1" ]]; then
110 if [[ -z "${python_minimal_version}" && -z "${python_maximal_version}" ]]; then
111 _PYTHON_ATOMS+=("dev-lang/python")
112 else
113 python_versions=("${_CPYTHON2_SUPPORTED_ABIS[@]}" "${_CPYTHON3_SUPPORTED_ABIS[@]}")
114 python_minimal_version="${python_minimal_version:-${python_versions[0]}}"
115 python_maximal_version="${python_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
116 _append_accepted_versions_range
117 fi
118 else
119 if [[ "${python3}" == "1" ]]; then
120 if [[ -z "${python3_minimal_version}" && -z "${python3_maximal_version}" ]]; then
121 _PYTHON_ATOMS+=("=dev-lang/python-3*")
122 else
123 python_versions=("${_CPYTHON3_SUPPORTED_ABIS[@]}")
124 python_minimal_version="${python3_minimal_version:-${python_versions[0]}}"
125 python_maximal_version="${python3_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
126 _append_accepted_versions_range
127 fi
128 fi
129 if [[ "${python2}" == "1" ]]; then
130 if [[ -z "${python2_minimal_version}" && -z "${python2_maximal_version}" ]]; then
131 _PYTHON_ATOMS+=("=dev-lang/python-2*")
132 else
133 python_versions=("${_CPYTHON2_SUPPORTED_ABIS[@]}")
134 python_minimal_version="${python2_minimal_version:-${python_versions[0]}}"
135 python_maximal_version="${python2_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
136 _append_accepted_versions_range
137 fi
138 fi
139 fi
140
141 unset -f _append_accepted_versions_range
142
143 if [[ "${#_PYTHON_ATOMS[@]}" -gt 1 ]]; then
144 DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${_PYTHON_ATOMS[@]} )${USE_flag:+ )}"
145 RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${_PYTHON_ATOMS[@]} )${USE_flag:+ )}"
146 else
147 DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }${_PYTHON_ATOMS[@]}${USE_flag:+ )}"
148 RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }${_PYTHON_ATOMS[@]}${USE_flag:+ )}"
149 fi
150 else
151 die "Invalid syntax of PYTHON_DEPEND"
152 fi
153}
154
155DEPEND=">=app-admin/eselect-python-20091230"
19 RDEPEND="${DEPEND}" 156RDEPEND="${DEPEND}"
157
158if [[ -n "${PYTHON_DEPEND}" && -n "${NEED_PYTHON}" ]]; then
159 die "PYTHON_DEPEND and NEED_PYTHON cannot be set simultaneously"
160elif [[ -n "${PYTHON_DEPEND}" ]]; then
161 _parse_PYTHON_DEPEND
162elif [[ -n "${NEED_PYTHON}" ]]; then
163 if ! has "${EAPI:-0}" 0 1 2; then
164 eerror "Use PYTHON_DEPEND instead of NEED_PYTHON."
165 die "NEED_PYTHON cannot be used in this EAPI"
166 fi
167 _PYTHON_ATOMS=(">=dev-lang/python-${NEED_PYTHON}")
168 DEPEND+="${DEPEND:+ }${_PYTHON_ATOMS[@]}"
169 RDEPEND+="${RDEPEND:+ }${_PYTHON_ATOMS[@]}"
20else 170else
21 PYTHON_ATOM="dev-lang/python" 171 _PYTHON_ATOMS=("dev-lang/python")
22fi 172fi
23 173
24if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 174# @ECLASS-VARIABLE: PYTHON_USE_WITH
25 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804" 175# @DESCRIPTION:
176# Set this to a space separated list of USE flags the Python slot in use must be built with.
177
178# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
179# @DESCRIPTION:
180# Set this to a space separated list of USE flags of which one must be turned on for the slot in use.
181
182# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
183# @DESCRIPTION:
184# Set this to a name of a USE flag if you need to make either PYTHON_USE_WITH or
185# PYTHON_USE_WITH_OR atoms conditional under a USE flag.
186
187# @FUNCTION: python_pkg_setup
188# @DESCRIPTION:
189# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
190# are respected. Only exported if one of those variables is set.
191if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
192 python_pkg_setup() {
193 # Check if phase is pkg_setup().
194 [[ "${EBUILD_PHASE}" != "setup" ]] && die "${FUNCNAME}() can be used only in pkg_setup() phase"
195
196 python_pkg_setup_fail() {
197 eerror "${1}"
198 die "${1}"
199 }
200
201 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
202
203 python_pkg_setup_check_USE_flags() {
204 local pyatom use
205 pyatom="$(python_get_implementational_package)"
206
207 for use in ${PYTHON_USE_WITH}; do
208 if ! has_version "${pyatom}[${use}]"; then
209 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
210 fi
211 done
212
213 for use in ${PYTHON_USE_WITH_OR}; do
214 if has_version "${pyatom}[${use}]"; then
215 return
216 fi
217 done
218
219 if [[ ${PYTHON_USE_WITH_OR} ]]; then
220 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
221 fi
222 }
223
224 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
225 python_execute_function -q python_pkg_setup_check_USE_flags
226 else
227 python_pkg_setup_check_USE_flags
228 fi
229
230 unset -f python_pkg_setup_check_USE_flags python_pkg_setup_fail
231 }
232
233 EXPORT_FUNCTIONS pkg_setup
234
235 _PYTHON_USE_WITH_ATOMS_ARRAY=()
236 if [[ -n "${PYTHON_USE_WITH}" ]]; then
237 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
238 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]")
239 done
240 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
241 for _USE_flag in ${PYTHON_USE_WITH_OR}; do
242 for _PYTHON_ATOM in "${_PYTHON_ATOMS[@]}"; do
243 _PYTHON_USE_WITH_ATOMS_ARRAY+=("${_PYTHON_ATOM}[${_USE_flag}]")
244 done
245 done
246 unset _USE_flag
247 fi
248 if [[ "${#_PYTHON_USE_WITH_ATOMS_ARRAY[@]}" -gt 1 ]]; then
249 _PYTHON_USE_WITH_ATOMS="|| ( ${_PYTHON_USE_WITH_ATOMS_ARRAY[@]} )"
250 else
251 _PYTHON_USE_WITH_ATOMS="${_PYTHON_USE_WITH_ATOMS_ARRAY[@]}"
252 fi
253 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
254 _PYTHON_USE_WITH_ATOMS="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOMS} )"
255 fi
256 DEPEND+=" ${_PYTHON_USE_WITH_ATOMS}"
257 RDEPEND+=" ${_PYTHON_USE_WITH_ATOMS}"
258 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOMS _PYTHON_USE_WITH_ATOMS_ARRAY
26fi 259fi
27 260
28__python_eclass_test() { 261unset _PYTHON_ATOMS
29 __python_version_extract 2.3
30 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
31 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
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 262
46# @FUNCTION: python_version 263# ================================================================================================
264# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ========
265# ================================================================================================
266
267# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
47# @DESCRIPTION: 268# @DESCRIPTION:
48# Run without arguments and it will export the version of python 269# Set this in EAPI <= 4 to indicate that current package supports installation for
49# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 270# multiple versions of Python.
50__python_version_extract() { 271
51 local verstr=$1 272# @ECLASS-VARIABLE: PYTHON_EXPORT_PHASE_FUNCTIONS
52 export PYVER_MAJOR=${verstr:0:1} 273# @DESCRIPTION:
53 export PYVER_MINOR=${verstr:2:1} 274# Set this to export phase functions for the following ebuild phases:
54 if [[ ${verstr:3:1} == . ]]; then 275# src_prepare, src_configure, src_compile, src_test, src_install.
55 export PYVER_MICRO=${verstr:4} 276if ! has "${EAPI:-0}" 0 1; then
277 python_src_prepare() {
278 python_copy_sources
279 }
280
281 for python_default_function in src_configure src_compile src_test src_install; do
282 eval "python_${python_default_function}() {
283 python_execute_function -d -s \"\$@\"
284 }"
285 done
286 unset python_default_function
287
288 if [[ -n "${PYTHON_EXPORT_PHASE_FUNCTIONS}" ]]; then
289 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
290 fi
56 fi 291fi
57 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
58}
59 292
60python_version() { 293unset PYTHON_ABIS
61 [[ -n "${PYVER}" ]] && return 0
62 local tmpstr
63 python=${python:-/usr/bin/python}
64 tmpstr="$(${python} -V 2>&1 )"
65 export PYVER_ALL="${tmpstr#Python }"
66 __python_version_extract $PYVER_ALL
67}
68 294
69# @FUNCTION: PYTHON 295# @FUNCTION: validate_PYTHON_ABIS
70# @USAGE: [-a|--absolute-path] <Python_ABI="${PYTHON_ABI}">
71# @DESCRIPTION: 296# @DESCRIPTION:
72# Get Python interpreter filename for specified Python ABI. If Python_ABI argument 297# Ensure that PYTHON_ABIS variable has valid value.
73# is ommitted, then PYTHON_ABI environment variable must be set and is used. 298# This function usually should not be directly called in ebuilds.
74PYTHON() { 299validate_PYTHON_ABIS() {
75 local absolute_path="0" slot= 300 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable.
301 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
302 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
303 fi
76 304
77 while (($#)); do 305 _python_initial_sanity_checks
78 case "$1" in 306
79 -a|--absolute-path) 307 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
80 absolute_path="1" 308 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
81 ;; 309 local PYTHON_ABI restricted_ABI support_ABI supported_PYTHON_ABIS=
82 -*) 310 PYTHON_ABI_SUPPORTED_VALUES="${_CPYTHON2_SUPPORTED_ABIS[@]} ${_CPYTHON3_SUPPORTED_ABIS[@]} ${_JYTHON_SUPPORTED_ABIS[@]}"
83 die "${FUNCNAME}(): Unrecognized option $1" 311
84 ;; 312 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
85 *) 313 local cpython_enabled="0"
314
315 if [[ -z "${USE_PYTHON}" ]]; then
316 die "USE_PYTHON variable is empty"
317 fi
318
319 for PYTHON_ABI in ${USE_PYTHON}; do
320 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
321 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
322 fi
323
324 if has "${PYTHON_ABI}" "${_CPYTHON2_SUPPORTED_ABIS[@]}" "${_CPYTHON3_SUPPORTED_ABIS[@]}"; then
325 cpython_enabled="1"
326 fi
327
328 support_ABI="1"
329 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
330 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
331 support_ABI="0"
86 break 332 break
87 ;; 333 fi
88 esac 334 done
89 shift 335 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
90 done 336 done
91 337
92 if [[ "$#" -eq "0" ]]; then
93 if [[ -n "${PYTHON_ABI}" ]]; then 338 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
94 slot="${PYTHON_ABI}" 339 die "USE_PYTHON variable does not enable any version of Python supported by ${CATEGORY}/${PF}"
340 fi
341
342 if [[ "${cpython_enabled}" == "0" ]]; then
343 die "USE_PYTHON variable does not enable any version of CPython"
344 fi
95 else 345 else
96 die "${FUNCNAME}(): Invalid usage" 346 local python_version python2_version= python3_version= support_python_major_version
97 fi
98 elif [[ "$#" -eq "1" ]]; then
99 slot="$1"
100 else
101 die "${FUNCNAME}(): Invalid usage"
102 fi
103 347
104 if [[ "${absolute_path}" == "1" ]]; then 348 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
105 echo -n "/usr/bin/python${slot}"
106 else
107 echo -n "python${slot}"
108 fi
109}
110 349
111# @FUNCTION: validate_PYTHON_ABIS 350 if has_version "=dev-lang/python-2*"; then
112# @DESCRIPTION: 351 if [[ "$(readlink "${EPREFIX}/usr/bin/python2")" != "python2."* ]]; then
113# Make sure PYTHON_ABIS variable has valid value. 352 die "'${EPREFIX}/usr/bin/python2' is not valid symlink"
114validate_PYTHON_ABIS() {
115 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
116 if [[ "$(</usr/bin/python)" != *"Gentoo Python wrapper program"* ]]; then
117 die "/usr/bin/python isn't valid program"
118 fi
119 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
120 die "/usr/bin/python-config isn't valid script"
121 fi
122
123 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4.
124 if [[ -z "${PYTHON_ABIS}" ]] && has "${EAPI:-0}" 0 1 2 3; then
125 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI
126 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
127 for ABI in ${USE_PYTHON}; do
128 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
129 ewarn "Ignoring unsupported Python ABI '${ABI}'"
130 continue
131 fi 353 fi
132 support_ABI="1" 354
355 python2_version="$("${EPREFIX}/usr/bin/python2" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
356
357 for PYTHON_ABI in "${_CPYTHON2_SUPPORTED_ABIS[@]}"; do
358 support_python_major_version="1"
133 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 359 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
134 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 360 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
135 support_ABI="0" 361 support_python_major_version="0"
136 break
137 fi 362 fi
363 done
364 [[ "${support_python_major_version}" == "1" ]] && break
138 done 365 done
139 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 366 if [[ "${support_python_major_version}" == "1" ]]; then
367 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
368 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
369 die "Active version of Python 2 is not supported by ${CATEGORY}/${PF}"
370 fi
140 done 371 done
141 export PYTHON_ABIS="${supported_PYTHON_ABIS# }" 372 else
142 fi
143
144 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
145 python_version 373 python2_version=""
146 export PYTHON_ABIS="${PYVER}" 374 fi
147 fi
148}
149
150# @FUNCTION: python_copy_sources
151# @USAGE: [directory]
152# @DESCRIPTION:
153# Copy unpacked sources of given package for each Python ABI.
154python_copy_sources() {
155 local dir dirs=() PYTHON_ABI
156
157 if [[ "$#" -eq "0" ]]; then
158 if [[ "${WORKDIR}" == "${S}" ]]; then
159 die "${FUNCNAME}() cannot be used"
160 fi 375 fi
161 dirs="${S}"
162 else
163 dirs="$@"
164 fi
165 376
166 validate_PYTHON_ABIS 377 if has_version "=dev-lang/python-3*"; then
378 if [[ "$(readlink "${EPREFIX}/usr/bin/python3")" != "python3."* ]]; then
379 die "'${EPREFIX}/usr/bin/python3' is not valid symlink"
380 fi
381
382 python3_version="$("${EPREFIX}/usr/bin/python3" -c 'from sys import version_info; print(".".join(str(x) for x in version_info[:2]))')"
383
167 for PYTHON_ABI in ${PYTHON_ABIS}; do 384 for PYTHON_ABI in "${_CPYTHON3_SUPPORTED_ABIS[@]}"; do
168 for dir in "${dirs[@]}"; do 385 support_python_major_version="1"
169 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed" 386 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
387 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
388 support_python_major_version="0"
389 fi
390 done
391 [[ "${support_python_major_version}" == "1" ]] && break
170 done 392 done
171 done 393 if [[ "${support_python_major_version}" == "1" ]]; then
172} 394 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
395 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
396 die "Active version of Python 3 is not supported by ${CATEGORY}/${PF}"
397 fi
398 done
399 else
400 python3_version=""
401 fi
402 fi
173 403
174# @FUNCTION: python_set_build_dir_symlink 404 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
175# @USAGE: [directory="build"] 405 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python2' symlink"
176# @DESCRIPTION: 406 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
177# Create build directory symlink. 407 die "Incorrect configuration of Python"
178python_set_build_dir_symlink() { 408 fi
179 local dir="$1" 409 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
410 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python3' symlink"
411 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
412 die "Incorrect configuration of Python"
413 fi
180 414
181 [[ -z "${PYTHON_ABIS}" ]] && die "PYTHON_ABIS variable not set" 415 PYTHON_ABIS="${python2_version} ${python3_version}"
182 [[ -z "${dir}" ]] && dir="build" 416 PYTHON_ABIS="${PYTHON_ABIS# }"
417 export PYTHON_ABIS="${PYTHON_ABIS% }"
418 fi
419 fi
183 420
184 # Don't delete preexistent directories. 421 _python_final_sanity_checks
185 rm -f "${dir}" || die "Deletion of '${dir}' failed"
186 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
187} 422}
188 423
189# @FUNCTION: python_execute_function 424# @FUNCTION: python_execute_function
190# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] <function> [arguments] 425# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [-f|--final-ABI] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
191# @DESCRIPTION: 426# @DESCRIPTION:
192# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 427# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
193# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 428# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
194python_execute_function() { 429python_execute_function() {
195 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" PYTHON_ABI quiet="0" separate_build_dirs="0" 430 _python_set_color_variables
431
432 local action action_message action_message_template= default_function="0" failure_message failure_message_template= final_ABI="0" function i iterated_PYTHON_ABIS nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
196 433
197 while (($#)); do 434 while (($#)); do
198 case "$1" in 435 case "$1" in
199 --action-message) 436 --action-message)
200 action_message_template="$2" 437 action_message_template="$2"
205 ;; 442 ;;
206 --failure-message) 443 --failure-message)
207 failure_message_template="$2" 444 failure_message_template="$2"
208 shift 445 shift
209 ;; 446 ;;
447 -f|--final-ABI)
448 final_ABI="1"
449 ;;
210 --nonfatal) 450 --nonfatal)
211 nonfatal="1" 451 nonfatal="1"
212 ;; 452 ;;
213 -q|--quiet) 453 -q|--quiet)
214 quiet="1" 454 quiet="1"
215 ;; 455 ;;
216 -s|--separate-build-dirs) 456 -s|--separate-build-dirs)
217 separate_build_dirs="1" 457 separate_build_dirs="1"
218 ;; 458 ;;
459 --source-dir)
460 source_dir="$2"
461 shift
462 ;;
463 --)
464 shift
465 break
466 ;;
219 -*) 467 -*)
220 die "${FUNCNAME}(): Unrecognized option $1" 468 die "${FUNCNAME}(): Unrecognized option '$1'"
221 ;; 469 ;;
222 *) 470 *)
223 break 471 break
224 ;; 472 ;;
225 esac 473 esac
226 shift 474 shift
227 done 475 done
228 476
477 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
478 die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
479 fi
480
229 if [[ "${default_function}" == "0" ]]; then 481 if [[ "${default_function}" == "0" ]]; then
230 if [[ "$#" -eq "0" ]]; then 482 if [[ "$#" -eq 0 ]]; then
231 die "${FUNCNAME}(): Missing function name" 483 die "${FUNCNAME}(): Missing function name"
232 fi 484 fi
233 function="$1" 485 function="$1"
234 shift 486 shift
487
488 if [[ -z "$(type -t "${function}")" ]]; then
489 die "${FUNCNAME}(): '${function}' function is not defined"
490 fi
235 else 491 else
236 if [[ "$#" -ne "0" ]]; then
237 die "${FUNCNAME}(): --default-function option and function name cannot be specified simultaneously"
238 fi
239 if has "${EAPI:-0}" 0 1; then 492 if has "${EAPI:-0}" 0 1; then
240 die "${FUNCNAME}(): --default-function option cannot be used in this EAPI" 493 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
241 fi 494 fi
242 495
243 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 496 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
497 if has "${EAPI}" 2 3; then
244 python_default_function() { 498 python_default_function() {
245 econf 499 econf "$@"
246 } 500 }
501 else
502 python_default_function() {
503 nonfatal econf "$@"
504 }
505 fi
247 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then 506 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
248 python_default_function() { 507 python_default_function() {
249 emake 508 emake "$@"
250 } 509 }
251 elif [[ "${EBUILD_PHASE}" == "test" ]]; then 510 elif [[ "${EBUILD_PHASE}" == "test" ]]; then
252 python_default_function() { 511 python_default_function() {
253 if emake -j1 -n check &> /dev/null; then 512 if emake -j1 -n check &> /dev/null; then
254 emake -j1 check 513 emake -j1 check "$@"
255 elif emake -j1 -n test &> /dev/null; then 514 elif emake -j1 -n test &> /dev/null; then
256 emake -j1 test 515 emake -j1 test "$@"
257 fi 516 fi
258 } 517 }
259 elif [[ "${EBUILD_PHASE}" == "install" ]]; then 518 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
260 python_default_function() { 519 python_default_function() {
261 emake DESTDIR="${D}" install 520 emake DESTDIR="${D}" install "$@"
262 } 521 }
263 else 522 else
264 die "${FUNCNAME}(): --default-function option cannot be used in this ebuild phase" 523 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
265 fi 524 fi
266 function="python_default_function" 525 function="python_default_function"
267 fi 526 fi
527
528 for ((i = 1; i < "${#FUNCNAME[@]}"; i++)); do
529 if [[ "${FUNCNAME[${i}]}" == "${FUNCNAME}" ]]; then
530 die "${FUNCNAME}(): Invalid call stack"
531 fi
532 done
268 533
269 if [[ "${quiet}" == "0" ]]; then 534 if [[ "${quiet}" == "0" ]]; then
270 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up" 535 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
271 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking" 536 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
272 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation" 537 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
278 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation" 543 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
279 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation" 544 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
280 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation" 545 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
281 fi 546 fi
282 547
283 local RED GREEN BLUE NORMAL 548 validate_PYTHON_ABIS
284 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then 549 if [[ "${final_ABI}" == "1" ]]; then
285 RED=$'\e[1;31m' 550 iterated_PYTHON_ABIS="$(PYTHON -f --ABI)"
286 GREEN=$'\e[1;32m'
287 BLUE=$'\e[1;34m'
288 NORMAL=$'\e[0m'
289 else 551 else
290 RED= 552 iterated_PYTHON_ABIS="${PYTHON_ABIS}"
291 GREEN=
292 BLUE=
293 NORMAL=
294 fi 553 fi
295
296 validate_PYTHON_ABIS
297 for PYTHON_ABI in ${PYTHON_ABIS}; do 554 for PYTHON_ABI in ${iterated_PYTHON_ABIS}; do
298 if [[ "${quiet}" == "0" ]]; then 555 if [[ "${quiet}" == "0" ]]; then
299 if [[ -n "${action_message_template}" ]]; then 556 if [[ -n "${action_message_template}" ]]; then
300 action_message="$(eval echo -n "${action_message_template}")" 557 action_message="$(eval echo -n "${action_message_template}")"
301 else 558 else
302 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..." 559 action_message="${action} of ${CATEGORY}/${PF} with $(python_get_implementation) $(python_get_version)..."
303 fi 560 fi
304 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 561 echo " ${_GREEN}*${_NORMAL} ${_BLUE}${action_message}${_NORMAL}"
305 fi 562 fi
563
306 if [[ "${separate_build_dirs}" == "1" ]]; then 564 if [[ "${separate_build_dirs}" == "1" ]]; then
565 if [[ -n "${source_dir}" ]]; then
566 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
567 else
307 export BUILDDIR="${S}-${PYTHON_ABI}" 568 export BUILDDIR="${S}-${PYTHON_ABI}"
569 fi
308 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 570 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
309 else 571 else
310 export BUILDDIR="${S}" 572 export BUILDDIR="${S}"
311 fi 573 fi
574
575 previous_directory="$(pwd)"
576 previous_directory_stack="$(dirs -p)"
577 previous_directory_stack_length="$(dirs -p | wc -l)"
578
579 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
580 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
581 else
312 if ! EPYTHON="$(PYTHON)" "${function}" "$@"; then 582 EPYTHON="$(PYTHON)" "${function}" "$@"
583 fi
584
585 if [[ "$?" != "0" ]]; then
313 if [[ -n "${failure_message_template}" ]]; then 586 if [[ -n "${failure_message_template}" ]]; then
314 failure_message="$(eval echo -n "${failure_message_template}")" 587 failure_message="$(eval echo -n "${failure_message_template}")"
315 else 588 else
316 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function" 589 failure_message="${action} failed with $(python_get_implementation) $(python_get_version) in ${function}() function"
317 fi 590 fi
318 if [[ "${nonfatal}" == "1" ]] || has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 591
319 local ABI enabled_PYTHON_ABIS 592 if [[ "${nonfatal}" == "1" ]]; then
320 for ABI in ${PYTHON_ABIS}; do
321 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
322 done
323 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
324 if [[ "${quiet}" == "0" ]]; then 593 if [[ "${quiet}" == "0" ]]; then
325 ewarn "${RED}${failure_message}${NORMAL}" 594 ewarn "${_RED}${failure_message}${_NORMAL}"
595 fi
596 elif [[ "${final_ABI}" == "0" ]] && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
597 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
598 local enabled_PYTHON_ABIS= other_PYTHON_ABI
599 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
600 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
601 done
602 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
603 fi
604 if [[ "${quiet}" == "0" ]]; then
605 ewarn "${_RED}${failure_message}${_NORMAL}"
606 fi
607 if [[ -z "${PYTHON_ABIS}" ]]; then
608 die "${function}() function failed with all enabled versions of Python"
326 fi 609 fi
327 else 610 else
328 die "${failure_message}" 611 die "${failure_message}"
329 fi 612 fi
330 fi 613 fi
614
615 # Ensure that directory stack has not been decreased.
616 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
617 die "Directory stack decreased illegally"
618 fi
619
620 # Avoid side effects of earlier returning from the specified function.
621 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
622 popd > /dev/null || die "popd failed"
623 done
624
625 # Ensure that the bottom part of directory stack has not been changed. Restore
626 # previous directory (from before running of the specified function) before
627 # comparison of directory stacks to avoid mismatch of directory stacks after
628 # potential using of 'cd' to change current directory. Restoration of previous
629 # directory allows to safely use 'cd' to change current directory in the
630 # specified function without changing it back to original directory.
631 cd "${previous_directory}"
632 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
633 die "Directory stack changed illegally"
634 fi
635
331 if [[ "${separate_build_dirs}" == "1" ]]; then 636 if [[ "${separate_build_dirs}" == "1" ]]; then
332 popd > /dev/null || die "popd failed" 637 popd > /dev/null || die "popd failed"
333 fi 638 fi
334 unset BUILDDIR 639 unset BUILDDIR
335 done 640 done
337 if [[ "${default_function}" == "1" ]]; then 642 if [[ "${default_function}" == "1" ]]; then
338 unset -f python_default_function 643 unset -f python_default_function
339 fi 644 fi
340} 645}
341 646
647# @FUNCTION: python_copy_sources
648# @USAGE: <directory="${S}"> [directory]
649# @DESCRIPTION:
650# Copy unpacked sources of current package to separate build directory for each Python ABI.
651python_copy_sources() {
652 local dir dirs=() PYTHON_ABI
342 653
654 if [[ "$#" -eq 0 ]]; then
655 if [[ "${WORKDIR}" == "${S}" ]]; then
656 die "${FUNCNAME}() cannot be used"
657 fi
658 dirs=("${S%/}")
659 else
660 dirs=("$@")
661 fi
662
663 validate_PYTHON_ABIS
664 for PYTHON_ABI in ${PYTHON_ABIS}; do
665 for dir in "${dirs[@]}"; do
666 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
667 done
668 done
669}
670
671# @FUNCTION: python_set_build_dir_symlink
672# @USAGE: <directory="build">
673# @DESCRIPTION:
674# Create build directory symlink.
675python_set_build_dir_symlink() {
676 local dir="$1"
677
678 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
679 [[ -z "${dir}" ]] && dir="build"
680
681 # Do not delete preexistent directories.
682 rm -f "${dir}" || die "Deletion of '${dir}' failed"
683 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
684}
685
686# @FUNCTION: python_generate_wrapper_scripts
687# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
688# @DESCRIPTION:
689# Generate wrapper scripts. Existing files are overwritten only with --force option.
690# If --respect-EPYTHON option is specified, then generated wrapper scripts will
691# respect EPYTHON variable at run time.
692python_generate_wrapper_scripts() {
693 _python_initialize_prefix_variables
694
695 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python3_enabled="0" respect_EPYTHON="0"
696
697 while (($#)); do
698 case "$1" in
699 -E|--respect-EPYTHON)
700 respect_EPYTHON="1"
701 ;;
702 -f|--force)
703 force="1"
704 ;;
705 -q|--quiet)
706 quiet="1"
707 ;;
708 --)
709 shift
710 break
711 ;;
712 -*)
713 die "${FUNCNAME}(): Unrecognized option '$1'"
714 ;;
715 *)
716 break
717 ;;
718 esac
719 shift
720 done
721
722 if [[ "$#" -eq 0 ]]; then
723 die "${FUNCNAME}(): Missing arguments"
724 fi
725
726 validate_PYTHON_ABIS
727 for PYTHON_ABI in "${_CPYTHON2_SUPPORTED_ABIS[@]}"; do
728 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
729 python2_enabled="1"
730 fi
731 done
732 for PYTHON_ABI in "${_CPYTHON3_SUPPORTED_ABIS[@]}"; do
733 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
734 python3_enabled="1"
735 fi
736 done
737
738 if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
739 eselect_python_option=
740 elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
741 eselect_python_option="--python2"
742 elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
743 eselect_python_option="--python3"
744 else
745 die "${FUNCNAME}(): Unsupported environment"
746 fi
747
748 for file in "$@"; do
749 if [[ -f "${file}" && "${force}" == "0" ]]; then
750 die "${FUNCNAME}(): '$1' already exists"
751 fi
752
753 if [[ "${quiet}" == "0" ]]; then
754 einfo "Generating '${file#${ED%/}}' wrapper script"
755 fi
756
757 cat << EOF > "${file}"
758#!/usr/bin/env python
759# Gentoo '${file##*/}' wrapper script generated by python_generate_wrapper_scripts()
760
761import os
762import re
763import subprocess
764import sys
765
766EPYTHON_re = re.compile(r"^python(\d+\.\d+)$")
767python_shebang_re = re.compile(r"^#! *(${EPREFIX}/usr/bin/python|(${EPREFIX})?/usr/bin/env +(${EPREFIX}/usr/bin/)?python)")
768python_verification_output_re = re.compile("^GENTOO_PYTHON_TARGET_SCRIPT_PATH supported\n$")
769
770EOF
771 if [[ "$?" != "0" ]]; then
772 die "${FUNCNAME}(): Generation of '$1' failed"
773 fi
774 if [[ "${respect_EPYTHON}" == "1" ]]; then
775 cat << EOF >> "${file}"
776EPYTHON = os.environ.get("EPYTHON")
777if EPYTHON:
778 EPYTHON_matched = EPYTHON_re.match(EPYTHON)
779 if EPYTHON_matched:
780 PYTHON_ABI = EPYTHON_matched.group(1)
781 else:
782 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
783 sys.exit(1)
784else:
785 try:
786 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
787 if eselect_process.wait() != 0:
788 raise ValueError
789 except (OSError, ValueError):
790 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
791 sys.exit(1)
792
793 EPYTHON = eselect_process.stdout.read().rstrip("\n")
794 if not isinstance(EPYTHON, str):
795 # Python 3
796 EPYTHON = EPYTHON.decode()
797
798 EPYTHON_matched = EPYTHON_re.match(EPYTHON)
799 if EPYTHON_matched:
800 PYTHON_ABI = EPYTHON_matched.group(1)
801 else:
802 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % EPYTHON)
803 sys.exit(1)
804EOF
805 if [[ "$?" != "0" ]]; then
806 die "${FUNCNAME}(): Generation of '$1' failed"
807 fi
808 else
809 cat << EOF >> "${file}"
810try:
811 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
812 if eselect_process.wait() != 0:
813 raise ValueError
814except (OSError, ValueError):
815 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
816 sys.exit(1)
817
818EPYTHON = eselect_process.stdout.read().rstrip("\n")
819if not isinstance(EPYTHON, str):
820 # Python 3
821 EPYTHON = EPYTHON.decode()
822
823EPYTHON_matched = EPYTHON_re.match(EPYTHON)
824if EPYTHON_matched:
825 PYTHON_ABI = EPYTHON_matched.group(1)
826else:
827 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % EPYTHON)
828 sys.exit(1)
829EOF
830 if [[ "$?" != "0" ]]; then
831 die "${FUNCNAME}(): Generation of '$1' failed"
832 fi
833 fi
834 cat << EOF >> "${file}"
835
836wrapper_script_path = os.path.realpath(sys.argv[0])
837target_executable_path = "%s-%s" % (wrapper_script_path, PYTHON_ABI)
838os.environ["GENTOO_PYTHON_WRAPPER_SCRIPT_PATH"] = sys.argv[0]
839os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH"] = target_executable_path
840if not os.path.exists(target_executable_path):
841 sys.stderr.write("'%s' does not exist\n" % target_executable_path)
842 sys.exit(1)
843
844target_executable = open(target_executable_path, "rb")
845target_executable_first_line = target_executable.readline()
846if not isinstance(target_executable_first_line, str):
847 # Python 3
848 target_executable_first_line = target_executable_first_line.decode("utf_8", "replace")
849
850python_shebang_matched = python_shebang_re.match(target_executable_first_line)
851target_executable.close()
852
853if python_shebang_matched:
854 try:
855 python_interpreter_path = "${EPREFIX}/usr/bin/%s" % EPYTHON
856 os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"] = "1"
857 python_verification_process = subprocess.Popen([python_interpreter_path, "-c", "pass"], stdout=subprocess.PIPE)
858 del os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"]
859 if python_verification_process.wait() != 0:
860 raise ValueError
861
862 python_verification_output = python_verification_process.stdout.read()
863 if not isinstance(python_verification_output, str):
864 # Python 3
865 python_verification_output = python_verification_output.decode()
866
867 if not python_verification_output_re.match(python_verification_output):
868 raise ValueError
869
870 os.execv(python_interpreter_path, [python_interpreter_path] + sys.argv)
871 except:
872 pass
873 if "GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION" in os.environ:
874 del os.environ["GENTOO_PYTHON_TARGET_SCRIPT_PATH_VERIFICATION"]
875
876os.execv(target_executable_path, sys.argv)
877EOF
878 if [[ "$?" != "0" ]]; then
879 die "${FUNCNAME}(): Generation of '$1' failed"
880 fi
881 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed"
882 done
883}
884
885# ================================================================================================
886# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======
887# ================================================================================================
888
889# @FUNCTION: python_set_active_version
890# @USAGE: <CPython_ABI|2|3>
891# @DESCRIPTION:
892# Set specified version of CPython as active version of Python.
893python_set_active_version() {
894 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
895 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
896 fi
897
898 if [[ "$#" -ne 1 ]]; then
899 die "${FUNCNAME}() requires 1 argument"
900 fi
901
902 _python_initial_sanity_checks
903
904 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
905 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
906 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
907 fi
908 export EPYTHON="$(PYTHON "$1")"
909 elif [[ "$1" == "2" ]]; then
910 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
911 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
912 fi
913 export EPYTHON="$(PYTHON -2)"
914 elif [[ "$1" == "3" ]]; then
915 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
916 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
917 fi
918 export EPYTHON="$(PYTHON -3)"
919 else
920 die "${FUNCNAME}(): Unrecognized argument '$1'"
921 fi
922
923 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
924 # so it does not need to be exported to subprocesses.
925 PYTHON_ABI="${EPYTHON#python}"
926 PYTHON_ABI="${PYTHON_ABI%%-*}"
927
928 _python_final_sanity_checks
929
930 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
931 PYTHON_REQUESTED_ACTIVE_VERSION="$1"
932}
933
934# @FUNCTION: python_need_rebuild
935# @DESCRIPTION: Mark current package for rebuilding by python-updater after
936# switching of active version of Python.
937python_need_rebuild() {
938 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
939 die "${FUNCNAME}() cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
940 fi
941
942 export PYTHON_NEED_REBUILD="$(PYTHON --ABI)"
943}
944
945# ================================================================================================
946# ======================================= GETTER FUNCTIONS =======================================
947# ================================================================================================
948
949_PYTHON_ABI_EXTRACTION_COMMAND='import platform
950import sys
951sys.stdout.write(".".join(str(x) for x in sys.version_info[:2]))
952if platform.system()[:4] == "Java":
953 sys.stdout.write("-jython")'
954
955_python_get_implementation() {
956 if [[ "$#" -ne 1 ]]; then
957 die "${FUNCNAME}() requires 1 argument"
958 fi
959
960 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
961 echo "CPython"
962 elif [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+-jython$ ]]; then
963 echo "Jython"
964 else
965 die "${FUNCNAME}(): Unrecognized Python ABI '$1'"
966 fi
967}
968
969# @FUNCTION: PYTHON
970# @USAGE: [-2] [-3] [--ABI] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
971# @DESCRIPTION:
972# Print filename of Python interpreter for specified Python ABI. If Python_ABI argument
973# is ommitted, then PYTHON_ABI environment variable must be set and is used.
974# If -2 option is specified, then active version of Python 2 is used.
975# If -3 option is specified, then active version of Python 3 is used.
976# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
977# -2, -3 and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
978# If --ABI option is specified, then only specified Python ABI is printed instead of
979# filename of Python interpreter.
980# If --absolute-path option is specified, then absolute path to Python interpreter is printed.
981# --ABI and --absolute-path options cannot be specified simultaneously.
982PYTHON() {
983 local ABI_output="0" absolute_path_output="0" final_ABI="0" PYTHON_ABI="${PYTHON_ABI}" python_interpreter python2="0" python3="0"
984
985 while (($#)); do
986 case "$1" in
987 -2)
988 python2="1"
989 ;;
990 -3)
991 python3="1"
992 ;;
993 --ABI)
994 ABI_output="1"
995 ;;
996 -a|--absolute-path)
997 absolute_path_output="1"
998 ;;
999 -f|--final-ABI)
1000 final_ABI="1"
1001 ;;
1002 --)
1003 shift
1004 break
1005 ;;
1006 -*)
1007 die "${FUNCNAME}(): Unrecognized option '$1'"
1008 ;;
1009 *)
1010 break
1011 ;;
1012 esac
1013 shift
1014 done
1015
1016 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
1017 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
1018 fi
1019
1020 if [[ "$((${python2} + ${python3} + ${final_ABI}))" -gt 1 ]]; then
1021 die "${FUNCNAME}(): '-2', '-3' or '--final-ABI' options cannot be specified simultaneously"
1022 fi
1023
1024 if [[ "$#" -eq 0 ]]; then
1025 if [[ "${final_ABI}" == "1" ]]; then
1026 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
1027 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python"
1028 fi
1029 validate_PYTHON_ABIS
1030 PYTHON_ABI="${PYTHON_ABIS##* }"
1031 elif [[ "${python2}" == "1" ]]; then
1032 PYTHON_ABI="$(eselect python show --python2 --ABI)"
1033 if [[ -z "${PYTHON_ABI}" ]]; then
1034 die "${FUNCNAME}(): Active Python 2 interpreter not set"
1035 elif [[ "${PYTHON_ABI}" != "2."* ]]; then
1036 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1037 fi
1038 elif [[ "${python3}" == "1" ]]; then
1039 PYTHON_ABI="$(eselect python show --python3 --ABI)"
1040 if [[ -z "${PYTHON_ABI}" ]]; then
1041 die "${FUNCNAME}(): Active Python 3 interpreter not set"
1042 elif [[ "${PYTHON_ABI}" != "3."* ]]; then
1043 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1044 fi
1045 elif [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
1046 PYTHON_ABI="$("${EPREFIX}/usr/bin/python" -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")"
1047 elif [[ -z "${PYTHON_ABI}" ]]; then
1048 die "${FUNCNAME}(): Invalid usage: ${FUNCNAME}() should be used in ABI-specific local scope"
1049 fi
1050 elif [[ "$#" -eq 1 ]]; then
1051 if [[ "${final_ABI}" == "1" ]]; then
1052 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
1053 fi
1054 if [[ "${python2}" == "1" ]]; then
1055 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
1056 fi
1057 if [[ "${python3}" == "1" ]]; then
1058 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
1059 fi
1060 PYTHON_ABI="$1"
1061 else
1062 die "${FUNCNAME}(): Invalid usage"
1063 fi
1064
1065 if [[ "${ABI_output}" == "1" ]]; then
1066 echo -n "${PYTHON_ABI}"
1067 return
1068 else
1069 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1070 python_interpreter="python${PYTHON_ABI}"
1071 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1072 python_interpreter="jython-${PYTHON_ABI%-jython}"
1073 fi
1074
1075 if [[ "${absolute_path_output}" == "1" ]]; then
1076 echo -n "${EPREFIX}/usr/bin/${python_interpreter}"
1077 else
1078 echo -n "${python_interpreter}"
1079 fi
1080 fi
1081
1082 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
1083 echo -n "-${ABI}"
1084 fi
1085}
1086
1087# @FUNCTION: python_get_implementation
1088# @USAGE: [-f|--final-ABI]
1089# @DESCRIPTION:
1090# Print name of Python implementation.
1091# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1092python_get_implementation() {
1093 local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
1094
1095 while (($#)); do
1096 case "$1" in
1097 -f|--final-ABI)
1098 final_ABI="1"
1099 ;;
1100 -*)
1101 die "${FUNCNAME}(): Unrecognized option '$1'"
1102 ;;
1103 *)
1104 die "${FUNCNAME}(): Invalid usage"
1105 ;;
1106 esac
1107 shift
1108 done
1109
1110 if [[ "${final_ABI}" == "1" ]]; then
1111 PYTHON_ABI="$(PYTHON -f --ABI)"
1112 elif [[ -z "${PYTHON_ABI}" ]]; then
1113 PYTHON_ABI="$(PYTHON --ABI)"
1114 fi
1115
1116 echo "$(_python_get_implementation "${PYTHON_ABI}")"
1117}
1118
1119# @FUNCTION: python_get_implementational_package
1120# @USAGE: [-f|--final-ABI]
1121# @DESCRIPTION:
1122# Print category, name and slot of package providing Python implementation.
1123# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1124python_get_implementational_package() {
1125 local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
1126
1127 while (($#)); do
1128 case "$1" in
1129 -f|--final-ABI)
1130 final_ABI="1"
1131 ;;
1132 -*)
1133 die "${FUNCNAME}(): Unrecognized option '$1'"
1134 ;;
1135 *)
1136 die "${FUNCNAME}(): Invalid usage"
1137 ;;
1138 esac
1139 shift
1140 done
1141
1142 if [[ "${final_ABI}" == "1" ]]; then
1143 PYTHON_ABI="$(PYTHON -f --ABI)"
1144 elif [[ -z "${PYTHON_ABI}" ]]; then
1145 PYTHON_ABI="$(PYTHON --ABI)"
1146 fi
1147
1148 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1149 echo "dev-lang/python:${PYTHON_ABI}"
1150 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1151 echo "dev-java/jython:${PYTHON_ABI%-jython}"
1152 fi
1153}
1154
1155# @FUNCTION: python_get_includedir
1156# @USAGE: [-f|--final-ABI]
1157# @DESCRIPTION:
1158# Print path to Python include directory.
1159# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1160python_get_includedir() {
1161 local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
1162
1163 while (($#)); do
1164 case "$1" in
1165 -f|--final-ABI)
1166 final_ABI="1"
1167 ;;
1168 -*)
1169 die "${FUNCNAME}(): Unrecognized option '$1'"
1170 ;;
1171 *)
1172 die "${FUNCNAME}(): Invalid usage"
1173 ;;
1174 esac
1175 shift
1176 done
1177
1178 if [[ "${final_ABI}" == "1" ]]; then
1179 PYTHON_ABI="$(PYTHON -f --ABI)"
1180 elif [[ -z "${PYTHON_ABI}" ]]; then
1181 PYTHON_ABI="$(PYTHON --ABI)"
1182 fi
1183
1184 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1185 echo "/usr/include/python${PYTHON_ABI}"
1186 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1187 echo "/usr/share/jython-${PYTHON_ABI%-jython}/Include"
1188 fi
1189}
1190
1191# @FUNCTION: python_get_libdir
1192# @USAGE: [-f|--final-ABI]
1193# @DESCRIPTION:
1194# Print path to Python library directory.
1195# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1196python_get_libdir() {
1197 local final_ABI="0" PYTHON_ABI="${PYTHON_ABI}"
1198
1199 while (($#)); do
1200 case "$1" in
1201 -f|--final-ABI)
1202 final_ABI="1"
1203 ;;
1204 -*)
1205 die "${FUNCNAME}(): Unrecognized option '$1'"
1206 ;;
1207 *)
1208 die "${FUNCNAME}(): Invalid usage"
1209 ;;
1210 esac
1211 shift
1212 done
1213
1214 if [[ "${final_ABI}" == "1" ]]; then
1215 PYTHON_ABI="$(PYTHON -f --ABI)"
1216 elif [[ -z "${PYTHON_ABI}" ]]; then
1217 PYTHON_ABI="$(PYTHON --ABI)"
1218 fi
1219
1220 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1221 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
1222 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1223 echo "/usr/share/jython-${PYTHON_ABI%-jython}/Lib"
1224 fi
1225}
1226
1227# @FUNCTION: python_get_sitedir
1228# @USAGE: [-f|--final-ABI]
1229# @DESCRIPTION:
1230# Print path to Python site-packages directory.
1231# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1232python_get_sitedir() {
1233 local options=()
1234
1235 while (($#)); do
1236 case "$1" in
1237 -f|--final-ABI)
1238 options+=("$1")
1239 ;;
1240 -*)
1241 die "${FUNCNAME}(): Unrecognized option '$1'"
1242 ;;
1243 *)
1244 die "${FUNCNAME}(): Invalid usage"
1245 ;;
1246 esac
1247 shift
1248 done
1249
1250 echo "$(python_get_libdir "${options[@]}")/site-packages"
1251}
1252
1253# @FUNCTION: python_get_library
1254# @USAGE: [-f|--final-ABI] [-l|--linker-option]
1255# @DESCRIPTION:
1256# Print path to Python library.
1257# If --linker-option is specified, then "-l${library}" linker option is printed.
1258# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1259python_get_library() {
1260 local final_ABI="0" linker_option="0" PYTHON_ABI="${PYTHON_ABI}"
1261
1262 while (($#)); do
1263 case "$1" in
1264 -f|--final-ABI)
1265 final_ABI="1"
1266 ;;
1267 -l|--linker-option)
1268 linker_option="1"
1269 ;;
1270 -*)
1271 die "${FUNCNAME}(): Unrecognized option '$1'"
1272 ;;
1273 *)
1274 die "${FUNCNAME}(): Invalid usage"
1275 ;;
1276 esac
1277 shift
1278 done
1279
1280 if [[ "${final_ABI}" == "1" ]]; then
1281 PYTHON_ABI="$(PYTHON -f --ABI)"
1282 elif [[ -z "${PYTHON_ABI}" ]]; then
1283 PYTHON_ABI="$(PYTHON --ABI)"
1284 fi
1285
1286 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1287 if [[ "${linker_option}" == "1" ]]; then
1288 echo "-lpython${PYTHON_ABI}"
1289 else
1290 echo "/usr/$(get_libdir)/libpython${PYTHON_ABI}$(get_libname)"
1291 fi
1292 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1293 die "${FUNCNAME}(): Jython does not have shared library"
1294 fi
1295}
1296
1297# @FUNCTION: python_get_version
1298# @USAGE: [-f|--final-ABI] [--full] [--major] [--minor] [--micro]
1299# @DESCRIPTION:
1300# Print Python version.
1301# --full, --major, --minor and --micro options cannot be specified simultaneously.
1302# If --full, --major, --minor and --micro options are not specified, then "${major_version}.${minor_version}" is printed.
1303# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1304python_get_version() {
1305 local final_ABI="0" full="0" major="0" minor="0" micro="0" python_command
1306
1307 while (($#)); do
1308 case "$1" in
1309 -f|--final-ABI)
1310 final_ABI="1"
1311 ;;
1312 --full)
1313 full="1"
1314 ;;
1315 --major)
1316 major="1"
1317 ;;
1318 --minor)
1319 minor="1"
1320 ;;
1321 --micro)
1322 micro="1"
1323 ;;
1324 -*)
1325 die "${FUNCNAME}(): Unrecognized option '$1'"
1326 ;;
1327 *)
1328 die "${FUNCNAME}(): Invalid usage"
1329 ;;
1330 esac
1331 shift
1332 done
1333
1334 if [[ "$((${full} + ${major} + ${minor} + ${micro}))" -gt 1 ]]; then
1335 die "${FUNCNAME}(): '--full', '--major', '--minor' or '--micro' options cannot be specified simultaneously"
1336 fi
1337
1338 if [[ "${full}" == "1" ]]; then
1339 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:3]))"
1340 elif [[ "${major}" == "1" ]]; then
1341 python_command="from sys import version_info; print(version_info[0])"
1342 elif [[ "${minor}" == "1" ]]; then
1343 python_command="from sys import version_info; print(version_info[1])"
1344 elif [[ "${micro}" == "1" ]]; then
1345 python_command="from sys import version_info; print(version_info[2])"
1346 else
1347 if [[ -n "${PYTHON_ABI}" && "${final_ABI}" == "0" ]]; then
1348 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "CPython" ]]; then
1349 echo "${PYTHON_ABI}"
1350 elif [[ "$(_python_get_implementation "${PYTHON_ABI}")" == "Jython" ]]; then
1351 echo "${PYTHON_ABI%-jython}"
1352 fi
1353 return
1354 fi
1355 python_command="from sys import version_info; print('.'.join(str(x) for x in version_info[:2]))"
1356 fi
1357
1358 if [[ "${final_ABI}" == "1" ]]; then
1359 "$(PYTHON -f)" -c "${python_command}"
1360 else
1361 "$(PYTHON ${PYTHON_ABI})" -c "${python_command}"
1362 fi
1363}
1364
1365# ================================================================================================
1366# =================================== MISCELLANEOUS FUNCTIONS ====================================
1367# ================================================================================================
1368
1369_python_implementation() {
1370 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1371 return 0
1372 elif [[ "${CATEGORY}/${PN}" == "dev-java/jython" ]]; then
1373 return 0
1374 else
1375 return 1
1376 fi
1377}
1378
1379_python_initialize_prefix_variables() {
1380 if has "${EAPI:-0}" 0 1 2; then
1381 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
1382 EROOT="${ROOT%/}${EPREFIX}/"
1383 fi
1384 if [[ -n "${D}" && -z "${ED}" ]]; then
1385 ED="${D%/}${EPREFIX}/"
1386 fi
1387 fi
1388}
1389
1390unset PYTHON_SANITY_CHECKS
1391
1392_python_initial_sanity_checks() {
1393 if [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1394 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
1395 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
1396 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
1397 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1398 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
1399 fi
1400 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
1401 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
1402 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
1403 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
1404 fi
1405 fi
1406}
1407
1408_python_final_sanity_checks() {
1409 if ! _python_implementation && [[ "$(declare -p PYTHON_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_SANITY_CHECKS="* ]]; then
1410 local PYTHON_ABI="${PYTHON_ABI}"
1411 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI}}; do
1412 # Ensure that appropriate version of Python is installed.
1413 if ! has_version "$(python_get_implementational_package)"; then
1414 die "$(python_get_implementational_package) is not installed"
1415 fi
1416
1417 # Ensure that EPYTHON variable is respected.
1418 if [[ "$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")" != "${PYTHON_ABI}" ]]; then
1419 eerror "python: '$(type -p python)'"
1420 eerror "ABI: '${ABI}'"
1421 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
1422 eerror "EPYTHON: '$(PYTHON)'"
1423 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
1424 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c "${_PYTHON_ABI_EXTRACTION_COMMAND}")'"
1425 die "'python' does not respect EPYTHON variable"
1426 fi
1427 done
1428 fi
1429 PYTHON_SANITY_CHECKS="1"
1430}
1431
1432_python_set_color_variables() {
1433 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1434 _BOLD=$'\e[1m'
1435 _RED=$'\e[1;31m'
1436 _GREEN=$'\e[1;32m'
1437 _BLUE=$'\e[1;34m'
1438 _CYAN=$'\e[1;36m'
1439 _NORMAL=$'\e[0m'
1440 else
1441 _BOLD=
1442 _RED=
1443 _GREEN=
1444 _BLUE=
1445 _CYAN=
1446 _NORMAL=
1447 fi
1448}
1449
1450# @FUNCTION: python_convert_shebangs
1451# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1452# @DESCRIPTION:
1453# Convert shebangs in specified files. Directories can be specified only with --recursive option.
1454python_convert_shebangs() {
1455 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
1456
1457 while (($#)); do
1458 case "$1" in
1459 -r|--recursive)
1460 recursive="1"
1461 ;;
1462 -q|--quiet)
1463 quiet="1"
1464 ;;
1465 -x|--only-executables)
1466 only_executables="1"
1467 ;;
1468 --)
1469 shift
1470 break
1471 ;;
1472 -*)
1473 die "${FUNCNAME}(): Unrecognized option '$1'"
1474 ;;
1475 *)
1476 break
1477 ;;
1478 esac
1479 shift
1480 done
1481
1482 if [[ "$#" -eq 0 ]]; then
1483 die "${FUNCNAME}(): Missing Python version and files or directories"
1484 elif [[ "$#" -eq 1 ]]; then
1485 die "${FUNCNAME}(): Missing files or directories"
1486 fi
1487
1488 python_version="$1"
1489 shift
1490
1491 for argument in "$@"; do
1492 if [[ ! -e "${argument}" ]]; then
1493 die "${FUNCNAME}(): '${argument}' does not exist"
1494 elif [[ -f "${argument}" ]]; then
1495 files+=("${argument}")
1496 elif [[ -d "${argument}" ]]; then
1497 if [[ "${recursive}" == "1" ]]; then
1498 if [[ "${only_executables}" == "1" ]]; then
1499 files+=($(find "${argument}" -perm /111 -type f))
1500 else
1501 files+=($(find "${argument}" -type f))
1502 fi
1503 else
1504 die "${FUNCNAME}(): '${argument}' is not a regular file"
1505 fi
1506 else
1507 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
1508 fi
1509 done
1510
1511 for file in "${files[@]}"; do
1512 file="${file#./}"
1513 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1514
1515 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1516 [[ "$(sed -ne "2p" "${file}")" =~ ^"# Gentoo '".*"' wrapper script generated by python_generate_wrapper_scripts()"$ ]] && continue
1517
1518 if [[ "${quiet}" == "0" ]]; then
1519 einfo "Converting shebang in '${file}'"
1520 fi
1521
1522 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1523
1524 # Delete potential whitespace after "#!".
1525 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1526 fi
1527 done
1528}
1529
1530# @FUNCTION: python_clean_sitedirs
1531# @DESCRIPTION:
1532# Delete needless files in site-packages directories in ${ED}.
1533python_clean_sitedirs() {
1534 _python_initialize_prefix_variables
1535
1536 find "${ED}"usr/$(get_libdir)/python*/site-packages "(" -name "*.c" -o -name "*.h" -o -name "*.la" ")" -type f -print0 | xargs -0 rm -f
1537}
1538
1539# ================================================================================================
1540# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1541# ================================================================================================
1542
343# @ECLASS-VARIABLE: PYTHON_USE_WITH 1543# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
344# @DESCRIPTION: 1544# @DESCRIPTION:
345# Set this to a space separated list of use flags 1545# User-configurable verbosity of tests of Python modules.
346# the python slot in use must be built with. 1546# Supported values: 0, 1, 2, 3, 4.
1547PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
347 1548
348# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR 1549_python_test_hook() {
349# @DESCRIPTION: 1550 if [[ "$#" -ne 1 ]]; then
350# Set this to a space separated list of use flags 1551 die "${FUNCNAME}() requires 1 argument"
351# of which one must be turned on for the slot of 1552 fi
352# in use.
353 1553
354# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT 1554 if [[ -n "${SUPPORT_PYTHON_ABIS}" && "$(type -t "${FUNCNAME[3]}_$1_hook")" == "function" ]]; then
355# @DESCRIPTION: 1555 "${FUNCNAME[3]}_$1_hook"
356# Set this if you need to make either PYTHON_USE_WITH or 1556 fi
357# PYTHON_USE_WITH_OR atoms conditional under a use flag. 1557}
358 1558
359# @FUNCTION: python_pkg_setup 1559# @FUNCTION: python_execute_nosetests
1560# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
360# @DESCRIPTION: 1561# @DESCRIPTION:
361# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 1562# Execute nosetests for all enabled versions of Python.
362# are respected. Only exported if one of those variables is set. 1563# In ebuilds of packages supporting installation for multiple versions of Python, this function
363if ! has ${EAPI} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 1564# calls python_execute_nosetests_pre_hook() and python_execute_nosetests_post_hook(), if they are defined.
364 python_pkg_setup_fail() { 1565python_execute_nosetests() {
365 eerror "${1}" 1566 _python_set_color_variables
366 die "${1}" 1567
1568 local PYTHONPATH_template= separate_build_dirs=
1569
1570 while (($#)); do
1571 case "$1" in
1572 -P|--PYTHONPATH)
1573 PYTHONPATH_template="$2"
1574 shift
1575 ;;
1576 -s|--separate-build-dirs)
1577 separate_build_dirs="1"
1578 ;;
1579 --)
1580 shift
1581 break
1582 ;;
1583 -*)
1584 die "${FUNCNAME}(): Unrecognized option '$1'"
1585 ;;
1586 *)
1587 break
1588 ;;
1589 esac
1590 shift
1591 done
1592
1593 python_test_function() {
1594 local evaluated_PYTHONPATH=
1595
1596 if [[ -n "${PYTHONPATH_template}" ]]; then
1597 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1598 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1599 unset evaluated_PYTHONPATH
1600 fi
1601 fi
1602
1603 _python_test_hook pre
1604
1605 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1606 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
1607 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
1608 else
1609 echo ${_BOLD}nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"${_NORMAL}
1610 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@" || return "$?"
1611 fi
1612
1613 _python_test_hook post
367 } 1614 }
368 1615 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
369 python_pkg_setup() { 1616 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
370 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return 1617 else
371 1618 if [[ -n "${separate_build_dirs}" ]]; then
372 python_version 1619 die "${FUNCNAME}(): Invalid usage"
373 local failed
374 local pyatom="dev-lang/python:${PYVER}"
375
376 for use in ${PYTHON_USE_WITH}; do
377 if ! has_version "${pyatom}[${use}]"; then
378 python_pkg_setup_fail \
379 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}"
380 fi 1620 fi
1621 python_test_function "$@" || die "Testing failed"
1622 fi
1623
1624 unset -f python_test_function
1625}
1626
1627# @FUNCTION: python_execute_py.test
1628# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1629# @DESCRIPTION:
1630# Execute py.test for all enabled versions of Python.
1631# In ebuilds of packages supporting installation for multiple versions of Python, this function
1632# calls python_execute_py.test_pre_hook() and python_execute_py.test_post_hook(), if they are defined.
1633python_execute_py.test() {
1634 _python_set_color_variables
1635
1636 local PYTHONPATH_template= separate_build_dirs=
1637
1638 while (($#)); do
1639 case "$1" in
1640 -P|--PYTHONPATH)
1641 PYTHONPATH_template="$2"
1642 shift
1643 ;;
1644 -s|--separate-build-dirs)
1645 separate_build_dirs="1"
1646 ;;
1647 --)
1648 shift
1649 break
1650 ;;
1651 -*)
1652 die "${FUNCNAME}(): Unrecognized option '$1'"
1653 ;;
1654 *)
1655 break
1656 ;;
1657 esac
1658 shift
381 done 1659 done
382 1660
383 for use in ${PYTHON_USE_WITH_OR}; do 1661 python_test_function() {
384 if has_version "${pyatom}[${use}]"; then 1662 local evaluated_PYTHONPATH=
385 return
386 fi
387 done
388 1663
389 if [[ ${PYTHON_USE_WITH_OR} ]]; then 1664 if [[ -n "${PYTHONPATH_template}" ]]; then
390 python_pkg_setup_fail \ 1665 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
391 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}" 1666 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1667 unset evaluated_PYTHONPATH
392 fi 1668 fi
1669 fi
1670
1671 _python_test_hook pre
1672
1673 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1674 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"${_NORMAL}
1675 PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@" || return "$?"
1676 else
1677 echo ${_BOLD}py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"${_NORMAL}
1678 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@" || return "$?"
1679 fi
1680
1681 _python_test_hook post
393 } 1682 }
1683 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1684 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1685 else
1686 if [[ -n "${separate_build_dirs}" ]]; then
1687 die "${FUNCNAME}(): Invalid usage"
1688 fi
1689 python_test_function "$@" || die "Testing failed"
1690 fi
394 1691
395 EXPORT_FUNCTIONS pkg_setup 1692 unset -f python_test_function
1693}
396 1694
397 if [[ ${PYTHON_USE_WITH} ]]; then 1695# @FUNCTION: python_execute_trial
398 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 1696# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
399 elif [[ ${PYTHON_USE_WITH_OR} ]]; then 1697# @DESCRIPTION:
400 PYTHON_USE_WITH_ATOM="|| ( " 1698# Execute trial for all enabled versions of Python.
401 for use in ${PYTHON_USE_WITH_OR}; do 1699# In ebuilds of packages supporting installation for multiple versions of Python, this function
402 PYTHON_USE_WITH_ATOM=" 1700# calls python_execute_trial_pre_hook() and python_execute_trial_post_hook(), if they are defined.
403 ${PYTHON_USE_WITH_ATOM} 1701python_execute_trial() {
404 ${PYTHON_ATOM}[${use}]" 1702 _python_set_color_variables
1703
1704 local PYTHONPATH_template= separate_build_dirs=
1705
1706 while (($#)); do
1707 case "$1" in
1708 -P|--PYTHONPATH)
1709 PYTHONPATH_template="$2"
1710 shift
1711 ;;
1712 -s|--separate-build-dirs)
1713 separate_build_dirs="1"
1714 ;;
1715 --)
1716 shift
1717 break
1718 ;;
1719 -*)
1720 die "${FUNCNAME}(): Unrecognized option '$1'"
1721 ;;
1722 *)
1723 break
1724 ;;
1725 esac
1726 shift
405 done 1727 done
406 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )" 1728
1729 python_test_function() {
1730 local evaluated_PYTHONPATH=
1731
1732 if [[ -n "${PYTHONPATH_template}" ]]; then
1733 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1734 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1735 unset evaluated_PYTHONPATH
1736 fi
407 fi 1737 fi
408 if [[ ${PYTHON_USE_WITH_OPT} ]]; then 1738
409 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 1739 _python_test_hook pre
1740
1741 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1742 echo ${_BOLD}PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
1743 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
1744 else
1745 echo ${_BOLD}trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"${_NORMAL}
1746 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@" || return "$?"
410 fi 1747 fi
411 DEPEND="${PYTHON_USE_WITH_ATOM}" 1748
412 RDEPEND="${PYTHON_USE_WITH_ATOM}" 1749 _python_test_hook post
1750 }
1751 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1752 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1753 else
1754 if [[ -n "${separate_build_dirs}" ]]; then
1755 die "${FUNCNAME}(): Invalid usage"
1756 fi
1757 python_test_function "$@" || die "Testing failed"
413fi 1758 fi
414 1759
415# @FUNCTION: python_disable_pyc 1760 unset -f python_test_function
416# @DESCRIPTION:
417# Tells python not to automatically recompile modules to .pyc/.pyo
418# even if the timestamps/version stamps don't match. This is done
419# to protect sandbox.
420#
421# note: supported by >=dev-lang/python-2.2.3-r3 only.
422#
423python_disable_pyc() {
424 export PYTHONDONTWRITEBYTECODE=1 # For 2.6 and above
425 export PYTHON_DONTCOMPILE=1 # For 2.5 and below
426} 1761}
1762
1763# ================================================================================================
1764# ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
1765# ================================================================================================
427 1766
428# @FUNCTION: python_enable_pyc 1767# @FUNCTION: python_enable_pyc
429# @DESCRIPTION: 1768# @DESCRIPTION:
430# Tells python to automatically recompile modules to .pyc/.pyo if the 1769# Tell Python to automatically recompile modules to .pyc/.pyo if the
431# timestamps/version stamps change 1770# timestamps/version stamps have changed.
432python_enable_pyc() { 1771python_enable_pyc() {
433 unset PYTHONDONTWRITEBYTECODE 1772 unset PYTHONDONTWRITEBYTECODE
434 unset PYTHON_DONTCOMPILE
435} 1773}
436 1774
1775# @FUNCTION: python_disable_pyc
1776# @DESCRIPTION:
1777# Tell Python not to automatically recompile modules to .pyc/.pyo
1778# even if the timestamps/version stamps do not match. This is done
1779# to protect sandbox.
437python_disable_pyc 1780python_disable_pyc() {
438 1781 export PYTHONDONTWRITEBYTECODE="1"
439# @FUNCTION: python_need_rebuild
440# @DESCRIPTION: Run without arguments, specifies that the package should be
441# rebuilt after a python upgrade.
442python_need_rebuild() {
443 python_version
444 export PYTHON_NEED_REBUILD=${PYVER}
445}
446
447# @FUNCTION: python_get_includedir
448# @DESCRIPTION:
449# Run without arguments, returns the Python include directory.
450python_get_includedir() {
451 if [[ -n "${PYTHON_ABI}" ]]; then
452 echo "/usr/include/python${PYTHON_ABI}"
453 else
454 python_version
455 echo "/usr/include/python${PYVER}"
456 fi
457}
458
459# @FUNCTION: python_get_libdir
460# @DESCRIPTION:
461# Run without arguments, returns the Python library directory.
462python_get_libdir() {
463 if [[ -n "${PYTHON_ABI}" ]]; then
464 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
465 else
466 python_version
467 echo "/usr/$(get_libdir)/python${PYVER}"
468 fi
469}
470
471# @FUNCTION: python_get_sitedir
472# @DESCRIPTION:
473# Run without arguments, returns the Python site-packages directory.
474python_get_sitedir() {
475 echo "$(python_get_libdir)/site-packages"
476}
477
478# @FUNCTION: python_makesym
479# @DESCRIPTION:
480# Run without arguments, it will create the /usr/bin/python symlinks
481# to the latest installed version
482python_makesym() {
483 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
484 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
485}
486
487# @FUNCTION: python_tkinter_exists
488# @DESCRIPTION:
489# Run without arguments, checks if python was compiled with Tkinter
490# support. If not, prints an error message and dies.
491python_tkinter_exists() {
492 if ! python -c "import Tkinter" >/dev/null 2>&1; then
493 eerror "You need to recompile python with Tkinter support."
494 eerror "Try adding: 'dev-lang/python tk'"
495 eerror "in to /etc/portage/package.use"
496 echo
497 die "missing tkinter support with installed python"
498 fi
499}
500
501# @FUNCTION: python_mod_exists
502# @USAGE: <module>
503# @DESCRIPTION:
504# Run with the module name as an argument. it will check if a
505# python module is installed and loadable. it will return
506# TRUE(0) if the module exists, and FALSE(1) if the module does
507# not exist.
508#
509# Example:
510# if python_mod_exists gtk; then
511# echo "gtk support enabled"
512# fi
513python_mod_exists() {
514 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
515 python -c "import $1" &>/dev/null
516}
517
518# @FUNCTION: python_mod_compile
519# @USAGE: <file> [more files ...]
520# @DESCRIPTION:
521# Given filenames, it will pre-compile the module's .pyc and .pyo.
522# This function should only be run in pkg_postinst()
523#
524# Example:
525# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
526#
527python_mod_compile() {
528 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
529 die "${FUNCNAME}() cannot be used in this EAPI"
530 fi
531
532 local f myroot myfiles=()
533
534 # Check if phase is pkg_postinst()
535 [[ ${EBUILD_PHASE} != postinst ]] &&\
536 die "${FUNCNAME} should only be run in pkg_postinst()"
537
538 # allow compiling for older python versions
539 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
540 PYVER=${PYTHON_OVERRIDE_PYVER}
541 else
542 python_version
543 fi
544
545 # strip trailing slash
546 myroot="${ROOT%/}"
547
548 # respect ROOT
549 for f in "$@"; do
550 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
551 done
552
553 if ((${#myfiles[@]})); then
554 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
555 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
556 else
557 ewarn "No files to compile!"
558 fi
559} 1782}
560 1783
561# @FUNCTION: python_mod_optimize 1784# @FUNCTION: python_mod_optimize
562# @USAGE: [options] [directory|file] 1785# @USAGE: [options] [directory|file]
563# @DESCRIPTION: 1786# @DESCRIPTION:
564# If no arguments supplied, it will recompile not recursively all modules 1787# If no arguments supplied, it will recompile not recursively all modules
565# under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages). 1788# under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages).
566# 1789#
567# If supplied with arguments, it will recompile all modules recursively 1790# If supplied with arguments, it will recompile all modules recursively
568# in the supplied directory. 1791# in the supplied directory.
569# This function should only be run in pkg_postinst().
570# 1792#
571# Options passed to this function are passed to compileall.py. 1793# Options passed to this function are passed to compileall.py.
572# 1794#
573# Example: 1795# This function can be used only in pkg_postinst() phase.
574# python_mod_optimize ctypesgencore
575python_mod_optimize() { 1796python_mod_optimize() {
1797 _python_initialize_prefix_variables
1798
576 # Check if phase is pkg_postinst(). 1799 # Check if phase is pkg_postinst().
577 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()" 1800 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase"
578 1801
579 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1802 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
580 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=() 1803 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI="${PYTHON_ABI}" return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
581 1804
582 # Strip trailing slash from ROOT. 1805 # Strip trailing slash from ROOT.
583 root="${ROOT%/}" 1806 root="${EROOT%/}"
584 1807
585 # Respect ROOT and options passed to compileall.py. 1808 # Respect ROOT and options passed to compileall.py.
586 while (($#)); do 1809 while (($#)); do
587 case "$1" in 1810 case "$1" in
588 -l|-f|-q) 1811 -l|-f|-q)
591 -d|-x) 1814 -d|-x)
592 options+=("$1" "$2") 1815 options+=("$1" "$2")
593 shift 1816 shift
594 ;; 1817 ;;
595 -*) 1818 -*)
596 ewarn "${FUNCNAME}: Ignoring compile option $1" 1819 ewarn "${FUNCNAME}(): Ignoring option '$1'"
597 ;; 1820 ;;
598 *) 1821 *)
599 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1822 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
600 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 1823 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
601 elif [[ "$1" =~ ^/ ]]; then 1824 elif [[ "$1" =~ ^/ ]]; then
602 if [[ -d "${root}/$1" ]]; then 1825 if [[ -d "${root}/$1" ]]; then
603 other_dirs+=("${root}/$1") 1826 other_dirs+=("${root}/$1")
604 elif [[ -f "${root}/$1" ]]; then 1827 elif [[ -f "${root}/$1" ]]; then
605 other_files+=("${root}/$1") 1828 other_files+=("${root}/$1")
606 elif [[ -e "${root}/$1" ]]; then 1829 elif [[ -e "${root}/$1" ]]; then
607 ewarn "'${root}/$1' is not a file or a directory!" 1830 ewarn "'${root}/$1' is not a file or a directory!"
608 else 1831 else
609 ewarn "'${root}/$1' doesn't exist!" 1832 ewarn "'${root}/$1' does not exist!"
610 fi 1833 fi
611 else 1834 else
612 for PYTHON_ABI in ${PYTHON_ABIS}; do 1835 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
613 if [[ -d "${root}/$(python_get_sitedir)/$1" ]]; then 1836 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
614 site_packages_dirs+=("$1") 1837 site_packages_dirs+=("$1")
615 break 1838 break
616 elif [[ -f "${root}/$(python_get_sitedir)/$1" ]]; then 1839 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
617 site_packages_files+=("$1") 1840 site_packages_files+=("$1")
618 break 1841 break
619 elif [[ -e "${root}/$(python_get_sitedir)/$1" ]]; then 1842 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
620 ewarn "'$1' is not a file or a directory!" 1843 ewarn "'$1' is not a file or a directory!"
621 else 1844 else
622 ewarn "'$1' doesn't exist!" 1845 ewarn "'$1' does not exist!"
623 fi 1846 fi
624 done 1847 done
625 fi 1848 fi
626 ;; 1849 ;;
627 esac 1850 esac
629 done 1852 done
630 1853
631 # Set additional options. 1854 # Set additional options.
632 options+=("-q") 1855 options+=("-q")
633 1856
634 for PYTHON_ABI in ${PYTHON_ABIS}; do 1857 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
635 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 1858 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
636 return_code="0" 1859 return_code="0"
637 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" 1860 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
638 if ((${#site_packages_dirs[@]})); then 1861 if ((${#site_packages_dirs[@]})); then
639 for dir in "${site_packages_dirs[@]}"; do 1862 for dir in "${site_packages_dirs[@]}"; do
640 site_packages_absolute_dirs+=("${root}/$(python_get_sitedir)/${dir}") 1863 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
641 done 1864 done
642 "$(PYTHON)" "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" 1865 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
1866 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then
643 "$(PYTHON)" -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" 1867 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
1868 fi
644 fi 1869 fi
645 if ((${#site_packages_files[@]})); then 1870 if ((${#site_packages_files[@]})); then
646 for file in "${site_packages_files[@]}"; do 1871 for file in "${site_packages_files[@]}"; do
647 site_packages_absolute_files+=("${root}/$(python_get_sitedir)/${file}") 1872 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
648 done 1873 done
649 "$(PYTHON)" "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" 1874 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
1875 if [[ "$(_python_get_implementation "${PYTHON_ABI}")" != "Jython" ]]; then
650 "$(PYTHON)" -O "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" 1876 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
1877 fi
651 fi 1878 fi
652 eend "${return_code}" 1879 eend "${return_code}"
653 fi 1880 fi
654 unset site_packages_absolute_dirs site_packages_absolute_files 1881 unset site_packages_absolute_dirs site_packages_absolute_files
655 done 1882 done
656 1883
657 # Don't use PYTHON_ABI in next calls to python_get_libdir(). 1884 # Restore previous value of PYTHON_ABI.
1885 if [[ -n "${previous_PYTHON_ABI}" ]]; then
1886 PYTHON_ABI="${previous_PYTHON_ABI}"
1887 else
658 unset PYTHON_ABI 1888 unset PYTHON_ABI
1889 fi
659 1890
660 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1891 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
661 return_code="0" 1892 return_code="0"
662 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." 1893 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for $(python_get_implementation) $(python_get_version)"
663 if ((${#other_dirs[@]})); then 1894 if ((${#other_dirs[@]})); then
664 python${PYVER} "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 1895 "$(PYTHON ${PYTHON_ABI})" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
1896 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
665 python${PYVER} -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 1897 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
1898 fi
666 fi 1899 fi
667 if ((${#other_files[@]})); then 1900 if ((${#other_files[@]})); then
668 python${PYVER} "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 1901 "$(PYTHON ${PYTHON_ABI})" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
1902 if [[ "$(_python_get_implementation "${PYTHON_ABI-$(PYTHON --ABI)}")" != "Jython" ]]; then
669 python${PYVER} -O "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 1903 "$(PYTHON ${PYTHON_ABI})" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
1904 fi
670 fi 1905 fi
671 eend "${return_code}" 1906 eend "${return_code}"
672 fi 1907 fi
673 else 1908 else
674 local myroot mydirs=() myfiles=() myopts=() 1909 local myroot mydirs=() myfiles=() myopts=() return_code="0"
675 1910
676 # strip trailing slash 1911 # strip trailing slash
677 myroot="${ROOT%/}" 1912 myroot="${EROOT%/}"
678 1913
679 # respect ROOT and options passed to compileall.py 1914 # respect ROOT and options passed to compileall.py
680 while (($#)); do 1915 while (($#)); do
681 case "$1" in 1916 case "$1" in
682 -l|-f|-q) 1917 -l|-f|-q)
685 -d|-x) 1920 -d|-x)
686 myopts+=("$1" "$2") 1921 myopts+=("$1" "$2")
687 shift 1922 shift
688 ;; 1923 ;;
689 -*) 1924 -*)
690 ewarn "${FUNCNAME}: Ignoring compile option $1" 1925 ewarn "${FUNCNAME}(): Ignoring option '$1'"
691 ;; 1926 ;;
692 *) 1927 *)
693 if [[ -d "${myroot}"/$1 ]]; then 1928 if [[ -d "${myroot}"/$1 ]]; then
694 mydirs+=("${myroot}/$1") 1929 mydirs+=("${myroot}/$1")
695 elif [[ -f "${myroot}"/$1 ]]; then 1930 elif [[ -f "${myroot}"/$1 ]]; then
696 # Files are passed to python_mod_compile which is ROOT-aware 1931 # Files are passed to python_mod_compile which is ROOT-aware
697 myfiles+=("$1") 1932 myfiles+=("$1")
698 elif [[ -e "${myroot}/$1" ]]; then 1933 elif [[ -e "${myroot}/$1" ]]; then
699 ewarn "${myroot}/$1 is not a file or directory!" 1934 ewarn "${myroot}/$1 is not a file or directory!"
700 else 1935 else
701 ewarn "${myroot}/$1 doesn't exist!" 1936 ewarn "${myroot}/$1 does not exist!"
702 fi 1937 fi
703 ;; 1938 ;;
704 esac 1939 esac
705 shift 1940 shift
706 done 1941 done
707 1942
708 # allow compiling for older python versions
709 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
710 PYVER=${PYTHON_OVERRIDE_PYVER}
711 else
712 python_version
713 fi
714
715 # set additional opts 1943 # set additional opts
716 myopts+=(-q) 1944 myopts+=(-q)
717 1945
718 ebegin "Byte compiling python modules for python-${PYVER} .." 1946 ebegin "Compilation and optimization of Python modules for $(python_get_implementation) $(python_get_version)"
719 if ((${#mydirs[@]})); then 1947 if ((${#mydirs[@]})); then
720 python${PYVER} \ 1948 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
721 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 1949 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
722 "${myopts[@]}" "${mydirs[@]}"
723 python${PYVER} -O \
724 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
725 "${myopts[@]}" "${mydirs[@]}"
726 fi 1950 fi
727 1951
728 if ((${#myfiles[@]})); then 1952 if ((${#myfiles[@]})); then
729 python_mod_compile "${myfiles[@]}" 1953 python_mod_compile "${myfiles[@]}"
730 fi 1954 fi
731 1955
732 eend $? 1956 eend "${return_code}"
733 fi 1957 fi
734} 1958}
735 1959
736# @FUNCTION: python_mod_cleanup 1960# @FUNCTION: python_mod_cleanup
737# @USAGE: [directory] 1961# @USAGE: [directory|file]
738# @DESCRIPTION: 1962# @DESCRIPTION:
739# Run with optional arguments, where arguments are directories of 1963# Run with optional arguments, where arguments are Python modules. If none given,
740# python modules. If none given, it will look in /usr/lib/python[0-9].[0-9]. 1964# it will look in /usr/lib/python[0-9].[0-9].
741# 1965#
742# It will recursively scan all compiled Python modules in the directories and 1966# It will recursively scan all compiled Python modules in the directories and
743# determine if they are orphaned (i.e. their corresponding .py files are missing.) 1967# determine if they are orphaned (i.e. their corresponding .py files are missing.)
744# If they are, then it will remove their corresponding .pyc and .pyo files. 1968# If they are, then it will remove their corresponding .pyc and .pyo files.
745# 1969#
746# This function should only be run in pkg_postrm(). 1970# This function can be used only in pkg_postrm() phase.
747python_mod_cleanup() { 1971python_mod_cleanup() {
748 local PYTHON_ABI SEARCH_PATH=() root src_py 1972 _python_initialize_prefix_variables
1973 _python_set_color_variables
1974
1975 local path py_file PYTHON_ABI="${PYTHON_ABI}" SEARCH_PATH=() root
749 1976
750 # Check if phase is pkg_postrm(). 1977 # Check if phase is pkg_postrm().
751 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" 1978 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME}() can be used only in pkg_postrm() phase"
752 1979
753 # Strip trailing slash from ROOT. 1980 # Strip trailing slash from ROOT.
754 root="${ROOT%/}" 1981 root="${EROOT%/}"
755 1982
756 if (($#)); then 1983 if (($#)); then
757 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1984 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
758 while (($#)); do 1985 while (($#)); do
759 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1986 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
760 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 1987 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
761 elif [[ "$1" =~ ^/ ]]; then 1988 elif [[ "$1" =~ ^/ ]]; then
762 SEARCH_PATH+=("${root}/${1#/}") 1989 SEARCH_PATH+=("${root}/${1#/}")
763 else 1990 else
764 for PYTHON_ABI in ${PYTHON_ABIS}; do 1991 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI:-$(PYTHON --ABI)}}; do
765 SEARCH_PATH+=("${root}/$(python_get_sitedir)/$1") 1992 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
766 done 1993 done
767 fi 1994 fi
768 shift 1995 shift
769 done 1996 done
770 else 1997 else
771 SEARCH_PATH=("${@#/}") 1998 SEARCH_PATH=("${@#/}")
772 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 1999 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
773 fi 2000 fi
774 else 2001 else
775 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) 2002 local dir sitedir
2003 for dir in "${root}"/usr/lib*; do
2004 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
2005 for sitedir in "${dir}"/python*/site-packages; do
2006 if [[ -d "${sitedir}" ]]; then
2007 SEARCH_PATH+=("${sitedir}")
2008 fi
2009 done
2010 fi
2011 done
2012 for sitedir in "${root}"/usr/share/jython-*/Lib/site-packages; do
2013 if [[ -d "${sitedir}" ]]; then
2014 SEARCH_PATH+=("${sitedir}")
2015 fi
2016 done
776 fi 2017 fi
777 2018
778 for path in "${SEARCH_PATH[@]}"; do 2019 for path in "${SEARCH_PATH[@]}"; do
779 einfo "Cleaning orphaned Python bytecode from ${path} .." 2020 if [[ -d "${path}" ]]; then
780 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 2021 find "${path}" "(" -name "*.py[co]" -o -name "*\$py.class" ")" -print0 | while read -rd ''; do
2022 if [[ "${REPLY}" == *[co] ]]; then
781 src_py="${REPLY%[co]}" 2023 py_file="${REPLY%[co]}"
782 [[ -f "${src_py}" ]] && continue 2024 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
783 einfo "Purging ${src_py}[co]" 2025 echo "${_BLUE}<<< ${py_file}[co]${_NORMAL}"
784 rm -f "${src_py}"[co] 2026 rm -f "${py_file}"[co]
2027 elif [[ "${REPLY}" == *\$py.class ]]; then
2028 py_file="${REPLY%\$py.class}.py"
2029 [[ -f "${py_file}" || ! -f "${py_file%.py}\$py.class" ]] && continue
2030 echo "${_BLUE}<<< ${py_file%.py}\$py.class${_NORMAL}"
2031 rm -f "${py_file%.py}\$py.class"
2032 fi
785 done 2033 done
786 2034
787 # Attempt to remove directories that may be empty. 2035 # Attempt to delete directories, which may be empty.
788 find "${path}" -type d | sort -r | while read -r dir; do 2036 find "${path}" -type d | sort -r | while read -r dir; do
789 rmdir "${dir}" 2>/dev/null 2037 rmdir "${dir}" 2>/dev/null && echo "${_CYAN}<<< ${dir}${_NORMAL}"
790 done 2038 done
2039 elif [[ "${path}" == *.py && ! -f "${path}" ]]; then
2040 if [[ (-f "${path}c" || -f "${path}o") ]]; then
2041 echo "${_BLUE}<<< ${path}[co]${_NORMAL}"
2042 rm -f "${path}"[co]
2043 fi
2044 if [[ -f "${path%.py}\$py.class" ]]; then
2045 echo "${_BLUE}<<< ${path%.py}\$py.class${_NORMAL}"
2046 rm -f "${path%.py}\$py.class"
2047 fi
2048 fi
791 done 2049 done
792} 2050}
2051
2052# ================================================================================================
2053# ===================================== DEPRECATED FUNCTIONS =====================================
2054# ================================================================================================
2055
2056# @FUNCTION: python_version
2057# @DESCRIPTION:
2058# Run without arguments and it will export the version of python
2059# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
2060python_version() {
2061 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
2062 eerror "Use PYTHON() and/or python_get_*() instead of ${FUNCNAME}()."
2063 die "${FUNCNAME}() cannot be used in this EAPI"
2064 fi
2065
2066 if [[ "${FUNCNAME[1]}" != "distutils_python_version" ]]; then
2067 einfo
2068 einfo "Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01."
2069 einfo "Use PYTHON() instead of python variable. Use python_get_*() instead of PYVER* variables."
2070 einfo "The ebuild needs to be fixed. Please report a bug, if it has not been already reported."
2071 einfo
2072 fi
2073
2074 [[ -n "${PYVER}" ]] && return 0
2075 local tmpstr
2076 python="${python:-${EPREFIX}/usr/bin/python}"
2077 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
2078 export PYVER_ALL="${tmpstr#Python }"
2079 export PYVER_MAJOR="${PYVER_ALL:0:1}"
2080 export PYVER_MINOR="${PYVER_ALL:2:1}"
2081 if [[ "${PYVER_ALL:3:1}" == "." ]]; then
2082 export PYVER_MICRO="${PYVER_ALL:4}"
2083 fi
2084 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
2085}
2086
2087# @FUNCTION: python_mod_exists
2088# @USAGE: <module>
2089# @DESCRIPTION:
2090# Run with the module name as an argument. It will check if a
2091# Python module is installed and loadable. It will return
2092# TRUE(0) if the module exists, and FALSE(1) if the module does
2093# not exist.
2094#
2095# Example:
2096# if python_mod_exists gtk; then
2097# echo "gtk support enabled"
2098# fi
2099python_mod_exists() {
2100 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
2101 eerror "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()."
2102 die "${FUNCNAME}() cannot be used in this EAPI"
2103 fi
2104
2105 einfo
2106 einfo "Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01."
2107 einfo "Use USE dependencies and/or has_version() instead of ${FUNCNAME}()."
2108 einfo "The ebuild needs to be fixed. Please report a bug, if it has not been already reported."
2109 einfo
2110
2111 if [[ "$#" -ne 1 ]]; then
2112 die "${FUNCNAME}() requires 1 argument"
2113 fi
2114 "$(PYTHON ${PYTHON_ABI})" -c "import $1" &> /dev/null
2115}
2116
2117# @FUNCTION: python_tkinter_exists
2118# @DESCRIPTION:
2119# Run without arguments, checks if Python was compiled with Tkinter
2120# support. If not, prints an error message and dies.
2121python_tkinter_exists() {
2122 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
2123 eerror "Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}()."
2124 die "${FUNCNAME}() cannot be used in this EAPI"
2125 fi
2126
2127 if [[ "${FUNCNAME[1]}" != "distutils_python_tkinter" ]]; then
2128 einfo
2129 einfo "Deprecation Warning: ${FUNCNAME}() is deprecated and will be banned on 2010-07-01."
2130 einfo "Use PYTHON_USE_WITH=\"xml\" and python_pkg_setup() instead of ${FUNCNAME}()."
2131 einfo "The ebuild needs to be fixed. Please report a bug, if it has not been already reported."
2132 einfo
2133 fi
2134
2135 if ! "$(PYTHON ${PYTHON_ABI})" -c "from sys import version_info
2136if version_info[0] == 3:
2137 import tkinter
2138else:
2139 import Tkinter" &> /dev/null; then
2140 eerror "Python needs to be rebuilt with tkinter support enabled."
2141 eerror "Add the following line to '${EPREFIX}/etc/portage/package.use' and rebuild Python"
2142 eerror "dev-lang/python tk"
2143 die "Python installed without support for tkinter"
2144 fi
2145}
2146
2147# @FUNCTION: python_mod_compile
2148# @USAGE: <file> [more files ...]
2149# @DESCRIPTION:
2150# Given filenames, it will pre-compile the module's .pyc and .pyo.
2151# This function can be used only in pkg_postinst() phase.
2152#
2153# Example:
2154# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
2155#
2156python_mod_compile() {
2157 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
2158 eerror "Use python_mod_optimize() instead of ${FUNCNAME}()."
2159 die "${FUNCNAME}() cannot be used in this EAPI"
2160 fi
2161
2162 _python_initialize_prefix_variables
2163
2164 local f myroot myfiles=()
2165
2166 # Check if phase is pkg_postinst()
2167 [[ ${EBUILD_PHASE} != postinst ]] && die "${FUNCNAME}() can be used only in pkg_postinst() phase"
2168
2169 # strip trailing slash
2170 myroot="${EROOT%/}"
2171
2172 # respect ROOT
2173 for f in "$@"; do
2174 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
2175 done
2176
2177 if ((${#myfiles[@]})); then
2178 "$(PYTHON ${PYTHON_ABI})" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
2179 "$(PYTHON ${PYTHON_ABI})" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
2180 else
2181 ewarn "No files to compile!"
2182 fi
2183}

Legend:
Removed from v.1.60  
changed lines
  Added in v.1.94

  ViewVC Help
Powered by ViewVC 1.1.20