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

Diff of /eclass/python.eclass

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

Revision 1.66 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.66 2009/08/28 16:08:51 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 die "${FUNCNAME}(): Unrecognized option $1"
85 ;;
86 *)
87 break
88 ;;
89 esac
90 shift
91 done
92
93 if [[ "$#" -eq "0" ]]; then
94 if [[ -n "${PYTHON_ABI}" ]]; then
95 slot="${PYTHON_ABI}"
96 else
97 die "${FUNCNAME}(): Invalid usage"
98 fi
99 elif [[ "$#" -eq "1" ]]; then
100 slot="$1"
101 else
102 die "${FUNCNAME}(): Invalid usage"
103 fi
104
105 if [[ "${absolute_path}" == "1" ]]; then
106 echo -n "/usr/bin/python${slot}"
107 else
108 echo -n "python${slot}"
109 fi
110}
111 136
112# @FUNCTION: validate_PYTHON_ABIS 137# @FUNCTION: validate_PYTHON_ABIS
113# @DESCRIPTION: 138# @DESCRIPTION:
114# 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.
115validate_PYTHON_ABIS() { 141validate_PYTHON_ABIS() {
116 # 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.
117 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
118 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"
119 fi 145 fi
120 146
121 # 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.
122 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."
123 die "'/usr/bin/python' isn't valid symlink" 151 die "'/usr/bin/python' is not valid symlink"
124 fi 152 fi
125 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."
126 die "'/usr/bin/python-config' isn't valid script" 156 die "'/usr/bin/python-config' is not valid script"
127 fi 157 fi
128 158
129 # 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.
130 if [[ -z "${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
131 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=
132 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"
165
166 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
167 local python2_enabled="0" python3_enabled="0"
168
169 if [[ -z "${USE_PYTHON}" ]]; then
170 die "USE_PYTHON variable is empty"
171 fi
172
133 for ABI in ${USE_PYTHON}; do 173 for PYTHON_ABI in ${USE_PYTHON}; do
134 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 174 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
135 ewarn "Ignoring unsupported Python ABI '${ABI}'" 175 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
136 continue
137 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
138 support_ABI="1" 185 support_ABI="1"
139 if [[ -z "${IGNORE_RESTRICT_PYTHON_ABIS}" ]]; then
140 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 186 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
141 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 187 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
142 support_ABI="0" 188 support_ABI="0"
143 break 189 break
144 fi 190 fi
145 done 191 done
192 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
193 done
194
195 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
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."
204 fi
205 else
206 local python_version python2_version= python3_version= support_python_major_version
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"
146 fi 213 fi
147 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 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"
219 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
220 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
221 support_python_major_version="0"
222 fi
223 done
224 [[ "${support_python_major_version}" == "1" ]] && break
225 done
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
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% }"
278 fi
279 fi
280
281 if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
282 local PYTHON_ABI
283 for PYTHON_ABI in ${PYTHON_ABIS}; do
284 # Ensure that appropriate version of Python is installed.
285 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
286 die "dev-lang/python:${PYTHON_ABI} is not installed"
287 fi
288
289 # Ensure that EPYTHON variable is respected.
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]]))')'"
297 die "'python' does not respect EPYTHON variable"
298 fi
148 done 299 done
149 export PYTHON_ABIS="${supported_PYTHON_ABIS# }" 300 PYTHON_ABIS_SANITY_CHECKS="1"
150 fi
151
152 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
153 python_version
154 export PYTHON_ABIS="${PYVER}"
155 fi
156
157 # Ensure that EPYTHON variable is respected.
158 local PYTHON_ABI
159 for PYTHON_ABI in ${PYTHON_ABIS}; do
160 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
161 die "'python' doesn't respect EPYTHON variable"
162 fi 301 fi
163 done
164}
165
166# @FUNCTION: python_copy_sources
167# @USAGE: [directory]
168# @DESCRIPTION:
169# Copy unpacked sources of given package for each Python ABI.
170python_copy_sources() {
171 local dir dirs=() PYTHON_ABI
172
173 if [[ "$#" -eq "0" ]]; then
174 if [[ "${WORKDIR}" == "${S}" ]]; then
175 die "${FUNCNAME}() cannot be used"
176 fi
177 dirs="${S}"
178 else
179 dirs="$@"
180 fi
181
182 validate_PYTHON_ABIS
183 for PYTHON_ABI in ${PYTHON_ABIS}; do
184 for dir in "${dirs[@]}"; do
185 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
186 done
187 done
188}
189
190# @FUNCTION: python_set_build_dir_symlink
191# @USAGE: [directory="build"]
192# @DESCRIPTION:
193# Create build directory symlink.
194python_set_build_dir_symlink() {
195 local dir="$1"
196
197 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
198 [[ -z "${dir}" ]] && dir="build"
199
200 # Don't delete preexistent directories.
201 rm -f "${dir}" || die "Deletion of '${dir}' failed"
202 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
203} 302}
204 303
205# @FUNCTION: python_execute_function 304# @FUNCTION: python_execute_function
206# @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]
207# @DESCRIPTION: 306# @DESCRIPTION:
208# 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
209# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 308# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
210python_execute_function() { 309python_execute_function() {
211 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" 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=
212 311
213 while (($#)); do 312 while (($#)); do
214 case "$1" in 313 case "$1" in
215 --action-message) 314 --action-message)
216 action_message_template="$2" 315 action_message_template="$2"
230 quiet="1" 329 quiet="1"
231 ;; 330 ;;
232 -s|--separate-build-dirs) 331 -s|--separate-build-dirs)
233 separate_build_dirs="1" 332 separate_build_dirs="1"
234 ;; 333 ;;
334 --source-dir)
335 source_dir="$2"
336 shift
337 ;;
338 --)
339 break
340 ;;
235 -*) 341 -*)
236 die "${FUNCNAME}(): Unrecognized option $1" 342 die "${FUNCNAME}(): Unrecognized option '$1'"
237 ;; 343 ;;
238 *) 344 *)
239 break 345 break
240 ;; 346 ;;
241 esac 347 esac
242 shift 348 shift
243 done 349 done
244 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
245 if [[ "${default_function}" == "0" ]]; then 355 if [[ "${default_function}" == "0" ]]; then
246 if [[ "$#" -eq "0" ]]; then 356 if [[ "$#" -eq 0 ]]; then
247 die "${FUNCNAME}(): Missing function name" 357 die "${FUNCNAME}(): Missing function name"
248 fi 358 fi
249 function="$1" 359 function="$1"
250 shift 360 shift
361
362 if [[ -z "$(type -t "${function}")" ]]; then
363 die "${FUNCNAME}(): '${function}' function is not defined"
364 fi
251 else 365 else
252 if [[ "$#" -ne "0" ]]; then 366 if [[ "$#" -ne "0" ]]; then
253 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"
254 fi 368 fi
255 if has "${EAPI:-0}" 0 1; then 369 if has "${EAPI:-0}" 0 1; then
256 die "${FUNCNAME}(): --default-function option cannot be used in this EAPI" 370 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
257 fi 371 fi
258 372
259 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 373 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
260 if has "${EAPI}" 2; then 374 if has "${EAPI}" 2 3; then
261 python_default_function() { 375 python_default_function() {
262 econf 376 econf
263 } 377 }
264 else 378 else
265 python_default_function() { 379 python_default_function() {
281 elif [[ "${EBUILD_PHASE}" == "install" ]]; then 395 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
282 python_default_function() { 396 python_default_function() {
283 emake DESTDIR="${D}" install 397 emake DESTDIR="${D}" install
284 } 398 }
285 else 399 else
286 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"
287 fi 401 fi
288 function="python_default_function" 402 function="python_default_function"
289 fi 403 fi
290 404
291 if [[ "${quiet}" == "0" ]]; then 405 if [[ "${quiet}" == "0" ]]; then
325 fi 439 fi
326 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 440 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
327 fi 441 fi
328 442
329 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
330 export BUILDDIR="${S}-${PYTHON_ABI}" 447 export BUILDDIR="${S}-${PYTHON_ABI}"
448 fi
331 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 449 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
332 else 450 else
333 export BUILDDIR="${S}" 451 export BUILDDIR="${S}"
334 fi 452 fi
335 453
454 previous_directory="$(pwd)"
455 previous_directory_stack="$(dirs -p)"
456 previous_directory_stack_length="$(dirs -p | wc -l)"
457
336 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
337 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@" 459 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
338 else 460 else
339 EPYTHON="$(PYTHON)" "${function}" "$@" 461 EPYTHON="$(PYTHON)" "${function}" "$@"
340 fi 462 fi
341 463
344 failure_message="$(eval echo -n "${failure_message_template}")" 466 failure_message="$(eval echo -n "${failure_message_template}")"
345 else 467 else
346 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function" 468 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
347 fi 469 fi
348 470
349 if [[ "${nonfatal}" == "1" ]] || has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 471 if [[ "${nonfatal}" == "1" ]]; then
350 local ABI enabled_PYTHON_ABIS
351 for ABI in ${PYTHON_ABIS}; do
352 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
353 done
354 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
355 if [[ "${quiet}" == "0" ]]; then 472 if [[ "${quiet}" == "0" ]]; then
356 ewarn "${RED}${failure_message}${NORMAL}" 473 ewarn "${RED}${failure_message}${NORMAL}"
357 fi 474 fi
475 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
476 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
477 local enabled_PYTHON_ABIS= other_PYTHON_ABI
478 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
479 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
480 done
481 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
482 fi
483 if [[ "${quiet}" == "0" ]]; then
484 ewarn "${RED}${failure_message}${NORMAL}"
485 fi
486 if [[ -z "${PYTHON_ABIS}" ]]; then
487 die "${function}() function failed with all enabled versions of Python"
488 fi
358 else 489 else
359 die "${failure_message}" 490 die "${failure_message}"
360 fi 491 fi
361 fi 492 fi
362 493
494 # Ensure that directory stack has not been decreased.
495 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
496 die "Directory stack decreased illegally"
497 fi
498
499 # Avoid side effects of earlier returning from the specified function.
500 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
501 popd > /dev/null || die "popd failed"
502 done
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
363 if [[ "${separate_build_dirs}" == "1" ]]; then 515 if [[ "${separate_build_dirs}" == "1" ]]; then
364 popd > /dev/null || die "popd failed" 516 popd > /dev/null || die "popd failed"
365 fi 517 fi
366 unset BUILDDIR 518 unset BUILDDIR
367 done 519 done
369 if [[ "${default_function}" == "1" ]]; then 521 if [[ "${default_function}" == "1" ]]; then
370 unset -f python_default_function 522 unset -f python_default_function
371 fi 523 fi
372} 524}
373 525
374
375# @ECLASS-VARIABLE: PYTHON_USE_WITH
376# @DESCRIPTION:
377# Set this to a space separated list of use flags
378# the python slot in use must be built with.
379
380# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
381# @DESCRIPTION:
382# Set this to a space separated list of use flags
383# of which one must be turned on for the slot of
384# in use.
385
386# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
387# @DESCRIPTION:
388# Set this if you need to make either PYTHON_USE_WITH or
389# PYTHON_USE_WITH_OR atoms conditional under a use flag.
390
391# @FUNCTION: python_pkg_setup 526# @FUNCTION: python_copy_sources
527# @USAGE: [--no-link] [--] [directory]
392# @DESCRIPTION: 528# @DESCRIPTION:
393# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 529# Copy unpacked sources of given package for each Python ABI.
394# are respected. Only exported if one of those variables is set. 530python_copy_sources() {
395if ! has ${EAPI:-0} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 531 local dir dirs=() no_link="0" PYTHON_ABI
396 python_pkg_setup_fail() {
397 eerror "${1}"
398 die "${1}"
399 }
400 532
401 python_pkg_setup() { 533 while (($#)); do
402 [[ ${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
403 550
404 python_version 551 if [[ "$#" -eq 0 ]]; then
405 local failed 552 if [[ "${WORKDIR}" == "${S}" ]]; then
406 local pyatom="dev-lang/python:${PYVER}" 553 die "${FUNCNAME}() cannot be used"
554 fi
555 dirs="${S}"
556 else
557 dirs="$@"
558 fi
407 559
408 for use in ${PYTHON_USE_WITH}; do 560 validate_PYTHON_ABIS
409 if ! has_version "${pyatom}[${use}]"; then 561 for PYTHON_ABI in ${PYTHON_ABIS}; do
410 python_pkg_setup_fail \ 562 for dir in "${dirs[@]}"; do
411 "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"
412 fi 567 fi
413 done 568 done
414
415 for use in ${PYTHON_USE_WITH_OR}; do
416 if has_version "${pyatom}[${use}]"; then
417 return
418 fi
419 done 569 done
570}
420 571
421 if [[ ${PYTHON_USE_WITH_OR} ]]; then 572# @FUNCTION: python_set_build_dir_symlink
422 python_pkg_setup_fail \ 573# @USAGE: [directory="build"]
423 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}" 574# @DESCRIPTION:
424 fi 575# Create build directory symlink.
425 } 576python_set_build_dir_symlink() {
577 local dir="$1"
426 578
427 EXPORT_FUNCTIONS pkg_setup 579 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
580 [[ -z "${dir}" ]] && dir="build"
428 581
429 if [[ ${PYTHON_USE_WITH} ]]; then 582 # Do not delete preexistent directories.
430 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 583 rm -f "${dir}" || die "Deletion of '${dir}' failed"
431 elif [[ ${PYTHON_USE_WITH_OR} ]]; then 584 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
432 PYTHON_USE_WITH_ATOM="|| ( " 585}
433 for use in ${PYTHON_USE_WITH_OR}; do 586
434 PYTHON_USE_WITH_ATOM=" 587# @FUNCTION: python_generate_wrapper_scripts
435 ${PYTHON_USE_WITH_ATOM} 588# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
436 ${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
437 done 620 done
438 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"
439 fi 630 fi
440 if [[ ${PYTHON_USE_WITH_OPT} ]]; then 631 done
441 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"
442 fi 635 fi
443 DEPEND="${PYTHON_USE_WITH_ATOM}" 636 done
444 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"
445fi 646 fi
446 647
447# @FUNCTION: python_disable_pyc 648 for file in "$@"; do
448# @DESCRIPTION: 649 if [[ -f "${file}" && "${force}" == "0" ]]; then
449# Tell Python not to automatically recompile modules to .pyc/.pyo 650 die "${FUNCNAME}(): '$1' already exists"
450# even if the timestamps/version stamps don't match. This is done 651 fi
451# to protect sandbox.
452python_disable_pyc() {
453 export PYTHONDONTWRITEBYTECODE="1"
454}
455 652
456# @FUNCTION: python_enable_pyc 653 if [[ "${quiet}" == "0" ]]; then
457# @DESCRIPTION: 654 einfo "Generating '${file#${D%/}}' wrapper script"
458# Tell Python to automatically recompile modules to .pyc/.pyo if the 655 fi
459# timestamps/version stamps have changed.
460python_enable_pyc() {
461 unset PYTHONDONTWRITEBYTECODE
462}
463 656
464python_disable_pyc 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"
760 fi
761
762 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
763 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
764 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
765 fi
766 export EPYTHON="$(PYTHON "$1")"
767 elif [[ "$1" == "2" ]]; then
768 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
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
780
781 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
782 # so it does not need to be exported to subprocesses.
783 PYTHON_ABI="${EPYTHON#python}"
784 PYTHON_ABI="${PYTHON_ABI%%-*}"
785}
465 786
466# @FUNCTION: python_need_rebuild 787# @FUNCTION: python_need_rebuild
467# @DESCRIPTION: Run without arguments, specifies that the package should be 788# @DESCRIPTION: Run without arguments, specifies that the package should be
468# 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.
469python_need_rebuild() { 792python_need_rebuild() {
470 python_version 793 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
471 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
472} 921}
473 922
474# @FUNCTION: python_get_includedir 923# @FUNCTION: python_get_includedir
924# @USAGE: [-f|--final-ABI]
475# @DESCRIPTION: 925# @DESCRIPTION:
476# 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.
477python_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)"
478 if [[ -n "${PYTHON_ABI}" ]]; then 948 elif [[ -n "${PYTHON_ABI}" ]]; then
479 echo "/usr/include/python${PYTHON_ABI}" 949 echo "/usr/include/python${PYTHON_ABI}"
480 else 950 else
481 python_version
482 echo "/usr/include/python${PYVER}" 951 echo "/usr/include/python$(PYTHON -A --ABI)"
483 fi 952 fi
484} 953}
485 954
486# @FUNCTION: python_get_libdir 955# @FUNCTION: python_get_libdir
956# @USAGE: [-f|--final-ABI]
487# @DESCRIPTION: 957# @DESCRIPTION:
488# 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.
489python_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)"
490 if [[ -n "${PYTHON_ABI}" ]]; then 980 elif [[ -n "${PYTHON_ABI}" ]]; then
491 echo "/usr/$(get_libdir)/python${PYTHON_ABI}" 981 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
492 else 982 else
493 python_version
494 echo "/usr/$(get_libdir)/python${PYVER}" 983 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)"
495 fi 984 fi
496} 985}
497 986
498# @FUNCTION: python_get_sitedir 987# @FUNCTION: python_get_sitedir
988# @USAGE: [-f|--final-ABI]
499# @DESCRIPTION: 989# @DESCRIPTION:
500# 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.
501python_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
502 echo "$(python_get_libdir)/site-packages" 1010 echo "$(python_get_libdir "${options[@]}")/site-packages"
503} 1011}
504 1012
505# @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]
506# @DESCRIPTION: 1027# @DESCRIPTION:
507# Run without arguments, checks if python was compiled with Tkinter 1028# Convert shebangs in specified files. Directories can be specified only with --recursive option.
508# support. If not, prints an error message and dies. 1029python_convert_shebangs() {
509python_tkinter_exists() { 1030 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
510 if ! python -c "import Tkinter" >/dev/null 2>&1; then 1031
511 eerror "You need to recompile python with Tkinter support." 1032 while (($#)); do
512 eerror "Try adding: 'dev-lang/python tk'" 1033 case "$1" in
513 eerror "in to /etc/portage/package.use" 1034 -r|--recursive)
514 echo 1035 recursive="1"
515 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"
516 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
517} 1099}
518 1100
519# @FUNCTION: python_mod_exists 1101# @FUNCTION: python_mod_exists
520# @USAGE: <module> 1102# @USAGE: <module>
521# @DESCRIPTION: 1103# @DESCRIPTION:
531python_mod_exists() { 1113python_mod_exists() {
532 [[ "$1" ]] || die "${FUNCNAME} requires an argument!" 1114 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
533 python -c "import $1" &>/dev/null 1115 python -c "import $1" &>/dev/null
534} 1116}
535 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
536# @FUNCTION: python_mod_compile 1144# @FUNCTION: python_disable_pyc
537# @USAGE: <file> [more files ...]
538# @DESCRIPTION: 1145# @DESCRIPTION:
539# Given filenames, it will pre-compile the module's .pyc and .pyo. 1146# Tell Python not to automatically recompile modules to .pyc/.pyo
540# This function should only be run in pkg_postinst() 1147# even if the timestamps/version stamps do not match. This is done
541# 1148# to protect sandbox.
542# Example: 1149python_disable_pyc() {
543# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 1150 export PYTHONDONTWRITEBYTECODE="1"
544#
545python_mod_compile() {
546 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
547 die "${FUNCNAME}() cannot be used in this EAPI"
548 fi
549
550 local f myroot myfiles=()
551
552 # Check if phase is pkg_postinst()
553 [[ ${EBUILD_PHASE} != postinst ]] &&\
554 die "${FUNCNAME} should only be run in pkg_postinst()"
555
556 # allow compiling for older python versions
557 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
558 PYVER=${PYTHON_OVERRIDE_PYVER}
559 else
560 python_version
561 fi
562
563 # strip trailing slash
564 myroot="${ROOT%/}"
565
566 # respect ROOT
567 for f in "$@"; do
568 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
569 done
570
571 if ((${#myfiles[@]})); then
572 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
573 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" &> /dev/null
574 else
575 ewarn "No files to compile!"
576 fi
577} 1151}
578 1152
579# @FUNCTION: python_mod_optimize 1153# @FUNCTION: python_mod_optimize
580# @USAGE: [options] [directory|file] 1154# @USAGE: [options] [directory|file]
581# @DESCRIPTION: 1155# @DESCRIPTION:
593python_mod_optimize() { 1167python_mod_optimize() {
594 # Check if phase is pkg_postinst(). 1168 # Check if phase is pkg_postinst().
595 [[ ${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()"
596 1170
597 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
598 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=()
599 1173
600 # Strip trailing slash from ROOT. 1174 # Strip trailing slash from ROOT.
601 root="${ROOT%/}" 1175 root="${ROOT%/}"
602 1176
603 # Respect ROOT and options passed to compileall.py. 1177 # Respect ROOT and options passed to compileall.py.
609 -d|-x) 1183 -d|-x)
610 options+=("$1" "$2") 1184 options+=("$1" "$2")
611 shift 1185 shift
612 ;; 1186 ;;
613 -*) 1187 -*)
614 ewarn "${FUNCNAME}: Ignoring compile option $1" 1188 ewarn "${FUNCNAME}: Ignoring option '$1'"
615 ;; 1189 ;;
616 *) 1190 *)
617 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1191 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
618 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"
619 elif [[ "$1" =~ ^/ ]]; then 1193 elif [[ "$1" =~ ^/ ]]; then
620 if [[ -d "${root}/$1" ]]; then 1194 if [[ -d "${root}/$1" ]]; then
621 other_dirs+=("${root}/$1") 1195 other_dirs+=("${root}/$1")
622 elif [[ -f "${root}/$1" ]]; then 1196 elif [[ -f "${root}/$1" ]]; then
623 other_files+=("${root}/$1") 1197 other_files+=("${root}/$1")
624 elif [[ -e "${root}/$1" ]]; then 1198 elif [[ -e "${root}/$1" ]]; then
625 ewarn "'${root}/$1' is not a file or a directory!" 1199 ewarn "'${root}/$1' is not a file or a directory!"
626 else 1200 else
627 ewarn "'${root}/$1' doesn't exist!" 1201 ewarn "'${root}/$1' does not exist!"
628 fi 1202 fi
629 else 1203 else
630 for PYTHON_ABI in ${PYTHON_ABIS}; do 1204 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
631 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then 1205 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
632 site_packages_dirs+=("$1") 1206 site_packages_dirs+=("$1")
633 break 1207 break
634 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then 1208 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
635 site_packages_files+=("$1") 1209 site_packages_files+=("$1")
636 break 1210 break
637 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then 1211 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
638 ewarn "'$1' is not a file or a directory!" 1212 ewarn "'$1' is not a file or a directory!"
639 else 1213 else
640 ewarn "'$1' doesn't exist!" 1214 ewarn "'$1' does not exist!"
641 fi 1215 fi
642 done 1216 done
643 fi 1217 fi
644 ;; 1218 ;;
645 esac 1219 esac
647 done 1221 done
648 1222
649 # Set additional options. 1223 # Set additional options.
650 options+=("-q") 1224 options+=("-q")
651 1225
652 for PYTHON_ABI in ${PYTHON_ABIS}; do 1226 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
653 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 1227 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
654 return_code="0" 1228 return_code="0"
655 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" 1229 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
656 if ((${#site_packages_dirs[@]})); then 1230 if ((${#site_packages_dirs[@]})); then
657 for dir in "${site_packages_dirs[@]}"; do 1231 for dir in "${site_packages_dirs[@]}"; do
670 eend "${return_code}" 1244 eend "${return_code}"
671 fi 1245 fi
672 unset site_packages_absolute_dirs site_packages_absolute_files 1246 unset site_packages_absolute_dirs site_packages_absolute_files
673 done 1247 done
674 1248
675 # 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
676 unset PYTHON_ABI 1253 unset PYTHON_ABI
1254 fi
677 1255
678 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1256 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
679 return_code="0" 1257 return_code="0"
680 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)"
681 if ((${#other_dirs[@]})); then 1259 if ((${#other_dirs[@]})); then
682 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"
683 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"
684 fi 1262 fi
685 if ((${#other_files[@]})); then 1263 if ((${#other_files[@]})); then
686 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"
687 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"
688 fi 1266 fi
689 eend "${return_code}" 1267 eend "${return_code}"
690 fi 1268 fi
691 else 1269 else
692 local myroot mydirs=() myfiles=() myopts=() return_code="0" 1270 local myroot mydirs=() myfiles=() myopts=() return_code="0"
703 -d|-x) 1281 -d|-x)
704 myopts+=("$1" "$2") 1282 myopts+=("$1" "$2")
705 shift 1283 shift
706 ;; 1284 ;;
707 -*) 1285 -*)
708 ewarn "${FUNCNAME}: Ignoring compile option $1" 1286 ewarn "${FUNCNAME}: Ignoring option '$1'"
709 ;; 1287 ;;
710 *) 1288 *)
711 if [[ -d "${myroot}"/$1 ]]; then 1289 if [[ -d "${myroot}"/$1 ]]; then
712 mydirs+=("${myroot}/$1") 1290 mydirs+=("${myroot}/$1")
713 elif [[ -f "${myroot}"/$1 ]]; then 1291 elif [[ -f "${myroot}"/$1 ]]; then
714 # Files are passed to python_mod_compile which is ROOT-aware 1292 # Files are passed to python_mod_compile which is ROOT-aware
715 myfiles+=("$1") 1293 myfiles+=("$1")
716 elif [[ -e "${myroot}/$1" ]]; then 1294 elif [[ -e "${myroot}/$1" ]]; then
717 ewarn "${myroot}/$1 is not a file or directory!" 1295 ewarn "${myroot}/$1 is not a file or directory!"
718 else 1296 else
719 ewarn "${myroot}/$1 doesn't exist!" 1297 ewarn "${myroot}/$1 does not exist!"
720 fi 1298 fi
721 ;; 1299 ;;
722 esac 1300 esac
723 shift 1301 shift
724 done 1302 done
725 1303
726 # allow compiling for older python versions
727 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
728 PYVER=${PYTHON_OVERRIDE_PYVER}
729 else
730 python_version
731 fi
732
733 # set additional opts 1304 # set additional opts
734 myopts+=(-q) 1305 myopts+=(-q)
735 1306
736 ebegin "Byte compiling python modules for python-${PYVER} .." 1307 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)"
737 if ((${#mydirs[@]})); then 1308 if ((${#mydirs[@]})); then
738 python${PYVER} \ 1309 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
739 "${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"
740 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
741 python${PYVER} -O \
742 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
743 "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
744 fi 1311 fi
745 1312
746 if ((${#myfiles[@]})); then 1313 if ((${#myfiles[@]})); then
747 python_mod_compile "${myfiles[@]}" 1314 python_mod_compile "${myfiles[@]}"
748 fi 1315 fi
750 eend "${return_code}" 1317 eend "${return_code}"
751 fi 1318 fi
752} 1319}
753 1320
754# @FUNCTION: python_mod_cleanup 1321# @FUNCTION: python_mod_cleanup
755# @USAGE: [directory] 1322# @USAGE: [directory|file]
756# @DESCRIPTION: 1323# @DESCRIPTION:
757# Run with optional arguments, where arguments are directories of 1324# Run with optional arguments, where arguments are Python modules. If none given,
758# 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].
759# 1326#
760# 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
761# 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.)
762# 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.
763# 1330#
764# This function should only be run in pkg_postrm(). 1331# This function should only be run in pkg_postrm().
765python_mod_cleanup() { 1332python_mod_cleanup() {
766 local PYTHON_ABI SEARCH_PATH=() root src_py 1333 local path py_file PYTHON_ABI SEARCH_PATH=() root
767 1334
768 # Check if phase is pkg_postrm(). 1335 # Check if phase is pkg_postrm().
769 [[ ${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()"
770 1337
771 # Strip trailing slash from ROOT. 1338 # Strip trailing slash from ROOT.
772 root="${ROOT%/}" 1339 root="${ROOT%/}"
773 1340
774 if (($#)); then 1341 if (($#)); then
775 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
776 while (($#)); do 1343 while (($#)); do
777 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1344 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
778 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"
779 elif [[ "$1" =~ ^/ ]]; then 1346 elif [[ "$1" =~ ^/ ]]; then
780 SEARCH_PATH+=("${root}/${1#/}") 1347 SEARCH_PATH+=("${root}/${1#/}")
781 else 1348 else
782 for PYTHON_ABI in ${PYTHON_ABIS}; do 1349 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
783 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 1350 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
784 done 1351 done
785 fi 1352 fi
786 shift 1353 shift
787 done 1354 done
788 else 1355 else
789 SEARCH_PATH=("${@#/}") 1356 SEARCH_PATH=("${@#/}")
790 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 1357 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
791 fi 1358 fi
792 else 1359 else
793 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=
794 fi 1381 fi
795 1382
796 for path in "${SEARCH_PATH[@]}"; do 1383 for path in "${SEARCH_PATH[@]}"; do
797 [[ ! -d "${path}" ]] && continue 1384 if [[ -d "${path}" ]]; then
798 einfo "Cleaning orphaned Python bytecode from ${path} .."
799 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 1385 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
800 src_py="${REPLY%[co]}" 1386 py_file="${REPLY%[co]}"
801 [[ -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
802 einfo "Purging ${src_py}[co]" 1388 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
803 rm -f "${src_py}"[co] 1389 rm -f "${py_file}"[co]
804 done 1390 done
805 1391
806 # Attempt to remove directories that may be empty. 1392 # Attempt to delete directories, which may be empty.
807 find "${path}" -type d | sort -r | while read -r dir; do 1393 find "${path}" -type d | sort -r | while read -r dir; do
808 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}" 1394 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
809 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
810 done 1400 done
811} 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.66  
changed lines
  Added in v.1.86

  ViewVC Help
Powered by ViewVC 1.1.20