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

Diff of /eclass/python.eclass

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

Revision 1.72 Revision 1.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.72 2009/09/11 19:55:05 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 break
85 ;;
86 -*)
87 die "${FUNCNAME}(): Unrecognized option $1"
88 ;;
89 *)
90 break
91 ;;
92 esac
93 shift
94 done 123 done
124 unset python_default_function
95 125
96 if [[ "$#" -eq "0" ]]; then 126 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
97 if [[ -n "${PYTHON_ABI}" ]]; then
98 slot="${PYTHON_ABI}"
99 else
100 die "${FUNCNAME}(): Invalid usage"
101 fi
102 elif [[ "$#" -eq "1" ]]; then
103 slot="$1"
104 else
105 die "${FUNCNAME}(): Invalid usage"
106 fi 127fi
107 128
108 if [[ "${absolute_path}" == "1" ]]; then 129unset PYTHON_ABIS
109 echo -n "/usr/bin/python${slot}" 130unset PYTHON_ABIS_SANITY_CHECKS
110 else
111 echo -n "python${slot}"
112 fi
113}
114 131
115# @FUNCTION: validate_PYTHON_ABIS 132# @FUNCTION: validate_PYTHON_ABIS
116# @DESCRIPTION: 133# @DESCRIPTION:
117# Make sure PYTHON_ABIS variable has valid value. 134# Ensure that PYTHON_ABIS variable has valid value.
118validate_PYTHON_ABIS() { 135validate_PYTHON_ABIS() {
119 # 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.
120 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
121 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"
122 fi 139 fi
123 140
124 # 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.
125 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."
126 die "'/usr/bin/python' isn't valid symlink" 145 die "'/usr/bin/python' is not valid symlink"
127 fi 146 fi
128 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."
129 die "'/usr/bin/python-config' isn't valid script" 150 die "'/usr/bin/python-config' is not valid script"
130 fi 151 fi
131 152
132 # 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.
133 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x 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
134 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=
135 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"
136 159
137 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then 160 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
161 local python2_enabled="0" python3_enabled="0"
162
138 if [[ -z "${USE_PYTHON}" ]]; then 163 if [[ -z "${USE_PYTHON}" ]]; then
139 die "USE_PYTHON variable is empty" 164 die "USE_PYTHON variable is empty"
140 fi 165 fi
141 166
142 for ABI in ${USE_PYTHON}; do 167 for PYTHON_ABI in ${USE_PYTHON}; do
143 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 168 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
144 die "USE_PYTHON variable contains invalid value '${ABI}'" 169 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
145 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
146 support_ABI="1" 179 support_ABI="1"
147 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 180 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
148 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 181 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
149 support_ABI="0" 182 support_ABI="0"
150 break 183 break
151 fi 184 fi
152 done 185 done
153 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 186 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
154 done 187 done
155 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
156 188
157 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then 189 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
158 die "USE_PYTHON variable doesn't enable any Python version supported by ${CATEGORY}/${PF}" 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."
159 fi 198 fi
160 else 199 else
161 local restricted_ABI 200 local python_version python2_version= python3_version= support_python_major_version
162 python_version
163 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"
207 fi
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"
164 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 213 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
165 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${PYVER}', '${restricted_ABI}'))"; then 214 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
166 die "Active Python version isn't supported by ${CATEGORY}/${PF}" 215 support_python_major_version="0"
167 fi 216 fi
217 done
218 [[ "${support_python_major_version}" == "1" ]] && break
168 done 219 done
169 export PYTHON_ABIS="${PYVER}" 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
170 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% }"
171 fi 272 fi
273 fi
172 274
275 if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
173 local PYTHON_ABI 276 local PYTHON_ABI
174 for PYTHON_ABI in ${PYTHON_ABIS}; do 277 for PYTHON_ABI in ${PYTHON_ABIS}; do
175 # Ensure that appropriate Python version is installed. 278 # Ensure that appropriate version of Python is installed.
176 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then 279 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
177 die "dev-lang/python:${PYTHON_ABI} isn't installed" 280 die "dev-lang/python:${PYTHON_ABI} is not installed"
178 fi 281 fi
179 282
180 # Ensure that EPYTHON variable is respected. 283 # Ensure that EPYTHON variable is respected.
181 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then 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]]))')'"
182 die "'python' doesn't respect EPYTHON variable" 291 die "'python' does not respect EPYTHON variable"
183 fi
184 done
185}
186
187# @FUNCTION: python_copy_sources
188# @USAGE: [--no-link] [--] [directory]
189# @DESCRIPTION:
190# Copy unpacked sources of given package for each Python ABI.
191python_copy_sources() {
192 local dir dirs=() no_link="0" PYTHON_ABI
193
194 while (($#)); do
195 case "$1" in
196 --no-link)
197 no_link="1"
198 ;;
199 --)
200 break
201 ;;
202 -*)
203 die "${FUNCNAME}(): Unrecognized option '$1'"
204 ;;
205 *)
206 break
207 ;;
208 esac
209 shift
210 done
211
212 if [[ "$#" -eq "0" ]]; then
213 if [[ "${WORKDIR}" == "${S}" ]]; then
214 die "${FUNCNAME}() cannot be used"
215 fi
216 dirs="${S}"
217 else
218 dirs="$@"
219 fi
220
221 validate_PYTHON_ABIS
222 for PYTHON_ABI in ${PYTHON_ABIS}; do
223 for dir in "${dirs[@]}"; do
224 if [[ "${no_link}" == "1" ]]; then
225 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
226 else
227 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
228 fi 292 fi
229 done 293 done
230 done 294 PYTHON_ABIS_SANITY_CHECKS="1"
231} 295 fi
232
233# @FUNCTION: python_set_build_dir_symlink
234# @USAGE: [directory="build"]
235# @DESCRIPTION:
236# Create build directory symlink.
237python_set_build_dir_symlink() {
238 local dir="$1"
239
240 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
241 [[ -z "${dir}" ]] && dir="build"
242
243 # Don't delete preexistent directories.
244 rm -f "${dir}" || die "Deletion of '${dir}' failed"
245 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
246} 296}
247 297
248# @FUNCTION: python_execute_function 298# @FUNCTION: python_execute_function
249# @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]
250# @DESCRIPTION: 300# @DESCRIPTION:
251# 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
252# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 302# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
253python_execute_function() { 303python_execute_function() {
254 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" 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=
255 305
256 while (($#)); do 306 while (($#)); do
257 case "$1" in 307 case "$1" in
258 --action-message) 308 --action-message)
259 action_message_template="$2" 309 action_message_template="$2"
273 quiet="1" 323 quiet="1"
274 ;; 324 ;;
275 -s|--separate-build-dirs) 325 -s|--separate-build-dirs)
276 separate_build_dirs="1" 326 separate_build_dirs="1"
277 ;; 327 ;;
328 --source-dir)
329 source_dir="$2"
330 shift
331 ;;
278 --) 332 --)
279 break 333 break
280 ;; 334 ;;
281 -*) 335 -*)
282 die "${FUNCNAME}(): Unrecognized option '$1'" 336 die "${FUNCNAME}(): Unrecognized option '$1'"
286 ;; 340 ;;
287 esac 341 esac
288 shift 342 shift
289 done 343 done
290 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
291 if [[ "${default_function}" == "0" ]]; then 349 if [[ "${default_function}" == "0" ]]; then
292 if [[ "$#" -eq "0" ]]; then 350 if [[ "$#" -eq 0 ]]; then
293 die "${FUNCNAME}(): Missing function name" 351 die "${FUNCNAME}(): Missing function name"
294 fi 352 fi
295 function="$1" 353 function="$1"
296 shift 354 shift
297 355
298 if [[ -z "$(type -t "${function}")" ]]; then 356 if [[ -z "$(type -t "${function}")" ]]; then
299 die "${FUNCNAME}(): '${function}' function isn't defined" 357 die "${FUNCNAME}(): '${function}' function is not defined"
300 fi 358 fi
301 else 359 else
302 if [[ "$#" -ne "0" ]]; then 360 if [[ "$#" -ne "0" ]]; then
303 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"
304 fi 362 fi
305 if has "${EAPI:-0}" 0 1; then 363 if has "${EAPI:-0}" 0 1; then
306 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI" 364 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
307 fi 365 fi
308 366
309 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 367 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
310 if has "${EAPI}" 2; then 368 if has "${EAPI}" 2 3; then
311 python_default_function() { 369 python_default_function() {
312 econf 370 econf
313 } 371 }
314 else 372 else
315 python_default_function() { 373 python_default_function() {
331 elif [[ "${EBUILD_PHASE}" == "install" ]]; then 389 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
332 python_default_function() { 390 python_default_function() {
333 emake DESTDIR="${D}" install 391 emake DESTDIR="${D}" install
334 } 392 }
335 else 393 else
336 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"
337 fi 395 fi
338 function="python_default_function" 396 function="python_default_function"
339 fi 397 fi
340 398
341 if [[ "${quiet}" == "0" ]]; then 399 if [[ "${quiet}" == "0" ]]; then
375 fi 433 fi
376 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 434 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
377 fi 435 fi
378 436
379 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
380 export BUILDDIR="${S}-${PYTHON_ABI}" 441 export BUILDDIR="${S}-${PYTHON_ABI}"
442 fi
381 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 443 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
382 else 444 else
383 export BUILDDIR="${S}" 445 export BUILDDIR="${S}"
384 fi 446 fi
385 447
386 previous_directory_stack_length="${#DIRSTACK[@]}" 448 previous_directory="$(pwd)"
449 previous_directory_stack="$(dirs -p)"
450 previous_directory_stack_length="$(dirs -p | wc -l)"
387 451
388 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
389 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@" 453 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
390 else 454 else
391 EPYTHON="$(PYTHON)" "${function}" "$@" 455 EPYTHON="$(PYTHON)" "${function}" "$@"
392 fi 456 fi
393 457
402 if [[ "${quiet}" == "0" ]]; then 466 if [[ "${quiet}" == "0" ]]; then
403 ewarn "${RED}${failure_message}${NORMAL}" 467 ewarn "${RED}${failure_message}${NORMAL}"
404 fi 468 fi
405 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 469 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
406 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then 470 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
407 local ABI enabled_PYTHON_ABIS= 471 local enabled_PYTHON_ABIS= other_PYTHON_ABI
408 for ABI in ${PYTHON_ABIS}; do 472 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
409 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}" 473 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
410 done 474 done
411 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }" 475 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
412 fi 476 fi
413 if [[ "${quiet}" == "0" ]]; then 477 if [[ "${quiet}" == "0" ]]; then
414 ewarn "${RED}${failure_message}${NORMAL}" 478 ewarn "${RED}${failure_message}${NORMAL}"
415 fi 479 fi
416 if [[ -z "${PYTHON_ABIS}" ]]; then 480 if [[ -z "${PYTHON_ABIS}" ]]; then
419 else 483 else
420 die "${failure_message}" 484 die "${failure_message}"
421 fi 485 fi
422 fi 486 fi
423 487
488 # Ensure that directory stack has not been decreased.
424 if [[ "${#DIRSTACK[@]}" -lt "${previous_directory_stack_length}" ]]; then 489 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
425 die "Directory stack decreased illegally" 490 die "Directory stack decreased illegally"
426 fi 491 fi
427 492
493 # Avoid side effects of earlier returning from the specified function.
428 while [[ "${#DIRSTACK[@]}" -gt "${previous_directory_stack_length}" ]]; do 494 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
429 popd > /dev/null || die "popd failed" 495 popd > /dev/null || die "popd failed"
430 done 496 done
431 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
432 if [[ "${separate_build_dirs}" == "1" ]]; then 509 if [[ "${separate_build_dirs}" == "1" ]]; then
433 popd > /dev/null || die "popd failed" 510 popd > /dev/null || die "popd failed"
434 fi 511 fi
435 unset BUILDDIR 512 unset BUILDDIR
436 done 513 done
438 if [[ "${default_function}" == "1" ]]; then 515 if [[ "${default_function}" == "1" ]]; then
439 unset -f python_default_function 516 unset -f python_default_function
440 fi 517 fi
441} 518}
442 519
443
444# @ECLASS-VARIABLE: PYTHON_USE_WITH
445# @DESCRIPTION:
446# Set this to a space separated list of use flags
447# the python slot in use must be built with.
448
449# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
450# @DESCRIPTION:
451# Set this to a space separated list of use flags
452# of which one must be turned on for the slot of
453# in use.
454
455# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
456# @DESCRIPTION:
457# Set this if you need to make either PYTHON_USE_WITH or
458# PYTHON_USE_WITH_OR atoms conditional under a use flag.
459
460# @FUNCTION: python_pkg_setup 520# @FUNCTION: python_copy_sources
521# @USAGE: [--no-link] [--] [directory]
461# @DESCRIPTION: 522# @DESCRIPTION:
462# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 523# Copy unpacked sources of given package for each Python ABI.
463# are respected. Only exported if one of those variables is set. 524python_copy_sources() {
464if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 525 local dir dirs=() no_link="0" PYTHON_ABI
465 python_pkg_setup_fail() {
466 eerror "${1}"
467 die "${1}"
468 }
469 526
470 python_pkg_setup() { 527 while (($#)); do
471 [[ ${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
472 544
473 python_version 545 if [[ "$#" -eq 0 ]]; then
474 local failed 546 if [[ "${WORKDIR}" == "${S}" ]]; then
475 local pyatom="dev-lang/python:${PYVER}" 547 die "${FUNCNAME}() cannot be used"
548 fi
549 dirs="${S}"
550 else
551 dirs="$@"
552 fi
476 553
477 for use in ${PYTHON_USE_WITH}; do 554 validate_PYTHON_ABIS
478 if ! has_version "${pyatom}[${use}]"; then 555 for PYTHON_ABI in ${PYTHON_ABIS}; do
479 python_pkg_setup_fail \ 556 for dir in "${dirs[@]}"; do
480 "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"
481 fi 561 fi
482 done 562 done
483
484 for use in ${PYTHON_USE_WITH_OR}; do
485 if has_version "${pyatom}[${use}]"; then
486 return
487 fi
488 done
489
490 if [[ ${PYTHON_USE_WITH_OR} ]]; then
491 python_pkg_setup_fail \
492 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}"
493 fi
494 }
495
496 EXPORT_FUNCTIONS pkg_setup
497
498 if [[ ${PYTHON_USE_WITH} ]]; then
499 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
500 elif [[ ${PYTHON_USE_WITH_OR} ]]; then
501 PYTHON_USE_WITH_ATOM="|| ( "
502 for use in ${PYTHON_USE_WITH_OR}; do
503 PYTHON_USE_WITH_ATOM="
504 ${PYTHON_USE_WITH_ATOM}
505 ${PYTHON_ATOM}[${use}]"
506 done
507 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )"
508 fi
509 if [[ ${PYTHON_USE_WITH_OPT} ]]; then
510 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
511 fi
512 DEPEND="${PYTHON_USE_WITH_ATOM}"
513 RDEPEND="${PYTHON_USE_WITH_ATOM}"
514fi
515
516# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
517# @DESCRIPTION:
518# Set this to define default functions for the following ebuild phases:
519# src_prepare, src_configure, src_compile, src_test, src_install.
520if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
521 python_src_prepare() {
522 python_copy_sources
523 }
524
525 for python_default_function in src_configure src_compile src_test src_install; do
526 eval "python_${python_default_function}() { python_execute_function -d -s; }"
527 done 563 done
528 unset python_default_function 564}
529 565
530 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install 566# @FUNCTION: python_set_build_dir_symlink
567# @USAGE: [directory="build"]
568# @DESCRIPTION:
569# Create build directory symlink.
570python_set_build_dir_symlink() {
571 local dir="$1"
572
573 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
574 [[ -z "${dir}" ]] && dir="build"
575
576 # Do not delete preexistent directories.
577 rm -f "${dir}" || die "Deletion of '${dir}' failed"
578 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
579}
580
581# @FUNCTION: python_generate_wrapper_scripts
582# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
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
614 done
615
616 if [[ "$#" -eq 0 ]]; then
617 die "${FUNCNAME}(): Missing arguments"
531fi 618 fi
532 619
533# @FUNCTION: python_disable_pyc 620 validate_PYTHON_ABIS
534# @DESCRIPTION: 621 for PYTHON_ABI in ${python2_supported_versions}; do
535# Tell Python not to automatically recompile modules to .pyc/.pyo 622 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
536# even if the timestamps/version stamps don't match. This is done 623 python2_enabled="1"
537# to protect sandbox. 624 fi
538python_disable_pyc() { 625 done
539 export PYTHONDONTWRITEBYTECODE="1" 626 for PYTHON_ABI in ${python3_supported_versions}; do
540} 627 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
628 python3_enabled="1"
629 fi
630 done
541 631
542# @FUNCTION: python_enable_pyc 632 if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
543# @DESCRIPTION: 633 eselect_python_option=
544# Tell Python to automatically recompile modules to .pyc/.pyo if the 634 elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
545# timestamps/version stamps have changed. 635 eselect_python_option="--python2"
546python_enable_pyc() { 636 elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
547 unset PYTHONDONTWRITEBYTECODE 637 eselect_python_option="--python3"
548} 638 else
639 die "${FUNCNAME}(): Unsupported environment"
640 fi
549 641
550python_disable_pyc 642 for file in "$@"; do
643 if [[ -f "${file}" && "${force}" == "0" ]]; then
644 die "${FUNCNAME}(): '$1' already exists"
645 fi
646
647 if [[ "${quiet}" == "0" ]]; then
648 einfo "Generating '${file#${D%/}}' wrapper script"
649 fi
650
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}
551 780
552# @FUNCTION: python_need_rebuild 781# @FUNCTION: python_need_rebuild
553# @DESCRIPTION: Run without arguments, specifies that the package should be 782# @DESCRIPTION: Run without arguments, specifies that the package should be
554# 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.
555python_need_rebuild() { 786python_need_rebuild() {
556 python_version 787 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
557 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
558} 912}
559 913
560# @FUNCTION: python_get_includedir 914# @FUNCTION: python_get_includedir
561# @DESCRIPTION: 915# @DESCRIPTION:
562# Run without arguments, returns the Python include directory. 916# Run without arguments, returns the Python include directory.
563python_get_includedir() { 917python_get_includedir() {
564 if [[ -n "${PYTHON_ABI}" ]]; then 918 if [[ -n "${PYTHON_ABI}" ]]; then
565 echo "/usr/include/python${PYTHON_ABI}" 919 echo "/usr/include/python${PYTHON_ABI}"
566 else 920 else
567 python_version
568 echo "/usr/include/python${PYVER}" 921 echo "/usr/include/python$(PYTHON -A --ABI)"
569 fi 922 fi
570} 923}
571 924
572# @FUNCTION: python_get_libdir 925# @FUNCTION: python_get_libdir
573# @DESCRIPTION: 926# @DESCRIPTION:
574# Run without arguments, returns the Python library directory. 927# Run without arguments, returns the Python library directory.
575python_get_libdir() { 928python_get_libdir() {
576 if [[ -n "${PYTHON_ABI}" ]]; then 929 if [[ -n "${PYTHON_ABI}" ]]; then
577 echo "/usr/$(get_libdir)/python${PYTHON_ABI}" 930 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
578 else 931 else
579 python_version
580 echo "/usr/$(get_libdir)/python${PYVER}" 932 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)"
581 fi 933 fi
582} 934}
583 935
584# @FUNCTION: python_get_sitedir 936# @FUNCTION: python_get_sitedir
585# @DESCRIPTION: 937# @DESCRIPTION:
586# Run without arguments, returns the Python site-packages directory. 938# Run without arguments, returns the Python site-packages directory.
587python_get_sitedir() { 939python_get_sitedir() {
588 echo "$(python_get_libdir)/site-packages" 940 echo "$(python_get_libdir)/site-packages"
589} 941}
590 942
591# @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]
592# @DESCRIPTION: 957# @DESCRIPTION:
593# Run without arguments, checks if python was compiled with Tkinter 958# Convert shebangs in specified files. Directories can be specified only with --recursive option.
594# support. If not, prints an error message and dies. 959python_convert_shebangs() {
595python_tkinter_exists() { 960 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
596 if ! python -c "import Tkinter" >/dev/null 2>&1; then 961
597 eerror "You need to recompile python with Tkinter support." 962 while (($#)); do
598 eerror "Try adding: 'dev-lang/python tk'" 963 case "$1" in
599 eerror "in to /etc/portage/package.use" 964 -r|--recursive)
600 echo 965 recursive="1"
601 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"
602 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
603} 1029}
604 1030
605# @FUNCTION: python_mod_exists 1031# @FUNCTION: python_mod_exists
606# @USAGE: <module> 1032# @USAGE: <module>
607# @DESCRIPTION: 1033# @DESCRIPTION:
617python_mod_exists() { 1043python_mod_exists() {
618 [[ "$1" ]] || die "${FUNCNAME} requires an argument!" 1044 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
619 python -c "import $1" &>/dev/null 1045 python -c "import $1" &>/dev/null
620} 1046}
621 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
622# @FUNCTION: python_mod_compile 1074# @FUNCTION: python_disable_pyc
623# @USAGE: <file> [more files ...]
624# @DESCRIPTION: 1075# @DESCRIPTION:
625# Given filenames, it will pre-compile the module's .pyc and .pyo. 1076# Tell Python not to automatically recompile modules to .pyc/.pyo
626# This function should only be run in pkg_postinst() 1077# even if the timestamps/version stamps do not match. This is done
627# 1078# to protect sandbox.
628# Example: 1079python_disable_pyc() {
629# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 1080 export PYTHONDONTWRITEBYTECODE="1"
630#
631python_mod_compile() {
632 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
633 die "${FUNCNAME}() cannot be used in this EAPI"
634 fi
635
636 local f myroot myfiles=()
637
638 # Check if phase is pkg_postinst()
639 [[ ${EBUILD_PHASE} != postinst ]] &&\
640 die "${FUNCNAME} should only be run in pkg_postinst()"
641
642 # allow compiling for older python versions
643 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
644 PYVER=${PYTHON_OVERRIDE_PYVER}
645 else
646 python_version
647 fi
648
649 # strip trailing slash
650 myroot="${ROOT%/}"
651
652 # respect ROOT
653 for f in "$@"; do
654 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
655 done
656
657 if ((${#myfiles[@]})); then
658 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
659 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" &> /dev/null
660 else
661 ewarn "No files to compile!"
662 fi
663} 1081}
664 1082
665# @FUNCTION: python_mod_optimize 1083# @FUNCTION: python_mod_optimize
666# @USAGE: [options] [directory|file] 1084# @USAGE: [options] [directory|file]
667# @DESCRIPTION: 1085# @DESCRIPTION:
698 ;; 1116 ;;
699 -*) 1117 -*)
700 ewarn "${FUNCNAME}: Ignoring compile option $1" 1118 ewarn "${FUNCNAME}: Ignoring compile option $1"
701 ;; 1119 ;;
702 *) 1120 *)
703 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1121 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
704 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 1122 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories"
705 elif [[ "$1" =~ ^/ ]]; then 1123 elif [[ "$1" =~ ^/ ]]; then
706 if [[ -d "${root}/$1" ]]; then 1124 if [[ -d "${root}/$1" ]]; then
707 other_dirs+=("${root}/$1") 1125 other_dirs+=("${root}/$1")
708 elif [[ -f "${root}/$1" ]]; then 1126 elif [[ -f "${root}/$1" ]]; then
709 other_files+=("${root}/$1") 1127 other_files+=("${root}/$1")
710 elif [[ -e "${root}/$1" ]]; then 1128 elif [[ -e "${root}/$1" ]]; then
711 ewarn "'${root}/$1' is not a file or a directory!" 1129 ewarn "'${root}/$1' is not a file or a directory!"
712 else 1130 else
713 ewarn "'${root}/$1' doesn't exist!" 1131 ewarn "'${root}/$1' does not exist!"
714 fi 1132 fi
715 else 1133 else
716 for PYTHON_ABI in ${PYTHON_ABIS}; do 1134 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
717 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then 1135 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
718 site_packages_dirs+=("$1") 1136 site_packages_dirs+=("$1")
719 break 1137 break
720 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then 1138 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
721 site_packages_files+=("$1") 1139 site_packages_files+=("$1")
722 break 1140 break
723 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then 1141 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
724 ewarn "'$1' is not a file or a directory!" 1142 ewarn "'$1' is not a file or a directory!"
725 else 1143 else
726 ewarn "'$1' doesn't exist!" 1144 ewarn "'$1' does not exist!"
727 fi 1145 fi
728 done 1146 done
729 fi 1147 fi
730 ;; 1148 ;;
731 esac 1149 esac
733 done 1151 done
734 1152
735 # Set additional options. 1153 # Set additional options.
736 options+=("-q") 1154 options+=("-q")
737 1155
738 for PYTHON_ABI in ${PYTHON_ABIS}; do 1156 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
739 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 1157 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
740 return_code="0" 1158 return_code="0"
741 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" 1159 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
742 if ((${#site_packages_dirs[@]})); then 1160 if ((${#site_packages_dirs[@]})); then
743 for dir in "${site_packages_dirs[@]}"; do 1161 for dir in "${site_packages_dirs[@]}"; do
756 eend "${return_code}" 1174 eend "${return_code}"
757 fi 1175 fi
758 unset site_packages_absolute_dirs site_packages_absolute_files 1176 unset site_packages_absolute_dirs site_packages_absolute_files
759 done 1177 done
760 1178
761 # 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().
762 unset PYTHON_ABI 1180 unset PYTHON_ABI
763 1181
764 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1182 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
765 return_code="0" 1183 return_code="0"
766 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)"
767 if ((${#other_dirs[@]})); then 1185 if ((${#other_dirs[@]})); then
768 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"
769 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"
770 fi 1188 fi
771 if ((${#other_files[@]})); then 1189 if ((${#other_files[@]})); then
772 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"
773 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"
774 fi 1192 fi
775 eend "${return_code}" 1193 eend "${return_code}"
776 fi 1194 fi
777 else 1195 else
778 local myroot mydirs=() myfiles=() myopts=() return_code="0" 1196 local myroot mydirs=() myfiles=() myopts=() return_code="0"
800 # Files are passed to python_mod_compile which is ROOT-aware 1218 # Files are passed to python_mod_compile which is ROOT-aware
801 myfiles+=("$1") 1219 myfiles+=("$1")
802 elif [[ -e "${myroot}/$1" ]]; then 1220 elif [[ -e "${myroot}/$1" ]]; then
803 ewarn "${myroot}/$1 is not a file or directory!" 1221 ewarn "${myroot}/$1 is not a file or directory!"
804 else 1222 else
805 ewarn "${myroot}/$1 doesn't exist!" 1223 ewarn "${myroot}/$1 does not exist!"
806 fi 1224 fi
807 ;; 1225 ;;
808 esac 1226 esac
809 shift 1227 shift
810 done 1228 done
811 1229
812 # allow compiling for older python versions
813 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
814 PYVER=${PYTHON_OVERRIDE_PYVER}
815 else
816 python_version
817 fi
818
819 # set additional opts 1230 # set additional opts
820 myopts+=(-q) 1231 myopts+=(-q)
821 1232
822 ebegin "Byte compiling python modules for python-${PYVER} .." 1233 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)"
823 if ((${#mydirs[@]})); then 1234 if ((${#mydirs[@]})); then
824 python${PYVER} \ 1235 "$(PYTHON -A)" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
825 "${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"
826 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
827 python${PYVER} -O \
828 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
829 "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
830 fi 1237 fi
831 1238
832 if ((${#myfiles[@]})); then 1239 if ((${#myfiles[@]})); then
833 python_mod_compile "${myfiles[@]}" 1240 python_mod_compile "${myfiles[@]}"
834 fi 1241 fi
836 eend "${return_code}" 1243 eend "${return_code}"
837 fi 1244 fi
838} 1245}
839 1246
840# @FUNCTION: python_mod_cleanup 1247# @FUNCTION: python_mod_cleanup
841# @USAGE: [directory] 1248# @USAGE: [directory|file]
842# @DESCRIPTION: 1249# @DESCRIPTION:
843# Run with optional arguments, where arguments are directories of 1250# Run with optional arguments, where arguments are Python modules. If none given,
844# python modules. If none given, it will look in /usr/lib/python[0-9].[0-9]. 1251# it will look in /usr/lib/python[0-9].[0-9].
845# 1252#
846# 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
847# 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.)
848# 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.
849# 1256#
850# This function should only be run in pkg_postrm(). 1257# This function should only be run in pkg_postrm().
851python_mod_cleanup() { 1258python_mod_cleanup() {
852 local PYTHON_ABI SEARCH_PATH=() root src_py 1259 local path py_file PYTHON_ABI SEARCH_PATH=() root
853 1260
854 # Check if phase is pkg_postrm(). 1261 # Check if phase is pkg_postrm().
855 [[ ${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()"
856 1263
857 # Strip trailing slash from ROOT. 1264 # Strip trailing slash from ROOT.
858 root="${ROOT%/}" 1265 root="${ROOT%/}"
859 1266
860 if (($#)); then 1267 if (($#)); then
861 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
862 while (($#)); do 1269 while (($#)); do
863 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1270 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
864 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 1271 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories"
865 elif [[ "$1" =~ ^/ ]]; then 1272 elif [[ "$1" =~ ^/ ]]; then
866 SEARCH_PATH+=("${root}/${1#/}") 1273 SEARCH_PATH+=("${root}/${1#/}")
867 else 1274 else
868 for PYTHON_ABI in ${PYTHON_ABIS}; do 1275 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
869 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 1276 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
870 done 1277 done
871 fi 1278 fi
872 shift 1279 shift
873 done 1280 done
874 else 1281 else
875 SEARCH_PATH=("${@#/}") 1282 SEARCH_PATH=("${@#/}")
876 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 1283 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
877 fi 1284 fi
878 else 1285 else
879 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=
880 fi 1307 fi
881 1308
882 for path in "${SEARCH_PATH[@]}"; do 1309 for path in "${SEARCH_PATH[@]}"; do
883 [[ ! -d "${path}" ]] && continue 1310 if [[ -d "${path}" ]]; then
884 einfo "Cleaning orphaned Python bytecode from ${path} .."
885 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 1311 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
886 src_py="${REPLY%[co]}" 1312 py_file="${REPLY%[co]}"
887 [[ -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
888 einfo "Purging ${src_py}[co]" 1314 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
889 rm -f "${src_py}"[co] 1315 rm -f "${py_file}"[co]
890 done 1316 done
891 1317
892 # Attempt to remove directories that may be empty. 1318 # Attempt to delete directories, which may be empty.
893 find "${path}" -type d | sort -r | while read -r dir; do 1319 find "${path}" -type d | sort -r | while read -r dir; do
894 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}" 1320 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
895 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
896 done 1326 done
897} 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.72  
changed lines
  Added in v.1.85

  ViewVC Help
Powered by ViewVC 1.1.20