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

Diff of /eclass/python.eclass

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

Revision 1.86 Revision 1.87
1# Copyright 1999-2010 Gentoo Foundation 1# Copyright 1999-2010 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.86 2010/01/15 14:46:20 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.87 2010/02/02 18:55:00 arfrever Exp $
4 4
5# @ECLASS: python.eclass 5# @ECLASS: python.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# python@gentoo.org 7# python@gentoo.org
8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules. 8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules.
9# @DESCRIPTION: 9# @DESCRIPTION:
10# Some useful functions for dealing with Python. 10# Some useful functions for dealing with Python.
11 11
12inherit multilib 12inherit multilib
13 13
14if ! has "${EAPI:-0}" 0 1 2; then 14if ! has "${EAPI:-0}" 0 1 2 3; then
15 die "API of python.eclass in EAPI=\"${EAPI}\" not established" 15 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
16fi 16fi
17 17
18_PYTHON2_SUPPORTED_VERSIONS=(2.4 2.5 2.6 2.7)
19_PYTHON3_SUPPORTED_VERSIONS=(3.0 3.1 3.2)
20
21# @ECLASS-VARIABLE: PYTHON_DEPEND
22# @DESCRIPTION:
23# Specification of dependency on dev-lang/python.
24# Syntax:
25# PYTHON_DEPEND: [[!]USE_flag? ]<version_components_group>[ version_components_group]
26# version_components_group: <major_version[:[minimal_version][:maximal_version]]>
27# major_version: <2|3|*>
28# minimal_version: <minimal_major_version.minimal_minor_version>
29# maximal_version: <maximal_major_version.maximal_minor_version>
30
31_parse_PYTHON_DEPEND() {
32 local accepted_version accepted_versions=() major_version maximal_version minimal_version python_all="0" python_atoms= 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
33
34 version_components_group_regex="(2|3|\*)(:([[:digit:]]+\.[[:digit:]]+)?(:([[:digit:]]+\.[[:digit:]]+)?)?)?"
35 version_components_groups="${PYTHON_DEPEND}"
36
37 if [[ "${version_components_groups}" =~ ^((\!)?[[:alnum:]_-]+\?\ )?${version_components_group_regex}(\ ${version_components_group_regex})?$ ]]; then
38 if [[ "${version_components_groups}" =~ ^(\!)?[[:alnum:]_-]+\? ]]; then
39 USE_flag="${version_components_groups%\? *}"
40 version_components_groups="${version_components_groups#* }"
41 fi
42 if [[ "${version_components_groups}" =~ ("*".*" "|" *"|^2.*\ (2|\*)|^3.*\ (3|\*)) ]]; then
43 die "Invalid syntax of PYTHON_DEPEND: Incorrectly specified groups of versions"
44 fi
45
46 version_components_groups="${version_components_groups// /$'\n'}"
47 while read version_components_group; do
48 major_version="${version_components_group:0:1}"
49 minimal_version="${version_components_group:2}"
50 minimal_version="${minimal_version%:*}"
51 maximal_version="${version_components_group:$((3 + ${#minimal_version}))}"
52
53 if [[ "${major_version}" =~ ^(2|3)$ ]]; then
54 if [[ -n "${minimal_version}" && "${major_version}" != "${minimal_version:0:1}" ]]; then
55 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' not in specified group of versions"
56 fi
57 if [[ -n "${maximal_version}" && "${major_version}" != "${maximal_version:0:1}" ]]; then
58 die "Invalid syntax of PYTHON_DEPEND: Maximal version '${maximal_version}' not in specified group of versions"
59 fi
60 fi
61
62 if [[ "${major_version}" == "2" ]]; then
63 python2="1"
64 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}")
65 python2_minimal_version="${minimal_version}"
66 python2_maximal_version="${maximal_version}"
67 elif [[ "${major_version}" == "3" ]]; then
68 python3="1"
69 python_versions=("${_PYTHON3_SUPPORTED_VERSIONS[@]}")
70 python3_minimal_version="${minimal_version}"
71 python3_maximal_version="${maximal_version}"
72 else
73 python_all="1"
74 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}")
75 python_minimal_version="${minimal_version}"
76 python_maximal_version="${maximal_version}"
77 fi
78
79 if [[ -n "${minimal_version}" ]] && ! has "${minimal_version}" "${python_versions[@]}"; then
80 die "Invalid syntax of PYTHON_DEPEND: Unrecognized minimal version '${minimal_version}'"
81 fi
82 if [[ -n "${maximal_version}" ]] && ! has "${maximal_version}" "${python_versions[@]}"; then
83 die "Invalid syntax of PYTHON_DEPEND: Unrecognized maximal version '${maximal_version}'"
84 fi
85
86 if [[ -n "${minimal_version}" && -n "${maximal_version}" && "${minimal_version}" > "${maximal_version}" ]]; then
87 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' greater than maximal version '${maximal_version}'"
88 fi
89 done <<< "${version_components_groups}"
90
91 _create_accepted_versions_range() {
92 local accepted_version="0" i
93 for ((i = "${#python_versions[@]}"; i >= 0; i--)); do
94 if [[ "${python_versions[${i}]}" == "${python_maximal_version}" ]]; then
95 accepted_version="1"
96 fi
97 if [[ "${accepted_version}" == "1" ]]; then
98 accepted_versions+=("${python_versions[${i}]}")
99 fi
100 if [[ "${python_versions[${i}]}" == "${python_minimal_version}" ]]; then
101 accepted_version="0"
102 fi
103 done
104 }
105
106 if [[ "${python_all}" == "1" ]]; then
107 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}")
108 python_minimal_version="${python_minimal_version:-${python_versions[0]}}"
109 python_maximal_version="${python_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
110 _create_accepted_versions_range
111 else
112 if [[ "${python3}" == "1" ]]; then
113 python_versions=("${_PYTHON3_SUPPORTED_VERSIONS[@]}")
114 python_minimal_version="${python3_minimal_version:-${python_versions[0]}}"
115 python_maximal_version="${python3_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
116 _create_accepted_versions_range
117 fi
118 if [[ "${python2}" == "1" ]]; then
119 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}")
120 python_minimal_version="${python2_minimal_version:-${python_versions[0]}}"
121 python_maximal_version="${python2_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
122 _create_accepted_versions_range
123 fi
124 fi
125
126 for accepted_version in "${accepted_versions[@]}"; do
127 python_atoms+="${python_atoms:+ }=dev-lang/python-${accepted_version}*"
128 done
129
130 _PYTHON_ATOMS="${python_atoms// /$'\n'}"
131 DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${python_atoms} )${USE_flag:+ )}"
132 RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${python_atoms} )${USE_flag:+ )}"
133 else
134 die "Invalid syntax of PYTHON_DEPEND"
135 fi
136}
137
138DEPEND=">=app-admin/eselect-python-20090804"
139RDEPEND="${DEPEND}"
140
141if [[ -n "${PYTHON_DEPEND}" && -n "${NEED_PYTHON}" ]]; then
142 die "PYTHON_DEPEND and NEED_PYTHON cannot be set simultaneously"
143elif [[ -n "${PYTHON_DEPEND}" ]]; then
144 _parse_PYTHON_DEPEND
18if [[ -n "${NEED_PYTHON}" ]]; then 145elif [[ -n "${NEED_PYTHON}" ]]; then
146 if ! has "${EAPI:-0}" 0 1 2; then
147 eerror "Use PYTHON_DEPEND instead of NEED_PYTHON."
148 die "NEED_PYTHON cannot be used in this EAPI"
149 fi
19 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" 150 _PYTHON_ATOMS=">=dev-lang/python-${NEED_PYTHON}"
20 DEPEND="${PYTHON_ATOM}" 151 DEPEND+="${DEPEND:+ }${_PYTHON_ATOMS}"
21 RDEPEND="${DEPEND}" 152 RDEPEND+="${RDEPEND:+ }${_PYTHON_ATOMS}"
22else 153else
23 PYTHON_ATOM="dev-lang/python" 154 _PYTHON_ATOMS="dev-lang/python"
24fi 155fi
25 156
26DEPEND+=" >=app-admin/eselect-python-20090804"
27
28# @ECLASS-VARIABLE: PYTHON_USE_WITH 157# @ECLASS-VARIABLE: PYTHON_USE_WITH
29# @DESCRIPTION: 158# @DESCRIPTION:
30# Set this to a space separated list of use flags 159# Set this to a space separated list of USE flags the Python slot in use must be built with.
31# the python slot in use must be built with.
32 160
33# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR 161# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
34# @DESCRIPTION: 162# @DESCRIPTION:
35# Set this to a space separated list of use flags 163# Set this to a space separated list of USE flags of which one must be turned on for the slot in use.
36# of which one must be turned on for the slot of
37# in use.
38 164
39# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT 165# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
40# @DESCRIPTION: 166# @DESCRIPTION:
41# Set this if you need to make either PYTHON_USE_WITH or 167# Set this to a name of a USE flag if you need to make either PYTHON_USE_WITH or
42# PYTHON_USE_WITH_OR atoms conditional under a use flag. 168# PYTHON_USE_WITH_OR atoms conditional under a USE flag.
43 169
44# @FUNCTION: python_pkg_setup 170# @FUNCTION: python_pkg_setup
45# @DESCRIPTION: 171# @DESCRIPTION:
46# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 172# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
47# are respected. Only exported if one of those variables is set. 173# are respected. Only exported if one of those variables is set.
86 fi 212 fi
87 } 213 }
88 214
89 EXPORT_FUNCTIONS pkg_setup 215 EXPORT_FUNCTIONS pkg_setup
90 216
217 _PYTHON_USE_WITH_ATOM=""
91 if [[ -n "${PYTHON_USE_WITH}" ]]; then 218 if [[ -n "${PYTHON_USE_WITH}" ]]; then
219 while read _PYTHON_ATOM; do
92 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 220 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
221 done <<< "${_PYTHON_ATOMS}"
222 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
93 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then 223 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
94 PYTHON_USE_WITH_ATOM="|| ( "
95 for use in ${PYTHON_USE_WITH_OR}; do 224 for _USE_flag in ${PYTHON_USE_WITH_OR}; do
96 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]" 225 while read _PYTHON_ATOM; do
226 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${_USE_flag}]"
227 done <<< "${_PYTHON_ATOMS}"
97 done 228 done
98 unset use 229 unset _USE_flag
99 PYTHON_USE_WITH_ATOM+=" )" 230 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
100 fi 231 fi
101 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then 232 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
102 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 233 _PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOM} )"
103 fi 234 fi
104 DEPEND+=" ${PYTHON_USE_WITH_ATOM}" 235 DEPEND+=" ${_PYTHON_USE_WITH_ATOM}"
105 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}" 236 RDEPEND+=" ${_PYTHON_USE_WITH_ATOM}"
237 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOM
106fi 238fi
239
240unset _PYTHON_ATOMS
107 241
108# ================================================================================================ 242# ================================================================================================
109# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======== 243# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ========
110# ================================================================================================ 244# ================================================================================================
111 245
143 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 277 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
144 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable" 278 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
145 fi 279 fi
146 280
147 # Ensure that /usr/bin/python and /usr/bin/python-config are valid. 281 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
148 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then 282 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
149 eerror "'/usr/bin/python' is not valid symlink." 283 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
150 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem." 284 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
151 die "'/usr/bin/python' is not valid symlink" 285 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
152 fi 286 fi
153 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then 287 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
154 eerror "'/usr/bin/python-config' is not valid script" 288 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
155 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem." 289 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
156 die "'/usr/bin/python-config' is not valid script" 290 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
157 fi 291 fi
158 292
159 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5. 293 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
160 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then 294 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
161 local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS= 295 local PYTHON_ABI restricted_ABI support_ABI supported_PYTHON_ABIS=
162 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" 296 PYTHON_ABI_SUPPORTED_VALUES="${_PYTHON2_SUPPORTED_VERSIONS[@]} ${_PYTHON3_SUPPORTED_VERSIONS[@]}"
163 python2_supported_versions="2.4 2.5 2.6 2.7"
164 python3_supported_versions="3.0 3.1 3.2"
165 297
166 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then 298 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
167 local python2_enabled="0" python3_enabled="0" 299 local python2_enabled="0" python3_enabled="0"
168 300
169 if [[ -z "${USE_PYTHON}" ]]; then 301 if [[ -z "${USE_PYTHON}" ]]; then
173 for PYTHON_ABI in ${USE_PYTHON}; do 305 for PYTHON_ABI in ${USE_PYTHON}; do
174 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 306 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
175 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'" 307 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
176 fi 308 fi
177 309
178 if has "${PYTHON_ABI}" ${python2_supported_versions}; then 310 if has "${PYTHON_ABI}" "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; then
179 python2_enabled="1" 311 python2_enabled="1"
180 fi 312 fi
181 if has "${PYTHON_ABI}" ${python3_supported_versions}; then 313 if has "${PYTHON_ABI}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; then
182 python3_enabled="1" 314 python3_enabled="1"
183 fi 315 fi
184 316
185 support_ABI="1" 317 support_ABI="1"
186 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 318 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
203 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported." 335 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported."
204 fi 336 fi
205 else 337 else
206 local python_version python2_version= python3_version= support_python_major_version 338 local python_version python2_version= python3_version= support_python_major_version
207 339
208 python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" 340 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
209 341
210 if has_version "=dev-lang/python-2*"; then 342 if has_version "=dev-lang/python-2*"; then
211 if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then 343 if [[ "$(readlink "${EPREFIX}/usr/bin/python2")" != "python2."* ]]; then
212 die "'/usr/bin/python2' is not valid symlink" 344 die "'${EPREFIX}/usr/bin/python2' is not valid symlink"
213 fi 345 fi
214 346
215 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" 347 python2_version="$("${EPREFIX}/usr/bin/python2" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
216 348
217 for PYTHON_ABI in ${python2_supported_versions}; do 349 for PYTHON_ABI in "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; do
218 support_python_major_version="1" 350 support_python_major_version="1"
219 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 351 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
220 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then 352 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
221 support_python_major_version="0" 353 support_python_major_version="0"
222 fi 354 fi
233 python2_version="" 365 python2_version=""
234 fi 366 fi
235 fi 367 fi
236 368
237 if has_version "=dev-lang/python-3*"; then 369 if has_version "=dev-lang/python-3*"; then
238 if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then 370 if [[ "$(readlink "${EPREFIX}/usr/bin/python3")" != "python3."* ]]; then
239 die "'/usr/bin/python3' is not valid symlink" 371 die "'${EPREFIX}/usr/bin/python3' is not valid symlink"
240 fi 372 fi
241 373
242 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" 374 python3_version="$("${EPREFIX}/usr/bin/python3" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
243 375
244 for PYTHON_ABI in ${python3_supported_versions}; do 376 for PYTHON_ABI in "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; do
245 support_python_major_version="1" 377 support_python_major_version="1"
246 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 378 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
247 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then 379 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
248 support_python_major_version="0" 380 support_python_major_version="0"
249 fi 381 fi
260 python3_version="" 392 python3_version=""
261 fi 393 fi
262 fi 394 fi
263 395
264 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then 396 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
265 eerror "Python wrapper is configured incorrectly or /usr/bin/python2 symlink" 397 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python2' symlink"
266 eerror "is set incorrectly. Use \`eselect python\` to fix configuration." 398 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
267 die "Incorrect configuration of Python" 399 die "Incorrect configuration of Python"
268 fi 400 fi
269 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then 401 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
270 eerror "Python wrapper is configured incorrectly or /usr/bin/python3 symlink" 402 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python3' symlink"
271 eerror "is set incorrectly. Use \`eselect python\` to fix configuration." 403 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
272 die "Incorrect configuration of Python" 404 die "Incorrect configuration of Python"
273 fi 405 fi
274 406
275 PYTHON_ABIS="${python2_version} ${python3_version}" 407 PYTHON_ABIS="${python2_version} ${python3_version}"
305# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments] 437# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
306# @DESCRIPTION: 438# @DESCRIPTION:
307# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 439# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
308# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 440# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
309python_execute_function() { 441python_execute_function() {
310 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir= 442 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function i nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
311 443
312 while (($#)); do 444 while (($#)); do
313 case "$1" in 445 case "$1" in
314 --action-message) 446 --action-message)
315 action_message_template="$2" 447 action_message_template="$2"
334 --source-dir) 466 --source-dir)
335 source_dir="$2" 467 source_dir="$2"
336 shift 468 shift
337 ;; 469 ;;
338 --) 470 --)
471 shift
339 break 472 break
340 ;; 473 ;;
341 -*) 474 -*)
342 die "${FUNCNAME}(): Unrecognized option '$1'" 475 die "${FUNCNAME}(): Unrecognized option '$1'"
343 ;; 476 ;;
361 494
362 if [[ -z "$(type -t "${function}")" ]]; then 495 if [[ -z "$(type -t "${function}")" ]]; then
363 die "${FUNCNAME}(): '${function}' function is not defined" 496 die "${FUNCNAME}(): '${function}' function is not defined"
364 fi 497 fi
365 else 498 else
366 if [[ "$#" -ne "0" ]]; then 499 if [[ "$#" -ne 0 ]]; then
367 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously" 500 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
368 fi 501 fi
369 if has "${EAPI:-0}" 0 1; then 502 if has "${EAPI:-0}" 0 1; then
370 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI" 503 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
371 fi 504 fi
399 else 532 else
400 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase" 533 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
401 fi 534 fi
402 function="python_default_function" 535 function="python_default_function"
403 fi 536 fi
537
538 for ((i = 1; i < "${#FUNCNAME[@]}"; i++)); do
539 if [[ "${FUNCNAME[${i}]}" == "python_execute_function" ]]; then
540 die "${FUNCNAME}(): Invalid call stack"
541 fi
542 done
404 543
405 if [[ "${quiet}" == "0" ]]; then 544 if [[ "${quiet}" == "0" ]]; then
406 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up" 545 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
407 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking" 546 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
408 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation" 547 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
524} 663}
525 664
526# @FUNCTION: python_copy_sources 665# @FUNCTION: python_copy_sources
527# @USAGE: [--no-link] [--] [directory] 666# @USAGE: [--no-link] [--] [directory]
528# @DESCRIPTION: 667# @DESCRIPTION:
529# Copy unpacked sources of given package for each Python ABI. 668# Copy unpacked sources of given package to separate build directory for each Python ABI.
530python_copy_sources() { 669python_copy_sources() {
531 local dir dirs=() no_link="0" PYTHON_ABI 670 local dir dirs=() no_link="0" PYTHON_ABI
532 671
533 while (($#)); do 672 while (($#)); do
534 case "$1" in 673 case "$1" in
535 --no-link) 674 --no-link)
536 no_link="1" 675 no_link="1"
537 ;; 676 ;;
538 --) 677 --)
678 shift
539 break 679 break
540 ;; 680 ;;
541 -*) 681 -*)
542 die "${FUNCNAME}(): Unrecognized option '$1'" 682 die "${FUNCNAME}(): Unrecognized option '$1'"
543 ;; 683 ;;
589# @DESCRIPTION: 729# @DESCRIPTION:
590# Generate wrapper scripts. Existing files are overwritten only with --force option. 730# Generate wrapper scripts. Existing files are overwritten only with --force option.
591# If --respect-EPYTHON option is specified, then generated wrapper scripts will 731# If --respect-EPYTHON option is specified, then generated wrapper scripts will
592# respect EPYTHON variable at run time. 732# respect EPYTHON variable at run time.
593python_generate_wrapper_scripts() { 733python_generate_wrapper_scripts() {
734 _python_initialize_prefix_variables
735
594 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python2_supported_versions python3_enabled="0" python3_supported_versions respect_EPYTHON="0" 736 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python3_enabled="0" respect_EPYTHON="0"
595 python2_supported_versions="2.4 2.5 2.6 2.7"
596 python3_supported_versions="3.0 3.1 3.2"
597 737
598 while (($#)); do 738 while (($#)); do
599 case "$1" in 739 case "$1" in
600 -E|--respect-EPYTHON) 740 -E|--respect-EPYTHON)
601 respect_EPYTHON="1" 741 respect_EPYTHON="1"
605 ;; 745 ;;
606 -q|--quiet) 746 -q|--quiet)
607 quiet="1" 747 quiet="1"
608 ;; 748 ;;
609 --) 749 --)
750 shift
610 break 751 break
611 ;; 752 ;;
612 -*) 753 -*)
613 die "${FUNCNAME}(): Unrecognized option '$1'" 754 die "${FUNCNAME}(): Unrecognized option '$1'"
614 ;; 755 ;;
622 if [[ "$#" -eq 0 ]]; then 763 if [[ "$#" -eq 0 ]]; then
623 die "${FUNCNAME}(): Missing arguments" 764 die "${FUNCNAME}(): Missing arguments"
624 fi 765 fi
625 766
626 validate_PYTHON_ABIS 767 validate_PYTHON_ABIS
627 for PYTHON_ABI in ${python2_supported_versions}; do 768 for PYTHON_ABI in "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; do
628 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then 769 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
629 python2_enabled="1" 770 python2_enabled="1"
630 fi 771 fi
631 done 772 done
632 for PYTHON_ABI in ${python3_supported_versions}; do 773 for PYTHON_ABI in "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; do
633 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then 774 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
634 python3_enabled="1" 775 python3_enabled="1"
635 fi 776 fi
636 done 777 done
637 778
679 else: 820 else:
680 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON) 821 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
681 sys.exit(1) 822 sys.exit(1)
682else: 823else:
683 try: 824 try:
684 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE) 825 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
685 if eselect_process.wait() != 0: 826 if eselect_process.wait() != 0:
686 raise ValueError 827 raise ValueError
687 except (OSError, ValueError): 828 except (OSError, ValueError):
688 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n") 829 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
689 sys.exit(1) 830 sys.exit(1)
704 die "${FUNCNAME}(): Generation of '$1' failed" 845 die "${FUNCNAME}(): Generation of '$1' failed"
705 fi 846 fi
706 else 847 else
707 cat << EOF >> "${file}" 848 cat << EOF >> "${file}"
708try: 849try:
709 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE) 850 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
710 if eselect_process.wait() != 0: 851 if eselect_process.wait() != 0:
711 raise ValueError 852 raise ValueError
712except (OSError, ValueError): 853except (OSError, ValueError):
713 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n") 854 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
714 sys.exit(1) 855 sys.exit(1)
740os.execv(target_executable, sys.argv) 881os.execv(target_executable, sys.argv)
741EOF 882EOF
742 if [[ "$?" != "0" ]]; then 883 if [[ "$?" != "0" ]]; then
743 die "${FUNCNAME}(): Generation of '$1' failed" 884 die "${FUNCNAME}(): Generation of '$1' failed"
744 fi 885 fi
745 fperms +x "${file#${D%/}}" || die "fperms '${file}' failed" 886 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed"
746 done 887 done
747} 888}
748 889
749# ================================================================================================ 890# ================================================================================================
750# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ====== 891# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======
753# @FUNCTION: python_set_active_version 894# @FUNCTION: python_set_active_version
754# @USAGE: <Python_ABI|2|3> 895# @USAGE: <Python_ABI|2|3>
755# @DESCRIPTION: 896# @DESCRIPTION:
756# Set active version of Python. 897# Set active version of Python.
757python_set_active_version() { 898python_set_active_version() {
758 if [[ "$#" -ne "1" ]]; then 899 if [[ "$#" -ne 1 ]]; then
759 die "${FUNCNAME}() requires 1 argument" 900 die "${FUNCNAME}() requires 1 argument"
760 fi 901 fi
761 902
762 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then 903 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
763 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then 904 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
780 921
781 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses, 922 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
782 # so it does not need to be exported to subprocesses. 923 # so it does not need to be exported to subprocesses.
783 PYTHON_ABI="${EPYTHON#python}" 924 PYTHON_ABI="${EPYTHON#python}"
784 PYTHON_ABI="${PYTHON_ABI%%-*}" 925 PYTHON_ABI="${PYTHON_ABI%%-*}"
926
927 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
928 PYTHON_REQUESTED_ACTIVE_VERSION="$1"
785} 929}
786 930
787# @FUNCTION: python_need_rebuild 931# @FUNCTION: python_need_rebuild
788# @DESCRIPTION: Run without arguments, specifies that the package should be 932# @DESCRIPTION: Run without arguments, specifies that the package should be
789# rebuilt after a python upgrade. 933# rebuilt after a python upgrade.
798# ================================================================================================ 942# ================================================================================================
799 943
800# @FUNCTION: PYTHON 944# @FUNCTION: PYTHON
801# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}"> 945# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
802# @DESCRIPTION: 946# @DESCRIPTION:
803# Get Python interpreter filename for specified Python ABI. If Python_ABI argument 947# Print Python interpreter filename for specified Python ABI. If Python_ABI argument
804# is ommitted, then PYTHON_ABI environment variable must be set and is used. 948# is ommitted, then PYTHON_ABI environment variable must be set and is used.
805# If -2 option is specified, then active version of Python 2 is used. 949# If -2 option is specified, then active version of Python 2 is used.
806# If -3 option is specified, then active version of Python 3 is used. 950# If -3 option is specified, then active version of Python 3 is used.
807# If --active option is specified, then active version of Python is used. 951# If --active option is specified, then active version of Python is used.
808# Active version of Python can be set by python_set_active_version(). 952# Active version of Python can be set by python_set_active_version().
833 ;; 977 ;;
834 -f|--final-ABI) 978 -f|--final-ABI)
835 final_ABI="1" 979 final_ABI="1"
836 ;; 980 ;;
837 --) 981 --)
982 shift
838 break 983 break
839 ;; 984 ;;
840 -*) 985 -*)
841 die "${FUNCNAME}(): Unrecognized option '$1'" 986 die "${FUNCNAME}(): Unrecognized option '$1'"
842 ;; 987 ;;
858 if [[ "$#" -eq 0 ]]; then 1003 if [[ "$#" -eq 0 ]]; then
859 if [[ "${active}" == "1" ]]; then 1004 if [[ "${active}" == "1" ]]; then
860 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1005 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
861 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python" 1006 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
862 fi 1007 fi
863 slot="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" 1008 slot="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
864 elif [[ "${final_ABI}" == "1" ]]; then 1009 elif [[ "${final_ABI}" == "1" ]]; then
865 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 1010 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
866 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python" 1011 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python"
867 fi 1012 fi
868 validate_PYTHON_ABIS 1013 validate_PYTHON_ABIS
908 1053
909 if [[ "${ABI_output}" == "1" ]]; then 1054 if [[ "${ABI_output}" == "1" ]]; then
910 echo -n "${slot}" 1055 echo -n "${slot}"
911 return 1056 return
912 elif [[ "${absolute_path_output}" == "1" ]]; then 1057 elif [[ "${absolute_path_output}" == "1" ]]; then
913 echo -n "/usr/bin/python${slot}" 1058 echo -n "${EPREFIX}/usr/bin/python${slot}"
914 else 1059 else
915 echo -n "python${slot}" 1060 echo -n "python${slot}"
916 fi 1061 fi
917 1062
918 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then 1063 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
921} 1066}
922 1067
923# @FUNCTION: python_get_includedir 1068# @FUNCTION: python_get_includedir
924# @USAGE: [-f|--final-ABI] 1069# @USAGE: [-f|--final-ABI]
925# @DESCRIPTION: 1070# @DESCRIPTION:
926# Print Python include directory. 1071# Print path to Python include directory.
927# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used. 1072# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
928python_get_includedir() { 1073python_get_includedir() {
929 local final_ABI="0" 1074 local final_ABI="0"
930 1075
931 while (($#)); do 1076 while (($#)); do
953} 1098}
954 1099
955# @FUNCTION: python_get_libdir 1100# @FUNCTION: python_get_libdir
956# @USAGE: [-f|--final-ABI] 1101# @USAGE: [-f|--final-ABI]
957# @DESCRIPTION: 1102# @DESCRIPTION:
958# Print Python library directory. 1103# Print path to Python library directory.
959# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used. 1104# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
960python_get_libdir() { 1105python_get_libdir() {
961 local final_ABI="0" 1106 local final_ABI="0"
962 1107
963 while (($#)); do 1108 while (($#)); do
985} 1130}
986 1131
987# @FUNCTION: python_get_sitedir 1132# @FUNCTION: python_get_sitedir
988# @USAGE: [-f|--final-ABI] 1133# @USAGE: [-f|--final-ABI]
989# @DESCRIPTION: 1134# @DESCRIPTION:
990# Print Python site-packages directory. 1135# Print path to Python site-packages directory.
991# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used. 1136# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
992python_get_sitedir() { 1137python_get_sitedir() {
993 local options=() 1138 local options=()
994 1139
995 while (($#)); do 1140 while (($#)); do
1008 done 1153 done
1009 1154
1010 echo "$(python_get_libdir "${options[@]}")/site-packages" 1155 echo "$(python_get_libdir "${options[@]}")/site-packages"
1011} 1156}
1012 1157
1158# @FUNCTION: python_get_library
1159# @USAGE: [-f|--final-ABI] [-l|--linker-option]
1160# @DESCRIPTION:
1161# Print path to Python library.
1162# If --linker-option is specified, then "-l${library}" linker option is printed.
1163# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1164python_get_library() {
1165 local final_ABI="0" linker_option="0" python_version
1166
1167 while (($#)); do
1168 case "$1" in
1169 -f|--final-ABI)
1170 final_ABI="1"
1171 ;;
1172 -l|--linker-option)
1173 linker_option="1"
1174 ;;
1175 -*)
1176 die "${FUNCNAME}(): Unrecognized option '$1'"
1177 ;;
1178 *)
1179 die "${FUNCNAME}(): Invalid usage"
1180 ;;
1181 esac
1182 shift
1183 done
1184
1185 if [[ "${final_ABI}" == "1" ]]; then
1186 python_version="$(PYTHON -f --ABI)"
1187 elif [[ -n "${PYTHON_ABI}" ]]; then
1188 python_version="${PYTHON_ABI}"
1189 else
1190 python_version="$(PYTHON -A --ABI)"
1191 fi
1192
1193 if [[ "${linker_option}" == "1" ]]; then
1194 echo "-lpython${python_version}"
1195 else
1196 echo "/usr/$(get_libdir)/libpython${python_version}$(get_libname)"
1197 fi
1198}
1199
1200# @FUNCTION: python_get_version
1201# @USAGE: [-f|--final-ABI] [--major] [--minor] [--micro]
1202# @DESCRIPTION:
1203# Print Python version.
1204# --major, --minor and --micro options cannot be specified simultaneously.
1205# If --major, --minor and --micro options are not specified, then "${major_version}.${minor_version}" is printed.
1206# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1207python_get_version() {
1208 local final_ABI="0" major="0" minor="0" micro="0" python_command
1209
1210 while (($#)); do
1211 case "$1" in
1212 -f|--final-ABI)
1213 final_ABI="1"
1214 ;;
1215 --major)
1216 major="1"
1217 ;;
1218 --minor)
1219 minor="1"
1220 ;;
1221 --micro)
1222 micro="1"
1223 ;;
1224 -*)
1225 die "${FUNCNAME}(): Unrecognized option '$1'"
1226 ;;
1227 *)
1228 die "${FUNCNAME}(): Invalid usage"
1229 ;;
1230 esac
1231 shift
1232 done
1233
1234 if [[ "$((${major} + ${minor} + ${micro}))" -gt 1 ]]; then
1235 die "${FUNCNAME}(): '--major', '--minor' or '--micro' options cannot be specified simultaneously"
1236 fi
1237
1238 if [[ "${major}" == "1" ]]; then
1239 python_command="from sys import version_info; print(version_info[0])"
1240 elif [[ "${minor}" == "1" ]]; then
1241 python_command="from sys import version_info; print(version_info[1])"
1242 elif [[ "${micro}" == "1" ]]; then
1243 python_command="from sys import version_info; print(version_info[2])"
1244 else
1245 python_command="from sys import version_info; print('.'.join([str(x) for x in version_info[:2]]))"
1246 fi
1247
1248 if [[ "${final_ABI}" == "1" ]]; then
1249 "$(PYTHON -f)" -c "${python_command}"
1250 else
1251 "$(PYTHON "${PYTHON_ABI--A}")" -c "${python_command}"
1252 fi
1253}
1254
1013# ================================================================================================ 1255# ================================================================================================
1014# =================================== MISCELLANEOUS FUNCTIONS ==================================== 1256# =================================== MISCELLANEOUS FUNCTIONS ====================================
1015# ================================================================================================ 1257# ================================================================================================
1016 1258
1017_python_implementation() { 1259_python_implementation() {
1018 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then 1260 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1019 return 0 1261 return 0
1020 else 1262 else
1021 return 1 1263 return 1
1264 fi
1265}
1266
1267_python_initialize_prefix_variables() {
1268 if has "${EAPI:-0}" 0 1 2; then
1269 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
1270 EROOT="${ROOT%/}${EPREFIX}/"
1271 fi
1272 if [[ -n "${D}" && -z "${ED}" ]]; then
1273 ED="${D%/}${EPREFIX}/"
1274 fi
1022 fi 1275 fi
1023} 1276}
1024 1277
1025# @FUNCTION: python_convert_shebangs 1278# @FUNCTION: python_convert_shebangs
1026# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories] 1279# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1039 ;; 1292 ;;
1040 -x|--only-executables) 1293 -x|--only-executables)
1041 only_executables="1" 1294 only_executables="1"
1042 ;; 1295 ;;
1043 --) 1296 --)
1297 shift
1044 break 1298 break
1045 ;; 1299 ;;
1046 -*) 1300 -*)
1047 die "${FUNCNAME}(): Unrecognized option '$1'" 1301 die "${FUNCNAME}(): Unrecognized option '$1'"
1048 ;; 1302 ;;
1099} 1353}
1100 1354
1101# @FUNCTION: python_mod_exists 1355# @FUNCTION: python_mod_exists
1102# @USAGE: <module> 1356# @USAGE: <module>
1103# @DESCRIPTION: 1357# @DESCRIPTION:
1104# Run with the module name as an argument. it will check if a 1358# Run with the module name as an argument. It will check if a
1105# python module is installed and loadable. it will return 1359# Python module is installed and loadable. It will return
1106# TRUE(0) if the module exists, and FALSE(1) if the module does 1360# TRUE(0) if the module exists, and FALSE(1) if the module does
1107# not exist. 1361# not exist.
1108# 1362#
1109# Example: 1363# Example:
1110# if python_mod_exists gtk; then 1364# if python_mod_exists gtk; then
1111# echo "gtk support enabled" 1365# echo "gtk support enabled"
1112# fi 1366# fi
1113python_mod_exists() { 1367python_mod_exists() {
1368 if [[ "$#" -ne 1 ]]; then
1114 [[ "$1" ]] || die "${FUNCNAME} requires an argument!" 1369 die "${FUNCNAME}() requires 1 argument"
1115 python -c "import $1" &>/dev/null 1370 fi
1371 "$(PYTHON "${PYTHON_ABI--A}")" -c "import $1" &> /dev/null
1116} 1372}
1117 1373
1118# @FUNCTION: python_tkinter_exists 1374# @FUNCTION: python_tkinter_exists
1119# @DESCRIPTION: 1375# @DESCRIPTION:
1120# Run without arguments, checks if python was compiled with Tkinter 1376# Run without arguments, checks if Python was compiled with Tkinter
1121# support. If not, prints an error message and dies. 1377# support. If not, prints an error message and dies.
1122python_tkinter_exists() { 1378python_tkinter_exists() {
1123 if ! python -c "import Tkinter" >/dev/null 2>&1; then 1379 if ! "$(PYTHON "${PYTHON_ABI--A}")" -c "from sys import version_info
1124 eerror "You need to recompile python with Tkinter support." 1380if version_info[0] == 3:
1381 import tkinter
1382else:
1383 import Tkinter" &> /dev/null; then
1384 eerror "Python needs to be rebuilt with tkinter support enabled."
1385 eerror "Add the following line to '${EPREFIX}/etc/portage/package.use' and rebuild Python"
1125 eerror "Try adding: 'dev-lang/python tk'" 1386 eerror "dev-lang/python tk"
1126 eerror "in to /etc/portage/package.use" 1387 die "Python installed without support for tkinter"
1127 echo 1388 fi
1128 die "missing tkinter support with installed python" 1389}
1390
1391# ================================================================================================
1392# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1393# ================================================================================================
1394
1395# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
1396# @DESCRIPTION:
1397# User-configurable verbosity of tests of Python modules.
1398# Supported values: 0, 1, 2, 3, 4.
1399PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
1400
1401# @FUNCTION: python_execute_nosetests
1402# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1403# @DESCRIPTION:
1404# Execute nosetests for all enabled versions of Python.
1405python_execute_nosetests() {
1406 local PYTHONPATH_template= separate_build_dirs=
1407
1408 while (($#)); do
1409 case "$1" in
1410 -P|--PYTHONPATH)
1411 PYTHONPATH_template="$2"
1412 shift
1413 ;;
1414 -s|--separate-build-dirs)
1415 separate_build_dirs="1"
1416 ;;
1417 --)
1418 shift
1419 break
1420 ;;
1421 -*)
1422 die "${FUNCNAME}(): Unrecognized option '$1'"
1423 ;;
1424 *)
1425 break
1426 ;;
1427 esac
1428 shift
1429 done
1430
1431 python_test_function() {
1432 local evaluated_PYTHONPATH=
1433
1434 if [[ -n "${PYTHONPATH_template}" ]]; then
1435 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1436 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1437 unset evaluated_PYTHONPATH
1438 fi
1439 fi
1440
1441 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1442 echo PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1443 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1444 else
1445 echo nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1446 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1447 fi
1448 }
1449 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1450 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1451 else
1452 if [[ -n "${separate_build_dirs}" ]]; then
1453 die "${FUNCNAME}(): Invalid usage"
1454 fi
1455 python_test_function "$@"
1456 fi
1457}
1458
1459# @FUNCTION: python_execute_py.test
1460# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1461# @DESCRIPTION:
1462# Execute py.test for all enabled versions of Python.
1463python_execute_py.test() {
1464 local PYTHONPATH_template= separate_build_dirs=
1465
1466 while (($#)); do
1467 case "$1" in
1468 -P|--PYTHONPATH)
1469 PYTHONPATH_template="$2"
1470 shift
1471 ;;
1472 -s|--separate-build-dirs)
1473 separate_build_dirs="1"
1474 ;;
1475 --)
1476 shift
1477 break
1478 ;;
1479 -*)
1480 die "${FUNCNAME}(): Unrecognized option '$1'"
1481 ;;
1482 *)
1483 break
1484 ;;
1485 esac
1486 shift
1487 done
1488
1489 python_test_function() {
1490 local evaluated_PYTHONPATH=
1491
1492 if [[ -n "${PYTHONPATH_template}" ]]; then
1493 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1494 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1495 unset evaluated_PYTHONPATH
1496 fi
1497 fi
1498
1499 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1500 echo PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"
1501 PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"
1502 else
1503 echo py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"
1504 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"
1505 fi
1506 }
1507 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1508 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1509 else
1510 if [[ -n "${separate_build_dirs}" ]]; then
1511 die "${FUNCNAME}(): Invalid usage"
1512 fi
1513 python_test_function "$@"
1514 fi
1515}
1516
1517# @FUNCTION: python_execute_trial
1518# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1519# @DESCRIPTION:
1520# Execute trial for all enabled versions of Python.
1521python_execute_trial() {
1522 local PYTHONPATH_template= separate_build_dirs=
1523
1524 while (($#)); do
1525 case "$1" in
1526 -P|--PYTHONPATH)
1527 PYTHONPATH_template="$2"
1528 shift
1529 ;;
1530 -s|--separate-build-dirs)
1531 separate_build_dirs="1"
1532 ;;
1533 --)
1534 shift
1535 break
1536 ;;
1537 -*)
1538 die "${FUNCNAME}(): Unrecognized option '$1'"
1539 ;;
1540 *)
1541 break
1542 ;;
1543 esac
1544 shift
1545 done
1546
1547 python_test_function() {
1548 local evaluated_PYTHONPATH=
1549
1550 if [[ -n "${PYTHONPATH_template}" ]]; then
1551 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1552 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1553 unset evaluated_PYTHONPATH
1554 fi
1555 fi
1556
1557 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1558 echo PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1559 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1560 else
1561 echo trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1562 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1563 fi
1564 }
1565 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1566 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1567 else
1568 if [[ -n "${separate_build_dirs}" ]]; then
1569 die "${FUNCNAME}(): Invalid usage"
1570 fi
1571 python_test_function "$@"
1129 fi 1572 fi
1130} 1573}
1131 1574
1132# ================================================================================================ 1575# ================================================================================================
1133# ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ======================== 1576# ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
1163# Options passed to this function are passed to compileall.py. 1606# Options passed to this function are passed to compileall.py.
1164# 1607#
1165# Example: 1608# Example:
1166# python_mod_optimize ctypesgencore 1609# python_mod_optimize ctypesgencore
1167python_mod_optimize() { 1610python_mod_optimize() {
1611 _python_initialize_prefix_variables
1612
1168 # Check if phase is pkg_postinst(). 1613 # Check if phase is pkg_postinst().
1169 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()" 1614 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME}() should only be run in pkg_postinst()"
1170 1615
1171 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1616 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1172 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=() 1617 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
1173 1618
1174 # Strip trailing slash from ROOT. 1619 # Strip trailing slash from ROOT.
1175 root="${ROOT%/}" 1620 root="${EROOT%/}"
1176 1621
1177 # Respect ROOT and options passed to compileall.py. 1622 # Respect ROOT and options passed to compileall.py.
1178 while (($#)); do 1623 while (($#)); do
1179 case "$1" in 1624 case "$1" in
1180 -l|-f|-q) 1625 -l|-f|-q)
1183 -d|-x) 1628 -d|-x)
1184 options+=("$1" "$2") 1629 options+=("$1" "$2")
1185 shift 1630 shift
1186 ;; 1631 ;;
1187 -*) 1632 -*)
1188 ewarn "${FUNCNAME}: Ignoring option '$1'" 1633 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1189 ;; 1634 ;;
1190 *) 1635 *)
1191 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1636 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1192 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories" 1637 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1193 elif [[ "$1" =~ ^/ ]]; then 1638 elif [[ "$1" =~ ^/ ]]; then
1194 if [[ -d "${root}/$1" ]]; then 1639 if [[ -d "${root}/$1" ]]; then
1195 other_dirs+=("${root}/$1") 1640 other_dirs+=("${root}/$1")
1196 elif [[ -f "${root}/$1" ]]; then 1641 elif [[ -f "${root}/$1" ]]; then
1197 other_files+=("${root}/$1") 1642 other_files+=("${root}/$1")
1268 fi 1713 fi
1269 else 1714 else
1270 local myroot mydirs=() myfiles=() myopts=() return_code="0" 1715 local myroot mydirs=() myfiles=() myopts=() return_code="0"
1271 1716
1272 # strip trailing slash 1717 # strip trailing slash
1273 myroot="${ROOT%/}" 1718 myroot="${EROOT%/}"
1274 1719
1275 # respect ROOT and options passed to compileall.py 1720 # respect ROOT and options passed to compileall.py
1276 while (($#)); do 1721 while (($#)); do
1277 case "$1" in 1722 case "$1" in
1278 -l|-f|-q) 1723 -l|-f|-q)
1281 -d|-x) 1726 -d|-x)
1282 myopts+=("$1" "$2") 1727 myopts+=("$1" "$2")
1283 shift 1728 shift
1284 ;; 1729 ;;
1285 -*) 1730 -*)
1286 ewarn "${FUNCNAME}: Ignoring option '$1'" 1731 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1287 ;; 1732 ;;
1288 *) 1733 *)
1289 if [[ -d "${myroot}"/$1 ]]; then 1734 if [[ -d "${myroot}"/$1 ]]; then
1290 mydirs+=("${myroot}/$1") 1735 mydirs+=("${myroot}/$1")
1291 elif [[ -f "${myroot}"/$1 ]]; then 1736 elif [[ -f "${myroot}"/$1 ]]; then
1328# determine if they are orphaned (i.e. their corresponding .py files are missing.) 1773# determine if they are orphaned (i.e. their corresponding .py files are missing.)
1329# If they are, then it will remove their corresponding .pyc and .pyo files. 1774# If they are, then it will remove their corresponding .pyc and .pyo files.
1330# 1775#
1331# This function should only be run in pkg_postrm(). 1776# This function should only be run in pkg_postrm().
1332python_mod_cleanup() { 1777python_mod_cleanup() {
1778 _python_initialize_prefix_variables
1779
1333 local path py_file PYTHON_ABI SEARCH_PATH=() root 1780 local path py_file PYTHON_ABI SEARCH_PATH=() root
1334 1781
1335 # Check if phase is pkg_postrm(). 1782 # Check if phase is pkg_postrm().
1336 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" 1783 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME}() should only be run in pkg_postrm()"
1337 1784
1338 # Strip trailing slash from ROOT. 1785 # Strip trailing slash from ROOT.
1339 root="${ROOT%/}" 1786 root="${EROOT%/}"
1340 1787
1341 if (($#)); then 1788 if (($#)); then
1342 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1789 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1343 while (($#)); do 1790 while (($#)); do
1344 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1791 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1345 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories" 1792 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1346 elif [[ "$1" =~ ^/ ]]; then 1793 elif [[ "$1" =~ ^/ ]]; then
1347 SEARCH_PATH+=("${root}/${1#/}") 1794 SEARCH_PATH+=("${root}/${1#/}")
1348 else 1795 else
1349 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1796 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1350 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 1797 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
1407# @FUNCTION: python_version 1854# @FUNCTION: python_version
1408# @DESCRIPTION: 1855# @DESCRIPTION:
1409# Run without arguments and it will export the version of python 1856# Run without arguments and it will export the version of python
1410# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 1857# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
1411python_version() { 1858python_version() {
1859 if ! has "${EAPI:-0}" 0 1 2; then
1860 eerror "Use PYTHON() and/or python_get_*() instead of python_version()."
1861 die "${FUNCNAME}() cannot be used in this EAPI"
1862 fi
1863
1412 [[ -n "${PYVER}" ]] && return 0 1864 [[ -n "${PYVER}" ]] && return 0
1413 local tmpstr 1865 local tmpstr
1414 python="${python:-/usr/bin/python}" 1866 python="${python:-${EPREFIX}/usr/bin/python}"
1415 tmpstr="$(EPYTHON= ${python} -V 2>&1 )" 1867 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
1416 export PYVER_ALL="${tmpstr#Python }" 1868 export PYVER_ALL="${tmpstr#Python }"
1417 export PYVER_MAJOR="${PYVER_ALL:0:1}" 1869 export PYVER_MAJOR="${PYVER_ALL:0:1}"
1418 export PYVER_MINOR="${PYVER_ALL:2:1}" 1870 export PYVER_MINOR="${PYVER_ALL:2:1}"
1419 if [[ "${PYVER_ALL:3:1}" == "." ]]; then 1871 if [[ "${PYVER_ALL:3:1}" == "." ]]; then
1431# Example: 1883# Example:
1432# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 1884# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
1433# 1885#
1434python_mod_compile() { 1886python_mod_compile() {
1435 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1887 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1888 eerror "Use python_mod_optimize() instead of python_mod_compile()."
1436 die "${FUNCNAME}() cannot be used in this EAPI" 1889 die "${FUNCNAME}() cannot be used in this EAPI"
1437 fi 1890 fi
1438 1891
1892 _python_initialize_prefix_variables
1893
1439 local f myroot myfiles=() 1894 local f myroot myfiles=()
1440 1895
1441 # Check if phase is pkg_postinst() 1896 # Check if phase is pkg_postinst()
1442 [[ ${EBUILD_PHASE} != postinst ]] &&\ 1897 [[ ${EBUILD_PHASE} != postinst ]] && die "${FUNCNAME}() should only be run in pkg_postinst()"
1443 die "${FUNCNAME} should only be run in pkg_postinst()"
1444 1898
1445 # strip trailing slash 1899 # strip trailing slash
1446 myroot="${ROOT%/}" 1900 myroot="${EROOT%/}"
1447 1901
1448 # respect ROOT 1902 # respect ROOT
1449 for f in "$@"; do 1903 for f in "$@"; do
1450 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") 1904 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
1451 done 1905 done

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

  ViewVC Help
Powered by ViewVC 1.1.20