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

Diff of /eclass/python.eclass

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

Revision 1.72 Revision 1.86
1# Copyright 1999-2009 Gentoo Foundation 1# Copyright 1999-2010 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.72 2009/09/11 19:55:05 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.86 2010/01/15 14:46:20 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#
9# original author: Alastair Tse <liquidx@gentoo.org>
10# @BLURB: A Utility Eclass that should be inherited by anything that deals with Python or Python modules. 8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules.
11# @DESCRIPTION: 9# @DESCRIPTION:
12# Some useful functions for dealing with Python. 10# Some useful functions for dealing with Python.
13 11
14inherit multilib 12inherit multilib
15 13
14if ! has "${EAPI:-0}" 0 1 2; then
15 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
16fi
17
16if [[ -n "${NEED_PYTHON}" ]] ; then 18if [[ -n "${NEED_PYTHON}" ]]; then
17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" 19 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
18 DEPEND="${PYTHON_ATOM}" 20 DEPEND="${PYTHON_ATOM}"
19 RDEPEND="${DEPEND}" 21 RDEPEND="${DEPEND}"
20else 22else
21 PYTHON_ATOM="dev-lang/python" 23 PYTHON_ATOM="dev-lang/python"
22fi 24fi
23 25
24DEPEND="${DEPEND} >=app-shells/bash-3.2" 26DEPEND+=" >=app-admin/eselect-python-20090804"
27
28# @ECLASS-VARIABLE: PYTHON_USE_WITH
29# @DESCRIPTION:
30# Set this to a space separated list of use flags
31# the python slot in use must be built with.
32
33# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
34# @DESCRIPTION:
35# Set this to a space separated list of use flags
36# of which one must be turned on for the slot of
37# in use.
38
39# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
40# @DESCRIPTION:
41# Set this if you need to make either PYTHON_USE_WITH or
42# PYTHON_USE_WITH_OR atoms conditional under a use flag.
43
44# @FUNCTION: python_pkg_setup
45# @DESCRIPTION:
46# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
47# are respected. Only exported if one of those variables is set.
48if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
49 python_pkg_setup() {
50 python_pkg_setup_fail() {
51 eerror "${1}"
52 die "${1}"
53 }
54
55 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
56
57 python_pkg_setup_check_USE_flags() {
58 local pyatom use
59 if [[ -n "${PYTHON_ABI}" ]]; then
60 pyatom="dev-lang/python:${PYTHON_ABI}"
61 else
62 pyatom="dev-lang/python:$(PYTHON -A --ABI)"
63 fi
64
65 for use in ${PYTHON_USE_WITH}; do
66 if ! has_version "${pyatom}[${use}]"; then
67 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
68 fi
69 done
70
71 for use in ${PYTHON_USE_WITH_OR}; do
72 if has_version "${pyatom}[${use}]"; then
73 return
74 fi
75 done
76
77 if [[ ${PYTHON_USE_WITH_OR} ]]; then
78 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
79 fi
80 }
81
25if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 82 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
26 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804" 83 python_execute_function -q python_pkg_setup_check_USE_flags
84 else
85 python_pkg_setup_check_USE_flags
86 fi
87 }
88
89 EXPORT_FUNCTIONS pkg_setup
90
91 if [[ -n "${PYTHON_USE_WITH}" ]]; then
92 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
93 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
94 PYTHON_USE_WITH_ATOM="|| ( "
95 for use in ${PYTHON_USE_WITH_OR}; do
96 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
97 done
98 unset use
99 PYTHON_USE_WITH_ATOM+=" )"
100 fi
101 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
102 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
103 fi
104 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
105 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
27fi 106fi
28 107
29__python_eclass_test() { 108# ================================================================================================
30 __python_version_extract 2.3 109# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ========
31 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 110# ================================================================================================
32 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
33 __python_version_extract 2.3.4
34 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
35 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
36 __python_version_extract 2.3.5
37 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
38 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
39 __python_version_extract 2.4
40 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
41 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
42 __python_version_extract 2.5b3
43 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
44 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
45}
46 111
47# @FUNCTION: python_version 112# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
48# @DESCRIPTION: 113# @DESCRIPTION:
49# Run without arguments and it will export the version of python 114# Set this in EAPI <= 4 to indicate that given package supports installation for
50# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 115# multiple versions of Python.
51__python_version_extract() { 116
52 local verstr=$1 117# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
53 export PYVER_MAJOR=${verstr:0:1} 118# @DESCRIPTION:
54 export PYVER_MINOR=${verstr:2:1} 119# Set this to define default functions for the following ebuild phases:
55 if [[ ${verstr:3:1} == . ]]; then 120# src_prepare, src_configure, src_compile, src_test, src_install.
56 export PYVER_MICRO=${verstr:4} 121if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
122 python_src_prepare() {
123 python_copy_sources
124 }
125
126 for python_default_function in src_configure src_compile src_test src_install; do
127 eval "python_${python_default_function}() { python_execute_function -d -s; }"
128 done
129 unset python_default_function
130
131 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
57 fi 132fi
58 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
59}
60 133
61python_version() { 134unset PYTHON_ABIS
62 [[ -n "${PYVER}" ]] && return 0 135unset PYTHON_ABIS_SANITY_CHECKS
63 local tmpstr
64 python=${python:-/usr/bin/python}
65 tmpstr="$(${python} -V 2>&1 )"
66 export PYVER_ALL="${tmpstr#Python }"
67 __python_version_extract $PYVER_ALL
68}
69
70# @FUNCTION: PYTHON
71# @USAGE: [-a|--absolute-path] [--] <Python_ABI="${PYTHON_ABI}">
72# @DESCRIPTION:
73# Get Python interpreter filename for specified Python ABI. If Python_ABI argument
74# is ommitted, then PYTHON_ABI environment variable must be set and is used.
75PYTHON() {
76 local absolute_path="0" slot=
77
78 while (($#)); do
79 case "$1" in
80 -a|--absolute-path)
81 absolute_path="1"
82 ;;
83 --)
84 break
85 ;;
86 -*)
87 die "${FUNCNAME}(): Unrecognized option $1"
88 ;;
89 *)
90 break
91 ;;
92 esac
93 shift
94 done
95
96 if [[ "$#" -eq "0" ]]; then
97 if [[ -n "${PYTHON_ABI}" ]]; then
98 slot="${PYTHON_ABI}"
99 else
100 die "${FUNCNAME}(): Invalid usage"
101 fi
102 elif [[ "$#" -eq "1" ]]; then
103 slot="$1"
104 else
105 die "${FUNCNAME}(): Invalid usage"
106 fi
107
108 if [[ "${absolute_path}" == "1" ]]; then
109 echo -n "/usr/bin/python${slot}"
110 else
111 echo -n "python${slot}"
112 fi
113}
114 136
115# @FUNCTION: validate_PYTHON_ABIS 137# @FUNCTION: validate_PYTHON_ABIS
116# @DESCRIPTION: 138# @DESCRIPTION:
117# Make sure PYTHON_ABIS variable has valid value. 139# Ensure that PYTHON_ABIS variable has valid value.
140# This function usually should not be directly called in ebuilds.
118validate_PYTHON_ABIS() { 141validate_PYTHON_ABIS() {
119 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable. 142 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable.
120 if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 143 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
121 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable" 144 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
122 fi 145 fi
123 146
124 # Ensure that /usr/bin/python and /usr/bin/python-config are valid. 147 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
125 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then 148 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then
149 eerror "'/usr/bin/python' is not valid symlink."
150 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
126 die "'/usr/bin/python' isn't valid symlink" 151 die "'/usr/bin/python' is not valid symlink"
127 fi 152 fi
128 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then 153 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
154 eerror "'/usr/bin/python-config' is not valid script"
155 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
129 die "'/usr/bin/python-config' isn't valid script" 156 die "'/usr/bin/python-config' is not valid script"
130 fi 157 fi
131 158
132 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. 159 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
133 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then 160 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
134 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI 161 local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS=
135 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" 162 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
163 python2_supported_versions="2.4 2.5 2.6 2.7"
164 python3_supported_versions="3.0 3.1 3.2"
136 165
137 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then 166 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
167 local python2_enabled="0" python3_enabled="0"
168
138 if [[ -z "${USE_PYTHON}" ]]; then 169 if [[ -z "${USE_PYTHON}" ]]; then
139 die "USE_PYTHON variable is empty" 170 die "USE_PYTHON variable is empty"
140 fi 171 fi
141 172
142 for ABI in ${USE_PYTHON}; do 173 for PYTHON_ABI in ${USE_PYTHON}; do
143 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 174 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
144 die "USE_PYTHON variable contains invalid value '${ABI}'" 175 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
145 fi 176 fi
177
178 if has "${PYTHON_ABI}" ${python2_supported_versions}; then
179 python2_enabled="1"
180 fi
181 if has "${PYTHON_ABI}" ${python3_supported_versions}; then
182 python3_enabled="1"
183 fi
184
146 support_ABI="1" 185 support_ABI="1"
147 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 186 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
148 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 187 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
149 support_ABI="0" 188 support_ABI="0"
150 break 189 break
151 fi 190 fi
152 done 191 done
153 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 192 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
154 done 193 done
155 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
156 194
157 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then 195 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
158 die "USE_PYTHON variable doesn't enable any Python version supported by ${CATEGORY}/${PF}" 196 die "USE_PYTHON variable does not enable any version of Python supported by ${CATEGORY}/${PF}"
197 fi
198
199 if [[ "${python2_enabled}" == "0" ]]; then
200 ewarn "USE_PYTHON variable does not enable any version of Python 2. This configuration is unsupported."
201 fi
202 if [[ "${python3_enabled}" == "0" ]]; then
203 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported."
159 fi 204 fi
160 else 205 else
161 local restricted_ABI 206 local python_version python2_version= python3_version= support_python_major_version
162 python_version
163 207
208 python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
209
210 if has_version "=dev-lang/python-2*"; then
211 if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then
212 die "'/usr/bin/python2' is not valid symlink"
213 fi
214
215 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
216
217 for PYTHON_ABI in ${python2_supported_versions}; do
218 support_python_major_version="1"
164 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 219 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
165 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${PYVER}', '${restricted_ABI}'))"; then 220 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
166 die "Active Python version isn't supported by ${CATEGORY}/${PF}" 221 support_python_major_version="0"
167 fi 222 fi
223 done
224 [[ "${support_python_major_version}" == "1" ]] && break
168 done 225 done
169 export PYTHON_ABIS="${PYVER}" 226 if [[ "${support_python_major_version}" == "1" ]]; then
227 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
228 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
229 die "Active version of Python 2 is not supported by ${CATEGORY}/${PF}"
230 fi
231 done
232 else
233 python2_version=""
234 fi
170 fi 235 fi
236
237 if has_version "=dev-lang/python-3*"; then
238 if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then
239 die "'/usr/bin/python3' is not valid symlink"
240 fi
241
242 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
243
244 for PYTHON_ABI in ${python3_supported_versions}; do
245 support_python_major_version="1"
246 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
247 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
248 support_python_major_version="0"
249 fi
250 done
251 [[ "${support_python_major_version}" == "1" ]] && break
252 done
253 if [[ "${support_python_major_version}" == "1" ]]; then
254 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
255 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
256 die "Active version of Python 3 is not supported by ${CATEGORY}/${PF}"
257 fi
258 done
259 else
260 python3_version=""
261 fi
262 fi
263
264 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
265 eerror "Python wrapper is configured incorrectly or /usr/bin/python2 symlink"
266 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
267 die "Incorrect configuration of Python"
268 fi
269 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
270 eerror "Python wrapper is configured incorrectly or /usr/bin/python3 symlink"
271 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
272 die "Incorrect configuration of Python"
273 fi
274
275 PYTHON_ABIS="${python2_version} ${python3_version}"
276 PYTHON_ABIS="${PYTHON_ABIS# }"
277 export PYTHON_ABIS="${PYTHON_ABIS% }"
171 fi 278 fi
279 fi
172 280
281 if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
173 local PYTHON_ABI 282 local PYTHON_ABI
174 for PYTHON_ABI in ${PYTHON_ABIS}; do 283 for PYTHON_ABI in ${PYTHON_ABIS}; do
175 # Ensure that appropriate Python version is installed. 284 # Ensure that appropriate version of Python is installed.
176 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then 285 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
177 die "dev-lang/python:${PYTHON_ABI} isn't installed" 286 die "dev-lang/python:${PYTHON_ABI} is not installed"
178 fi 287 fi
179 288
180 # Ensure that EPYTHON variable is respected. 289 # Ensure that EPYTHON variable is respected.
181 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then 290 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
291 eerror "python: '$(type -p python)'"
292 eerror "ABI: '${ABI}'"
293 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
294 eerror "EPYTHON: '$(PYTHON)'"
295 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
296 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')'"
182 die "'python' doesn't respect EPYTHON variable" 297 die "'python' does not respect EPYTHON variable"
183 fi
184 done
185}
186
187# @FUNCTION: python_copy_sources
188# @USAGE: [--no-link] [--] [directory]
189# @DESCRIPTION:
190# Copy unpacked sources of given package for each Python ABI.
191python_copy_sources() {
192 local dir dirs=() no_link="0" PYTHON_ABI
193
194 while (($#)); do
195 case "$1" in
196 --no-link)
197 no_link="1"
198 ;;
199 --)
200 break
201 ;;
202 -*)
203 die "${FUNCNAME}(): Unrecognized option '$1'"
204 ;;
205 *)
206 break
207 ;;
208 esac
209 shift
210 done
211
212 if [[ "$#" -eq "0" ]]; then
213 if [[ "${WORKDIR}" == "${S}" ]]; then
214 die "${FUNCNAME}() cannot be used"
215 fi
216 dirs="${S}"
217 else
218 dirs="$@"
219 fi
220
221 validate_PYTHON_ABIS
222 for PYTHON_ABI in ${PYTHON_ABIS}; do
223 for dir in "${dirs[@]}"; do
224 if [[ "${no_link}" == "1" ]]; then
225 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
226 else
227 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
228 fi 298 fi
229 done 299 done
230 done 300 PYTHON_ABIS_SANITY_CHECKS="1"
231} 301 fi
232
233# @FUNCTION: python_set_build_dir_symlink
234# @USAGE: [directory="build"]
235# @DESCRIPTION:
236# Create build directory symlink.
237python_set_build_dir_symlink() {
238 local dir="$1"
239
240 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
241 [[ -z "${dir}" ]] && dir="build"
242
243 # Don't delete preexistent directories.
244 rm -f "${dir}" || die "Deletion of '${dir}' failed"
245 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
246} 302}
247 303
248# @FUNCTION: python_execute_function 304# @FUNCTION: python_execute_function
249# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments] 305# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
250# @DESCRIPTION: 306# @DESCRIPTION:
251# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 307# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
252# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 308# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
253python_execute_function() { 309python_execute_function() {
254 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" 310 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
255 311
256 while (($#)); do 312 while (($#)); do
257 case "$1" in 313 case "$1" in
258 --action-message) 314 --action-message)
259 action_message_template="$2" 315 action_message_template="$2"
273 quiet="1" 329 quiet="1"
274 ;; 330 ;;
275 -s|--separate-build-dirs) 331 -s|--separate-build-dirs)
276 separate_build_dirs="1" 332 separate_build_dirs="1"
277 ;; 333 ;;
334 --source-dir)
335 source_dir="$2"
336 shift
337 ;;
278 --) 338 --)
279 break 339 break
280 ;; 340 ;;
281 -*) 341 -*)
282 die "${FUNCNAME}(): Unrecognized option '$1'" 342 die "${FUNCNAME}(): Unrecognized option '$1'"
286 ;; 346 ;;
287 esac 347 esac
288 shift 348 shift
289 done 349 done
290 350
351 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
352 die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
353 fi
354
291 if [[ "${default_function}" == "0" ]]; then 355 if [[ "${default_function}" == "0" ]]; then
292 if [[ "$#" -eq "0" ]]; then 356 if [[ "$#" -eq 0 ]]; then
293 die "${FUNCNAME}(): Missing function name" 357 die "${FUNCNAME}(): Missing function name"
294 fi 358 fi
295 function="$1" 359 function="$1"
296 shift 360 shift
297 361
298 if [[ -z "$(type -t "${function}")" ]]; then 362 if [[ -z "$(type -t "${function}")" ]]; then
299 die "${FUNCNAME}(): '${function}' function isn't defined" 363 die "${FUNCNAME}(): '${function}' function is not defined"
300 fi 364 fi
301 else 365 else
302 if [[ "$#" -ne "0" ]]; then 366 if [[ "$#" -ne "0" ]]; then
303 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously" 367 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
304 fi 368 fi
305 if has "${EAPI:-0}" 0 1; then 369 if has "${EAPI:-0}" 0 1; then
306 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI" 370 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
307 fi 371 fi
308 372
309 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 373 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
310 if has "${EAPI}" 2; then 374 if has "${EAPI}" 2 3; then
311 python_default_function() { 375 python_default_function() {
312 econf 376 econf
313 } 377 }
314 else 378 else
315 python_default_function() { 379 python_default_function() {
331 elif [[ "${EBUILD_PHASE}" == "install" ]]; then 395 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
332 python_default_function() { 396 python_default_function() {
333 emake DESTDIR="${D}" install 397 emake DESTDIR="${D}" install
334 } 398 }
335 else 399 else
336 die "${FUNCNAME}(): --default-function option cannot be used in this ebuild phase" 400 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
337 fi 401 fi
338 function="python_default_function" 402 function="python_default_function"
339 fi 403 fi
340 404
341 if [[ "${quiet}" == "0" ]]; then 405 if [[ "${quiet}" == "0" ]]; then
375 fi 439 fi
376 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 440 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
377 fi 441 fi
378 442
379 if [[ "${separate_build_dirs}" == "1" ]]; then 443 if [[ "${separate_build_dirs}" == "1" ]]; then
444 if [[ -n "${source_dir}" ]]; then
445 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
446 else
380 export BUILDDIR="${S}-${PYTHON_ABI}" 447 export BUILDDIR="${S}-${PYTHON_ABI}"
448 fi
381 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 449 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
382 else 450 else
383 export BUILDDIR="${S}" 451 export BUILDDIR="${S}"
384 fi 452 fi
385 453
386 previous_directory_stack_length="${#DIRSTACK[@]}" 454 previous_directory="$(pwd)"
455 previous_directory_stack="$(dirs -p)"
456 previous_directory_stack_length="$(dirs -p | wc -l)"
387 457
388 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 458 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
389 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@" 459 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
390 else 460 else
391 EPYTHON="$(PYTHON)" "${function}" "$@" 461 EPYTHON="$(PYTHON)" "${function}" "$@"
392 fi 462 fi
393 463
402 if [[ "${quiet}" == "0" ]]; then 472 if [[ "${quiet}" == "0" ]]; then
403 ewarn "${RED}${failure_message}${NORMAL}" 473 ewarn "${RED}${failure_message}${NORMAL}"
404 fi 474 fi
405 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 475 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
406 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then 476 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
407 local ABI enabled_PYTHON_ABIS= 477 local enabled_PYTHON_ABIS= other_PYTHON_ABI
408 for ABI in ${PYTHON_ABIS}; do 478 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
409 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}" 479 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
410 done 480 done
411 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }" 481 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
412 fi 482 fi
413 if [[ "${quiet}" == "0" ]]; then 483 if [[ "${quiet}" == "0" ]]; then
414 ewarn "${RED}${failure_message}${NORMAL}" 484 ewarn "${RED}${failure_message}${NORMAL}"
415 fi 485 fi
416 if [[ -z "${PYTHON_ABIS}" ]]; then 486 if [[ -z "${PYTHON_ABIS}" ]]; then
419 else 489 else
420 die "${failure_message}" 490 die "${failure_message}"
421 fi 491 fi
422 fi 492 fi
423 493
494 # Ensure that directory stack has not been decreased.
424 if [[ "${#DIRSTACK[@]}" -lt "${previous_directory_stack_length}" ]]; then 495 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
425 die "Directory stack decreased illegally" 496 die "Directory stack decreased illegally"
426 fi 497 fi
427 498
499 # Avoid side effects of earlier returning from the specified function.
428 while [[ "${#DIRSTACK[@]}" -gt "${previous_directory_stack_length}" ]]; do 500 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
429 popd > /dev/null || die "popd failed" 501 popd > /dev/null || die "popd failed"
430 done 502 done
431 503
504 # Ensure that the bottom part of directory stack has not been changed. Restore
505 # previous directory (from before running of the specified function) before
506 # comparison of directory stacks to avoid mismatch of directory stacks after
507 # potential using of 'cd' to change current directory. Restoration of previous
508 # directory allows to safely use 'cd' to change current directory in the
509 # specified function without changing it back to original directory.
510 cd "${previous_directory}"
511 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
512 die "Directory stack changed illegally"
513 fi
514
432 if [[ "${separate_build_dirs}" == "1" ]]; then 515 if [[ "${separate_build_dirs}" == "1" ]]; then
433 popd > /dev/null || die "popd failed" 516 popd > /dev/null || die "popd failed"
434 fi 517 fi
435 unset BUILDDIR 518 unset BUILDDIR
436 done 519 done
438 if [[ "${default_function}" == "1" ]]; then 521 if [[ "${default_function}" == "1" ]]; then
439 unset -f python_default_function 522 unset -f python_default_function
440 fi 523 fi
441} 524}
442 525
443
444# @ECLASS-VARIABLE: PYTHON_USE_WITH
445# @DESCRIPTION:
446# Set this to a space separated list of use flags
447# the python slot in use must be built with.
448
449# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
450# @DESCRIPTION:
451# Set this to a space separated list of use flags
452# of which one must be turned on for the slot of
453# in use.
454
455# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
456# @DESCRIPTION:
457# Set this if you need to make either PYTHON_USE_WITH or
458# PYTHON_USE_WITH_OR atoms conditional under a use flag.
459
460# @FUNCTION: python_pkg_setup 526# @FUNCTION: python_copy_sources
527# @USAGE: [--no-link] [--] [directory]
461# @DESCRIPTION: 528# @DESCRIPTION:
462# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 529# Copy unpacked sources of given package for each Python ABI.
463# are respected. Only exported if one of those variables is set. 530python_copy_sources() {
464if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 531 local dir dirs=() no_link="0" PYTHON_ABI
465 python_pkg_setup_fail() {
466 eerror "${1}"
467 die "${1}"
468 }
469 532
470 python_pkg_setup() { 533 while (($#)); do
471 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return 534 case "$1" in
535 --no-link)
536 no_link="1"
537 ;;
538 --)
539 break
540 ;;
541 -*)
542 die "${FUNCNAME}(): Unrecognized option '$1'"
543 ;;
544 *)
545 break
546 ;;
547 esac
548 shift
549 done
472 550
473 python_version 551 if [[ "$#" -eq 0 ]]; then
474 local failed 552 if [[ "${WORKDIR}" == "${S}" ]]; then
475 local pyatom="dev-lang/python:${PYVER}" 553 die "${FUNCNAME}() cannot be used"
554 fi
555 dirs="${S}"
556 else
557 dirs="$@"
558 fi
476 559
477 for use in ${PYTHON_USE_WITH}; do 560 validate_PYTHON_ABIS
478 if ! has_version "${pyatom}[${use}]"; then 561 for PYTHON_ABI in ${PYTHON_ABIS}; do
479 python_pkg_setup_fail \ 562 for dir in "${dirs[@]}"; do
480 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}" 563 if [[ "${no_link}" == "1" ]]; then
564 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
565 else
566 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
481 fi 567 fi
482 done 568 done
483
484 for use in ${PYTHON_USE_WITH_OR}; do
485 if has_version "${pyatom}[${use}]"; then
486 return
487 fi
488 done 569 done
570}
489 571
490 if [[ ${PYTHON_USE_WITH_OR} ]]; then 572# @FUNCTION: python_set_build_dir_symlink
491 python_pkg_setup_fail \ 573# @USAGE: [directory="build"]
492 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}" 574# @DESCRIPTION:
493 fi 575# Create build directory symlink.
494 } 576python_set_build_dir_symlink() {
577 local dir="$1"
495 578
496 EXPORT_FUNCTIONS pkg_setup 579 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
580 [[ -z "${dir}" ]] && dir="build"
497 581
498 if [[ ${PYTHON_USE_WITH} ]]; then 582 # Do not delete preexistent directories.
499 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 583 rm -f "${dir}" || die "Deletion of '${dir}' failed"
500 elif [[ ${PYTHON_USE_WITH_OR} ]]; then 584 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
501 PYTHON_USE_WITH_ATOM="|| ( " 585}
502 for use in ${PYTHON_USE_WITH_OR}; do 586
503 PYTHON_USE_WITH_ATOM=" 587# @FUNCTION: python_generate_wrapper_scripts
504 ${PYTHON_USE_WITH_ATOM} 588# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
505 ${PYTHON_ATOM}[${use}]" 589# @DESCRIPTION:
590# Generate wrapper scripts. Existing files are overwritten only with --force option.
591# If --respect-EPYTHON option is specified, then generated wrapper scripts will
592# respect EPYTHON variable at run time.
593python_generate_wrapper_scripts() {
594 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python2_supported_versions python3_enabled="0" python3_supported_versions respect_EPYTHON="0"
595 python2_supported_versions="2.4 2.5 2.6 2.7"
596 python3_supported_versions="3.0 3.1 3.2"
597
598 while (($#)); do
599 case "$1" in
600 -E|--respect-EPYTHON)
601 respect_EPYTHON="1"
602 ;;
603 -f|--force)
604 force="1"
605 ;;
606 -q|--quiet)
607 quiet="1"
608 ;;
609 --)
610 break
611 ;;
612 -*)
613 die "${FUNCNAME}(): Unrecognized option '$1'"
614 ;;
615 *)
616 break
617 ;;
618 esac
619 shift
506 done 620 done
507 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )" 621
622 if [[ "$#" -eq 0 ]]; then
623 die "${FUNCNAME}(): Missing arguments"
624 fi
625
626 validate_PYTHON_ABIS
627 for PYTHON_ABI in ${python2_supported_versions}; do
628 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
629 python2_enabled="1"
508 fi 630 fi
509 if [[ ${PYTHON_USE_WITH_OPT} ]]; then 631 done
510 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 632 for PYTHON_ABI in ${python3_supported_versions}; do
633 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
634 python3_enabled="1"
511 fi 635 fi
512 DEPEND="${PYTHON_USE_WITH_ATOM}" 636 done
513 RDEPEND="${PYTHON_USE_WITH_ATOM}" 637
638 if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
639 eselect_python_option=
640 elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
641 eselect_python_option="--python2"
642 elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
643 eselect_python_option="--python3"
644 else
645 die "${FUNCNAME}(): Unsupported environment"
514fi 646 fi
515 647
516# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS 648 for file in "$@"; do
517# @DESCRIPTION: 649 if [[ -f "${file}" && "${force}" == "0" ]]; then
518# Set this to define default functions for the following ebuild phases: 650 die "${FUNCNAME}(): '$1' already exists"
519# src_prepare, src_configure, src_compile, src_test, src_install. 651 fi
520if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
521 python_src_prepare() {
522 python_copy_sources
523 }
524 652
525 for python_default_function in src_configure src_compile src_test src_install; do 653 if [[ "${quiet}" == "0" ]]; then
526 eval "python_${python_default_function}() { python_execute_function -d -s; }" 654 einfo "Generating '${file#${D%/}}' wrapper script"
527 done 655 fi
528 unset python_default_function
529 656
530 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install 657 cat << EOF > "${file}"
658#!/usr/bin/env python
659# Gentoo '${file##*/}' wrapper script
660
661import os
662import re
663import subprocess
664import sys
665
666EPYTHON_re = re.compile(r"^python(\d+\.\d+)$")
667
668EOF
669 if [[ "$?" != "0" ]]; then
670 die "${FUNCNAME}(): Generation of '$1' failed"
671 fi
672 if [[ "${respect_EPYTHON}" == "1" ]]; then
673 cat << EOF >> "${file}"
674EPYTHON = os.environ.get("EPYTHON")
675if EPYTHON:
676 EPYTHON_matched = EPYTHON_re.match(EPYTHON)
677 if EPYTHON_matched:
678 PYTHON_ABI = EPYTHON_matched.group(1)
679 else:
680 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
681 sys.exit(1)
682else:
683 try:
684 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
685 if eselect_process.wait() != 0:
686 raise ValueError
687 except (OSError, ValueError):
688 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
689 sys.exit(1)
690
691 eselect_output = eselect_process.stdout.read()
692 if not isinstance(eselect_output, str):
693 # Python 3
694 eselect_output = eselect_output.decode()
695
696 EPYTHON_matched = EPYTHON_re.match(eselect_output)
697 if EPYTHON_matched:
698 PYTHON_ABI = EPYTHON_matched.group(1)
699 else:
700 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
701 sys.exit(1)
702EOF
703 if [[ "$?" != "0" ]]; then
704 die "${FUNCNAME}(): Generation of '$1' failed"
705 fi
706 else
707 cat << EOF >> "${file}"
708try:
709 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
710 if eselect_process.wait() != 0:
711 raise ValueError
712except (OSError, ValueError):
713 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
714 sys.exit(1)
715
716eselect_output = eselect_process.stdout.read()
717if not isinstance(eselect_output, str):
718 # Python 3
719 eselect_output = eselect_output.decode()
720
721EPYTHON_matched = EPYTHON_re.match(eselect_output)
722if EPYTHON_matched:
723 PYTHON_ABI = EPYTHON_matched.group(1)
724else:
725 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
726 sys.exit(1)
727EOF
728 if [[ "$?" != "0" ]]; then
729 die "${FUNCNAME}(): Generation of '$1' failed"
730 fi
731 fi
732 cat << EOF >> "${file}"
733
734os.environ["PYTHON_PROCESS_NAME"] = sys.argv[0]
735target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI)
736if not os.path.exists(target_executable):
737 sys.stderr.write("'%s' does not exist\n" % target_executable)
738 sys.exit(1)
739
740os.execv(target_executable, sys.argv)
741EOF
742 if [[ "$?" != "0" ]]; then
743 die "${FUNCNAME}(): Generation of '$1' failed"
744 fi
745 fperms +x "${file#${D%/}}" || die "fperms '${file}' failed"
746 done
747}
748
749# ================================================================================================
750# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======
751# ================================================================================================
752
753# @FUNCTION: python_set_active_version
754# @USAGE: <Python_ABI|2|3>
755# @DESCRIPTION:
756# Set active version of Python.
757python_set_active_version() {
758 if [[ "$#" -ne "1" ]]; then
759 die "${FUNCNAME}() requires 1 argument"
531fi 760 fi
532 761
533# @FUNCTION: python_disable_pyc 762 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
534# @DESCRIPTION: 763 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
535# Tell Python not to automatically recompile modules to .pyc/.pyo 764 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
536# even if the timestamps/version stamps don't match. This is done 765 fi
537# to protect sandbox. 766 export EPYTHON="$(PYTHON "$1")"
538python_disable_pyc() { 767 elif [[ "$1" == "2" ]]; then
539 export PYTHONDONTWRITEBYTECODE="1" 768 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
540} 769 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
770 fi
771 export EPYTHON="$(PYTHON -2)"
772 elif [[ "$1" == "3" ]]; then
773 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
774 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
775 fi
776 export EPYTHON="$(PYTHON -3)"
777 else
778 die "${FUNCNAME}(): Unrecognized argument '$1'"
779 fi
541 780
542# @FUNCTION: python_enable_pyc 781 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
543# @DESCRIPTION: 782 # so it does not need to be exported to subprocesses.
544# Tell Python to automatically recompile modules to .pyc/.pyo if the 783 PYTHON_ABI="${EPYTHON#python}"
545# timestamps/version stamps have changed. 784 PYTHON_ABI="${PYTHON_ABI%%-*}"
546python_enable_pyc() {
547 unset PYTHONDONTWRITEBYTECODE
548} 785}
549
550python_disable_pyc
551 786
552# @FUNCTION: python_need_rebuild 787# @FUNCTION: python_need_rebuild
553# @DESCRIPTION: Run without arguments, specifies that the package should be 788# @DESCRIPTION: Run without arguments, specifies that the package should be
554# rebuilt after a python upgrade. 789# rebuilt after a python upgrade.
790# Do not use this function in ebuilds of packages supporting installation
791# for multiple versions of Python.
555python_need_rebuild() { 792python_need_rebuild() {
556 python_version 793 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
557 export PYTHON_NEED_REBUILD=${PYVER} 794}
795
796# ================================================================================================
797# ======================================= GETTER FUNCTIONS =======================================
798# ================================================================================================
799
800# @FUNCTION: PYTHON
801# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
802# @DESCRIPTION:
803# Get Python interpreter filename for specified Python ABI. If Python_ABI argument
804# is ommitted, then PYTHON_ABI environment variable must be set and is used.
805# If -2 option is specified, then active version of Python 2 is used.
806# If -3 option is specified, then active version of Python 3 is used.
807# If --active option is specified, then active version of Python is used.
808# Active version of Python can be set by python_set_active_version().
809# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
810# -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
811# If --ABI option is specified, then only specified Python ABI is printed instead of
812# Python interpreter filename.
813# --ABI and --absolute-path options cannot be specified simultaneously.
814PYTHON() {
815 local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" python2="0" python3="0" slot=
816
817 while (($#)); do
818 case "$1" in
819 -2)
820 python2="1"
821 ;;
822 -3)
823 python3="1"
824 ;;
825 --ABI)
826 ABI_output="1"
827 ;;
828 -A|--active)
829 active="1"
830 ;;
831 -a|--absolute-path)
832 absolute_path_output="1"
833 ;;
834 -f|--final-ABI)
835 final_ABI="1"
836 ;;
837 --)
838 break
839 ;;
840 -*)
841 die "${FUNCNAME}(): Unrecognized option '$1'"
842 ;;
843 *)
844 break
845 ;;
846 esac
847 shift
848 done
849
850 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
851 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
852 fi
853
854 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then
855 die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously"
856 fi
857
858 if [[ "$#" -eq 0 ]]; then
859 if [[ "${active}" == "1" ]]; then
860 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
861 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
862 fi
863 slot="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
864 elif [[ "${final_ABI}" == "1" ]]; then
865 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
866 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python"
867 fi
868 validate_PYTHON_ABIS
869 slot="${PYTHON_ABIS##* }"
870 elif [[ "${python2}" == "1" ]]; then
871 slot="$(eselect python show --python2)"
872 if [[ -z "${slot}" ]]; then
873 die "${FUNCNAME}(): Active Python 2 interpreter not set"
874 elif [[ "${slot}" != "python2."* ]]; then
875 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
876 fi
877 slot="${slot#python}"
878 elif [[ "${python3}" == "1" ]]; then
879 slot="$(eselect python show --python3)"
880 if [[ -z "${slot}" ]]; then
881 die "${FUNCNAME}(): Active Python 3 interpreter not set"
882 elif [[ "${slot}" != "python3."* ]]; then
883 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
884 fi
885 slot="${slot#python}"
886 elif [[ -n "${PYTHON_ABI}" ]]; then
887 slot="${PYTHON_ABI}"
888 else
889 die "${FUNCNAME}(): Invalid usage"
890 fi
891 elif [[ "$#" -eq 1 ]]; then
892 if [[ "${active}" == "1" ]]; then
893 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
894 fi
895 if [[ "${final_ABI}" == "1" ]]; then
896 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
897 fi
898 if [[ "${python2}" == "1" ]]; then
899 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
900 fi
901 if [[ "${python3}" == "1" ]]; then
902 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
903 fi
904 slot="$1"
905 else
906 die "${FUNCNAME}(): Invalid usage"
907 fi
908
909 if [[ "${ABI_output}" == "1" ]]; then
910 echo -n "${slot}"
911 return
912 elif [[ "${absolute_path_output}" == "1" ]]; then
913 echo -n "/usr/bin/python${slot}"
914 else
915 echo -n "python${slot}"
916 fi
917
918 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
919 echo -n "-${ABI}"
920 fi
558} 921}
559 922
560# @FUNCTION: python_get_includedir 923# @FUNCTION: python_get_includedir
924# @USAGE: [-f|--final-ABI]
561# @DESCRIPTION: 925# @DESCRIPTION:
562# Run without arguments, returns the Python include directory. 926# Print Python include directory.
927# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
563python_get_includedir() { 928python_get_includedir() {
929 local final_ABI="0"
930
931 while (($#)); do
932 case "$1" in
933 -f|--final-ABI)
934 final_ABI="1"
935 ;;
936 -*)
937 die "${FUNCNAME}(): Unrecognized option '$1'"
938 ;;
939 *)
940 die "${FUNCNAME}(): Invalid usage"
941 ;;
942 esac
943 shift
944 done
945
946 if [[ "${final_ABI}" == "1" ]]; then
947 echo "/usr/include/python$(PYTHON -f --ABI)"
564 if [[ -n "${PYTHON_ABI}" ]]; then 948 elif [[ -n "${PYTHON_ABI}" ]]; then
565 echo "/usr/include/python${PYTHON_ABI}" 949 echo "/usr/include/python${PYTHON_ABI}"
566 else 950 else
567 python_version
568 echo "/usr/include/python${PYVER}" 951 echo "/usr/include/python$(PYTHON -A --ABI)"
569 fi 952 fi
570} 953}
571 954
572# @FUNCTION: python_get_libdir 955# @FUNCTION: python_get_libdir
956# @USAGE: [-f|--final-ABI]
573# @DESCRIPTION: 957# @DESCRIPTION:
574# Run without arguments, returns the Python library directory. 958# Print Python library directory.
959# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
575python_get_libdir() { 960python_get_libdir() {
961 local final_ABI="0"
962
963 while (($#)); do
964 case "$1" in
965 -f|--final-ABI)
966 final_ABI="1"
967 ;;
968 -*)
969 die "${FUNCNAME}(): Unrecognized option '$1'"
970 ;;
971 *)
972 die "${FUNCNAME}(): Invalid usage"
973 ;;
974 esac
975 shift
976 done
977
978 if [[ "${final_ABI}" == "1" ]]; then
979 echo "/usr/$(get_libdir)/python$(PYTHON -f --ABI)"
576 if [[ -n "${PYTHON_ABI}" ]]; then 980 elif [[ -n "${PYTHON_ABI}" ]]; then
577 echo "/usr/$(get_libdir)/python${PYTHON_ABI}" 981 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
578 else 982 else
579 python_version
580 echo "/usr/$(get_libdir)/python${PYVER}" 983 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)"
581 fi 984 fi
582} 985}
583 986
584# @FUNCTION: python_get_sitedir 987# @FUNCTION: python_get_sitedir
988# @USAGE: [-f|--final-ABI]
585# @DESCRIPTION: 989# @DESCRIPTION:
586# Run without arguments, returns the Python site-packages directory. 990# Print Python site-packages directory.
991# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
587python_get_sitedir() { 992python_get_sitedir() {
993 local options=()
994
995 while (($#)); do
996 case "$1" in
997 -f|--final-ABI)
998 options+=("$1")
999 ;;
1000 -*)
1001 die "${FUNCNAME}(): Unrecognized option '$1'"
1002 ;;
1003 *)
1004 die "${FUNCNAME}(): Invalid usage"
1005 ;;
1006 esac
1007 shift
1008 done
1009
588 echo "$(python_get_libdir)/site-packages" 1010 echo "$(python_get_libdir "${options[@]}")/site-packages"
589} 1011}
590 1012
591# @FUNCTION: python_tkinter_exists 1013# ================================================================================================
1014# =================================== MISCELLANEOUS FUNCTIONS ====================================
1015# ================================================================================================
1016
1017_python_implementation() {
1018 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1019 return 0
1020 else
1021 return 1
1022 fi
1023}
1024
1025# @FUNCTION: python_convert_shebangs
1026# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
592# @DESCRIPTION: 1027# @DESCRIPTION:
593# Run without arguments, checks if python was compiled with Tkinter 1028# Convert shebangs in specified files. Directories can be specified only with --recursive option.
594# support. If not, prints an error message and dies. 1029python_convert_shebangs() {
595python_tkinter_exists() { 1030 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
596 if ! python -c "import Tkinter" >/dev/null 2>&1; then 1031
597 eerror "You need to recompile python with Tkinter support." 1032 while (($#)); do
598 eerror "Try adding: 'dev-lang/python tk'" 1033 case "$1" in
599 eerror "in to /etc/portage/package.use" 1034 -r|--recursive)
600 echo 1035 recursive="1"
601 die "missing tkinter support with installed python" 1036 ;;
1037 -q|--quiet)
1038 quiet="1"
1039 ;;
1040 -x|--only-executables)
1041 only_executables="1"
1042 ;;
1043 --)
1044 break
1045 ;;
1046 -*)
1047 die "${FUNCNAME}(): Unrecognized option '$1'"
1048 ;;
1049 *)
1050 break
1051 ;;
1052 esac
1053 shift
1054 done
1055
1056 if [[ "$#" -eq 0 ]]; then
1057 die "${FUNCNAME}(): Missing Python version and files or directories"
1058 elif [[ "$#" -eq 1 ]]; then
1059 die "${FUNCNAME}(): Missing files or directories"
1060 fi
1061
1062 python_version="$1"
1063 shift
1064
1065 for argument in "$@"; do
1066 if [[ ! -e "${argument}" ]]; then
1067 die "${FUNCNAME}(): '${argument}' does not exist"
1068 elif [[ -f "${argument}" ]]; then
1069 files+=("${argument}")
1070 elif [[ -d "${argument}" ]]; then
1071 if [[ "${recursive}" == "1" ]]; then
1072 if [[ "${only_executables}" == "1" ]]; then
1073 files+=($(find "${argument}" -perm /111 -type f))
1074 else
1075 files+=($(find "${argument}" -type f))
1076 fi
1077 else
1078 die "${FUNCNAME}(): '${argument}' is not a regular file"
1079 fi
1080 else
1081 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
602 fi 1082 fi
1083 done
1084
1085 for file in "${files[@]}"; do
1086 file="${file#./}"
1087 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1088
1089 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1090 if [[ "${quiet}" == "0" ]]; then
1091 einfo "Converting shebang in '${file}'"
1092 fi
1093 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1094
1095 # Delete potential whitespace after "#!".
1096 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1097 fi
1098 done
603} 1099}
604 1100
605# @FUNCTION: python_mod_exists 1101# @FUNCTION: python_mod_exists
606# @USAGE: <module> 1102# @USAGE: <module>
607# @DESCRIPTION: 1103# @DESCRIPTION:
617python_mod_exists() { 1113python_mod_exists() {
618 [[ "$1" ]] || die "${FUNCNAME} requires an argument!" 1114 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
619 python -c "import $1" &>/dev/null 1115 python -c "import $1" &>/dev/null
620} 1116}
621 1117
1118# @FUNCTION: python_tkinter_exists
1119# @DESCRIPTION:
1120# Run without arguments, checks if python was compiled with Tkinter
1121# support. If not, prints an error message and dies.
1122python_tkinter_exists() {
1123 if ! python -c "import Tkinter" >/dev/null 2>&1; then
1124 eerror "You need to recompile python with Tkinter support."
1125 eerror "Try adding: 'dev-lang/python tk'"
1126 eerror "in to /etc/portage/package.use"
1127 echo
1128 die "missing tkinter support with installed python"
1129 fi
1130}
1131
1132# ================================================================================================
1133# ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
1134# ================================================================================================
1135
1136# @FUNCTION: python_enable_pyc
1137# @DESCRIPTION:
1138# Tell Python to automatically recompile modules to .pyc/.pyo if the
1139# timestamps/version stamps have changed.
1140python_enable_pyc() {
1141 unset PYTHONDONTWRITEBYTECODE
1142}
1143
622# @FUNCTION: python_mod_compile 1144# @FUNCTION: python_disable_pyc
623# @USAGE: <file> [more files ...]
624# @DESCRIPTION: 1145# @DESCRIPTION:
625# Given filenames, it will pre-compile the module's .pyc and .pyo. 1146# Tell Python not to automatically recompile modules to .pyc/.pyo
626# This function should only be run in pkg_postinst() 1147# even if the timestamps/version stamps do not match. This is done
627# 1148# to protect sandbox.
628# Example: 1149python_disable_pyc() {
629# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 1150 export PYTHONDONTWRITEBYTECODE="1"
630#
631python_mod_compile() {
632 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
633 die "${FUNCNAME}() cannot be used in this EAPI"
634 fi
635
636 local f myroot myfiles=()
637
638 # Check if phase is pkg_postinst()
639 [[ ${EBUILD_PHASE} != postinst ]] &&\
640 die "${FUNCNAME} should only be run in pkg_postinst()"
641
642 # allow compiling for older python versions
643 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
644 PYVER=${PYTHON_OVERRIDE_PYVER}
645 else
646 python_version
647 fi
648
649 # strip trailing slash
650 myroot="${ROOT%/}"
651
652 # respect ROOT
653 for f in "$@"; do
654 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
655 done
656
657 if ((${#myfiles[@]})); then
658 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
659 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" &> /dev/null
660 else
661 ewarn "No files to compile!"
662 fi
663} 1151}
664 1152
665# @FUNCTION: python_mod_optimize 1153# @FUNCTION: python_mod_optimize
666# @USAGE: [options] [directory|file] 1154# @USAGE: [options] [directory|file]
667# @DESCRIPTION: 1155# @DESCRIPTION:
679python_mod_optimize() { 1167python_mod_optimize() {
680 # Check if phase is pkg_postinst(). 1168 # Check if phase is pkg_postinst().
681 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()" 1169 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()"
682 1170
683 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1171 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
684 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=() 1172 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
685 1173
686 # Strip trailing slash from ROOT. 1174 # Strip trailing slash from ROOT.
687 root="${ROOT%/}" 1175 root="${ROOT%/}"
688 1176
689 # Respect ROOT and options passed to compileall.py. 1177 # Respect ROOT and options passed to compileall.py.
695 -d|-x) 1183 -d|-x)
696 options+=("$1" "$2") 1184 options+=("$1" "$2")
697 shift 1185 shift
698 ;; 1186 ;;
699 -*) 1187 -*)
700 ewarn "${FUNCNAME}: Ignoring compile option $1" 1188 ewarn "${FUNCNAME}: Ignoring option '$1'"
701 ;; 1189 ;;
702 *) 1190 *)
703 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1191 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
704 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 1192 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories"
705 elif [[ "$1" =~ ^/ ]]; then 1193 elif [[ "$1" =~ ^/ ]]; then
706 if [[ -d "${root}/$1" ]]; then 1194 if [[ -d "${root}/$1" ]]; then
707 other_dirs+=("${root}/$1") 1195 other_dirs+=("${root}/$1")
708 elif [[ -f "${root}/$1" ]]; then 1196 elif [[ -f "${root}/$1" ]]; then
709 other_files+=("${root}/$1") 1197 other_files+=("${root}/$1")
710 elif [[ -e "${root}/$1" ]]; then 1198 elif [[ -e "${root}/$1" ]]; then
711 ewarn "'${root}/$1' is not a file or a directory!" 1199 ewarn "'${root}/$1' is not a file or a directory!"
712 else 1200 else
713 ewarn "'${root}/$1' doesn't exist!" 1201 ewarn "'${root}/$1' does not exist!"
714 fi 1202 fi
715 else 1203 else
716 for PYTHON_ABI in ${PYTHON_ABIS}; do 1204 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
717 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then 1205 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
718 site_packages_dirs+=("$1") 1206 site_packages_dirs+=("$1")
719 break 1207 break
720 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then 1208 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
721 site_packages_files+=("$1") 1209 site_packages_files+=("$1")
722 break 1210 break
723 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then 1211 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
724 ewarn "'$1' is not a file or a directory!" 1212 ewarn "'$1' is not a file or a directory!"
725 else 1213 else
726 ewarn "'$1' doesn't exist!" 1214 ewarn "'$1' does not exist!"
727 fi 1215 fi
728 done 1216 done
729 fi 1217 fi
730 ;; 1218 ;;
731 esac 1219 esac
733 done 1221 done
734 1222
735 # Set additional options. 1223 # Set additional options.
736 options+=("-q") 1224 options+=("-q")
737 1225
738 for PYTHON_ABI in ${PYTHON_ABIS}; do 1226 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
739 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 1227 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
740 return_code="0" 1228 return_code="0"
741 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" 1229 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
742 if ((${#site_packages_dirs[@]})); then 1230 if ((${#site_packages_dirs[@]})); then
743 for dir in "${site_packages_dirs[@]}"; do 1231 for dir in "${site_packages_dirs[@]}"; do
756 eend "${return_code}" 1244 eend "${return_code}"
757 fi 1245 fi
758 unset site_packages_absolute_dirs site_packages_absolute_files 1246 unset site_packages_absolute_dirs site_packages_absolute_files
759 done 1247 done
760 1248
761 # Don't use PYTHON_ABI in next calls to python_get_libdir(). 1249 # Restore previous value of PYTHON_ABI.
1250 if [[ -n "${previous_PYTHON_ABI}" ]]; then
1251 PYTHON_ABI="${previous_PYTHON_ABI}"
1252 else
762 unset PYTHON_ABI 1253 unset PYTHON_ABI
1254 fi
763 1255
764 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1256 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
765 return_code="0" 1257 return_code="0"
766 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." 1258 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)"
767 if ((${#other_dirs[@]})); then 1259 if ((${#other_dirs[@]})); then
768 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 1260 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
769 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" 1261 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
770 fi 1262 fi
771 if ((${#other_files[@]})); then 1263 if ((${#other_files[@]})); then
772 python${PYVER} "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 1264 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
773 python${PYVER} -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1" 1265 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
774 fi 1266 fi
775 eend "${return_code}" 1267 eend "${return_code}"
776 fi 1268 fi
777 else 1269 else
778 local myroot mydirs=() myfiles=() myopts=() return_code="0" 1270 local myroot mydirs=() myfiles=() myopts=() return_code="0"
789 -d|-x) 1281 -d|-x)
790 myopts+=("$1" "$2") 1282 myopts+=("$1" "$2")
791 shift 1283 shift
792 ;; 1284 ;;
793 -*) 1285 -*)
794 ewarn "${FUNCNAME}: Ignoring compile option $1" 1286 ewarn "${FUNCNAME}: Ignoring option '$1'"
795 ;; 1287 ;;
796 *) 1288 *)
797 if [[ -d "${myroot}"/$1 ]]; then 1289 if [[ -d "${myroot}"/$1 ]]; then
798 mydirs+=("${myroot}/$1") 1290 mydirs+=("${myroot}/$1")
799 elif [[ -f "${myroot}"/$1 ]]; then 1291 elif [[ -f "${myroot}"/$1 ]]; then
800 # Files are passed to python_mod_compile which is ROOT-aware 1292 # Files are passed to python_mod_compile which is ROOT-aware
801 myfiles+=("$1") 1293 myfiles+=("$1")
802 elif [[ -e "${myroot}/$1" ]]; then 1294 elif [[ -e "${myroot}/$1" ]]; then
803 ewarn "${myroot}/$1 is not a file or directory!" 1295 ewarn "${myroot}/$1 is not a file or directory!"
804 else 1296 else
805 ewarn "${myroot}/$1 doesn't exist!" 1297 ewarn "${myroot}/$1 does not exist!"
806 fi 1298 fi
807 ;; 1299 ;;
808 esac 1300 esac
809 shift 1301 shift
810 done 1302 done
811 1303
812 # allow compiling for older python versions
813 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
814 PYVER=${PYTHON_OVERRIDE_PYVER}
815 else
816 python_version
817 fi
818
819 # set additional opts 1304 # set additional opts
820 myopts+=(-q) 1305 myopts+=(-q)
821 1306
822 ebegin "Byte compiling python modules for python-${PYVER} .." 1307 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)"
823 if ((${#mydirs[@]})); then 1308 if ((${#mydirs[@]})); then
824 python${PYVER} \ 1309 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
825 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 1310 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
826 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
827 python${PYVER} -O \
828 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
829 "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
830 fi 1311 fi
831 1312
832 if ((${#myfiles[@]})); then 1313 if ((${#myfiles[@]})); then
833 python_mod_compile "${myfiles[@]}" 1314 python_mod_compile "${myfiles[@]}"
834 fi 1315 fi
836 eend "${return_code}" 1317 eend "${return_code}"
837 fi 1318 fi
838} 1319}
839 1320
840# @FUNCTION: python_mod_cleanup 1321# @FUNCTION: python_mod_cleanup
841# @USAGE: [directory] 1322# @USAGE: [directory|file]
842# @DESCRIPTION: 1323# @DESCRIPTION:
843# Run with optional arguments, where arguments are directories of 1324# Run with optional arguments, where arguments are Python modules. If none given,
844# python modules. If none given, it will look in /usr/lib/python[0-9].[0-9]. 1325# it will look in /usr/lib/python[0-9].[0-9].
845# 1326#
846# It will recursively scan all compiled Python modules in the directories and 1327# It will recursively scan all compiled Python modules in the directories and
847# determine if they are orphaned (i.e. their corresponding .py files are missing.) 1328# determine if they are orphaned (i.e. their corresponding .py files are missing.)
848# If they are, then it will remove their corresponding .pyc and .pyo files. 1329# If they are, then it will remove their corresponding .pyc and .pyo files.
849# 1330#
850# This function should only be run in pkg_postrm(). 1331# This function should only be run in pkg_postrm().
851python_mod_cleanup() { 1332python_mod_cleanup() {
852 local PYTHON_ABI SEARCH_PATH=() root src_py 1333 local path py_file PYTHON_ABI SEARCH_PATH=() root
853 1334
854 # Check if phase is pkg_postrm(). 1335 # Check if phase is pkg_postrm().
855 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" 1336 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
856 1337
857 # Strip trailing slash from ROOT. 1338 # Strip trailing slash from ROOT.
858 root="${ROOT%/}" 1339 root="${ROOT%/}"
859 1340
860 if (($#)); then 1341 if (($#)); then
861 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1342 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
862 while (($#)); do 1343 while (($#)); do
863 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1344 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
864 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 1345 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories"
865 elif [[ "$1" =~ ^/ ]]; then 1346 elif [[ "$1" =~ ^/ ]]; then
866 SEARCH_PATH+=("${root}/${1#/}") 1347 SEARCH_PATH+=("${root}/${1#/}")
867 else 1348 else
868 for PYTHON_ABI in ${PYTHON_ABIS}; do 1349 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
869 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 1350 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
870 done 1351 done
871 fi 1352 fi
872 shift 1353 shift
873 done 1354 done
874 else 1355 else
875 SEARCH_PATH=("${@#/}") 1356 SEARCH_PATH=("${@#/}")
876 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 1357 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
877 fi 1358 fi
878 else 1359 else
879 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) 1360 local dir sitedir
1361 for dir in "${root}"/usr/lib*; do
1362 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
1363 for sitedir in "${dir}"/python*/site-packages; do
1364 if [[ -d "${sitedir}" ]]; then
1365 SEARCH_PATH+=("${sitedir}")
1366 fi
1367 done
1368 fi
1369 done
1370 fi
1371
1372 local BLUE CYAN NORMAL
1373 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1374 BLUE=$'\e[1;34m'
1375 CYAN=$'\e[1;36m'
1376 NORMAL=$'\e[0m'
1377 else
1378 BLUE=
1379 CYAN=
1380 NORMAL=
880 fi 1381 fi
881 1382
882 for path in "${SEARCH_PATH[@]}"; do 1383 for path in "${SEARCH_PATH[@]}"; do
883 [[ ! -d "${path}" ]] && continue 1384 if [[ -d "${path}" ]]; then
884 einfo "Cleaning orphaned Python bytecode from ${path} .."
885 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 1385 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
886 src_py="${REPLY%[co]}" 1386 py_file="${REPLY%[co]}"
887 [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue 1387 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
888 einfo "Purging ${src_py}[co]" 1388 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
889 rm -f "${src_py}"[co] 1389 rm -f "${py_file}"[co]
890 done 1390 done
891 1391
892 # Attempt to remove directories that may be empty. 1392 # Attempt to delete directories, which may be empty.
893 find "${path}" -type d | sort -r | while read -r dir; do 1393 find "${path}" -type d | sort -r | while read -r dir; do
894 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}" 1394 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
895 done 1395 done
1396 elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then
1397 einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1398 rm -f "${path}"[co]
1399 fi
896 done 1400 done
897} 1401}
1402
1403# ================================================================================================
1404# ===================================== DEPRECATED FUNCTIONS =====================================
1405# ================================================================================================
1406
1407# @FUNCTION: python_version
1408# @DESCRIPTION:
1409# Run without arguments and it will export the version of python
1410# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
1411python_version() {
1412 [[ -n "${PYVER}" ]] && return 0
1413 local tmpstr
1414 python="${python:-/usr/bin/python}"
1415 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
1416 export PYVER_ALL="${tmpstr#Python }"
1417 export PYVER_MAJOR="${PYVER_ALL:0:1}"
1418 export PYVER_MINOR="${PYVER_ALL:2:1}"
1419 if [[ "${PYVER_ALL:3:1}" == "." ]]; then
1420 export PYVER_MICRO="${PYVER_ALL:4}"
1421 fi
1422 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
1423}
1424
1425# @FUNCTION: python_mod_compile
1426# @USAGE: <file> [more files ...]
1427# @DESCRIPTION:
1428# Given filenames, it will pre-compile the module's .pyc and .pyo.
1429# This function should only be run in pkg_postinst()
1430#
1431# Example:
1432# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
1433#
1434python_mod_compile() {
1435 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1436 die "${FUNCNAME}() cannot be used in this EAPI"
1437 fi
1438
1439 local f myroot myfiles=()
1440
1441 # Check if phase is pkg_postinst()
1442 [[ ${EBUILD_PHASE} != postinst ]] &&\
1443 die "${FUNCNAME} should only be run in pkg_postinst()"
1444
1445 # strip trailing slash
1446 myroot="${ROOT%/}"
1447
1448 # respect ROOT
1449 for f in "$@"; do
1450 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
1451 done
1452
1453 if ((${#myfiles[@]})); then
1454 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
1455 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
1456 else
1457 ewarn "No files to compile!"
1458 fi
1459}

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

  ViewVC Help
Powered by ViewVC 1.1.20