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

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

  ViewVC Help
Powered by ViewVC 1.1.20