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

Legend:
Removed from v.1.66  
changed lines
  Added in v.1.85

  ViewVC Help
Powered by ViewVC 1.1.20