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

Diff of /eclass/python.eclass

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

Revision 1.7 Revision 1.57
1# Copyright 1999-2003 Gentoo Technologies, Inc. 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.7 2003/10/24 07:12:42 pythonhead Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.57 2009/08/02 16:56:41 arfrever Exp $
4
5# @ECLASS: python.eclass
6# @MAINTAINER:
7# python@gentoo.org
4# 8#
5# Author: Alastair Tse <liquidx@gentoo.org> 9# original author: Alastair Tse <liquidx@gentoo.org>
6#
7# A Utility Eclass that should be inherited by anything that deals with 10# @BLURB: A Utility Eclass that should be inherited by anything that deals with Python or Python modules.
8# Python or Python modules. 11# @DESCRIPTION:
9# 12# Some useful functions for dealing with python.
10# - Features:
11# python_version() - sets PYVER/PYVER_MAJOR/PYVER_MINOR
12# python_tkinter_exists() - Checks for tkinter support in python
13# python_mod_exists() - Checks if a python module exists
14# python_mod_compile() - Compiles a .py file to a .pyc/.pyo
15# python_mod_optimize() - Generates .pyc/.pyo precompiled scripts
16# python_mod_cleanup() - Goes through /usr/lib/python* to remove
17# orphaned *.pyc *.pyo
18# python_makesym() - Makes /usr/bin/python symlinks
19
20inherit alternatives 13inherit alternatives multilib
21 14
22ECLASS="python"
23INHERITED="$INHERITED $ECLASS"
24 15
25# 16if [[ -n "${NEED_PYTHON}" ]] ; then
26# name: python_disable/enable_pyc 17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
27# desc: tells python not to automatically recompile modules to .pyc/.pyo 18 DEPEND="${PYTHON_ATOM}"
28# even if the timestamps/version stamps don't match. this is 19 RDEPEND="${DEPEND}"
29# done to protect sandbox. 20else
30# 21 PYTHON_ATOM="dev-lang/python"
31# note: supported by >=dev-lang/python-2.2.3-r3 only. 22fi
32#
33python_disable_pyc() {
34 export PYTHON_DONTCOMPILE=1
35}
36 23
37python_enable_pyc() { 24if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
38 unset PYTHON_DONTCOMPILE 25 DEPEND="${DEPEND} >=app-admin/eselect-python-20090801"
39} 26fi
40 27
41python_disable_pyc 28__python_eclass_test() {
29 __python_version_extract 2.3
30 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
31 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
32 __python_version_extract 2.3.4
33 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
34 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
35 __python_version_extract 2.3.5
36 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
37 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
38 __python_version_extract 2.4
39 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
40 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
41 __python_version_extract 2.5b3
42 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
43 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
44}
42 45
43# 46# @FUNCTION: python_version
44# name: python_version 47# @DESCRIPTION:
45# desc: run without arguments and it will export the version of python 48# Run without arguments and it will export the version of python
46# currently in use as $PYVER 49# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
47# 50__python_version_extract() {
51 local verstr=$1
52 export PYVER_MAJOR=${verstr:0:1}
53 export PYVER_MINOR=${verstr:2:1}
54 if [[ ${verstr:3:1} == . ]]; then
55 export PYVER_MICRO=${verstr:4}
56 fi
57 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
58}
59
48python_version() { 60python_version() {
61 [[ -n "${PYVER}" ]] && return 0
49 local tmpstr 62 local tmpstr
50 python=${python:-/usr/bin/python} 63 python=${python:-/usr/bin/python}
51 tmpstr="$(${python} -V 2>&1 )" 64 tmpstr="$(${python} -V 2>&1 )"
52 export PYVER_ALL="${tmpstr#Python }" 65 export PYVER_ALL="${tmpstr#Python }"
53 66 __python_version_extract $PYVER_ALL
54 export PYVER_MAJOR=$(echo ${PYVER_ALL} | cut -d. -f1)
55 export PYVER_MINOR=$(echo ${PYVER_ALL} | cut -d. -f2)
56 export PYVER_MICRO=$(echo ${PYVER_ALL} | cut -d. -f3-)
57 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
58} 67}
59 68
69# @FUNCTION: get_python
70# @USAGE: [-a|--absolute-path] <Python_ABI="${PYTHON_ABI}">
71# @DESCRIPTION:
72# 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.
74get_python() {
75 local absolute_path="0" slot=
76
77 while (($#)); do
78 case "$1" in
79 -a|--absolute-path)
80 absolute_path="1"
81 ;;
82 -*)
83 die "${FUNCNAME}(): Unrecognized option $1"
84 ;;
85 *)
86 break
87 ;;
88 esac
89 shift
90 done
91
92 if [[ "$#" -eq "0" ]]; then
93 if [[ -n "${PYTHON_ABI}" ]]; then
94 slot="${PYTHON_ABI}"
95 else
96 die "${FUNCNAME}(): Invalid usage"
97 fi
98 elif [[ "$#" -eq "1" ]]; then
99 slot="$1"
100 else
101 die "${FUNCNAME}(): Invalid usage"
102 fi
103
104 if [[ "${absolute_path}" == "1" ]]; then
105 echo -n "/usr/bin/python${slot}"
106 else
107 echo -n "python${slot}"
108 fi
109}
110
111# @FUNCTION: validate_PYTHON_ABIS
112# @DESCRIPTION:
113# Make sure PYTHON_ABIS variable has valid value.
114validate_PYTHON_ABIS() {
115 # Ensure that /usr/bin/python and /usr/bin/python-config are scripts.
116 if [[ "$(</usr/bin/python)" != *"Gentoo Python wrapper script"* ]]; then
117 die "/usr/bin/python isn't valid script"
118 fi
119 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
120 die "/usr/bin/python-config isn't valid script"
121 fi
122
123 # 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
125 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"
127 for ABI in ${USE_PYTHON}; do
128 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
129 ewarn "Ignoring unsupported Python ABI '${ABI}'"
130 continue
131 fi
132 support_ABI="1"
133 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
134 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then
135 support_ABI="0"
136 break
137 fi
138 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}"
147 fi
148}
149
150# @FUNCTION: python_copy_sources
151# @USAGE: [directory]
152# @DESCRIPTION:
153# Copy unpacked sources of given package for each Python ABI.
154python_copy_sources() {
155 local dir dirs=() PYTHON_ABI
156
157 if [[ "$#" -eq "0" ]]; then
158 if [[ "${WORKDIR}" == "${S}" ]]; then
159 die "${FUNCNAME}() cannot be used"
160 fi
161 dirs="${S}"
162 else
163 dirs="$@"
164 fi
165
166 validate_PYTHON_ABIS
167 for PYTHON_ABI in ${PYTHON_ABIS}; do
168 for dir in "${dirs[@]}"; do
169 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
170 done
171 done
172}
173
174# @FUNCTION: python_set_build_dir_symlink
175# @USAGE: [directory="build"]
176# @DESCRIPTION:
177# Create build directory symlink.
178python_set_build_dir_symlink() {
179 local dir="$1"
180
181 [[ -z "${PYTHON_ABIS}" ]] && die "PYTHON_ABIS variable not set"
182 [[ -z "${dir}" ]] && dir="build"
183
184 # Don't delete preexistent directories.
185 rm -f "${dir}" || die "Deletion of '${dir}' failed"
186 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
187}
188
189# @FUNCTION: python_execute_function
190# @USAGE: [--action-message message] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] <function> [arguments]
191# @DESCRIPTION:
192# Execute specified function for each value of PYTHON_ABIS, optionally passing
193# additional arguments. The specified function can use PYTHON_ABI variable.
194python_execute_function() {
195 local action action_message action_message_template= failure_message failure_message_template= function nonfatal="0" PYTHON_ABI quiet="0" separate_build_dirs="0"
196
197 while (($#)); do
198 case "$1" in
199 --action-message)
200 action_message_template="$2"
201 shift
202 ;;
203 --failure-message)
204 failure_message_template="$2"
205 shift
206 ;;
207 --nonfatal)
208 nonfatal="1"
209 ;;
210 -q|--quiet)
211 quiet="1"
212 ;;
213 -s|--separate-build-dirs)
214 separate_build_dirs="1"
215 ;;
216 -*)
217 die "${FUNCNAME}(): Unrecognized option $1"
218 ;;
219 *)
220 break
221 ;;
222 esac
223 shift
224 done
225
226 if [[ "$#" -eq "0" ]]; then
227 die "${FUNCNAME}(): Missing function name"
228 fi
229 function="$1"
230 shift
231
232 if [[ "${quiet}" == "0" ]]; then
233 [[ "${EBUILD_PHASE}" == "setup" ]] && action="Setting up"
234 [[ "${EBUILD_PHASE}" == "unpack" ]] && action="Unpacking"
235 [[ "${EBUILD_PHASE}" == "prepare" ]] && action="Preparation"
236 [[ "${EBUILD_PHASE}" == "configure" ]] && action="Configuration"
237 [[ "${EBUILD_PHASE}" == "compile" ]] && action="Building"
238 [[ "${EBUILD_PHASE}" == "test" ]] && action="Testing"
239 [[ "${EBUILD_PHASE}" == "install" ]] && action="Installation"
240 [[ "${EBUILD_PHASE}" == "preinst" ]] && action="Preinstallation"
241 [[ "${EBUILD_PHASE}" == "postinst" ]] && action="Postinstallation"
242 [[ "${EBUILD_PHASE}" == "prerm" ]] && action="Preuninstallation"
243 [[ "${EBUILD_PHASE}" == "postrm" ]] && action="Postuninstallation"
244 fi
245
246 local RED GREEN BLUE NORMAL
247 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
248 RED=$'\e[1;31m'
249 GREEN=$'\e[1;32m'
250 BLUE=$'\e[1;34m'
251 NORMAL=$'\e[0m'
252 else
253 RED=
254 GREEN=
255 BLUE=
256 NORMAL=
257 fi
258
259 validate_PYTHON_ABIS
260 for PYTHON_ABI in ${PYTHON_ABIS}; do
261 if [[ "${quiet}" == "0" ]]; then
262 if [[ -n "${action_message_template}" ]]; then
263 action_message="$(eval echo -n "${action_message_template}")"
264 else
265 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..."
266 fi
267 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
268 fi
269 if [[ "${separate_build_dirs}" == "1" ]]; then
270 pushd "${S}-${PYTHON_ABI}" > /dev/null || die "pushd failed"
271 fi
272 if ! EPYTHON="$(get_python)" "${function}" "$@"; then
273 if [[ -n "${failure_message_template}" ]]; then
274 failure_message="$(eval echo -n "${failure_message_template}")"
275 else
276 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
277 fi
278 if [[ "${nonfatal}" == "1" ]] || has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
279 local ABI enabled_PYTHON_ABIS
280 for ABI in ${PYTHON_ABIS}; do
281 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
282 done
283 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
284 if [[ "${quiet}" == "0" ]]; then
285 ewarn "${RED}${failure_message}${NORMAL}"
286 fi
287 else
288 die "${failure_message}"
289 fi
290 fi
291 if [[ "${separate_build_dirs}" == "1" ]]; then
292 popd > /dev/null || die "popd failed"
293 fi
294 done
295}
296
297
298# @ECLASS-VARIABLE: PYTHON_USE_WITH
299# @DESCRIPTION:
300# Set this to a space separated list of use flags
301# the python slot in use must be built with.
302
303# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
304# @DESCRIPTION:
305# Set this to a space separated list of use flags
306# of which one must be turned on for the slot of
307# in use.
308
309# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
310# @DESCRIPTION:
311# Set this if you need to make either PYTHON_USE_WITH or
312# PYTHON_USE_WITH_OR atoms conditional under a use flag.
313
314# @FUNCTION: python_pkg_setup
315# @DESCRIPTION:
316# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
317# are respected. Only exported if one of those variables is set.
318if ! has ${EAPI} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
319 python_pkg_setup_fail() {
320 eerror "${1}"
321 die "${1}"
322 }
323
324 python_pkg_setup() {
325 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
326
327 python_version
328 local failed
329 local pyatom="dev-lang/python:${PYVER}"
330
331 for use in ${PYTHON_USE_WITH}; do
332 if ! has_version "${pyatom}[${use}]"; then
333 python_pkg_setup_fail \
334 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}"
335 fi
336 done
337
338 for use in ${PYTHON_USE_WITH_OR}; do
339 if has_version "${pyatom}[${use}]"; then
340 return
341 fi
342 done
343
344 if [[ ${PYTHON_USE_WITH_OR} ]]; then
345 python_pkg_setup_fail \
346 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}"
347 fi
348 }
349
350 EXPORT_FUNCTIONS pkg_setup
351
352 if [[ ${PYTHON_USE_WITH} ]]; then
353 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
354 elif [[ ${PYTHON_USE_WITH_OR} ]]; then
355 PYTHON_USE_WITH_ATOM="|| ( "
356 for use in ${PYTHON_USE_WITH_OR}; do
357 PYTHON_USE_WITH_ATOM="
358 ${PYTHON_USE_WITH_ATOM}
359 ${PYTHON_ATOM}[${use}]"
360 done
361 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )"
362 fi
363 if [[ ${PYTHON_USE_WITH_OPT} ]]; then
364 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
365 fi
366 DEPEND="${PYTHON_USE_WITH_ATOM}"
367 RDEPEND="${PYTHON_USE_WITH_ATOM}"
368fi
369
370# @FUNCTION: python_disable_pyc
371# @DESCRIPTION:
372# Tells python not to automatically recompile modules to .pyc/.pyo
373# even if the timestamps/version stamps don't match. This is done
374# to protect sandbox.
60# 375#
61# name: python_makesym 376# note: supported by >=dev-lang/python-2.2.3-r3 only.
377#
378python_disable_pyc() {
379 export PYTHONDONTWRITEBYTECODE=1 # For 2.6 and above
380 export PYTHON_DONTCOMPILE=1 # For 2.5 and below
381}
382
383# @FUNCTION: python_enable_pyc
384# @DESCRIPTION:
385# Tells python to automatically recompile modules to .pyc/.pyo if the
386# timestamps/version stamps change
387python_enable_pyc() {
388 unset PYTHONDONTWRITEBYTECODE
389 unset PYTHON_DONTCOMPILE
390}
391
392python_disable_pyc
393
394# @FUNCTION: python_need_rebuild
395# @DESCRIPTION: Run without arguments, specifies that the package should be
396# rebuilt after a python upgrade.
397python_need_rebuild() {
398 python_version
399 export PYTHON_NEED_REBUILD=${PYVER}
400}
401
402# @FUNCTION: python_get_includedir
403# @DESCRIPTION:
404# Run without arguments, returns the Python include directory.
405python_get_includedir() {
406 if [[ -n "${PYTHON_ABI}" ]]; then
407 echo "/usr/include/python${PYTHON_ABI}"
408 else
409 python_version
410 echo "/usr/include/python${PYVER}"
411 fi
412}
413
414# @FUNCTION: python_get_libdir
415# @DESCRIPTION:
416# Run without arguments, returns the Python library directory.
417python_get_libdir() {
418 if [[ -n "${PYTHON_ABI}" ]]; then
419 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
420 else
421 python_version
422 echo "/usr/$(get_libdir)/python${PYVER}"
423 fi
424}
425
426# @FUNCTION: python_get_sitedir
427# @DESCRIPTION:
428# Run without arguments, returns the Python site-packages directory.
429python_get_sitedir() {
430 echo "$(python_get_libdir)/site-packages"
431}
432
433# @FUNCTION: python_makesym
434# @DESCRIPTION:
62# desc: run without arguments, it will create the /usr/bin/python symlinks 435# Run without arguments, it will create the /usr/bin/python symlinks
63# to the latest installed version 436# to the latest installed version
64#
65python_makesym() { 437python_makesym() {
66 alternatives_auto_makesym "/usr/bin/python" "/usr/bin/python[0-9].[0-9]" 438 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
67 alternatives_auto_makesym "/usr/bin/python2" "/usr/bin/python[0-9].[0-9]" 439 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
68} 440}
69 441
70#
71# name: python_tkinter_exists 442# @FUNCTION: python_tkinter_exists
72# desc: run without arguments, it will return TRUE(0) if python is compiled 443# @DESCRIPTION:
73# with tkinter or FALSE(1) if python is compiled without tkinter. 444# Run without arguments, checks if python was compiled with Tkinter
74# 445# support. If not, prints an error message and dies.
75python_tkinter_exists() { 446python_tkinter_exists() {
76 if ! python -c "import Tkinter" >/dev/null 2>&1; then 447 if ! python -c "import Tkinter" >/dev/null 2>&1; then
77 eerror "You need to recompile python with Tkinter support." 448 eerror "You need to recompile python with Tkinter support."
78 eerror "That means: USE='tcltk' emerge python" 449 eerror "Try adding: 'dev-lang/python tk'"
450 eerror "in to /etc/portage/package.use"
79 echo 451 echo
80 die "missing tkinter support with installed python" 452 die "missing tkinter support with installed python"
81 fi 453 fi
82} 454}
83 455
84# 456# @FUNCTION: python_mod_exists
85# name: python_mod_exists 457# @USAGE: <module>
458# @DESCRIPTION:
86# desc: run with the module name as an argument. it will check if a 459# Run with the module name as an argument. it will check if a
87# python module is installed and loadable. it will return 460# python module is installed and loadable. it will return
88# TRUE(0) if the module exists, and FALSE(1) if the module does 461# TRUE(0) if the module exists, and FALSE(1) if the module does
89# not exist. 462# not exist.
90# exam: 463#
464# Example:
91# if python_mod_exists gtk; then 465# if python_mod_exists gtk; then
92# echo "gtk support enabled 466# echo "gtk support enabled"
93# fi 467# fi
94#
95python_mod_exists() { 468python_mod_exists() {
469 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
96 if ! python -c "import $1" >/dev/null 2>&1; then 470 python -c "import $1" &>/dev/null
97 return 1
98 fi
99 return 0
100} 471}
101 472
102# 473# @FUNCTION: python_mod_compile
103# name: python_mod_compile 474# @USAGE: <file> [more files ...]
475# @DESCRIPTION:
104# desc: given a filename, it will pre-compile the module's .pyc and .pyo. 476# Given filenames, it will pre-compile the module's .pyc and .pyo.
105# should only be run in pkg_postinst() 477# This function should only be run in pkg_postinst()
106# exam: 478#
479# Example:
107# python_mod_compile ${ROOT}usr/lib/python2.3/site-packages/pygoogle.py 480# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
108# 481#
109python_mod_compile() { 482python_mod_compile() {
483 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
484 die "${FUNCNAME}() cannot be used in this EAPI"
485 fi
486
487 local f myroot myfiles=()
488
489 # Check if phase is pkg_postinst()
490 [[ ${EBUILD_PHASE} != postinst ]] &&\
491 die "${FUNCNAME} should only be run in pkg_postinst()"
492
110 # allow compiling for older python versions 493 # allow compiling for older python versions
111 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 494 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
112 PYVER=${PYTHON_OVERRIDE_PYVER} 495 PYVER=${PYTHON_OVERRIDE_PYVER}
113 else 496 else
114 python_version 497 python_version
115 fi 498 fi
116 499
117 if [ -f "$1" ]; then 500 # strip trailing slash
118 python${PYVER} -c "import py_compile; py_compile.compile('${1}')" || \ 501 myroot="${ROOT%/}"
502
503 # respect ROOT
504 for f in "$@"; do
505 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
506 done
507
508 if ((${#myfiles[@]})); then
509 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
510 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
511 else
119 ewarn "Failed to compile ${1}" 512 ewarn "No files to compile!"
120 python${PYVER} -O -c "import py_compile; py_compile.compile('${1}')" || \
121 ewarn "Failed to compile ${1}"
122 else
123 ewarn "Unable to find ${1}"
124 fi 513 fi
125} 514}
126 515
127#
128# name: python_mod_optimize 516# @FUNCTION: python_mod_optimize
517# @USAGE: [options] [directory|file]
518# @DESCRIPTION:
129# desc: if no arguments supplied, it will recompile all modules under 519# If no arguments supplied, it will recompile not recursively all modules
130# sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..) 520# under sys.path (eg. /usr/lib/python2.6, /usr/lib/python2.6/site-packages).
131# no recursively
132# 521#
133# if supplied with arguments, it will recompile all modules recursively 522# If supplied with arguments, it will recompile all modules recursively
134# in the supplied directory 523# in the supplied directory.
135# exam: 524# This function should only be run in pkg_postinst().
136# python_mod_optimize ${ROOT}usr/share/codegen
137# 525#
526# Options passed to this function are passed to compileall.py.
527#
528# Example:
529# python_mod_optimize ctypesgencore
138python_mod_optimize() { 530python_mod_optimize() {
531 # Check if phase is pkg_postinst().
532 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()"
533
534 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
535 local dir file options=() other_dirs=() other_files=() PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
536
537 # Strip trailing slash from ROOT.
538 root="${ROOT%/}"
539
540 # Respect ROOT and options passed to compileall.py.
541 while (($#)); do
542 case "$1" in
543 -l|-f|-q)
544 options+=("$1")
545 ;;
546 -d|-x)
547 options+=("$1" "$2")
548 shift
549 ;;
550 -*)
551 ewarn "${FUNCNAME}: Ignoring compile option $1"
552 ;;
553 *)
554 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
555 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories"
556 elif [[ "$1" =~ ^/ ]]; then
557 if [[ -d "${root}/$1" ]]; then
558 other_dirs+=("${root}/$1")
559 elif [[ -f "${root}/$1" ]]; then
560 other_files+=("${root}/$1")
561 elif [[ -e "${root}/$1" ]]; then
562 ewarn "'${root}/$1' is not a file or a directory!"
563 else
564 ewarn "'${root}/$1' doesn't exist!"
565 fi
566 else
567 for PYTHON_ABI in ${PYTHON_ABIS}; do
568 if [[ -d "${root}/$(python_get_sitedir)/$1" ]]; then
569 site_packages_dirs+=("$1")
570 break
571 elif [[ -f "${root}/$(python_get_sitedir)/$1" ]]; then
572 site_packages_files+=("$1")
573 break
574 elif [[ -e "${root}/$(python_get_sitedir)/$1" ]]; then
575 ewarn "'$1' is not a file or a directory!"
576 else
577 ewarn "'$1' doesn't exist!"
578 fi
579 done
580 fi
581 ;;
582 esac
583 shift
584 done
585
586 # Set additional options.
587 options+=("-q")
588
589 for PYTHON_ABI in ${PYTHON_ABIS}; do
590 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
591 return_code="0"
592 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
593 if ((${#site_packages_dirs[@]})); then
594 for dir in "${site_packages_dirs[@]}"; do
595 site_packages_absolute_dirs+=("${root}/$(python_get_sitedir)/${dir}")
596 done
597 "$(get_python)" "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
598 "$(get_python)" -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
599 fi
600 if ((${#site_packages_files[@]})); then
601 for file in "${site_packages_files[@]}"; do
602 site_packages_absolute_files+=("${root}/$(python_get_sitedir)/${file}")
603 done
604 "$(get_python)" "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
605 "$(get_python)" -O "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
606 fi
607 eend "${return_code}"
608 fi
609 unset site_packages_absolute_dirs site_packages_absolute_files
610 done
611
612 # Don't use PYTHON_ABI in next calls to python_get_libdir().
613 unset PYTHON_ABI
614
615 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
616 return_code="0"
617 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..."
618 if ((${#other_dirs[@]})); then
619 python${PYVER} "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
620 python${PYVER} -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
621 fi
622 if ((${#other_files[@]})); then
623 python${PYVER} "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
624 python${PYVER} -O "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
625 fi
626 eend "${return_code}"
627 fi
628 else
629 local myroot mydirs=() myfiles=() myopts=()
630
631 # strip trailing slash
632 myroot="${ROOT%/}"
633
634 # respect ROOT and options passed to compileall.py
635 while (($#)); do
636 case "$1" in
637 -l|-f|-q)
638 myopts+=("$1")
639 ;;
640 -d|-x)
641 myopts+=("$1" "$2")
642 shift
643 ;;
644 -*)
645 ewarn "${FUNCNAME}: Ignoring compile option $1"
646 ;;
647 *)
648 if [[ -d "${myroot}"/$1 ]]; then
649 mydirs+=("${myroot}/$1")
650 elif [[ -f "${myroot}"/$1 ]]; then
651 # Files are passed to python_mod_compile which is ROOT-aware
652 myfiles+=("$1")
653 elif [[ -e "${myroot}/$1" ]]; then
654 ewarn "${myroot}/$1 is not a file or directory!"
655 else
656 ewarn "${myroot}/$1 doesn't exist!"
657 fi
658 ;;
659 esac
660 shift
661 done
662
139 # allow compiling for older python versions 663 # allow compiling for older python versions
140 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 664 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
141 PYVER=${PYTHON_OVERRIDE_PYVER} 665 PYVER=${PYTHON_OVERRIDE_PYVER}
142 else 666 else
143 python_version 667 python_version
144 fi 668 fi
145 669
146 # set opts 670 # set additional opts
147 if [ "${PYVER}" = "2.2" ]; then 671 myopts+=(-q)
148 compileopts=""
149 else
150 compileopts="-q"
151 fi
152 672
153 ebegin "Byte Compiling Python modules for ${PYVER} .." 673 ebegin "Byte compiling python modules for python-${PYVER} .."
154 python${PYVER} ${ROOT}usr/lib/python${PYVER}/compileall.py ${compileopts} $@ 674 if ((${#mydirs[@]})); then
155 python${PYVER} -O ${ROOT}usr/lib/python${PYVER}/compileall.py ${compileopts} $@ 675 python${PYVER} \
676 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
677 "${myopts[@]}" "${mydirs[@]}"
678 python${PYVER} -O \
679 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
680 "${myopts[@]}" "${mydirs[@]}"
681 fi
682
683 if ((${#myfiles[@]})); then
684 python_mod_compile "${myfiles[@]}"
685 fi
686
156 eend $? 687 eend $?
688 fi
157} 689}
158 690
159# 691# @FUNCTION: python_mod_cleanup
160# name: python_mod_cleanup 692# @USAGE: [directory]
693# @DESCRIPTION:
161# desc: run with optional arguments, where arguments are directories of 694# Run with optional arguments, where arguments are directories of
162# python modules. if none given, it will look in /usr/lib/python[0-9].[0-9] 695# python modules. If none given, it will look in /usr/lib/python[0-9].[0-9].
163# 696#
164# it will recursively scan all compiled python modules in the directories 697# It will recursively scan all compiled Python modules in the directories and
165# and determine if they are orphaned (eg. their corresponding .py is missing.) 698# determine if they are orphaned (i.e. their corresponding .py files are missing.)
166# if they are, then it will remove their corresponding .pyc and .pyo 699# If they are, then it will remove their corresponding .pyc and .pyo files.
167# 700#
701# This function should only be run in pkg_postrm().
168python_mod_cleanup() { 702python_mod_cleanup() {
169 local SEARCH_PATH 703 local PYTHON_ABI SEARCH_PATH=() root src_py
170 704
171 if [ $# -gt 0 ]; then 705 # Check if phase is pkg_postrm().
172 for path in $@; do 706 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
173 SEARCH_PATH="${SEARCH_PATH} ${ROOT}${path#/}" 707
708 # Strip trailing slash from ROOT.
709 root="${ROOT%/}"
710
711 if (($#)); then
712 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
713 while (($#)); do
714 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
715 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories"
716 elif [[ "$1" =~ ^/ ]]; then
717 SEARCH_PATH+=("${root}/${1#/}")
718 else
719 for PYTHON_ABI in ${PYTHON_ABIS}; do
720 SEARCH_PATH+=("${root}/$(python_get_sitedir)/$1")
721 done
722 fi
723 shift
174 done 724 done
175 else 725 else
176 for path in ${ROOT}usr/lib/python*/site-packages; do 726 SEARCH_PATH=("${@#/}")
177 SEARCH_PATH="${SEARCH_PATH} ${path}" 727 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
178 done
179 fi 728 fi
729 else
730 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages)
731 fi
180 732
181 for path in ${SEARCH_PATH}; do 733 for path in "${SEARCH_PATH[@]}"; do
182 einfo "Searching ${path} .." 734 einfo "Cleaning orphaned Python bytecode from ${path} .."
183 for obj in $(find ${path} -name *.pyc); do 735 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
184 src_py="$(echo $obj | sed 's:c$::')" 736 src_py="${REPLY%[co]}"
185 if [ ! -f "${src_py}" ]; then 737 [[ -f "${src_py}" ]] && continue
186 einfo "Purging ${src_py}[co]" 738 einfo "Purging ${src_py}[co]"
187 rm -f ${src_py}[co] 739 rm -f "${src_py}"[co]
188 fi 740 done
741
742 # Attempt to remove directories that may be empty.
743 find "${path}" -type d | sort -r | while read -r dir; do
744 rmdir "${dir}" 2>/dev/null
189 done 745 done
190 done 746 done
191} 747}
192
193

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.57

  ViewVC Help
Powered by ViewVC 1.1.20