/[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.87
1# Copyright 1999-2010 Gentoo Foundation 1# Copyright 1999-2010 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.84 2010/01/11 16:07:23 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.87 2010/02/02 18:55:00 arfrever Exp $
4 4
5# @ECLASS: python.eclass 5# @ECLASS: python.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# python@gentoo.org 7# python@gentoo.org
8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules. 8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules.
9# @DESCRIPTION: 9# @DESCRIPTION:
10# Some useful functions for dealing with Python. 10# Some useful functions for dealing with Python.
11 11
12inherit multilib 12inherit multilib
13 13
14if ! has "${EAPI:-0}" 0 1 2; then 14if ! has "${EAPI:-0}" 0 1 2 3; then
15 die "API of python.eclass in EAPI=\"${EAPI}\" not established" 15 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
16fi 16fi
17 17
18_PYTHON2_SUPPORTED_VERSIONS=(2.4 2.5 2.6 2.7)
19_PYTHON3_SUPPORTED_VERSIONS=(3.0 3.1 3.2)
20
21# @ECLASS-VARIABLE: PYTHON_DEPEND
22# @DESCRIPTION:
23# Specification of dependency on dev-lang/python.
24# Syntax:
25# PYTHON_DEPEND: [[!]USE_flag? ]<version_components_group>[ version_components_group]
26# version_components_group: <major_version[:[minimal_version][:maximal_version]]>
27# major_version: <2|3|*>
28# minimal_version: <minimal_major_version.minimal_minor_version>
29# maximal_version: <maximal_major_version.maximal_minor_version>
30
31_parse_PYTHON_DEPEND() {
32 local accepted_version accepted_versions=() major_version maximal_version minimal_version python_all="0" python_atoms= python_maximal_version python_minimal_version python_versions=() python2="0" python2_maximal_version python2_minimal_version python3="0" python3_maximal_version python3_minimal_version USE_flag= version_components_group version_components_group_regex version_components_groups
33
34 version_components_group_regex="(2|3|\*)(:([[:digit:]]+\.[[:digit:]]+)?(:([[:digit:]]+\.[[:digit:]]+)?)?)?"
35 version_components_groups="${PYTHON_DEPEND}"
36
37 if [[ "${version_components_groups}" =~ ^((\!)?[[:alnum:]_-]+\?\ )?${version_components_group_regex}(\ ${version_components_group_regex})?$ ]]; then
38 if [[ "${version_components_groups}" =~ ^(\!)?[[:alnum:]_-]+\? ]]; then
39 USE_flag="${version_components_groups%\? *}"
40 version_components_groups="${version_components_groups#* }"
41 fi
42 if [[ "${version_components_groups}" =~ ("*".*" "|" *"|^2.*\ (2|\*)|^3.*\ (3|\*)) ]]; then
43 die "Invalid syntax of PYTHON_DEPEND: Incorrectly specified groups of versions"
44 fi
45
46 version_components_groups="${version_components_groups// /$'\n'}"
47 while read version_components_group; do
48 major_version="${version_components_group:0:1}"
49 minimal_version="${version_components_group:2}"
50 minimal_version="${minimal_version%:*}"
51 maximal_version="${version_components_group:$((3 + ${#minimal_version}))}"
52
53 if [[ "${major_version}" =~ ^(2|3)$ ]]; then
54 if [[ -n "${minimal_version}" && "${major_version}" != "${minimal_version:0:1}" ]]; then
55 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' not in specified group of versions"
56 fi
57 if [[ -n "${maximal_version}" && "${major_version}" != "${maximal_version:0:1}" ]]; then
58 die "Invalid syntax of PYTHON_DEPEND: Maximal version '${maximal_version}' not in specified group of versions"
59 fi
60 fi
61
62 if [[ "${major_version}" == "2" ]]; then
63 python2="1"
64 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}")
65 python2_minimal_version="${minimal_version}"
66 python2_maximal_version="${maximal_version}"
67 elif [[ "${major_version}" == "3" ]]; then
68 python3="1"
69 python_versions=("${_PYTHON3_SUPPORTED_VERSIONS[@]}")
70 python3_minimal_version="${minimal_version}"
71 python3_maximal_version="${maximal_version}"
72 else
73 python_all="1"
74 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}")
75 python_minimal_version="${minimal_version}"
76 python_maximal_version="${maximal_version}"
77 fi
78
79 if [[ -n "${minimal_version}" ]] && ! has "${minimal_version}" "${python_versions[@]}"; then
80 die "Invalid syntax of PYTHON_DEPEND: Unrecognized minimal version '${minimal_version}'"
81 fi
82 if [[ -n "${maximal_version}" ]] && ! has "${maximal_version}" "${python_versions[@]}"; then
83 die "Invalid syntax of PYTHON_DEPEND: Unrecognized maximal version '${maximal_version}'"
84 fi
85
86 if [[ -n "${minimal_version}" && -n "${maximal_version}" && "${minimal_version}" > "${maximal_version}" ]]; then
87 die "Invalid syntax of PYTHON_DEPEND: Minimal version '${minimal_version}' greater than maximal version '${maximal_version}'"
88 fi
89 done <<< "${version_components_groups}"
90
91 _create_accepted_versions_range() {
92 local accepted_version="0" i
93 for ((i = "${#python_versions[@]}"; i >= 0; i--)); do
94 if [[ "${python_versions[${i}]}" == "${python_maximal_version}" ]]; then
95 accepted_version="1"
96 fi
97 if [[ "${accepted_version}" == "1" ]]; then
98 accepted_versions+=("${python_versions[${i}]}")
99 fi
100 if [[ "${python_versions[${i}]}" == "${python_minimal_version}" ]]; then
101 accepted_version="0"
102 fi
103 done
104 }
105
106 if [[ "${python_all}" == "1" ]]; then
107 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}")
108 python_minimal_version="${python_minimal_version:-${python_versions[0]}}"
109 python_maximal_version="${python_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
110 _create_accepted_versions_range
111 else
112 if [[ "${python3}" == "1" ]]; then
113 python_versions=("${_PYTHON3_SUPPORTED_VERSIONS[@]}")
114 python_minimal_version="${python3_minimal_version:-${python_versions[0]}}"
115 python_maximal_version="${python3_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
116 _create_accepted_versions_range
117 fi
118 if [[ "${python2}" == "1" ]]; then
119 python_versions=("${_PYTHON2_SUPPORTED_VERSIONS[@]}")
120 python_minimal_version="${python2_minimal_version:-${python_versions[0]}}"
121 python_maximal_version="${python2_maximal_version:-${python_versions[${#python_versions[@]}-1]}}"
122 _create_accepted_versions_range
123 fi
124 fi
125
126 for accepted_version in "${accepted_versions[@]}"; do
127 python_atoms+="${python_atoms:+ }=dev-lang/python-${accepted_version}*"
128 done
129
130 _PYTHON_ATOMS="${python_atoms// /$'\n'}"
131 DEPEND+="${DEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${python_atoms} )${USE_flag:+ )}"
132 RDEPEND+="${RDEPEND:+ }${USE_flag}${USE_flag:+? ( }|| ( ${python_atoms} )${USE_flag:+ )}"
133 else
134 die "Invalid syntax of PYTHON_DEPEND"
135 fi
136}
137
138DEPEND=">=app-admin/eselect-python-20090804"
139RDEPEND="${DEPEND}"
140
141if [[ -n "${PYTHON_DEPEND}" && -n "${NEED_PYTHON}" ]]; then
142 die "PYTHON_DEPEND and NEED_PYTHON cannot be set simultaneously"
143elif [[ -n "${PYTHON_DEPEND}" ]]; then
144 _parse_PYTHON_DEPEND
18if [[ -n "${NEED_PYTHON}" ]]; then 145elif [[ -n "${NEED_PYTHON}" ]]; then
146 if ! has "${EAPI:-0}" 0 1 2; then
147 eerror "Use PYTHON_DEPEND instead of NEED_PYTHON."
148 die "NEED_PYTHON cannot be used in this EAPI"
149 fi
19 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" 150 _PYTHON_ATOMS=">=dev-lang/python-${NEED_PYTHON}"
20 DEPEND="${PYTHON_ATOM}" 151 DEPEND+="${DEPEND:+ }${_PYTHON_ATOMS}"
21 RDEPEND="${DEPEND}" 152 RDEPEND+="${RDEPEND:+ }${_PYTHON_ATOMS}"
22else 153else
23 PYTHON_ATOM="dev-lang/python" 154 _PYTHON_ATOMS="dev-lang/python"
24fi 155fi
25 156
26DEPEND+=" >=app-admin/eselect-python-20090804" 157# @ECLASS-VARIABLE: PYTHON_USE_WITH
158# @DESCRIPTION:
159# Set this to a space separated list of USE flags the Python slot in use must be built with.
27 160
28__python_eclass_test() { 161# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
29 __python_version_extract 2.3 162# @DESCRIPTION:
30 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 163# 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 164
165# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
166# @DESCRIPTION:
167# Set this to a name of a USE flag if you need to make either PYTHON_USE_WITH or
168# PYTHON_USE_WITH_OR atoms conditional under a USE flag.
169
46# @FUNCTION: python_version 170# @FUNCTION: python_pkg_setup
47# @DESCRIPTION: 171# @DESCRIPTION:
48# Run without arguments and it will export the version of python 172# 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 173# are respected. Only exported if one of those variables is set.
50__python_version_extract() { 174if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
51 local verstr=$1 175 python_pkg_setup() {
52 export PYVER_MAJOR=${verstr:0:1} 176 python_pkg_setup_fail() {
53 export PYVER_MINOR=${verstr:2:1} 177 eerror "${1}"
54 if [[ ${verstr:3:1} == . ]]; then 178 die "${1}"
55 export PYVER_MICRO=${verstr:4} 179 }
56 fi
57 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
58}
59 180
60python_version() { 181 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
61 [[ -n "${PYVER}" ]] && return 0
62 local tmpstr
63 python=${python:-/usr/bin/python}
64 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
65 export PYVER_ALL="${tmpstr#Python }"
66 __python_version_extract $PYVER_ALL
67}
68 182
69# @FUNCTION: PYTHON 183 python_pkg_setup_check_USE_flags() {
70# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}"> 184 local pyatom use
71# @DESCRIPTION:
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
132 slot="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
133 elif [[ "${final_ABI}" == "1" ]]; then
134 validate_PYTHON_ABIS
135 slot="${PYTHON_ABIS##* }"
136 elif [[ "${python2}" == "1" ]]; then
137 slot="$(eselect python show --python2)"
138 if [[ -z "${slot}" ]]; then
139 die "${FUNCNAME}(): Active Python 2 interpreter not set"
140 elif [[ "${slot}" != "python2."* ]]; then
141 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
142 fi
143 slot="${slot#python}"
144 elif [[ "${python3}" == "1" ]]; then
145 slot="$(eselect python show --python3)"
146 if [[ -z "${slot}" ]]; then
147 die "${FUNCNAME}(): Active Python 3 interpreter not set"
148 elif [[ "${slot}" != "python3."* ]]; then
149 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
150 fi
151 slot="${slot#python}"
152 elif [[ -n "${PYTHON_ABI}" ]]; then 185 if [[ -n "${PYTHON_ABI}" ]]; then
153 slot="${PYTHON_ABI}" 186 pyatom="dev-lang/python:${PYTHON_ABI}"
187 else
188 pyatom="dev-lang/python:$(PYTHON -A --ABI)"
189 fi
190
191 for use in ${PYTHON_USE_WITH}; do
192 if ! has_version "${pyatom}[${use}]"; then
193 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
194 fi
195 done
196
197 for use in ${PYTHON_USE_WITH_OR}; do
198 if has_version "${pyatom}[${use}]"; then
199 return
200 fi
201 done
202
203 if [[ ${PYTHON_USE_WITH_OR} ]]; then
204 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
205 fi
206 }
207
208 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
209 python_execute_function -q python_pkg_setup_check_USE_flags
154 else 210 else
155 die "${FUNCNAME}(): Invalid usage" 211 python_pkg_setup_check_USE_flags
156 fi
157 elif [[ "$#" -eq 1 ]]; then
158 if [[ "${active}" == "1" ]]; then
159 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
160 fi
161 if [[ "${final_ABI}" == "1" ]]; then
162 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
163 fi
164 if [[ "${python2}" == "1" ]]; then
165 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
166 fi
167 if [[ "${python3}" == "1" ]]; then
168 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
169 fi
170 slot="$1"
171 else
172 die "${FUNCNAME}(): Invalid usage"
173 fi 212 fi
213 }
174 214
175 if [[ "${ABI_output}" == "1" ]]; then 215 EXPORT_FUNCTIONS pkg_setup
176 echo -n "${slot}"
177 return
178 elif [[ "${absolute_path_output}" == "1" ]]; then
179 echo -n "/usr/bin/python${slot}"
180 else
181 echo -n "python${slot}"
182 fi
183 216
184 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then 217 _PYTHON_USE_WITH_ATOM=""
185 echo -n "-${ABI}" 218 if [[ -n "${PYTHON_USE_WITH}" ]]; then
186 fi 219 while read _PYTHON_ATOM; do
187} 220 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
188 221 done <<< "${_PYTHON_ATOMS}"
189_python_implementation() { 222 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
190 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then 223 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
191 return 0 224 for _USE_flag in ${PYTHON_USE_WITH_OR}; do
192 else 225 while read _PYTHON_ATOM; do
193 return 1 226 _PYTHON_USE_WITH_ATOM+="${_PYTHON_USE_WITH_ATOM:+ }${_PYTHON_ATOM}[${_USE_flag}]"
194 fi 227 done <<< "${_PYTHON_ATOMS}"
195} 228 done
196 229 unset _USE_flag
197# @FUNCTION: python_set_active_version 230 _PYTHON_USE_WITH_ATOM="|| ( ${_PYTHON_USE_WITH_ATOM} )"
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 231 fi
210 export EPYTHON="$(PYTHON "$1")" 232 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
211 elif [[ "$1" == "2" ]]; then 233 _PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${_PYTHON_USE_WITH_ATOM} )"
212 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
213 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
214 fi 234 fi
215 export EPYTHON="$(PYTHON -2)" 235 DEPEND+=" ${_PYTHON_USE_WITH_ATOM}"
216 elif [[ "$1" == "3" ]]; then 236 RDEPEND+=" ${_PYTHON_USE_WITH_ATOM}"
217 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then 237 unset _PYTHON_ATOM _PYTHON_USE_WITH_ATOM
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 238fi
224 239
225 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses, 240unset _PYTHON_ATOMS
226 # so it does not need to be exported to subprocesses. 241
227 PYTHON_ABI="${EPYTHON#python}" 242# ================================================================================================
228 PYTHON_ABI="${PYTHON_ABI%%-*}" 243# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ========
229} 244# ================================================================================================
245
246# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
247# @DESCRIPTION:
248# Set this in EAPI <= 4 to indicate that given package supports installation for
249# multiple versions of Python.
250
251# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
252# @DESCRIPTION:
253# Set this to define default functions for the following ebuild phases:
254# src_prepare, src_configure, src_compile, src_test, src_install.
255if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
256 python_src_prepare() {
257 python_copy_sources
258 }
259
260 for python_default_function in src_configure src_compile src_test src_install; do
261 eval "python_${python_default_function}() { python_execute_function -d -s; }"
262 done
263 unset python_default_function
264
265 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
266fi
230 267
231unset PYTHON_ABIS 268unset PYTHON_ABIS
232unset PYTHON_ABIS_SANITY_CHECKS 269unset PYTHON_ABIS_SANITY_CHECKS
233 270
234# @FUNCTION: validate_PYTHON_ABIS 271# @FUNCTION: validate_PYTHON_ABIS
235# @DESCRIPTION: 272# @DESCRIPTION:
236# Ensure that PYTHON_ABIS variable has valid value. 273# Ensure that PYTHON_ABIS variable has valid value.
274# This function usually should not be directly called in ebuilds.
237validate_PYTHON_ABIS() { 275validate_PYTHON_ABIS() {
238 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable. 276 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable.
239 if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 277 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
240 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable" 278 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
241 fi 279 fi
242 280
243 # Ensure that /usr/bin/python and /usr/bin/python-config are valid. 281 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
244 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then 282 if [[ "$(readlink "${EPREFIX}/usr/bin/python")" != "python-wrapper" ]]; then
245 eerror "'/usr/bin/python' is not valid symlink." 283 eerror "'${EPREFIX}/usr/bin/python' is not valid symlink."
246 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem." 284 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
247 die "'/usr/bin/python' is not valid symlink" 285 die "'${EPREFIX}/usr/bin/python' is not valid symlink"
248 fi 286 fi
249 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then 287 if [[ "$(<"${EPREFIX}/usr/bin/python-config")" != *"Gentoo python-config wrapper script"* ]]; then
250 eerror "'/usr/bin/python-config' is not valid script" 288 eerror "'${EPREFIX}/usr/bin/python-config' is not valid script"
251 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem." 289 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
252 die "'/usr/bin/python-config' is not valid script" 290 die "'${EPREFIX}/usr/bin/python-config' is not valid script"
253 fi 291 fi
254 292
255 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5. 293 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
256 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then 294 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
257 local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS= 295 local PYTHON_ABI restricted_ABI support_ABI supported_PYTHON_ABIS=
258 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" 296 PYTHON_ABI_SUPPORTED_VALUES="${_PYTHON2_SUPPORTED_VERSIONS[@]} ${_PYTHON3_SUPPORTED_VERSIONS[@]}"
259 python2_supported_versions="2.4 2.5 2.6 2.7"
260 python3_supported_versions="3.0 3.1 3.2"
261 297
262 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then 298 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
263 local python2_enabled="0" python3_enabled="0" 299 local python2_enabled="0" python3_enabled="0"
264 300
265 if [[ -z "${USE_PYTHON}" ]]; then 301 if [[ -z "${USE_PYTHON}" ]]; then
269 for PYTHON_ABI in ${USE_PYTHON}; do 305 for PYTHON_ABI in ${USE_PYTHON}; do
270 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 306 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
271 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'" 307 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
272 fi 308 fi
273 309
274 if has "${PYTHON_ABI}" ${python2_supported_versions}; then 310 if has "${PYTHON_ABI}" "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; then
275 python2_enabled="1" 311 python2_enabled="1"
276 fi 312 fi
277 if has "${PYTHON_ABI}" ${python3_supported_versions}; then 313 if has "${PYTHON_ABI}" "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; then
278 python3_enabled="1" 314 python3_enabled="1"
279 fi 315 fi
280 316
281 support_ABI="1" 317 support_ABI="1"
282 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 318 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
299 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported." 335 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported."
300 fi 336 fi
301 else 337 else
302 local python_version python2_version= python3_version= support_python_major_version 338 local python_version python2_version= python3_version= support_python_major_version
303 339
304 python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" 340 python_version="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
305 341
306 if has_version "=dev-lang/python-2*"; then 342 if has_version "=dev-lang/python-2*"; then
307 if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then 343 if [[ "$(readlink "${EPREFIX}/usr/bin/python2")" != "python2."* ]]; then
308 die "'/usr/bin/python2' is not valid symlink" 344 die "'${EPREFIX}/usr/bin/python2' is not valid symlink"
309 fi 345 fi
310 346
311 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" 347 python2_version="$("${EPREFIX}/usr/bin/python2" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
312 348
313 for PYTHON_ABI in ${python2_supported_versions}; do 349 for PYTHON_ABI in "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; do
314 support_python_major_version="1" 350 support_python_major_version="1"
315 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 351 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
316 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then 352 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
317 support_python_major_version="0" 353 support_python_major_version="0"
318 fi 354 fi
329 python2_version="" 365 python2_version=""
330 fi 366 fi
331 fi 367 fi
332 368
333 if has_version "=dev-lang/python-3*"; then 369 if has_version "=dev-lang/python-3*"; then
334 if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then 370 if [[ "$(readlink "${EPREFIX}/usr/bin/python3")" != "python3."* ]]; then
335 die "'/usr/bin/python3' is not valid symlink" 371 die "'${EPREFIX}/usr/bin/python3' is not valid symlink"
336 fi 372 fi
337 373
338 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" 374 python3_version="$("${EPREFIX}/usr/bin/python3" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
339 375
340 for PYTHON_ABI in ${python3_supported_versions}; do 376 for PYTHON_ABI in "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; do
341 support_python_major_version="1" 377 support_python_major_version="1"
342 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 378 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
343 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then 379 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
344 support_python_major_version="0" 380 support_python_major_version="0"
345 fi 381 fi
356 python3_version="" 392 python3_version=""
357 fi 393 fi
358 fi 394 fi
359 395
360 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then 396 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
361 eerror "Python wrapper is configured incorrectly or /usr/bin/python2 symlink" 397 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python2' symlink"
362 eerror "is set incorrectly. Use \`eselect python\` to fix configuration." 398 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
363 die "Incorrect configuration of Python" 399 die "Incorrect configuration of Python"
364 fi 400 fi
365 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then 401 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
366 eerror "Python wrapper is configured incorrectly or /usr/bin/python3 symlink" 402 eerror "Python wrapper is configured incorrectly or '${EPREFIX}/usr/bin/python3' symlink"
367 eerror "is set incorrectly. Use \`eselect python\` to fix configuration." 403 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
368 die "Incorrect configuration of Python" 404 die "Incorrect configuration of Python"
369 fi 405 fi
370 406
371 PYTHON_ABIS="${python2_version} ${python3_version}" 407 PYTHON_ABIS="${python2_version} ${python3_version}"
395 done 431 done
396 PYTHON_ABIS_SANITY_CHECKS="1" 432 PYTHON_ABIS_SANITY_CHECKS="1"
397 fi 433 fi
398} 434}
399 435
400# @FUNCTION: python_copy_sources
401# @USAGE: [--no-link] [--] [directory]
402# @DESCRIPTION:
403# Copy unpacked sources of given package for each Python ABI.
404python_copy_sources() {
405 local dir dirs=() no_link="0" PYTHON_ABI
406
407 while (($#)); do
408 case "$1" in
409 --no-link)
410 no_link="1"
411 ;;
412 --)
413 break
414 ;;
415 -*)
416 die "${FUNCNAME}(): Unrecognized option '$1'"
417 ;;
418 *)
419 break
420 ;;
421 esac
422 shift
423 done
424
425 if [[ "$#" -eq 0 ]]; then
426 if [[ "${WORKDIR}" == "${S}" ]]; then
427 die "${FUNCNAME}() cannot be used"
428 fi
429 dirs="${S}"
430 else
431 dirs="$@"
432 fi
433
434 validate_PYTHON_ABIS
435 for PYTHON_ABI in ${PYTHON_ABIS}; do
436 for dir in "${dirs[@]}"; do
437 if [[ "${no_link}" == "1" ]]; then
438 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
439 else
440 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
441 fi
442 done
443 done
444}
445
446# @FUNCTION: python_set_build_dir_symlink
447# @USAGE: [directory="build"]
448# @DESCRIPTION:
449# Create build directory symlink.
450python_set_build_dir_symlink() {
451 local dir="$1"
452
453 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
454 [[ -z "${dir}" ]] && dir="build"
455
456 # Do not delete preexistent directories.
457 rm -f "${dir}" || die "Deletion of '${dir}' failed"
458 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
459}
460
461# @FUNCTION: python_execute_function 436# @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] 437# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
463# @DESCRIPTION: 438# @DESCRIPTION:
464# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 439# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
465# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 440# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
466python_execute_function() { 441python_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= 442 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function i nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
468 443
469 while (($#)); do 444 while (($#)); do
470 case "$1" in 445 case "$1" in
471 --action-message) 446 --action-message)
472 action_message_template="$2" 447 action_message_template="$2"
491 --source-dir) 466 --source-dir)
492 source_dir="$2" 467 source_dir="$2"
493 shift 468 shift
494 ;; 469 ;;
495 --) 470 --)
471 shift
496 break 472 break
497 ;; 473 ;;
498 -*) 474 -*)
499 die "${FUNCNAME}(): Unrecognized option '$1'" 475 die "${FUNCNAME}(): Unrecognized option '$1'"
500 ;; 476 ;;
518 494
519 if [[ -z "$(type -t "${function}")" ]]; then 495 if [[ -z "$(type -t "${function}")" ]]; then
520 die "${FUNCNAME}(): '${function}' function is not defined" 496 die "${FUNCNAME}(): '${function}' function is not defined"
521 fi 497 fi
522 else 498 else
523 if [[ "$#" -ne "0" ]]; then 499 if [[ "$#" -ne 0 ]]; then
524 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously" 500 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
525 fi 501 fi
526 if has "${EAPI:-0}" 0 1; then 502 if has "${EAPI:-0}" 0 1; then
527 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI" 503 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
528 fi 504 fi
556 else 532 else
557 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase" 533 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
558 fi 534 fi
559 function="python_default_function" 535 function="python_default_function"
560 fi 536 fi
537
538 for ((i = 1; i < "${#FUNCNAME[@]}"; i++)); do
539 if [[ "${FUNCNAME[${i}]}" == "python_execute_function" ]]; then
540 die "${FUNCNAME}(): Invalid call stack"
541 fi
542 done
561 543
562 if [[ "${quiet}" == "0" ]]; then 544 if [[ "${quiet}" == "0" ]]; then
563 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up" 545 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
564 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking" 546 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
565 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation" 547 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
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: [--no-link] [--] [directory]
685# @DESCRIPTION: 667# @DESCRIPTION:
686# Convert shebangs in specified files. Directories can be specified only with --recursive option. 668# Copy unpacked sources of given 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 local dir dirs=() no_link="0" PYTHON_ABI
689 671
690 while (($#)); do 672 while (($#)); do
691 case "$1" in 673 case "$1" in
692 -r|--recursive) 674 --no-link)
693 recursive="1" 675 no_link="1"
694 ;;
695 -q|--quiet)
696 quiet="1"
697 ;;
698 -x|--only-executables)
699 only_executables="1"
700 ;; 676 ;;
701 --) 677 --)
678 shift
702 break 679 break
703 ;; 680 ;;
704 -*) 681 -*)
705 die "${FUNCNAME}(): Unrecognized option '$1'" 682 die "${FUNCNAME}(): Unrecognized option '$1'"
706 ;; 683 ;;
710 esac 687 esac
711 shift 688 shift
712 done 689 done
713 690
714 if [[ "$#" -eq 0 ]]; then 691 if [[ "$#" -eq 0 ]]; then
715 die "${FUNCNAME}(): Missing Python version and files or directories" 692 if [[ "${WORKDIR}" == "${S}" ]]; then
716 elif [[ "$#" -eq 1 ]]; then 693 die "${FUNCNAME}() cannot be used"
717 die "${FUNCNAME}(): Missing files or directories"
718 fi 694 fi
695 dirs="${S}"
696 else
697 dirs="$@"
698 fi
719 699
720 python_version="$1" 700 validate_PYTHON_ABIS
721 shift 701 for PYTHON_ABI in ${PYTHON_ABIS}; do
722 702 for dir in "${dirs[@]}"; do
723 for argument in "$@"; do
724 if [[ ! -e "${argument}" ]]; then
725 die "${FUNCNAME}(): '${argument}' does not exist"
726 elif [[ -f "${argument}" ]]; then
727 files+=("${argument}")
728 elif [[ -d "${argument}" ]]; then
729 if [[ "${recursive}" == "1" ]]; then 703 if [[ "${no_link}" == "1" ]]; then
730 if [[ "${only_executables}" == "1" ]]; then 704 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
731 files+=($(find "${argument}" -perm /111 -type f))
732 else
733 files+=($(find "${argument}" -type f))
734 fi
735 else 705 else
736 die "${FUNCNAME}(): '${argument}' is not a regular file" 706 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
737 fi
738 else
739 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
740 fi 707 fi
741 done 708 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 709 done
710}
711
712# @FUNCTION: python_set_build_dir_symlink
713# @USAGE: [directory="build"]
714# @DESCRIPTION:
715# Create build directory symlink.
716python_set_build_dir_symlink() {
717 local dir="$1"
718
719 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
720 [[ -z "${dir}" ]] && dir="build"
721
722 # Do not delete preexistent directories.
723 rm -f "${dir}" || die "Deletion of '${dir}' failed"
724 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
757} 725}
758 726
759# @FUNCTION: python_generate_wrapper_scripts 727# @FUNCTION: python_generate_wrapper_scripts
760# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files] 728# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
761# @DESCRIPTION: 729# @DESCRIPTION:
762# Generate wrapper scripts. Existing files are overwritten only with --force option. 730# Generate wrapper scripts. Existing files are overwritten only with --force option.
763# If --respect-EPYTHON option is specified, then generated wrapper scripts will 731# If --respect-EPYTHON option is specified, then generated wrapper scripts will
764# respect EPYTHON variable at run time. 732# respect EPYTHON variable at run time.
765python_generate_wrapper_scripts() { 733python_generate_wrapper_scripts() {
734 _python_initialize_prefix_variables
735
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" 736 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 737
770 while (($#)); do 738 while (($#)); do
771 case "$1" in 739 case "$1" in
772 -E|--respect-EPYTHON) 740 -E|--respect-EPYTHON)
773 respect_EPYTHON="1" 741 respect_EPYTHON="1"
777 ;; 745 ;;
778 -q|--quiet) 746 -q|--quiet)
779 quiet="1" 747 quiet="1"
780 ;; 748 ;;
781 --) 749 --)
750 shift
782 break 751 break
783 ;; 752 ;;
784 -*) 753 -*)
785 die "${FUNCNAME}(): Unrecognized option '$1'" 754 die "${FUNCNAME}(): Unrecognized option '$1'"
786 ;; 755 ;;
794 if [[ "$#" -eq 0 ]]; then 763 if [[ "$#" -eq 0 ]]; then
795 die "${FUNCNAME}(): Missing arguments" 764 die "${FUNCNAME}(): Missing arguments"
796 fi 765 fi
797 766
798 validate_PYTHON_ABIS 767 validate_PYTHON_ABIS
799 for PYTHON_ABI in ${python2_supported_versions}; do 768 for PYTHON_ABI in "${_PYTHON2_SUPPORTED_VERSIONS[@]}"; do
800 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then 769 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
801 python2_enabled="1" 770 python2_enabled="1"
802 fi 771 fi
803 done 772 done
804 for PYTHON_ABI in ${python3_supported_versions}; do 773 for PYTHON_ABI in "${_PYTHON3_SUPPORTED_VERSIONS[@]}"; do
805 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then 774 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
806 python3_enabled="1" 775 python3_enabled="1"
807 fi 776 fi
808 done 777 done
809 778
851 else: 820 else:
852 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON) 821 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
853 sys.exit(1) 822 sys.exit(1)
854else: 823else:
855 try: 824 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) 825 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
857 if eselect_process.wait() != 0: 826 if eselect_process.wait() != 0:
858 raise ValueError 827 raise ValueError
859 except (OSError, ValueError): 828 except (OSError, ValueError):
860 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n") 829 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
861 sys.exit(1) 830 sys.exit(1)
876 die "${FUNCNAME}(): Generation of '$1' failed" 845 die "${FUNCNAME}(): Generation of '$1' failed"
877 fi 846 fi
878 else 847 else
879 cat << EOF >> "${file}" 848 cat << EOF >> "${file}"
880try: 849try:
881 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE) 850 eselect_process = subprocess.Popen(["${EPREFIX}/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
882 if eselect_process.wait() != 0: 851 if eselect_process.wait() != 0:
883 raise ValueError 852 raise ValueError
884except (OSError, ValueError): 853except (OSError, ValueError):
885 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n") 854 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
886 sys.exit(1) 855 sys.exit(1)
912os.execv(target_executable, sys.argv) 881os.execv(target_executable, sys.argv)
913EOF 882EOF
914 if [[ "$?" != "0" ]]; then 883 if [[ "$?" != "0" ]]; then
915 die "${FUNCNAME}(): Generation of '$1' failed" 884 die "${FUNCNAME}(): Generation of '$1' failed"
916 fi 885 fi
917 fperms +x "${file#${D%/}}" || die "fperms '${file}' failed" 886 fperms +x "${file#${ED%/}}" || die "fperms '${file}' failed"
918 done
919}
920
921# @ECLASS-VARIABLE: PYTHON_USE_WITH
922# @DESCRIPTION:
923# Set this to a space separated list of use flags
924# the python slot in use must be built with.
925
926# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
927# @DESCRIPTION:
928# Set this to a space separated list of use flags
929# of which one must be turned on for the slot of
930# in use.
931
932# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
933# @DESCRIPTION:
934# Set this if you need to make either PYTHON_USE_WITH or
935# PYTHON_USE_WITH_OR atoms conditional under a use flag.
936
937# @FUNCTION: python_pkg_setup
938# @DESCRIPTION:
939# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
940# are respected. Only exported if one of those variables is set.
941if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
942 python_pkg_setup() {
943 python_pkg_setup_fail() {
944 eerror "${1}"
945 die "${1}"
946 }
947
948 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
949
950 python_pkg_setup_check_USE_flags() {
951 local pyatom use
952 if [[ -n "${PYTHON_ABI}" ]]; then
953 pyatom="dev-lang/python:${PYTHON_ABI}"
954 else
955 pyatom="dev-lang/python:$(PYTHON -A --ABI)"
956 fi
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
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
978 python_pkg_setup_check_USE_flags
979 fi
980 }
981
982 EXPORT_FUNCTIONS pkg_setup
983
984 if [[ -n "${PYTHON_USE_WITH}" ]]; then
985 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
986 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
987 PYTHON_USE_WITH_ATOM="|| ( "
988 for use in ${PYTHON_USE_WITH_OR}; do
989 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
990 done 887 done
991 unset use 888}
992 PYTHON_USE_WITH_ATOM+=" )" 889
890# ================================================================================================
891# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======
892# ================================================================================================
893
894# @FUNCTION: python_set_active_version
895# @USAGE: <Python_ABI|2|3>
896# @DESCRIPTION:
897# Set active version of Python.
898python_set_active_version() {
899 if [[ "$#" -ne 1 ]]; then
900 die "${FUNCNAME}() requires 1 argument"
901 fi
902
903 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
904 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
905 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
993 fi 906 fi
994 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then 907 export EPYTHON="$(PYTHON "$1")"
995 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 908 elif [[ "$1" == "2" ]]; then
909 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
910 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
996 fi 911 fi
997 DEPEND+=" ${PYTHON_USE_WITH_ATOM}" 912 export EPYTHON="$(PYTHON -2)"
998 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}" 913 elif [[ "$1" == "3" ]]; then
914 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
915 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
916 fi
917 export EPYTHON="$(PYTHON -3)"
918 else
919 die "${FUNCNAME}(): Unrecognized argument '$1'"
999fi 920 fi
1000 921
1001# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS 922 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
1002# @DESCRIPTION: 923 # so it does not need to be exported to subprocesses.
1003# Set this to define default functions for the following ebuild phases: 924 PYTHON_ABI="${EPYTHON#python}"
1004# src_prepare, src_configure, src_compile, src_test, src_install. 925 PYTHON_ABI="${PYTHON_ABI%%-*}"
1005if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
1006 python_src_prepare() {
1007 python_copy_sources
1008 }
1009 926
1010 for python_default_function in src_configure src_compile src_test src_install; do 927 # python-updater checks PYTHON_REQUESTED_ACTIVE_VERSION variable.
1011 eval "python_${python_default_function}() { python_execute_function -d -s; }" 928 PYTHON_REQUESTED_ACTIVE_VERSION="$1"
1012 done
1013 unset python_default_function
1014
1015 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
1016fi
1017
1018# @FUNCTION: python_disable_pyc
1019# @DESCRIPTION:
1020# Tell Python not to automatically recompile modules to .pyc/.pyo
1021# even if the timestamps/version stamps do not match. This is done
1022# to protect sandbox.
1023python_disable_pyc() {
1024 export PYTHONDONTWRITEBYTECODE="1"
1025}
1026
1027# @FUNCTION: python_enable_pyc
1028# @DESCRIPTION:
1029# Tell Python to automatically recompile modules to .pyc/.pyo if the
1030# timestamps/version stamps have changed.
1031python_enable_pyc() {
1032 unset PYTHONDONTWRITEBYTECODE
1033} 929}
1034 930
1035# @FUNCTION: python_need_rebuild 931# @FUNCTION: python_need_rebuild
1036# @DESCRIPTION: Run without arguments, specifies that the package should be 932# @DESCRIPTION: Run without arguments, specifies that the package should be
1037# rebuilt after a python upgrade. 933# rebuilt after a python upgrade.
1039# for multiple versions of Python. 935# for multiple versions of Python.
1040python_need_rebuild() { 936python_need_rebuild() {
1041 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)" 937 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
1042} 938}
1043 939
940# ================================================================================================
941# ======================================= GETTER FUNCTIONS =======================================
942# ================================================================================================
943
944# @FUNCTION: PYTHON
945# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
946# @DESCRIPTION:
947# Print Python interpreter filename for specified Python ABI. If Python_ABI argument
948# is ommitted, then PYTHON_ABI environment variable must be set and is used.
949# If -2 option is specified, then active version of Python 2 is used.
950# If -3 option is specified, then active version of Python 3 is used.
951# If --active option is specified, then active version of Python is used.
952# Active version of Python can be set by python_set_active_version().
953# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
954# -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
955# If --ABI option is specified, then only specified Python ABI is printed instead of
956# Python interpreter filename.
957# --ABI and --absolute-path options cannot be specified simultaneously.
958PYTHON() {
959 local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" python2="0" python3="0" slot=
960
961 while (($#)); do
962 case "$1" in
963 -2)
964 python2="1"
965 ;;
966 -3)
967 python3="1"
968 ;;
969 --ABI)
970 ABI_output="1"
971 ;;
972 -A|--active)
973 active="1"
974 ;;
975 -a|--absolute-path)
976 absolute_path_output="1"
977 ;;
978 -f|--final-ABI)
979 final_ABI="1"
980 ;;
981 --)
982 shift
983 break
984 ;;
985 -*)
986 die "${FUNCNAME}(): Unrecognized option '$1'"
987 ;;
988 *)
989 break
990 ;;
991 esac
992 shift
993 done
994
995 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
996 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
997 fi
998
999 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then
1000 die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously"
1001 fi
1002
1003 if [[ "$#" -eq 0 ]]; then
1004 if [[ "${active}" == "1" ]]; then
1005 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1006 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
1007 fi
1008 slot="$("${EPREFIX}/usr/bin/python" -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
1009 elif [[ "${final_ABI}" == "1" ]]; then
1010 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
1011 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python"
1012 fi
1013 validate_PYTHON_ABIS
1014 slot="${PYTHON_ABIS##* }"
1015 elif [[ "${python2}" == "1" ]]; then
1016 slot="$(eselect python show --python2)"
1017 if [[ -z "${slot}" ]]; then
1018 die "${FUNCNAME}(): Active Python 2 interpreter not set"
1019 elif [[ "${slot}" != "python2."* ]]; then
1020 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
1021 fi
1022 slot="${slot#python}"
1023 elif [[ "${python3}" == "1" ]]; then
1024 slot="$(eselect python show --python3)"
1025 if [[ -z "${slot}" ]]; then
1026 die "${FUNCNAME}(): Active Python 3 interpreter not set"
1027 elif [[ "${slot}" != "python3."* ]]; then
1028 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
1029 fi
1030 slot="${slot#python}"
1031 elif [[ -n "${PYTHON_ABI}" ]]; then
1032 slot="${PYTHON_ABI}"
1033 else
1034 die "${FUNCNAME}(): Invalid usage"
1035 fi
1036 elif [[ "$#" -eq 1 ]]; then
1037 if [[ "${active}" == "1" ]]; then
1038 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
1039 fi
1040 if [[ "${final_ABI}" == "1" ]]; then
1041 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
1042 fi
1043 if [[ "${python2}" == "1" ]]; then
1044 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
1045 fi
1046 if [[ "${python3}" == "1" ]]; then
1047 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
1048 fi
1049 slot="$1"
1050 else
1051 die "${FUNCNAME}(): Invalid usage"
1052 fi
1053
1054 if [[ "${ABI_output}" == "1" ]]; then
1055 echo -n "${slot}"
1056 return
1057 elif [[ "${absolute_path_output}" == "1" ]]; then
1058 echo -n "${EPREFIX}/usr/bin/python${slot}"
1059 else
1060 echo -n "python${slot}"
1061 fi
1062
1063 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
1064 echo -n "-${ABI}"
1065 fi
1066}
1067
1044# @FUNCTION: python_get_includedir 1068# @FUNCTION: python_get_includedir
1069# @USAGE: [-f|--final-ABI]
1045# @DESCRIPTION: 1070# @DESCRIPTION:
1046# Run without arguments, returns the Python include directory. 1071# Print path to Python include directory.
1072# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1047python_get_includedir() { 1073python_get_includedir() {
1074 local final_ABI="0"
1075
1076 while (($#)); do
1077 case "$1" in
1078 -f|--final-ABI)
1079 final_ABI="1"
1080 ;;
1081 -*)
1082 die "${FUNCNAME}(): Unrecognized option '$1'"
1083 ;;
1084 *)
1085 die "${FUNCNAME}(): Invalid usage"
1086 ;;
1087 esac
1088 shift
1089 done
1090
1091 if [[ "${final_ABI}" == "1" ]]; then
1092 echo "/usr/include/python$(PYTHON -f --ABI)"
1048 if [[ -n "${PYTHON_ABI}" ]]; then 1093 elif [[ -n "${PYTHON_ABI}" ]]; then
1049 echo "/usr/include/python${PYTHON_ABI}" 1094 echo "/usr/include/python${PYTHON_ABI}"
1050 else 1095 else
1051 echo "/usr/include/python$(PYTHON -A --ABI)" 1096 echo "/usr/include/python$(PYTHON -A --ABI)"
1052 fi 1097 fi
1053} 1098}
1054 1099
1055# @FUNCTION: python_get_libdir 1100# @FUNCTION: python_get_libdir
1101# @USAGE: [-f|--final-ABI]
1056# @DESCRIPTION: 1102# @DESCRIPTION:
1057# Run without arguments, returns the Python library directory. 1103# Print path to Python library directory.
1104# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1058python_get_libdir() { 1105python_get_libdir() {
1106 local final_ABI="0"
1107
1108 while (($#)); do
1109 case "$1" in
1110 -f|--final-ABI)
1111 final_ABI="1"
1112 ;;
1113 -*)
1114 die "${FUNCNAME}(): Unrecognized option '$1'"
1115 ;;
1116 *)
1117 die "${FUNCNAME}(): Invalid usage"
1118 ;;
1119 esac
1120 shift
1121 done
1122
1123 if [[ "${final_ABI}" == "1" ]]; then
1124 echo "/usr/$(get_libdir)/python$(PYTHON -f --ABI)"
1059 if [[ -n "${PYTHON_ABI}" ]]; then 1125 elif [[ -n "${PYTHON_ABI}" ]]; then
1060 echo "/usr/$(get_libdir)/python${PYTHON_ABI}" 1126 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
1061 else 1127 else
1062 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)" 1128 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)"
1063 fi 1129 fi
1064} 1130}
1065 1131
1066# @FUNCTION: python_get_sitedir 1132# @FUNCTION: python_get_sitedir
1133# @USAGE: [-f|--final-ABI]
1067# @DESCRIPTION: 1134# @DESCRIPTION:
1068# Run without arguments, returns the Python site-packages directory. 1135# Print path to Python site-packages directory.
1136# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1069python_get_sitedir() { 1137python_get_sitedir() {
1138 local options=()
1139
1140 while (($#)); do
1141 case "$1" in
1142 -f|--final-ABI)
1143 options+=("$1")
1144 ;;
1145 -*)
1146 die "${FUNCNAME}(): Unrecognized option '$1'"
1147 ;;
1148 *)
1149 die "${FUNCNAME}(): Invalid usage"
1150 ;;
1151 esac
1152 shift
1153 done
1154
1070 echo "$(python_get_libdir)/site-packages" 1155 echo "$(python_get_libdir "${options[@]}")/site-packages"
1071} 1156}
1072 1157
1073# @FUNCTION: python_tkinter_exists 1158# @FUNCTION: python_get_library
1159# @USAGE: [-f|--final-ABI] [-l|--linker-option]
1074# @DESCRIPTION: 1160# @DESCRIPTION:
1075# Run without arguments, checks if python was compiled with Tkinter 1161# Print path to Python library.
1076# support. If not, prints an error message and dies. 1162# If --linker-option is specified, then "-l${library}" linker option is printed.
1077python_tkinter_exists() { 1163# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1078 if ! python -c "import Tkinter" >/dev/null 2>&1; then 1164python_get_library() {
1079 eerror "You need to recompile python with Tkinter support." 1165 local final_ABI="0" linker_option="0" python_version
1080 eerror "Try adding: 'dev-lang/python tk'" 1166
1081 eerror "in to /etc/portage/package.use" 1167 while (($#)); do
1082 echo 1168 case "$1" in
1083 die "missing tkinter support with installed python" 1169 -f|--final-ABI)
1170 final_ABI="1"
1171 ;;
1172 -l|--linker-option)
1173 linker_option="1"
1174 ;;
1175 -*)
1176 die "${FUNCNAME}(): Unrecognized option '$1'"
1177 ;;
1178 *)
1179 die "${FUNCNAME}(): Invalid usage"
1180 ;;
1181 esac
1182 shift
1183 done
1184
1185 if [[ "${final_ABI}" == "1" ]]; then
1186 python_version="$(PYTHON -f --ABI)"
1187 elif [[ -n "${PYTHON_ABI}" ]]; then
1188 python_version="${PYTHON_ABI}"
1189 else
1190 python_version="$(PYTHON -A --ABI)"
1191 fi
1192
1193 if [[ "${linker_option}" == "1" ]]; then
1194 echo "-lpython${python_version}"
1195 else
1196 echo "/usr/$(get_libdir)/libpython${python_version}$(get_libname)"
1197 fi
1198}
1199
1200# @FUNCTION: python_get_version
1201# @USAGE: [-f|--final-ABI] [--major] [--minor] [--micro]
1202# @DESCRIPTION:
1203# Print Python version.
1204# --major, --minor and --micro options cannot be specified simultaneously.
1205# If --major, --minor and --micro options are not specified, then "${major_version}.${minor_version}" is printed.
1206# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
1207python_get_version() {
1208 local final_ABI="0" major="0" minor="0" micro="0" python_command
1209
1210 while (($#)); do
1211 case "$1" in
1212 -f|--final-ABI)
1213 final_ABI="1"
1214 ;;
1215 --major)
1216 major="1"
1217 ;;
1218 --minor)
1219 minor="1"
1220 ;;
1221 --micro)
1222 micro="1"
1223 ;;
1224 -*)
1225 die "${FUNCNAME}(): Unrecognized option '$1'"
1226 ;;
1227 *)
1228 die "${FUNCNAME}(): Invalid usage"
1229 ;;
1230 esac
1231 shift
1232 done
1233
1234 if [[ "$((${major} + ${minor} + ${micro}))" -gt 1 ]]; then
1235 die "${FUNCNAME}(): '--major', '--minor' or '--micro' options cannot be specified simultaneously"
1236 fi
1237
1238 if [[ "${major}" == "1" ]]; then
1239 python_command="from sys import version_info; print(version_info[0])"
1240 elif [[ "${minor}" == "1" ]]; then
1241 python_command="from sys import version_info; print(version_info[1])"
1242 elif [[ "${micro}" == "1" ]]; then
1243 python_command="from sys import version_info; print(version_info[2])"
1244 else
1245 python_command="from sys import version_info; print('.'.join([str(x) for x in version_info[:2]]))"
1246 fi
1247
1248 if [[ "${final_ABI}" == "1" ]]; then
1249 "$(PYTHON -f)" -c "${python_command}"
1250 else
1251 "$(PYTHON "${PYTHON_ABI--A}")" -c "${python_command}"
1252 fi
1253}
1254
1255# ================================================================================================
1256# =================================== MISCELLANEOUS FUNCTIONS ====================================
1257# ================================================================================================
1258
1259_python_implementation() {
1260 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1261 return 0
1262 else
1263 return 1
1264 fi
1265}
1266
1267_python_initialize_prefix_variables() {
1268 if has "${EAPI:-0}" 0 1 2; then
1269 if [[ -n "${ROOT}" && -z "${EROOT}" ]]; then
1270 EROOT="${ROOT%/}${EPREFIX}/"
1084 fi 1271 fi
1272 if [[ -n "${D}" && -z "${ED}" ]]; then
1273 ED="${D%/}${EPREFIX}/"
1274 fi
1275 fi
1276}
1277
1278# @FUNCTION: python_convert_shebangs
1279# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
1280# @DESCRIPTION:
1281# Convert shebangs in specified files. Directories can be specified only with --recursive option.
1282python_convert_shebangs() {
1283 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
1284
1285 while (($#)); do
1286 case "$1" in
1287 -r|--recursive)
1288 recursive="1"
1289 ;;
1290 -q|--quiet)
1291 quiet="1"
1292 ;;
1293 -x|--only-executables)
1294 only_executables="1"
1295 ;;
1296 --)
1297 shift
1298 break
1299 ;;
1300 -*)
1301 die "${FUNCNAME}(): Unrecognized option '$1'"
1302 ;;
1303 *)
1304 break
1305 ;;
1306 esac
1307 shift
1308 done
1309
1310 if [[ "$#" -eq 0 ]]; then
1311 die "${FUNCNAME}(): Missing Python version and files or directories"
1312 elif [[ "$#" -eq 1 ]]; then
1313 die "${FUNCNAME}(): Missing files or directories"
1314 fi
1315
1316 python_version="$1"
1317 shift
1318
1319 for argument in "$@"; do
1320 if [[ ! -e "${argument}" ]]; then
1321 die "${FUNCNAME}(): '${argument}' does not exist"
1322 elif [[ -f "${argument}" ]]; then
1323 files+=("${argument}")
1324 elif [[ -d "${argument}" ]]; then
1325 if [[ "${recursive}" == "1" ]]; then
1326 if [[ "${only_executables}" == "1" ]]; then
1327 files+=($(find "${argument}" -perm /111 -type f))
1328 else
1329 files+=($(find "${argument}" -type f))
1330 fi
1331 else
1332 die "${FUNCNAME}(): '${argument}' is not a regular file"
1333 fi
1334 else
1335 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
1336 fi
1337 done
1338
1339 for file in "${files[@]}"; do
1340 file="${file#./}"
1341 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1342
1343 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1344 if [[ "${quiet}" == "0" ]]; then
1345 einfo "Converting shebang in '${file}'"
1346 fi
1347 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1348
1349 # Delete potential whitespace after "#!".
1350 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1351 fi
1352 done
1085} 1353}
1086 1354
1087# @FUNCTION: python_mod_exists 1355# @FUNCTION: python_mod_exists
1088# @USAGE: <module> 1356# @USAGE: <module>
1089# @DESCRIPTION: 1357# @DESCRIPTION:
1090# Run with the module name as an argument. it will check if a 1358# Run with the module name as an argument. It will check if a
1091# python module is installed and loadable. it will return 1359# Python module is installed and loadable. It will return
1092# TRUE(0) if the module exists, and FALSE(1) if the module does 1360# TRUE(0) if the module exists, and FALSE(1) if the module does
1093# not exist. 1361# not exist.
1094# 1362#
1095# Example: 1363# Example:
1096# if python_mod_exists gtk; then 1364# if python_mod_exists gtk; then
1097# echo "gtk support enabled" 1365# echo "gtk support enabled"
1098# fi 1366# fi
1099python_mod_exists() { 1367python_mod_exists() {
1368 if [[ "$#" -ne 1 ]]; then
1100 [[ "$1" ]] || die "${FUNCNAME} requires an argument!" 1369 die "${FUNCNAME}() requires 1 argument"
1101 python -c "import $1" &>/dev/null 1370 fi
1371 "$(PYTHON "${PYTHON_ABI--A}")" -c "import $1" &> /dev/null
1102} 1372}
1103 1373
1104# @FUNCTION: python_mod_compile 1374# @FUNCTION: python_tkinter_exists
1105# @USAGE: <file> [more files ...]
1106# @DESCRIPTION: 1375# @DESCRIPTION:
1107# Given filenames, it will pre-compile the module's .pyc and .pyo. 1376# Run without arguments, checks if Python was compiled with Tkinter
1108# This function should only be run in pkg_postinst() 1377# support. If not, prints an error message and dies.
1109# 1378python_tkinter_exists() {
1110# Example: 1379 if ! "$(PYTHON "${PYTHON_ABI--A}")" -c "from sys import version_info
1111# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 1380if version_info[0] == 3:
1112# 1381 import tkinter
1113python_mod_compile() { 1382else:
1114 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1383 import Tkinter" &> /dev/null; then
1115 die "${FUNCNAME}() cannot be used in this EAPI" 1384 eerror "Python needs to be rebuilt with tkinter support enabled."
1385 eerror "Add the following line to '${EPREFIX}/etc/portage/package.use' and rebuild Python"
1386 eerror "dev-lang/python tk"
1387 die "Python installed without support for tkinter"
1388 fi
1389}
1390
1391# ================================================================================================
1392# ================================ FUNCTIONS FOR RUNNING OF TESTS ================================
1393# ================================================================================================
1394
1395# @ECLASS-VARIABLE: PYTHON_TEST_VERBOSITY
1396# @DESCRIPTION:
1397# User-configurable verbosity of tests of Python modules.
1398# Supported values: 0, 1, 2, 3, 4.
1399PYTHON_TEST_VERBOSITY="${PYTHON_TEST_VERBOSITY:-1}"
1400
1401# @FUNCTION: python_execute_nosetests
1402# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1403# @DESCRIPTION:
1404# Execute nosetests for all enabled versions of Python.
1405python_execute_nosetests() {
1406 local PYTHONPATH_template= separate_build_dirs=
1407
1408 while (($#)); do
1409 case "$1" in
1410 -P|--PYTHONPATH)
1411 PYTHONPATH_template="$2"
1412 shift
1413 ;;
1414 -s|--separate-build-dirs)
1415 separate_build_dirs="1"
1416 ;;
1417 --)
1418 shift
1419 break
1420 ;;
1421 -*)
1422 die "${FUNCNAME}(): Unrecognized option '$1'"
1423 ;;
1424 *)
1425 break
1426 ;;
1427 esac
1428 shift
1429 done
1430
1431 python_test_function() {
1432 local evaluated_PYTHONPATH=
1433
1434 if [[ -n "${PYTHONPATH_template}" ]]; then
1435 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1436 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1437 unset evaluated_PYTHONPATH
1438 fi
1116 fi 1439 fi
1117 1440
1118 local f myroot myfiles=() 1441 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1119 1442 echo PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1120 # Check if phase is pkg_postinst() 1443 PYTHONPATH="${evaluated_PYTHONPATH}" nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
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 1444 else
1136 ewarn "No files to compile!" 1445 echo nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1446 nosetests --verbosity="${PYTHON_TEST_VERBOSITY}" "$@"
1137 fi 1447 fi
1448 }
1449 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1450 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1451 else
1452 if [[ -n "${separate_build_dirs}" ]]; then
1453 die "${FUNCNAME}(): Invalid usage"
1454 fi
1455 python_test_function "$@"
1456 fi
1457}
1458
1459# @FUNCTION: python_execute_py.test
1460# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1461# @DESCRIPTION:
1462# Execute py.test for all enabled versions of Python.
1463python_execute_py.test() {
1464 local PYTHONPATH_template= separate_build_dirs=
1465
1466 while (($#)); do
1467 case "$1" in
1468 -P|--PYTHONPATH)
1469 PYTHONPATH_template="$2"
1470 shift
1471 ;;
1472 -s|--separate-build-dirs)
1473 separate_build_dirs="1"
1474 ;;
1475 --)
1476 shift
1477 break
1478 ;;
1479 -*)
1480 die "${FUNCNAME}(): Unrecognized option '$1'"
1481 ;;
1482 *)
1483 break
1484 ;;
1485 esac
1486 shift
1487 done
1488
1489 python_test_function() {
1490 local evaluated_PYTHONPATH=
1491
1492 if [[ -n "${PYTHONPATH_template}" ]]; then
1493 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1494 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1495 unset evaluated_PYTHONPATH
1496 fi
1497 fi
1498
1499 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1500 echo PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"
1501 PYTHONPATH="${evaluated_PYTHONPATH}" py.test $([[ "${PYTHON_TEST_VERBOSITY}" -ge 2 ]] && echo -v) "$@"
1502 else
1503 echo py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"
1504 py.test $([[ "${PYTHON_TEST_VERBOSITY}" -gt 1 ]] && echo -v) "$@"
1505 fi
1506 }
1507 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1508 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1509 else
1510 if [[ -n "${separate_build_dirs}" ]]; then
1511 die "${FUNCNAME}(): Invalid usage"
1512 fi
1513 python_test_function "$@"
1514 fi
1515}
1516
1517# @FUNCTION: python_execute_trial
1518# @USAGE: [-P|--PYTHONPATH PYTHONPATH] [-s|--separate-build-dirs] [--] [arguments]
1519# @DESCRIPTION:
1520# Execute trial for all enabled versions of Python.
1521python_execute_trial() {
1522 local PYTHONPATH_template= separate_build_dirs=
1523
1524 while (($#)); do
1525 case "$1" in
1526 -P|--PYTHONPATH)
1527 PYTHONPATH_template="$2"
1528 shift
1529 ;;
1530 -s|--separate-build-dirs)
1531 separate_build_dirs="1"
1532 ;;
1533 --)
1534 shift
1535 break
1536 ;;
1537 -*)
1538 die "${FUNCNAME}(): Unrecognized option '$1'"
1539 ;;
1540 *)
1541 break
1542 ;;
1543 esac
1544 shift
1545 done
1546
1547 python_test_function() {
1548 local evaluated_PYTHONPATH=
1549
1550 if [[ -n "${PYTHONPATH_template}" ]]; then
1551 evaluated_PYTHONPATH="$(eval echo -n "${PYTHONPATH_template}")"
1552 if [[ ! -e "${evaluated_PYTHONPATH}" ]]; then
1553 unset evaluated_PYTHONPATH
1554 fi
1555 fi
1556
1557 if [[ -n "${evaluated_PYTHONPATH}" ]]; then
1558 echo PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1559 PYTHONPATH="${evaluated_PYTHONPATH}" trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1560 else
1561 echo trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1562 trial $([[ "${PYTHON_TEST_VERBOSITY}" -ge 4 ]] && echo --spew) "$@"
1563 fi
1564 }
1565 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1566 python_execute_function ${separate_build_dirs:+-s} python_test_function "$@"
1567 else
1568 if [[ -n "${separate_build_dirs}" ]]; then
1569 die "${FUNCNAME}(): Invalid usage"
1570 fi
1571 python_test_function "$@"
1572 fi
1573}
1574
1575# ================================================================================================
1576# ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
1577# ================================================================================================
1578
1579# @FUNCTION: python_enable_pyc
1580# @DESCRIPTION:
1581# Tell Python to automatically recompile modules to .pyc/.pyo if the
1582# timestamps/version stamps have changed.
1583python_enable_pyc() {
1584 unset PYTHONDONTWRITEBYTECODE
1585}
1586
1587# @FUNCTION: python_disable_pyc
1588# @DESCRIPTION:
1589# Tell Python not to automatically recompile modules to .pyc/.pyo
1590# even if the timestamps/version stamps do not match. This is done
1591# to protect sandbox.
1592python_disable_pyc() {
1593 export PYTHONDONTWRITEBYTECODE="1"
1138} 1594}
1139 1595
1140# @FUNCTION: python_mod_optimize 1596# @FUNCTION: python_mod_optimize
1141# @USAGE: [options] [directory|file] 1597# @USAGE: [options] [directory|file]
1142# @DESCRIPTION: 1598# @DESCRIPTION:
1150# Options passed to this function are passed to compileall.py. 1606# Options passed to this function are passed to compileall.py.
1151# 1607#
1152# Example: 1608# Example:
1153# python_mod_optimize ctypesgencore 1609# python_mod_optimize ctypesgencore
1154python_mod_optimize() { 1610python_mod_optimize() {
1611 _python_initialize_prefix_variables
1612
1155 # Check if phase is pkg_postinst(). 1613 # Check if phase is pkg_postinst().
1156 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()" 1614 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME}() should only be run in pkg_postinst()"
1157 1615
1158 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1616 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
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=() 1617 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
1160 1618
1161 # Strip trailing slash from ROOT. 1619 # Strip trailing slash from ROOT.
1162 root="${ROOT%/}" 1620 root="${EROOT%/}"
1163 1621
1164 # Respect ROOT and options passed to compileall.py. 1622 # Respect ROOT and options passed to compileall.py.
1165 while (($#)); do 1623 while (($#)); do
1166 case "$1" in 1624 case "$1" in
1167 -l|-f|-q) 1625 -l|-f|-q)
1170 -d|-x) 1628 -d|-x)
1171 options+=("$1" "$2") 1629 options+=("$1" "$2")
1172 shift 1630 shift
1173 ;; 1631 ;;
1174 -*) 1632 -*)
1175 ewarn "${FUNCNAME}: Ignoring compile option $1" 1633 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1176 ;; 1634 ;;
1177 *) 1635 *)
1178 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1636 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1179 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories" 1637 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1180 elif [[ "$1" =~ ^/ ]]; then 1638 elif [[ "$1" =~ ^/ ]]; then
1181 if [[ -d "${root}/$1" ]]; then 1639 if [[ -d "${root}/$1" ]]; then
1182 other_dirs+=("${root}/$1") 1640 other_dirs+=("${root}/$1")
1183 elif [[ -f "${root}/$1" ]]; then 1641 elif [[ -f "${root}/$1" ]]; then
1184 other_files+=("${root}/$1") 1642 other_files+=("${root}/$1")
1231 eend "${return_code}" 1689 eend "${return_code}"
1232 fi 1690 fi
1233 unset site_packages_absolute_dirs site_packages_absolute_files 1691 unset site_packages_absolute_dirs site_packages_absolute_files
1234 done 1692 done
1235 1693
1236 # Do not use PYTHON_ABI in next calls to python_get_libdir(). 1694 # Restore previous value of PYTHON_ABI.
1695 if [[ -n "${previous_PYTHON_ABI}" ]]; then
1696 PYTHON_ABI="${previous_PYTHON_ABI}"
1697 else
1237 unset PYTHON_ABI 1698 unset PYTHON_ABI
1699 fi
1238 1700
1239 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1701 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
1240 return_code="0" 1702 return_code="0"
1241 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)" 1703 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)"
1242 if ((${#other_dirs[@]})); then 1704 if ((${#other_dirs[@]})); then
1243 "$(PYTHON -A)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 1705 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
1244 "$(PYTHON -A)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" 1706 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
1245 fi 1707 fi
1246 if ((${#other_files[@]})); then 1708 if ((${#other_files[@]})); then
1247 "$(PYTHON -A)" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 1709 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
1248 "$(PYTHON -A)" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1" 1710 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
1249 fi 1711 fi
1250 eend "${return_code}" 1712 eend "${return_code}"
1251 fi 1713 fi
1252 else 1714 else
1253 local myroot mydirs=() myfiles=() myopts=() return_code="0" 1715 local myroot mydirs=() myfiles=() myopts=() return_code="0"
1254 1716
1255 # strip trailing slash 1717 # strip trailing slash
1256 myroot="${ROOT%/}" 1718 myroot="${EROOT%/}"
1257 1719
1258 # respect ROOT and options passed to compileall.py 1720 # respect ROOT and options passed to compileall.py
1259 while (($#)); do 1721 while (($#)); do
1260 case "$1" in 1722 case "$1" in
1261 -l|-f|-q) 1723 -l|-f|-q)
1264 -d|-x) 1726 -d|-x)
1265 myopts+=("$1" "$2") 1727 myopts+=("$1" "$2")
1266 shift 1728 shift
1267 ;; 1729 ;;
1268 -*) 1730 -*)
1269 ewarn "${FUNCNAME}: Ignoring compile option $1" 1731 ewarn "${FUNCNAME}(): Ignoring option '$1'"
1270 ;; 1732 ;;
1271 *) 1733 *)
1272 if [[ -d "${myroot}"/$1 ]]; then 1734 if [[ -d "${myroot}"/$1 ]]; then
1273 mydirs+=("${myroot}/$1") 1735 mydirs+=("${myroot}/$1")
1274 elif [[ -f "${myroot}"/$1 ]]; then 1736 elif [[ -f "${myroot}"/$1 ]]; then
1287 # set additional opts 1749 # set additional opts
1288 myopts+=(-q) 1750 myopts+=(-q)
1289 1751
1290 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)" 1752 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)"
1291 if ((${#mydirs[@]})); then 1753 if ((${#mydirs[@]})); then
1292 "$(PYTHON -A)" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1" 1754 "$(PYTHON "${PYTHON_ABI--A}")" "${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" 1755 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
1294 fi 1756 fi
1295 1757
1296 if ((${#myfiles[@]})); then 1758 if ((${#myfiles[@]})); then
1297 python_mod_compile "${myfiles[@]}" 1759 python_mod_compile "${myfiles[@]}"
1298 fi 1760 fi
1311# determine if they are orphaned (i.e. their corresponding .py files are missing.) 1773# determine if they are orphaned (i.e. their corresponding .py files are missing.)
1312# If they are, then it will remove their corresponding .pyc and .pyo files. 1774# If they are, then it will remove their corresponding .pyc and .pyo files.
1313# 1775#
1314# This function should only be run in pkg_postrm(). 1776# This function should only be run in pkg_postrm().
1315python_mod_cleanup() { 1777python_mod_cleanup() {
1778 _python_initialize_prefix_variables
1779
1316 local path py_file PYTHON_ABI SEARCH_PATH=() root 1780 local path py_file PYTHON_ABI SEARCH_PATH=() root
1317 1781
1318 # Check if phase is pkg_postrm(). 1782 # Check if phase is pkg_postrm().
1319 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" 1783 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME}() should only be run in pkg_postrm()"
1320 1784
1321 # Strip trailing slash from ROOT. 1785 # Strip trailing slash from ROOT.
1322 root="${ROOT%/}" 1786 root="${EROOT%/}"
1323 1787
1324 if (($#)); then 1788 if (($#)); then
1325 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1789 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1326 while (($#)); do 1790 while (($#)); do
1327 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1791 if ! _python_implementation && [[ "$1" =~ ^"${EPREFIX}"/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
1328 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories" 1792 die "${FUNCNAME}() does not support absolute paths of directories/files in site-packages directories"
1329 elif [[ "$1" =~ ^/ ]]; then 1793 elif [[ "$1" =~ ^/ ]]; then
1330 SEARCH_PATH+=("${root}/${1#/}") 1794 SEARCH_PATH+=("${root}/${1#/}")
1331 else 1795 else
1332 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do 1796 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
1333 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 1797 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
1380 einfo "${BLUE}<<< ${path}[co]${NORMAL}" 1844 einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1381 rm -f "${path}"[co] 1845 rm -f "${path}"[co]
1382 fi 1846 fi
1383 done 1847 done
1384} 1848}
1849
1850# ================================================================================================
1851# ===================================== DEPRECATED FUNCTIONS =====================================
1852# ================================================================================================
1853
1854# @FUNCTION: python_version
1855# @DESCRIPTION:
1856# Run without arguments and it will export the version of python
1857# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
1858python_version() {
1859 if ! has "${EAPI:-0}" 0 1 2; then
1860 eerror "Use PYTHON() and/or python_get_*() instead of python_version()."
1861 die "${FUNCNAME}() cannot be used in this EAPI"
1862 fi
1863
1864 [[ -n "${PYVER}" ]] && return 0
1865 local tmpstr
1866 python="${python:-${EPREFIX}/usr/bin/python}"
1867 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
1868 export PYVER_ALL="${tmpstr#Python }"
1869 export PYVER_MAJOR="${PYVER_ALL:0:1}"
1870 export PYVER_MINOR="${PYVER_ALL:2:1}"
1871 if [[ "${PYVER_ALL:3:1}" == "." ]]; then
1872 export PYVER_MICRO="${PYVER_ALL:4}"
1873 fi
1874 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
1875}
1876
1877# @FUNCTION: python_mod_compile
1878# @USAGE: <file> [more files ...]
1879# @DESCRIPTION:
1880# Given filenames, it will pre-compile the module's .pyc and .pyo.
1881# This function should only be run in pkg_postinst()
1882#
1883# Example:
1884# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
1885#
1886python_mod_compile() {
1887 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1888 eerror "Use python_mod_optimize() instead of python_mod_compile()."
1889 die "${FUNCNAME}() cannot be used in this EAPI"
1890 fi
1891
1892 _python_initialize_prefix_variables
1893
1894 local f myroot myfiles=()
1895
1896 # Check if phase is pkg_postinst()
1897 [[ ${EBUILD_PHASE} != postinst ]] && die "${FUNCNAME}() should only be run in pkg_postinst()"
1898
1899 # strip trailing slash
1900 myroot="${EROOT%/}"
1901
1902 # respect ROOT
1903 for f in "$@"; do
1904 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
1905 done
1906
1907 if ((${#myfiles[@]})); then
1908 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
1909 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
1910 else
1911 ewarn "No files to compile!"
1912 fi
1913}

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

  ViewVC Help
Powered by ViewVC 1.1.20