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

Diff of /eclass/python.eclass

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.20