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

Diff of /eclass/python.eclass

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

Revision 1.60 Revision 1.76
1# Copyright 1999-2009 Gentoo Foundation 1# Copyright 1999-2009 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.60 2009/08/05 18:31:30 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.76 2009/10/02 23:09:08 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.
13inherit alternatives multilib
14 11
12inherit multilib
15 13
16if [[ -n "${NEED_PYTHON}" ]] ; then 14if [[ -n "${NEED_PYTHON}" ]] ; then
17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" 15 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
18 DEPEND="${PYTHON_ATOM}" 16 DEPEND="${PYTHON_ATOM}"
19 RDEPEND="${DEPEND}" 17 RDEPEND="${DEPEND}"
20else 18else
21 PYTHON_ATOM="dev-lang/python" 19 PYTHON_ATOM="dev-lang/python"
22fi 20fi
23 21
24if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
25 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804" 22DEPEND+=" >=app-admin/eselect-python-20090804
26fi 23 >=app-shells/bash-3.2"
27 24
28__python_eclass_test() { 25__python_eclass_test() {
29 __python_version_extract 2.3 26 __python_version_extract 2.3
30 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 27 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
31 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" 28 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
65 export PYVER_ALL="${tmpstr#Python }" 62 export PYVER_ALL="${tmpstr#Python }"
66 __python_version_extract $PYVER_ALL 63 __python_version_extract $PYVER_ALL
67} 64}
68 65
69# @FUNCTION: PYTHON 66# @FUNCTION: PYTHON
70# @USAGE: [-a|--absolute-path] <Python_ABI="${PYTHON_ABI}"> 67# @USAGE: [-a|--absolute-path] [--] <Python_ABI="${PYTHON_ABI}">
71# @DESCRIPTION: 68# @DESCRIPTION:
72# Get Python interpreter filename for specified Python ABI. If Python_ABI argument 69# Get Python interpreter filename for specified Python ABI. If Python_ABI argument
73# is ommitted, then PYTHON_ABI environment variable must be set and is used. 70# is ommitted, then PYTHON_ABI environment variable must be set and is used.
74PYTHON() { 71PYTHON() {
75 local absolute_path="0" slot= 72 local absolute_path="0" slot=
76 73
77 while (($#)); do 74 while (($#)); do
78 case "$1" in 75 case "$1" in
79 -a|--absolute-path) 76 -a|--absolute-path)
80 absolute_path="1" 77 absolute_path="1"
78 ;;
79 --)
80 break
81 ;; 81 ;;
82 -*) 82 -*)
83 die "${FUNCNAME}(): Unrecognized option $1" 83 die "${FUNCNAME}(): Unrecognized option $1"
84 ;; 84 ;;
85 *) 85 *)
108 fi 108 fi
109} 109}
110 110
111# @FUNCTION: validate_PYTHON_ABIS 111# @FUNCTION: validate_PYTHON_ABIS
112# @DESCRIPTION: 112# @DESCRIPTION:
113# Make sure PYTHON_ABIS variable has valid value. 113# Ensure that PYTHON_ABIS variable has valid value.
114validate_PYTHON_ABIS() { 114validate_PYTHON_ABIS() {
115 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable.
116 if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
117 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
118 fi
119
115 # Ensure that /usr/bin/python and /usr/bin/python-config are valid. 120 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
116 if [[ "$(</usr/bin/python)" != *"Gentoo Python wrapper program"* ]]; then 121 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then
117 die "/usr/bin/python isn't valid program" 122 die "'/usr/bin/python' isn't valid symlink"
118 fi 123 fi
119 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then 124 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
120 die "/usr/bin/python-config isn't valid script" 125 die "'/usr/bin/python-config' isn't valid script"
121 fi 126 fi
122 127
123 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. 128 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4.
124 if [[ -z "${PYTHON_ABIS}" ]] && has "${EAPI:-0}" 0 1 2 3; then 129 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then
125 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI 130 local ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS=
126 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" 131 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
132 python2_supported_versions="2.4 2.5 2.6 2.7"
133 python3_supported_versions="3.0 3.1 3.2"
134
135 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
136 local python2_enabled="0" python3_enabled="0"
137
138 if [[ -z "${USE_PYTHON}" ]]; then
139 die "USE_PYTHON variable is empty"
140 fi
141
127 for ABI in ${USE_PYTHON}; do 142 for ABI in ${USE_PYTHON}; do
128 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 143 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
129 ewarn "Ignoring unsupported Python ABI '${ABI}'" 144 die "USE_PYTHON variable contains invalid value '${ABI}'"
130 continue
131 fi 145 fi
146
147 if has "${ABI}" ${python2_supported_versions}; then
148 python2_enabled="1"
149 fi
150 if has "${ABI}" ${python3_supported_versions}; then
151 python3_enabled="1"
152 fi
153
132 support_ABI="1" 154 support_ABI="1"
133 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 155 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
134 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 156 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then
135 support_ABI="0" 157 support_ABI="0"
136 break 158 break
137 fi 159 fi
160 done
161 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}"
138 done 162 done
139 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}"
140 done
141 export PYTHON_ABIS="${supported_PYTHON_ABIS# }" 163 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
142 fi
143 164
144 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then 165 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
166 die "USE_PYTHON variable doesn't enable any version of Python supported by ${CATEGORY}/${PF}"
167 fi
168
169 if [[ "${python2_enabled}" == "0" ]]; then
170 ewarn "USE_PYTHON variable doesn't enable any version of Python 2. This configuration is unsupported."
171 fi
172 if [[ "${python3_enabled}" == "0" ]]; then
173 ewarn "USE_PYTHON variable doesn't enable any version of Python 3. This configuration is unsupported."
174 fi
175 else
176 local python_version python2_version= python3_version= support_python_major_version
177
178 python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
179
180 if has_version "=dev-lang/python-2*"; then
181 if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then
182 die "'/usr/bin/python2' isn't valid symlink"
183 fi
184
185 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
186
187 for ABI in ${python2_supported_versions}; do
188 support_python_major_version="1"
189 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
190 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then
191 support_python_major_version="0"
192 fi
193 done
194 [[ "${support_python_major_version}" == "1" ]] && break
195 done
196 if [[ "${support_python_major_version}" == "1" ]]; then
197 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
198 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${python2_version}', '${restricted_ABI}'))"; then
199 die "Active version of Python 2 isn't supported by ${CATEGORY}/${PF}"
200 fi
201 done
202 else
145 python_version 203 python2_version=""
204 fi
205 fi
206
207 if has_version "=dev-lang/python-3*"; then
208 if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then
209 die "'/usr/bin/python3' isn't valid symlink"
210 fi
211
212 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
213
214 for ABI in ${python3_supported_versions}; do
215 support_python_major_version="1"
216 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
217 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then
218 support_python_major_version="0"
219 fi
220 done
221 [[ "${support_python_major_version}" == "1" ]] && break
222 done
223 if [[ "${support_python_major_version}" == "1" ]]; then
224 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
225 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${python3_version}', '${restricted_ABI}'))"; then
226 die "Active version of Python 3 isn't supported by ${CATEGORY}/${PF}"
227 fi
228 done
229 else
230 python3_version=""
231 fi
232 fi
233
234 if ! has "${python_version}" "${python2_version}" "${python3_version}"; then
235 eerror "Python wrapper is configured incorrectly or /usr/bin/python2 or /usr/bin/python3 symlink"
236 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
237 die "Incorrect configuration of Python"
238 fi
239
240 PYTHON_ABIS="${python2_version} ${python3_version}"
241 PYTHON_ABIS="${PYTHON_ABIS# }"
146 export PYTHON_ABIS="${PYVER}" 242 export PYTHON_ABIS="${PYTHON_ABIS% }"
147 fi 243 fi
244 fi
245
246 local PYTHON_ABI
247 for PYTHON_ABI in ${PYTHON_ABIS}; do
248 # Ensure that appropriate Python version is installed.
249 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
250 die "dev-lang/python:${PYTHON_ABI} isn't installed"
251 fi
252
253 # Ensure that EPYTHON variable is respected.
254 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
255 die "'python' doesn't respect EPYTHON variable"
256 fi
257 done
148} 258}
149 259
150# @FUNCTION: python_copy_sources 260# @FUNCTION: python_copy_sources
151# @USAGE: [directory] 261# @USAGE: [--no-link] [--] [directory]
152# @DESCRIPTION: 262# @DESCRIPTION:
153# Copy unpacked sources of given package for each Python ABI. 263# Copy unpacked sources of given package for each Python ABI.
154python_copy_sources() { 264python_copy_sources() {
155 local dir dirs=() PYTHON_ABI 265 local dir dirs=() no_link="0" PYTHON_ABI
266
267 while (($#)); do
268 case "$1" in
269 --no-link)
270 no_link="1"
271 ;;
272 --)
273 break
274 ;;
275 -*)
276 die "${FUNCNAME}(): Unrecognized option '$1'"
277 ;;
278 *)
279 break
280 ;;
281 esac
282 shift
283 done
156 284
157 if [[ "$#" -eq "0" ]]; then 285 if [[ "$#" -eq "0" ]]; then
158 if [[ "${WORKDIR}" == "${S}" ]]; then 286 if [[ "${WORKDIR}" == "${S}" ]]; then
159 die "${FUNCNAME}() cannot be used" 287 die "${FUNCNAME}() cannot be used"
160 fi 288 fi
164 fi 292 fi
165 293
166 validate_PYTHON_ABIS 294 validate_PYTHON_ABIS
167 for PYTHON_ABI in ${PYTHON_ABIS}; do 295 for PYTHON_ABI in ${PYTHON_ABIS}; do
168 for dir in "${dirs[@]}"; do 296 for dir in "${dirs[@]}"; do
297 if [[ "${no_link}" == "1" ]]; then
298 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
299 else
169 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed" 300 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
301 fi
170 done 302 done
171 done 303 done
172} 304}
173 305
174# @FUNCTION: python_set_build_dir_symlink 306# @FUNCTION: python_set_build_dir_symlink
176# @DESCRIPTION: 308# @DESCRIPTION:
177# Create build directory symlink. 309# Create build directory symlink.
178python_set_build_dir_symlink() { 310python_set_build_dir_symlink() {
179 local dir="$1" 311 local dir="$1"
180 312
181 [[ -z "${PYTHON_ABIS}" ]] && die "PYTHON_ABIS variable not set" 313 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
182 [[ -z "${dir}" ]] && dir="build" 314 [[ -z "${dir}" ]] && dir="build"
183 315
184 # Don't delete preexistent directories. 316 # Don't delete preexistent directories.
185 rm -f "${dir}" || die "Deletion of '${dir}' failed" 317 rm -f "${dir}" || die "Deletion of '${dir}' failed"
186 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed" 318 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
187} 319}
188 320
189# @FUNCTION: python_execute_function 321# @FUNCTION: python_execute_function
190# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] <function> [arguments] 322# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments]
191# @DESCRIPTION: 323# @DESCRIPTION:
192# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 324# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
193# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 325# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
194python_execute_function() { 326python_execute_function() {
195 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" 327 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"
196 328
197 while (($#)); do 329 while (($#)); do
198 case "$1" in 330 case "$1" in
199 --action-message) 331 --action-message)
200 action_message_template="$2" 332 action_message_template="$2"
214 quiet="1" 346 quiet="1"
215 ;; 347 ;;
216 -s|--separate-build-dirs) 348 -s|--separate-build-dirs)
217 separate_build_dirs="1" 349 separate_build_dirs="1"
218 ;; 350 ;;
351 --)
352 break
353 ;;
219 -*) 354 -*)
220 die "${FUNCNAME}(): Unrecognized option $1" 355 die "${FUNCNAME}(): Unrecognized option '$1'"
221 ;; 356 ;;
222 *) 357 *)
223 break 358 break
224 ;; 359 ;;
225 esac 360 esac
230 if [[ "$#" -eq "0" ]]; then 365 if [[ "$#" -eq "0" ]]; then
231 die "${FUNCNAME}(): Missing function name" 366 die "${FUNCNAME}(): Missing function name"
232 fi 367 fi
233 function="$1" 368 function="$1"
234 shift 369 shift
370
371 if [[ -z "$(type -t "${function}")" ]]; then
372 die "${FUNCNAME}(): '${function}' function isn't defined"
373 fi
235 else 374 else
236 if [[ "$#" -ne "0" ]]; then 375 if [[ "$#" -ne "0" ]]; then
237 die "${FUNCNAME}(): --default-function option and function name cannot be specified simultaneously" 376 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
238 fi 377 fi
239 if has "${EAPI:-0}" 0 1; then 378 if has "${EAPI:-0}" 0 1; then
240 die "${FUNCNAME}(): --default-function option cannot be used in this EAPI" 379 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
241 fi 380 fi
242 381
243 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 382 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
383 if has "${EAPI}" 2; then
244 python_default_function() { 384 python_default_function() {
245 econf 385 econf
246 } 386 }
387 else
388 python_default_function() {
389 nonfatal econf
390 }
391 fi
247 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then 392 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
248 python_default_function() { 393 python_default_function() {
249 emake 394 emake
250 } 395 }
251 elif [[ "${EBUILD_PHASE}" == "test" ]]; then 396 elif [[ "${EBUILD_PHASE}" == "test" ]]; then
301 else 446 else
302 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..." 447 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..."
303 fi 448 fi
304 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 449 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
305 fi 450 fi
451
306 if [[ "${separate_build_dirs}" == "1" ]]; then 452 if [[ "${separate_build_dirs}" == "1" ]]; then
307 export BUILDDIR="${S}-${PYTHON_ABI}" 453 export BUILDDIR="${S}-${PYTHON_ABI}"
308 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 454 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
309 else 455 else
310 export BUILDDIR="${S}" 456 export BUILDDIR="${S}"
311 fi 457 fi
458
459 previous_directory="$(pwd)"
460 previous_directory_stack="$(dirs -p)"
461 previous_directory_stack_length="$(dirs -p | wc -l)"
462
463 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
464 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
465 else
312 if ! EPYTHON="$(PYTHON)" "${function}" "$@"; then 466 EPYTHON="$(PYTHON)" "${function}" "$@"
467 fi
468
469 if [[ "$?" != "0" ]]; then
313 if [[ -n "${failure_message_template}" ]]; then 470 if [[ -n "${failure_message_template}" ]]; then
314 failure_message="$(eval echo -n "${failure_message_template}")" 471 failure_message="$(eval echo -n "${failure_message_template}")"
315 else 472 else
316 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function" 473 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
317 fi 474 fi
318 if [[ "${nonfatal}" == "1" ]] || has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 475
319 local ABI enabled_PYTHON_ABIS 476 if [[ "${nonfatal}" == "1" ]]; then
320 for ABI in ${PYTHON_ABIS}; do
321 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
322 done
323 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
324 if [[ "${quiet}" == "0" ]]; then 477 if [[ "${quiet}" == "0" ]]; then
325 ewarn "${RED}${failure_message}${NORMAL}" 478 ewarn "${RED}${failure_message}${NORMAL}"
326 fi 479 fi
480 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
481 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
482 local ABI enabled_PYTHON_ABIS=
483 for ABI in ${PYTHON_ABIS}; do
484 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
485 done
486 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
487 fi
488 if [[ "${quiet}" == "0" ]]; then
489 ewarn "${RED}${failure_message}${NORMAL}"
490 fi
491 if [[ -z "${PYTHON_ABIS}" ]]; then
492 die "${function}() function failed with all enabled versions of Python"
493 fi
327 else 494 else
328 die "${failure_message}" 495 die "${failure_message}"
329 fi 496 fi
330 fi 497 fi
498
499 # Ensure that directory stack hasn't been decreased.
500 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
501 die "Directory stack decreased illegally"
502 fi
503
504 # Avoid side effects of earlier returning from the specified function.
505 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
506 popd > /dev/null || die "popd failed"
507 done
508
509 # Ensure that the bottom part of directory stack hasn't been changed. Restore
510 # previous directory (from before running of the specified function) before
511 # comparison of directory stacks to avoid mismatch of directory stacks after
512 # potential using of 'cd' to change current directory. Restoration of previous
513 # directory allows to safely use 'cd' to change current directory in the
514 # specified function without changing it back to original directory.
515 cd "${previous_directory}"
516 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
517 die "Directory stack changed illegally"
518 fi
519
331 if [[ "${separate_build_dirs}" == "1" ]]; then 520 if [[ "${separate_build_dirs}" == "1" ]]; then
332 popd > /dev/null || die "popd failed" 521 popd > /dev/null || die "popd failed"
333 fi 522 fi
334 unset BUILDDIR 523 unset BUILDDIR
335 done 524 done
358 547
359# @FUNCTION: python_pkg_setup 548# @FUNCTION: python_pkg_setup
360# @DESCRIPTION: 549# @DESCRIPTION:
361# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 550# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
362# are respected. Only exported if one of those variables is set. 551# are respected. Only exported if one of those variables is set.
363if ! has ${EAPI} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 552if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
553 python_pkg_setup() {
364 python_pkg_setup_fail() { 554 python_pkg_setup_fail() {
365 eerror "${1}" 555 eerror "${1}"
366 die "${1}" 556 die "${1}"
557 }
558
559 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
560
561 python_pkg_setup_check_USE_flags() {
562 local pyatom use
563 if [[ -n "${PYTHON_ABI}" ]]; then
564 pyatom="dev-lang/python:${PYTHON_ABI}"
565 else
566 python_version
567 pyatom="dev-lang/python:${PYVER}"
568 fi
569
570 # Workaround for older versions of Portage.
571 # has_version() calls portageq which is implemented in Python.
572 if has_version "=dev-lang/python-2*"; then
573 local EPYTHON
574 export EPYTHON="$(readlink /usr/bin/python2)"
575 fi
576
577 for use in ${PYTHON_USE_WITH}; do
578 if ! has_version "${pyatom}[${use}]"; then
579 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
580 fi
581 done
582
583 for use in ${PYTHON_USE_WITH_OR}; do
584 if has_version "${pyatom}[${use}]"; then
585 return
586 fi
587 done
588
589 if [[ ${PYTHON_USE_WITH_OR} ]]; then
590 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
591 fi
592 }
593
594 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
595 python_execute_function -q python_pkg_setup_check_USE_flags
596 else
597 python_pkg_setup_check_USE_flags
598 fi
367 } 599 }
368 600
369 python_pkg_setup() {
370 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
371
372 python_version
373 local failed
374 local pyatom="dev-lang/python:${PYVER}"
375
376 for use in ${PYTHON_USE_WITH}; do
377 if ! has_version "${pyatom}[${use}]"; then
378 python_pkg_setup_fail \
379 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}"
380 fi
381 done
382
383 for use in ${PYTHON_USE_WITH_OR}; do
384 if has_version "${pyatom}[${use}]"; then
385 return
386 fi
387 done
388
389 if [[ ${PYTHON_USE_WITH_OR} ]]; then
390 python_pkg_setup_fail \
391 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}"
392 fi
393 }
394
395 EXPORT_FUNCTIONS pkg_setup 601 EXPORT_FUNCTIONS pkg_setup
396 602
397 if [[ ${PYTHON_USE_WITH} ]]; then 603 if [[ -n "${PYTHON_USE_WITH}" ]]; then
398 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 604 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
399 elif [[ ${PYTHON_USE_WITH_OR} ]]; then 605 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
400 PYTHON_USE_WITH_ATOM="|| ( " 606 PYTHON_USE_WITH_ATOM="|| ( "
401 for use in ${PYTHON_USE_WITH_OR}; do 607 for use in ${PYTHON_USE_WITH_OR}; do
402 PYTHON_USE_WITH_ATOM=" 608 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
403 ${PYTHON_USE_WITH_ATOM}
404 ${PYTHON_ATOM}[${use}]"
405 done 609 done
406 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )" 610 unset use
611 PYTHON_USE_WITH_ATOM+=" )"
407 fi 612 fi
408 if [[ ${PYTHON_USE_WITH_OPT} ]]; then 613 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
409 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 614 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
410 fi 615 fi
411 DEPEND="${PYTHON_USE_WITH_ATOM}" 616 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
412 RDEPEND="${PYTHON_USE_WITH_ATOM}" 617 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
413fi 618fi
414 619
620# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
621# @DESCRIPTION:
622# Set this to define default functions for the following ebuild phases:
623# src_prepare, src_configure, src_compile, src_test, src_install.
624if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
625 python_src_prepare() {
626 python_copy_sources
627 }
628
629 for python_default_function in src_configure src_compile src_test src_install; do
630 eval "python_${python_default_function}() { python_execute_function -d -s; }"
631 done
632 unset python_default_function
633
634 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
635fi
636
415# @FUNCTION: python_disable_pyc 637# @FUNCTION: python_disable_pyc
416# @DESCRIPTION: 638# @DESCRIPTION:
417# Tells python not to automatically recompile modules to .pyc/.pyo 639# Tell Python not to automatically recompile modules to .pyc/.pyo
418# even if the timestamps/version stamps don't match. This is done 640# even if the timestamps/version stamps don't match. This is done
419# to protect sandbox. 641# to protect sandbox.
420#
421# note: supported by >=dev-lang/python-2.2.3-r3 only.
422#
423python_disable_pyc() { 642python_disable_pyc() {
424 export PYTHONDONTWRITEBYTECODE=1 # For 2.6 and above 643 export PYTHONDONTWRITEBYTECODE="1"
425 export PYTHON_DONTCOMPILE=1 # For 2.5 and below
426} 644}
427 645
428# @FUNCTION: python_enable_pyc 646# @FUNCTION: python_enable_pyc
429# @DESCRIPTION: 647# @DESCRIPTION:
430# Tells python to automatically recompile modules to .pyc/.pyo if the 648# Tell Python to automatically recompile modules to .pyc/.pyo if the
431# timestamps/version stamps change 649# timestamps/version stamps have changed.
432python_enable_pyc() { 650python_enable_pyc() {
433 unset PYTHONDONTWRITEBYTECODE 651 unset PYTHONDONTWRITEBYTECODE
434 unset PYTHON_DONTCOMPILE
435} 652}
436 653
437python_disable_pyc 654python_disable_pyc
438 655
439# @FUNCTION: python_need_rebuild 656# @FUNCTION: python_need_rebuild
471# @FUNCTION: python_get_sitedir 688# @FUNCTION: python_get_sitedir
472# @DESCRIPTION: 689# @DESCRIPTION:
473# Run without arguments, returns the Python site-packages directory. 690# Run without arguments, returns the Python site-packages directory.
474python_get_sitedir() { 691python_get_sitedir() {
475 echo "$(python_get_libdir)/site-packages" 692 echo "$(python_get_libdir)/site-packages"
476}
477
478# @FUNCTION: python_makesym
479# @DESCRIPTION:
480# Run without arguments, it will create the /usr/bin/python symlinks
481# to the latest installed version
482python_makesym() {
483 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
484 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
485} 693}
486 694
487# @FUNCTION: python_tkinter_exists 695# @FUNCTION: python_tkinter_exists
488# @DESCRIPTION: 696# @DESCRIPTION:
489# Run without arguments, checks if python was compiled with Tkinter 697# Run without arguments, checks if python was compiled with Tkinter
550 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") 758 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
551 done 759 done
552 760
553 if ((${#myfiles[@]})); then 761 if ((${#myfiles[@]})); then
554 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" 762 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
555 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" 763 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" &> /dev/null
556 else 764 else
557 ewarn "No files to compile!" 765 ewarn "No files to compile!"
558 fi 766 fi
559} 767}
560 768
608 else 816 else
609 ewarn "'${root}/$1' doesn't exist!" 817 ewarn "'${root}/$1' doesn't exist!"
610 fi 818 fi
611 else 819 else
612 for PYTHON_ABI in ${PYTHON_ABIS}; do 820 for PYTHON_ABI in ${PYTHON_ABIS}; do
613 if [[ -d "${root}/$(python_get_sitedir)/$1" ]]; then 821 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
614 site_packages_dirs+=("$1") 822 site_packages_dirs+=("$1")
615 break 823 break
616 elif [[ -f "${root}/$(python_get_sitedir)/$1" ]]; then 824 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
617 site_packages_files+=("$1") 825 site_packages_files+=("$1")
618 break 826 break
619 elif [[ -e "${root}/$(python_get_sitedir)/$1" ]]; then 827 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
620 ewarn "'$1' is not a file or a directory!" 828 ewarn "'$1' is not a file or a directory!"
621 else 829 else
622 ewarn "'$1' doesn't exist!" 830 ewarn "'$1' doesn't exist!"
623 fi 831 fi
624 done 832 done
635 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 843 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
636 return_code="0" 844 return_code="0"
637 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" 845 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
638 if ((${#site_packages_dirs[@]})); then 846 if ((${#site_packages_dirs[@]})); then
639 for dir in "${site_packages_dirs[@]}"; do 847 for dir in "${site_packages_dirs[@]}"; do
640 site_packages_absolute_dirs+=("${root}/$(python_get_sitedir)/${dir}") 848 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
641 done 849 done
642 "$(PYTHON)" "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" 850 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
643 "$(PYTHON)" -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" 851 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
644 fi 852 fi
645 if ((${#site_packages_files[@]})); then 853 if ((${#site_packages_files[@]})); then
646 for file in "${site_packages_files[@]}"; do 854 for file in "${site_packages_files[@]}"; do
647 site_packages_absolute_files+=("${root}/$(python_get_sitedir)/${file}") 855 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
648 done 856 done
649 "$(PYTHON)" "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" 857 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
650 "$(PYTHON)" -O "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" 858 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
651 fi 859 fi
652 eend "${return_code}" 860 eend "${return_code}"
653 fi 861 fi
654 unset site_packages_absolute_dirs site_packages_absolute_files 862 unset site_packages_absolute_dirs site_packages_absolute_files
655 done 863 done
659 867
660 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 868 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
661 return_code="0" 869 return_code="0"
662 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." 870 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..."
663 if ((${#other_dirs[@]})); then 871 if ((${#other_dirs[@]})); then
664 python${PYVER} "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 872 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
665 python${PYVER} -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 873 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
666 fi 874 fi
667 if ((${#other_files[@]})); then 875 if ((${#other_files[@]})); then
668 python${PYVER} "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 876 python${PYVER} "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
669 python${PYVER} -O "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 877 python${PYVER} -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
670 fi 878 fi
671 eend "${return_code}" 879 eend "${return_code}"
672 fi 880 fi
673 else 881 else
674 local myroot mydirs=() myfiles=() myopts=() 882 local myroot mydirs=() myfiles=() myopts=() return_code="0"
675 883
676 # strip trailing slash 884 # strip trailing slash
677 myroot="${ROOT%/}" 885 myroot="${ROOT%/}"
678 886
679 # respect ROOT and options passed to compileall.py 887 # respect ROOT and options passed to compileall.py
717 925
718 ebegin "Byte compiling python modules for python-${PYVER} .." 926 ebegin "Byte compiling python modules for python-${PYVER} .."
719 if ((${#mydirs[@]})); then 927 if ((${#mydirs[@]})); then
720 python${PYVER} \ 928 python${PYVER} \
721 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 929 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
722 "${myopts[@]}" "${mydirs[@]}" 930 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
723 python${PYVER} -O \ 931 python${PYVER} -O \
724 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 932 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
725 "${myopts[@]}" "${mydirs[@]}" 933 "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
726 fi 934 fi
727 935
728 if ((${#myfiles[@]})); then 936 if ((${#myfiles[@]})); then
729 python_mod_compile "${myfiles[@]}" 937 python_mod_compile "${myfiles[@]}"
730 fi 938 fi
731 939
732 eend $? 940 eend "${return_code}"
733 fi 941 fi
734} 942}
735 943
736# @FUNCTION: python_mod_cleanup 944# @FUNCTION: python_mod_cleanup
737# @USAGE: [directory] 945# @USAGE: [directory]
760 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 968 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories"
761 elif [[ "$1" =~ ^/ ]]; then 969 elif [[ "$1" =~ ^/ ]]; then
762 SEARCH_PATH+=("${root}/${1#/}") 970 SEARCH_PATH+=("${root}/${1#/}")
763 else 971 else
764 for PYTHON_ABI in ${PYTHON_ABIS}; do 972 for PYTHON_ABI in ${PYTHON_ABIS}; do
765 SEARCH_PATH+=("${root}/$(python_get_sitedir)/$1") 973 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
766 done 974 done
767 fi 975 fi
768 shift 976 shift
769 done 977 done
770 else 978 else
774 else 982 else
775 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) 983 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages)
776 fi 984 fi
777 985
778 for path in "${SEARCH_PATH[@]}"; do 986 for path in "${SEARCH_PATH[@]}"; do
987 [[ ! -d "${path}" ]] && continue
779 einfo "Cleaning orphaned Python bytecode from ${path} .." 988 einfo "Cleaning orphaned Python bytecode from ${path} .."
780 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 989 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
781 src_py="${REPLY%[co]}" 990 src_py="${REPLY%[co]}"
782 [[ -f "${src_py}" ]] && continue 991 [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue
783 einfo "Purging ${src_py}[co]" 992 einfo "Purging ${src_py}[co]"
784 rm -f "${src_py}"[co] 993 rm -f "${src_py}"[co]
785 done 994 done
786 995
787 # Attempt to remove directories that may be empty. 996 # Attempt to remove directories that may be empty.
788 find "${path}" -type d | sort -r | while read -r dir; do 997 find "${path}" -type d | sort -r | while read -r dir; do
789 rmdir "${dir}" 2>/dev/null 998 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}"
790 done 999 done
791 done 1000 done
792} 1001}

Legend:
Removed from v.1.60  
changed lines
  Added in v.1.76

  ViewVC Help
Powered by ViewVC 1.1.20