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

Diff of /eclass/python.eclass

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

Revision 1.59 Revision 1.72
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.59 2009/08/04 21:01:25 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.72 2009/09/11 19:55:05 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# 8#
9# original author: Alastair Tse <liquidx@gentoo.org> 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. 10# @BLURB: A Utility Eclass that should be inherited by anything that deals with Python or Python modules.
11# @DESCRIPTION: 11# @DESCRIPTION:
12# Some useful functions for dealing with python. 12# Some useful functions for dealing with Python.
13inherit alternatives multilib
14 13
14inherit multilib
15 15
16if [[ -n "${NEED_PYTHON}" ]] ; then 16if [[ -n "${NEED_PYTHON}" ]] ; then
17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" 17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
18 DEPEND="${PYTHON_ATOM}" 18 DEPEND="${PYTHON_ATOM}"
19 RDEPEND="${DEPEND}" 19 RDEPEND="${DEPEND}"
20else 20else
21 PYTHON_ATOM="dev-lang/python" 21 PYTHON_ATOM="dev-lang/python"
22fi 22fi
23 23
24DEPEND="${DEPEND} >=app-shells/bash-3.2"
24if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 25if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
25 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804" 26 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804"
26fi 27fi
27 28
28__python_eclass_test() { 29__python_eclass_test() {
64 tmpstr="$(${python} -V 2>&1 )" 65 tmpstr="$(${python} -V 2>&1 )"
65 export PYVER_ALL="${tmpstr#Python }" 66 export PYVER_ALL="${tmpstr#Python }"
66 __python_version_extract $PYVER_ALL 67 __python_version_extract $PYVER_ALL
67} 68}
68 69
69# @FUNCTION: get_python 70# @FUNCTION: PYTHON
70# @USAGE: [-a|--absolute-path] <Python_ABI="${PYTHON_ABI}"> 71# @USAGE: [-a|--absolute-path] [--] <Python_ABI="${PYTHON_ABI}">
71# @DESCRIPTION: 72# @DESCRIPTION:
72# Get Python interpreter filename for specified Python ABI. If Python_ABI argument 73# 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. 74# is ommitted, then PYTHON_ABI environment variable must be set and is used.
74get_python() { 75PYTHON() {
75 local absolute_path="0" slot= 76 local absolute_path="0" slot=
76 77
77 while (($#)); do 78 while (($#)); do
78 case "$1" in 79 case "$1" in
79 -a|--absolute-path) 80 -a|--absolute-path)
80 absolute_path="1" 81 absolute_path="1"
82 ;;
83 --)
84 break
81 ;; 85 ;;
82 -*) 86 -*)
83 die "${FUNCNAME}(): Unrecognized option $1" 87 die "${FUNCNAME}(): Unrecognized option $1"
84 ;; 88 ;;
85 *) 89 *)
110 114
111# @FUNCTION: validate_PYTHON_ABIS 115# @FUNCTION: validate_PYTHON_ABIS
112# @DESCRIPTION: 116# @DESCRIPTION:
113# Make sure PYTHON_ABIS variable has valid value. 117# Make sure PYTHON_ABIS variable has valid value.
114validate_PYTHON_ABIS() { 118validate_PYTHON_ABIS() {
119 # 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
121 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
122 fi
123
115 # Ensure that /usr/bin/python and /usr/bin/python-config are valid. 124 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
116 if [[ "$(</usr/bin/python)" != *"Gentoo Python wrapper program"* ]]; then 125 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then
117 die "/usr/bin/python isn't valid program" 126 die "'/usr/bin/python' isn't valid symlink"
118 fi 127 fi
119 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then 128 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
120 die "/usr/bin/python-config isn't valid script" 129 die "'/usr/bin/python-config' isn't valid script"
121 fi 130 fi
122 131
123 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. 132 # 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 133 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 134 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI
126 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" 135 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
136
137 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
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
132 support_ABI="1" 146 support_ABI="1"
147 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
148 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then
149 support_ABI="0"
150 break
151 fi
152 done
153 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}"
154 done
155 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
156
157 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
158 die "USE_PYTHON variable doesn't enable any Python version supported by ${CATEGORY}/${PF}"
159 fi
160 else
161 local restricted_ABI
162 python_version
163
133 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 164 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
134 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 165 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${PYVER}', '${restricted_ABI}'))"; then
135 support_ABI="0" 166 die "Active Python version isn't supported by ${CATEGORY}/${PF}"
136 break
137 fi 167 fi
138 done 168 done
139 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}"
140 done
141 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
142 fi
143
144 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
145 python_version
146 export PYTHON_ABIS="${PYVER}" 169 export PYTHON_ABIS="${PYVER}"
147 fi 170 fi
171 fi
172
173 local PYTHON_ABI
174 for PYTHON_ABI in ${PYTHON_ABIS}; do
175 # Ensure that appropriate Python version is installed.
176 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
177 die "dev-lang/python:${PYTHON_ABI} isn't installed"
178 fi
179
180 # 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
182 die "'python' doesn't respect EPYTHON variable"
183 fi
184 done
148} 185}
149 186
150# @FUNCTION: python_copy_sources 187# @FUNCTION: python_copy_sources
151# @USAGE: [directory] 188# @USAGE: [--no-link] [--] [directory]
152# @DESCRIPTION: 189# @DESCRIPTION:
153# Copy unpacked sources of given package for each Python ABI. 190# Copy unpacked sources of given package for each Python ABI.
154python_copy_sources() { 191python_copy_sources() {
155 local dir dirs=() PYTHON_ABI 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
156 211
157 if [[ "$#" -eq "0" ]]; then 212 if [[ "$#" -eq "0" ]]; then
158 if [[ "${WORKDIR}" == "${S}" ]]; then 213 if [[ "${WORKDIR}" == "${S}" ]]; then
159 die "${FUNCNAME}() cannot be used" 214 die "${FUNCNAME}() cannot be used"
160 fi 215 fi
164 fi 219 fi
165 220
166 validate_PYTHON_ABIS 221 validate_PYTHON_ABIS
167 for PYTHON_ABI in ${PYTHON_ABIS}; do 222 for PYTHON_ABI in ${PYTHON_ABIS}; do
168 for dir in "${dirs[@]}"; 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
169 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed" 227 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
228 fi
170 done 229 done
171 done 230 done
172} 231}
173 232
174# @FUNCTION: python_set_build_dir_symlink 233# @FUNCTION: python_set_build_dir_symlink
176# @DESCRIPTION: 235# @DESCRIPTION:
177# Create build directory symlink. 236# Create build directory symlink.
178python_set_build_dir_symlink() { 237python_set_build_dir_symlink() {
179 local dir="$1" 238 local dir="$1"
180 239
181 [[ -z "${PYTHON_ABIS}" ]] && die "PYTHON_ABIS variable not set" 240 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
182 [[ -z "${dir}" ]] && dir="build" 241 [[ -z "${dir}" ]] && dir="build"
183 242
184 # Don't delete preexistent directories. 243 # Don't delete preexistent directories.
185 rm -f "${dir}" || die "Deletion of '${dir}' failed" 244 rm -f "${dir}" || die "Deletion of '${dir}' failed"
186 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed" 245 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
187} 246}
188 247
189# @FUNCTION: python_execute_function 248# @FUNCTION: python_execute_function
190# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] <function> [arguments] 249# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments]
191# @DESCRIPTION: 250# @DESCRIPTION:
192# Execute specified function for each value of PYTHON_ABIS, optionally passing 251# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
193# additional arguments. The specified function can use PYTHON_ABI variable. 252# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
194python_execute_function() { 253python_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" 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"
196 255
197 while (($#)); do 256 while (($#)); do
198 case "$1" in 257 case "$1" in
199 --action-message) 258 --action-message)
200 action_message_template="$2" 259 action_message_template="$2"
214 quiet="1" 273 quiet="1"
215 ;; 274 ;;
216 -s|--separate-build-dirs) 275 -s|--separate-build-dirs)
217 separate_build_dirs="1" 276 separate_build_dirs="1"
218 ;; 277 ;;
278 --)
279 break
280 ;;
219 -*) 281 -*)
220 die "${FUNCNAME}(): Unrecognized option $1" 282 die "${FUNCNAME}(): Unrecognized option '$1'"
221 ;; 283 ;;
222 *) 284 *)
223 break 285 break
224 ;; 286 ;;
225 esac 287 esac
230 if [[ "$#" -eq "0" ]]; then 292 if [[ "$#" -eq "0" ]]; then
231 die "${FUNCNAME}(): Missing function name" 293 die "${FUNCNAME}(): Missing function name"
232 fi 294 fi
233 function="$1" 295 function="$1"
234 shift 296 shift
297
298 if [[ -z "$(type -t "${function}")" ]]; then
299 die "${FUNCNAME}(): '${function}' function isn't defined"
300 fi
235 else 301 else
236 if [[ "$#" -ne "0" ]]; then 302 if [[ "$#" -ne "0" ]]; then
237 die "${FUNCNAME}(): --default-function option and function name cannot be specified simultaneously" 303 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
238 fi 304 fi
239 if has "${EAPI:-0}" 0 1; then 305 if has "${EAPI:-0}" 0 1; then
240 die "${FUNCNAME}(): --default-function option cannot be used in this EAPI" 306 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
241 fi 307 fi
242 308
243 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 309 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
310 if has "${EAPI}" 2; then
244 python_default_function() { 311 python_default_function() {
245 econf 312 econf
246 } 313 }
314 else
315 python_default_function() {
316 nonfatal econf
317 }
318 fi
247 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then 319 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
248 python_default_function() { 320 python_default_function() {
249 emake 321 emake
250 } 322 }
251 elif [[ "${EBUILD_PHASE}" == "test" ]]; then 323 elif [[ "${EBUILD_PHASE}" == "test" ]]; then
301 else 373 else
302 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..." 374 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..."
303 fi 375 fi
304 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 376 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
305 fi 377 fi
378
306 if [[ "${separate_build_dirs}" == "1" ]]; then 379 if [[ "${separate_build_dirs}" == "1" ]]; then
380 export BUILDDIR="${S}-${PYTHON_ABI}"
307 pushd "${S}-${PYTHON_ABI}" > /dev/null || die "pushd failed" 381 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
382 else
383 export BUILDDIR="${S}"
308 fi 384 fi
309 if ! EPYTHON="$(get_python)" "${function}" "$@"; then 385
386 previous_directory_stack_length="${#DIRSTACK[@]}"
387
388 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
389 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
390 else
391 EPYTHON="$(PYTHON)" "${function}" "$@"
392 fi
393
394 if [[ "$?" != "0" ]]; then
310 if [[ -n "${failure_message_template}" ]]; then 395 if [[ -n "${failure_message_template}" ]]; then
311 failure_message="$(eval echo -n "${failure_message_template}")" 396 failure_message="$(eval echo -n "${failure_message_template}")"
312 else 397 else
313 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function" 398 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
314 fi 399 fi
315 if [[ "${nonfatal}" == "1" ]] || has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 400
316 local ABI enabled_PYTHON_ABIS 401 if [[ "${nonfatal}" == "1" ]]; then
317 for ABI in ${PYTHON_ABIS}; do
318 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
319 done
320 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
321 if [[ "${quiet}" == "0" ]]; then 402 if [[ "${quiet}" == "0" ]]; then
322 ewarn "${RED}${failure_message}${NORMAL}" 403 ewarn "${RED}${failure_message}${NORMAL}"
323 fi 404 fi
405 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
406 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
407 local ABI enabled_PYTHON_ABIS=
408 for ABI in ${PYTHON_ABIS}; do
409 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
410 done
411 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
412 fi
413 if [[ "${quiet}" == "0" ]]; then
414 ewarn "${RED}${failure_message}${NORMAL}"
415 fi
416 if [[ -z "${PYTHON_ABIS}" ]]; then
417 die "${function}() function failed with all enabled versions of Python"
418 fi
324 else 419 else
325 die "${failure_message}" 420 die "${failure_message}"
326 fi 421 fi
327 fi 422 fi
423
424 if [[ "${#DIRSTACK[@]}" -lt "${previous_directory_stack_length}" ]]; then
425 die "Directory stack decreased illegally"
426 fi
427
428 while [[ "${#DIRSTACK[@]}" -gt "${previous_directory_stack_length}" ]]; do
429 popd > /dev/null || die "popd failed"
430 done
431
328 if [[ "${separate_build_dirs}" == "1" ]]; then 432 if [[ "${separate_build_dirs}" == "1" ]]; then
329 popd > /dev/null || die "popd failed" 433 popd > /dev/null || die "popd failed"
330 fi 434 fi
435 unset BUILDDIR
331 done 436 done
332 437
333 if [[ "${default_function}" == "1" ]]; then 438 if [[ "${default_function}" == "1" ]]; then
334 unset -f python_default_function 439 unset -f python_default_function
335 fi 440 fi
354 459
355# @FUNCTION: python_pkg_setup 460# @FUNCTION: python_pkg_setup
356# @DESCRIPTION: 461# @DESCRIPTION:
357# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 462# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
358# are respected. Only exported if one of those variables is set. 463# are respected. Only exported if one of those variables is set.
359if ! has ${EAPI} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 464if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
360 python_pkg_setup_fail() { 465 python_pkg_setup_fail() {
361 eerror "${1}" 466 eerror "${1}"
362 die "${1}" 467 die "${1}"
363 } 468 }
364 469
406 fi 511 fi
407 DEPEND="${PYTHON_USE_WITH_ATOM}" 512 DEPEND="${PYTHON_USE_WITH_ATOM}"
408 RDEPEND="${PYTHON_USE_WITH_ATOM}" 513 RDEPEND="${PYTHON_USE_WITH_ATOM}"
409fi 514fi
410 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
528 unset python_default_function
529
530 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
531fi
532
411# @FUNCTION: python_disable_pyc 533# @FUNCTION: python_disable_pyc
412# @DESCRIPTION: 534# @DESCRIPTION:
413# Tells python not to automatically recompile modules to .pyc/.pyo 535# Tell Python not to automatically recompile modules to .pyc/.pyo
414# even if the timestamps/version stamps don't match. This is done 536# even if the timestamps/version stamps don't match. This is done
415# to protect sandbox. 537# to protect sandbox.
416#
417# note: supported by >=dev-lang/python-2.2.3-r3 only.
418#
419python_disable_pyc() { 538python_disable_pyc() {
420 export PYTHONDONTWRITEBYTECODE=1 # For 2.6 and above 539 export PYTHONDONTWRITEBYTECODE="1"
421 export PYTHON_DONTCOMPILE=1 # For 2.5 and below
422} 540}
423 541
424# @FUNCTION: python_enable_pyc 542# @FUNCTION: python_enable_pyc
425# @DESCRIPTION: 543# @DESCRIPTION:
426# Tells python to automatically recompile modules to .pyc/.pyo if the 544# Tell Python to automatically recompile modules to .pyc/.pyo if the
427# timestamps/version stamps change 545# timestamps/version stamps have changed.
428python_enable_pyc() { 546python_enable_pyc() {
429 unset PYTHONDONTWRITEBYTECODE 547 unset PYTHONDONTWRITEBYTECODE
430 unset PYTHON_DONTCOMPILE
431} 548}
432 549
433python_disable_pyc 550python_disable_pyc
434 551
435# @FUNCTION: python_need_rebuild 552# @FUNCTION: python_need_rebuild
467# @FUNCTION: python_get_sitedir 584# @FUNCTION: python_get_sitedir
468# @DESCRIPTION: 585# @DESCRIPTION:
469# Run without arguments, returns the Python site-packages directory. 586# Run without arguments, returns the Python site-packages directory.
470python_get_sitedir() { 587python_get_sitedir() {
471 echo "$(python_get_libdir)/site-packages" 588 echo "$(python_get_libdir)/site-packages"
472}
473
474# @FUNCTION: python_makesym
475# @DESCRIPTION:
476# Run without arguments, it will create the /usr/bin/python symlinks
477# to the latest installed version
478python_makesym() {
479 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
480 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
481} 589}
482 590
483# @FUNCTION: python_tkinter_exists 591# @FUNCTION: python_tkinter_exists
484# @DESCRIPTION: 592# @DESCRIPTION:
485# Run without arguments, checks if python was compiled with Tkinter 593# Run without arguments, checks if python was compiled with Tkinter
546 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") 654 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
547 done 655 done
548 656
549 if ((${#myfiles[@]})); then 657 if ((${#myfiles[@]})); then
550 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" 658 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
551 python${PYVER} -O ${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
552 else 660 else
553 ewarn "No files to compile!" 661 ewarn "No files to compile!"
554 fi 662 fi
555} 663}
556 664
604 else 712 else
605 ewarn "'${root}/$1' doesn't exist!" 713 ewarn "'${root}/$1' doesn't exist!"
606 fi 714 fi
607 else 715 else
608 for PYTHON_ABI in ${PYTHON_ABIS}; do 716 for PYTHON_ABI in ${PYTHON_ABIS}; do
609 if [[ -d "${root}/$(python_get_sitedir)/$1" ]]; then 717 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
610 site_packages_dirs+=("$1") 718 site_packages_dirs+=("$1")
611 break 719 break
612 elif [[ -f "${root}/$(python_get_sitedir)/$1" ]]; then 720 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
613 site_packages_files+=("$1") 721 site_packages_files+=("$1")
614 break 722 break
615 elif [[ -e "${root}/$(python_get_sitedir)/$1" ]]; then 723 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
616 ewarn "'$1' is not a file or a directory!" 724 ewarn "'$1' is not a file or a directory!"
617 else 725 else
618 ewarn "'$1' doesn't exist!" 726 ewarn "'$1' doesn't exist!"
619 fi 727 fi
620 done 728 done
631 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 739 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
632 return_code="0" 740 return_code="0"
633 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" 741 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
634 if ((${#site_packages_dirs[@]})); then 742 if ((${#site_packages_dirs[@]})); then
635 for dir in "${site_packages_dirs[@]}"; do 743 for dir in "${site_packages_dirs[@]}"; do
636 site_packages_absolute_dirs+=("${root}/$(python_get_sitedir)/${dir}") 744 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
637 done 745 done
638 "$(get_python)" "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" 746 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
639 "$(get_python)" -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" 747 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
640 fi 748 fi
641 if ((${#site_packages_files[@]})); then 749 if ((${#site_packages_files[@]})); then
642 for file in "${site_packages_files[@]}"; do 750 for file in "${site_packages_files[@]}"; do
643 site_packages_absolute_files+=("${root}/$(python_get_sitedir)/${file}") 751 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
644 done 752 done
645 "$(get_python)" "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" 753 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
646 "$(get_python)" -O "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" 754 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
647 fi 755 fi
648 eend "${return_code}" 756 eend "${return_code}"
649 fi 757 fi
650 unset site_packages_absolute_dirs site_packages_absolute_files 758 unset site_packages_absolute_dirs site_packages_absolute_files
651 done 759 done
655 763
656 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 764 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
657 return_code="0" 765 return_code="0"
658 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." 766 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..."
659 if ((${#other_dirs[@]})); then 767 if ((${#other_dirs[@]})); then
660 python${PYVER} "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 768 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
661 python${PYVER} -O "${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"
662 fi 770 fi
663 if ((${#other_files[@]})); then 771 if ((${#other_files[@]})); then
664 python${PYVER} "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 772 python${PYVER} "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
665 python${PYVER} -O "${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"
666 fi 774 fi
667 eend "${return_code}" 775 eend "${return_code}"
668 fi 776 fi
669 else 777 else
670 local myroot mydirs=() myfiles=() myopts=() 778 local myroot mydirs=() myfiles=() myopts=() return_code="0"
671 779
672 # strip trailing slash 780 # strip trailing slash
673 myroot="${ROOT%/}" 781 myroot="${ROOT%/}"
674 782
675 # respect ROOT and options passed to compileall.py 783 # respect ROOT and options passed to compileall.py
713 821
714 ebegin "Byte compiling python modules for python-${PYVER} .." 822 ebegin "Byte compiling python modules for python-${PYVER} .."
715 if ((${#mydirs[@]})); then 823 if ((${#mydirs[@]})); then
716 python${PYVER} \ 824 python${PYVER} \
717 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 825 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
718 "${myopts[@]}" "${mydirs[@]}" 826 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
719 python${PYVER} -O \ 827 python${PYVER} -O \
720 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 828 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
721 "${myopts[@]}" "${mydirs[@]}" 829 "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
722 fi 830 fi
723 831
724 if ((${#myfiles[@]})); then 832 if ((${#myfiles[@]})); then
725 python_mod_compile "${myfiles[@]}" 833 python_mod_compile "${myfiles[@]}"
726 fi 834 fi
727 835
728 eend $? 836 eend "${return_code}"
729 fi 837 fi
730} 838}
731 839
732# @FUNCTION: python_mod_cleanup 840# @FUNCTION: python_mod_cleanup
733# @USAGE: [directory] 841# @USAGE: [directory]
756 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 864 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories"
757 elif [[ "$1" =~ ^/ ]]; then 865 elif [[ "$1" =~ ^/ ]]; then
758 SEARCH_PATH+=("${root}/${1#/}") 866 SEARCH_PATH+=("${root}/${1#/}")
759 else 867 else
760 for PYTHON_ABI in ${PYTHON_ABIS}; do 868 for PYTHON_ABI in ${PYTHON_ABIS}; do
761 SEARCH_PATH+=("${root}/$(python_get_sitedir)/$1") 869 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
762 done 870 done
763 fi 871 fi
764 shift 872 shift
765 done 873 done
766 else 874 else
770 else 878 else
771 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) 879 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages)
772 fi 880 fi
773 881
774 for path in "${SEARCH_PATH[@]}"; do 882 for path in "${SEARCH_PATH[@]}"; do
883 [[ ! -d "${path}" ]] && continue
775 einfo "Cleaning orphaned Python bytecode from ${path} .." 884 einfo "Cleaning orphaned Python bytecode from ${path} .."
776 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 885 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
777 src_py="${REPLY%[co]}" 886 src_py="${REPLY%[co]}"
778 [[ -f "${src_py}" ]] && continue 887 [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue
779 einfo "Purging ${src_py}[co]" 888 einfo "Purging ${src_py}[co]"
780 rm -f "${src_py}"[co] 889 rm -f "${src_py}"[co]
781 done 890 done
782 891
783 # Attempt to remove directories that may be empty. 892 # Attempt to remove directories that may be empty.
784 find "${path}" -type d | sort -r | while read -r dir; do 893 find "${path}" -type d | sort -r | while read -r dir; do
785 rmdir "${dir}" 2>/dev/null 894 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}"
786 done 895 done
787 done 896 done
788} 897}

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

  ViewVC Help
Powered by ViewVC 1.1.20