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

Diff of /eclass/python.eclass

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

Revision 1.24 Revision 1.55
1# Copyright 1999-2004 Gentoo Foundation 1# Copyright 1999-2008 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.24 2006/06/10 16:19:54 swegener Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.55 2009/05/27 22:49:32 betelgeuse 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
22 15
23if [[ -n "${NEED_PYTHON}" ]] ; then 16if [[ -n "${NEED_PYTHON}" ]] ; then
24 DEPEND=">=dev-lang/python-${NEED_PYTHON}" 17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
18 DEPEND="${PYTHON_ATOM}"
25 RDEPEND="${DEPEND}" 19 RDEPEND="${DEPEND}"
20else
21 PYTHON_ATOM="dev-lang/python"
26fi 22fi
27 23
28__python_eclass_test() { 24__python_eclass_test() {
29 __python_version_extract 2.3 25 __python_version_extract 2.3
30 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 26 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
36 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 32 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
37 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" 33 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
38 __python_version_extract 2.4 34 __python_version_extract 2.4
39 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 35 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
40 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" 36 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
37 __python_version_extract 2.5b3
38 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
39 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
41} 40}
42 41
43# 42# @FUNCTION: python_version
44# name: python_disable/enable_pyc 43# @DESCRIPTION:
45# desc: tells python not to automatically recompile modules to .pyc/.pyo
46# even if the timestamps/version stamps don't match. this is
47# done to protect sandbox.
48#
49# note: supported by >=dev-lang/python-2.2.3-r3 only.
50#
51python_disable_pyc() {
52 export PYTHON_DONTCOMPILE=1
53}
54
55python_enable_pyc() {
56 unset PYTHON_DONTCOMPILE
57}
58
59python_disable_pyc
60
61#
62# name: python_version
63# desc: run without arguments and it will export the version of python 44# Run without arguments and it will export the version of python
64# currently in use as $PYVER 45# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
65#
66
67__python_version_extract() { 46__python_version_extract() {
68 verstr=$1 47 local verstr=$1
69 export PYVER_MAJOR=${verstr:0:1} 48 export PYVER_MAJOR=${verstr:0:1}
70 export PYVER_MINOR=${verstr:2:1} 49 export PYVER_MINOR=${verstr:2:1}
50 if [[ ${verstr:3:1} == . ]]; then
71 export PYVER_MICRO=${verstr:4} 51 export PYVER_MICRO=${verstr:4}
52 fi
72 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}" 53 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
73} 54}
74 55
75python_version() { 56python_version() {
57 [[ -n "${PYVER}" ]] && return 0
76 local tmpstr 58 local tmpstr
77 python=${python:-/usr/bin/python} 59 python=${python:-/usr/bin/python}
78 tmpstr="$(${python} -V 2>&1 )" 60 tmpstr="$(${python} -V 2>&1 )"
79 export PYVER_ALL="${tmpstr#Python }" 61 export PYVER_ALL="${tmpstr#Python }"
80 __python_version_extract $PYVER_ALL 62 __python_version_extract $PYVER_ALL
81} 63}
82 64
65# @ECLASS-VARIABLE: PYTHON_USE_WITH
66# @DESCRIPTION:
67# Set this to a space separated list of use flags
68# the python slot in use must be built with.
69
70# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
71# @DESCRIPTION:
72# Set this to a space separated list of use flags
73# of which one must be turned on for the slot of
74# in use.
75
76# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
77# @DESCRIPTION:
78# Set this if you need to make either PYTHON_USE_WITH or
79# PYTHON_USE_WITH_OR atoms conditional under a use flag.
80
81# @FUNCTION: python_pkg_setup
82# @DESCRIPTION:
83# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
84# are respected. Only exported if one of those variables is set.
85if ! has ${EAPI} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
86 python_pkg_setup_fail() {
87 eerror "${1}"
88 die "${1}"
89 }
90
91 python_pkg_setup() {
92 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
93
94 python_version
95 local failed
96 local pyatom="dev-lang/python:${PYVER}"
97
98 for use in ${PYTHON_USE_WITH}; do
99 if ! has_version "${pyatom}[${use}]"; then
100 python_pkg_setup_fail \
101 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}"
102 fi
103 done
104
105 for use in ${PYTHON_USE_WITH_OR}; do
106 if has_version "${pyatom}[${use}]"; then
107 return
108 fi
109 done
110
111 if [[ ${PYTHON_USE_WITH_OR} ]]; then
112 python_pkg_setup_fail \
113 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}"
114 fi
115 }
116
117 EXPORT_FUNCTIONS pkg_setup
118
119 if [[ ${PYTHON_USE_WITH} ]]; then
120 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
121 elif [[ ${PYTHON_USE_WITH_OR} ]]; then
122 PYTHON_USE_WITH_ATOM="|| ( "
123 for use in ${PYTHON_USE_WITH_OR}; do
124 PYTHON_USE_WITH_ATOM="
125 ${PYTHON_USE_WITH_ATOM}
126 ${PYTHON_ATOM}[${use}]"
127 done
128 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )"
129 fi
130 if [[ ${PYTHON_USE_WITH_OPT} ]]; then
131 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
132 fi
133 DEPEND="${PYTHON_USE_WITH_ATOM}"
134 RDEPEND="${PYTHON_USE_WITH_ATOM}"
135fi
136
137# @FUNCTION: python_disable_pyc
138# @DESCRIPTION:
139# Tells python not to automatically recompile modules to .pyc/.pyo
140# even if the timestamps/version stamps don't match. This is done
141# to protect sandbox.
83# 142#
84# name: python_makesym 143# note: supported by >=dev-lang/python-2.2.3-r3 only.
144#
145python_disable_pyc() {
146 export PYTHONDONTWRITEBYTECODE=1 # For 2.6 and above
147 export PYTHON_DONTCOMPILE=1 # For 2.5 and below
148}
149
150# @FUNCTION: python_enable_pyc
151# @DESCRIPTION:
152# Tells python to automatically recompile modules to .pyc/.pyo if the
153# timestamps/version stamps change
154python_enable_pyc() {
155 unset PYTHONDONTWRITEBYTECODE
156 unset PYTHON_DONTCOMPILE
157}
158
159python_disable_pyc
160
161# @FUNCTION: python_need_rebuild
162# @DESCRIPTION: Run without arguments, specifies that the package should be
163# rebuilt after a python upgrade.
164python_need_rebuild() {
165 python_version
166 export PYTHON_NEED_REBUILD=${PYVER}
167}
168
169# @FUNCTION: python_get_libdir
170# @DESCRIPTION:
171# Run without arguments, returns the python library dir
172python_get_libdir() {
173 python_version
174 echo "/usr/$(get_libdir)/python${PYVER}"
175}
176
177# @FUNCTION: python_get_sitedir
178# @DESCRIPTION:
179# Run without arguments, returns the python site-packages dir
180python_get_sitedir() {
181 echo "$(python_get_libdir)/site-packages"
182}
183
184# @FUNCTION: python_makesym
185# @DESCRIPTION:
85# desc: run without arguments, it will create the /usr/bin/python symlinks 186# Run without arguments, it will create the /usr/bin/python symlinks
86# to the latest installed version 187# to the latest installed version
87#
88python_makesym() { 188python_makesym() {
89 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]" 189 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
90 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]" 190 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
91} 191}
92 192
93#
94# name: python_tkinter_exists 193# @FUNCTION: python_tkinter_exists
194# @DESCRIPTION:
95# desc: run without arguments, checks if python was compiled with Tkinter 195# Run without arguments, checks if python was compiled with Tkinter
96# support. If not, prints an error message and dies. 196# support. If not, prints an error message and dies.
97#
98python_tkinter_exists() { 197python_tkinter_exists() {
99 if ! python -c "import Tkinter" >/dev/null 2>&1; then 198 if ! python -c "import Tkinter" >/dev/null 2>&1; then
100 eerror "You need to recompile python with Tkinter support." 199 eerror "You need to recompile python with Tkinter support."
101 eerror "That means: USE='tcltk' emerge python" 200 eerror "Try adding: 'dev-lang/python tk'"
201 eerror "in to /etc/portage/package.use"
102 echo 202 echo
103 die "missing tkinter support with installed python" 203 die "missing tkinter support with installed python"
104 fi 204 fi
105} 205}
106 206
107# 207# @FUNCTION: python_mod_exists
108# name: python_mod_exists 208# @USAGE: < module >
209# @DESCRIPTION:
109# desc: run with the module name as an argument. it will check if a 210# Run with the module name as an argument. it will check if a
110# python module is installed and loadable. it will return 211# python module is installed and loadable. it will return
111# TRUE(0) if the module exists, and FALSE(1) if the module does 212# TRUE(0) if the module exists, and FALSE(1) if the module does
112# not exist. 213# not exist.
113# exam: 214#
215# Example:
114# if python_mod_exists gtk; then 216# if python_mod_exists gtk; then
115# echo "gtk support enabled 217# echo "gtk support enabled"
116# fi 218# fi
117#
118python_mod_exists() { 219python_mod_exists() {
119 [ -z "$1" ] && die "${FUNCTION} requires an argument!" 220 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
120 if ! python -c "import $1" >/dev/null 2>&1; then 221 python -c "import $1" >/dev/null 2>&1
121 return 1
122 fi
123 return 0
124} 222}
125 223
126# 224# @FUNCTION: python_mod_compile
127# name: python_mod_compile 225# @USAGE: < file > [more files ...]
226# @DESCRIPTION:
128# desc: given a filename, it will pre-compile the module's .pyc and .pyo. 227# Given filenames, it will pre-compile the module's .pyc and .pyo.
129# should only be run in pkg_postinst() 228# This function should only be run in pkg_postinst()
130# exam: 229#
230# Example:
131# python_mod_compile ${ROOT}usr/lib/python2.3/site-packages/pygoogle.py 231# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
132# 232#
133python_mod_compile() { 233python_mod_compile() {
234 local f myroot myfiles=()
235
236 # Check if phase is pkg_postinst()
237 [[ ${EBUILD_PHASE} != postinst ]] &&\
238 die "${FUNCNAME} should only be run in pkg_postinst()"
239
240 # allow compiling for older python versions
241 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
242 PYVER=${PYTHON_OVERRIDE_PYVER}
243 else
244 python_version
245 fi
246
247 # strip trailing slash
248 myroot="${ROOT%/}"
249
250 # respect ROOT
251 for f in "$@"; do
252 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
253 done
254
255 if ((${#myfiles[@]})); then
256 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
257 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
258 else
259 ewarn "No files to compile!"
260 fi
261}
262
263# @FUNCTION: python_mod_optimize
264# @USAGE: [ path ]
265# @DESCRIPTION:
266# If no arguments supplied, it will recompile all modules under
267# sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..)
268# no recursively
269#
270# If supplied with arguments, it will recompile all modules recursively
271# in the supplied directory
272# This function should only be run in pkg_postinst()
273#
274# Options passed to this function are passed to compileall.py
275#
276# Example:
277# python_mod_optimize /usr/share/codegen
278python_mod_optimize() {
279 local myroot mydirs=() myfiles=() myopts=()
280
281 # Check if phase is pkg_postinst()
282 [[ ${EBUILD_PHASE} != postinst ]] &&\
283 die "${FUNCNAME} should only be run in pkg_postinst()"
284
285 # strip trailing slash
286 myroot="${ROOT%/}"
287
288 # respect ROOT and options passed to compileall.py
289 while (($#)); do
290 case $1 in
291 -l|-f|-q)
292 myopts+=("$1")
293 ;;
294 -d|-x)
295 myopts+=("$1" "$2")
296 shift
297 ;;
298 -*)
299 ewarn "${FUNCNAME}: Ignoring compile option $1"
300 ;;
301 *)
302 if [[ -d "${myroot}"/$1 ]]; then
303 mydirs+=("${myroot}/$1")
304 elif [[ -f "${myroot}"/$1 ]]; then
305 # Files are passed to python_mod_compile which is ROOT-aware
306 myfiles+=("$1")
307 elif [[ -e "${myroot}/$1" ]]; then
308 ewarn "${myroot}/$1 is not a file or directory!"
309 else
310 ewarn "${myroot}/$1 doesn't exist!"
311 fi
312 ;;
313 esac
314 shift
315 done
316
134 # allow compiling for older python versions 317 # allow compiling for older python versions
135 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 318 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
136 PYVER=${PYTHON_OVERRIDE_PYVER} 319 PYVER=${PYTHON_OVERRIDE_PYVER}
137 else 320 else
138 python_version 321 python_version
139 fi 322 fi
140 323
141 if [ -f "$1" ]; then 324 # set additional opts
142 python${PYVER} -c "import py_compile; py_compile.compile('${1}')" || \ 325 myopts+=(-q)
143 ewarn "Failed to compile ${1}"
144 python${PYVER} -O -c "import py_compile; py_compile.compile('${1}')" || \
145 ewarn "Failed to compile ${1}"
146 else
147 ewarn "Unable to find ${1}"
148 fi
149}
150 326
327 ebegin "Byte compiling python modules for python-${PYVER} .."
328 if ((${#mydirs[@]})); then
329 python${PYVER} \
330 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
331 "${myopts[@]}" "${mydirs[@]}"
332 python${PYVER} -O \
333 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
334 "${myopts[@]}" "${mydirs[@]}"
335 fi
336
337 if ((${#myfiles[@]})); then
338 python_mod_compile "${myfiles[@]}"
339 fi
340
341 eend $?
342}
343
344# @FUNCTION: python_mod_cleanup
345# @USAGE: [ dir ]
346# @DESCRIPTION:
347# Run with optional arguments, where arguments are directories of
348# python modules. if none given, it will look in /usr/lib/python[0-9].[0-9]
151# 349#
152# name: python_mod_optimize 350# It will recursively scan all compiled python modules in the directories
153# desc: if no arguments supplied, it will recompile all modules under 351# and determine if they are orphaned (eg. their corresponding .py is missing.)
154# sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..) 352# if they are, then it will remove their corresponding .pyc and .pyo
155# no recursively
156# 353#
157# if supplied with arguments, it will recompile all modules recursively 354# This function should only be run in pkg_postrm()
158# in the supplied directory 355python_mod_cleanup() {
159# exam: 356 local SEARCH_PATH=() myroot src_py
160# python_mod_optimize ${ROOT}usr/share/codegen 357
161# 358 # Check if phase is pkg_postrm()
162python_mod_optimize() { 359 [[ ${EBUILD_PHASE} != postrm ]] &&\
163 local myroot 360 die "${FUNCNAME} should only be run in pkg_postrm()"
361
164 # strip trailing slash 362 # strip trailing slash
165 myroot="${ROOT%/}" 363 myroot="${ROOT%/}"
166 364
167 # allow compiling for older python versions 365 if (($#)); then
168 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 366 SEARCH_PATH=("${@#/}")
169 PYVER=${PYTHON_OVERRIDE_PYVER} 367 SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}")
170 else 368 else
171 python_version 369 SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages)
172 fi 370 fi
173 371
174 # set opts
175 if [ "${PYVER}" = "2.2" ]; then
176 compileopts=""
177 else
178 compileopts="-q"
179 fi
180
181 ebegin "Byte compiling python modules for python-${PYVER} .."
182 python${PYVER} ${myroot}/usr/lib/python${PYVER}/compileall.py ${compileopts} $@
183 python${PYVER} -O ${myroot}/usr/lib/python${PYVER}/compileall.py ${compileopts} $@
184 eend $?
185}
186
187#
188# name: python_mod_cleanup
189# desc: run with optional arguments, where arguments are directories of
190# python modules. if none given, it will look in /usr/lib/python[0-9].[0-9]
191#
192# it will recursively scan all compiled python modules in the directories
193# and determine if they are orphaned (eg. their corresponding .py is missing.)
194# if they are, then it will remove their corresponding .pyc and .pyo
195#
196python_mod_cleanup() {
197 local SEARCH_PATH myroot
198
199 # strip trailing slash
200 myroot="${ROOT%/}"
201
202 if [ $# -gt 0 ]; then
203 for path in $@; do
204 SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}"
205 done
206 else
207 for path in ${myroot}/usr/lib*/python*/site-packages; do
208 SEARCH_PATH="${SEARCH_PATH} ${path}"
209 done
210 fi
211
212 for path in ${SEARCH_PATH}; do 372 for path in "${SEARCH_PATH[@]}"; do
213 einfo "Cleaning orphaned Python bytecode from ${path} .." 373 einfo "Cleaning orphaned Python bytecode from ${path} .."
214 for obj in $(find ${path} -name *.pyc); do 374 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
215 src_py="${obj%c}" 375 src_py="${REPLY%[co]}"
216 if [ ! -f "${src_py}" ]; then 376 [[ -f "${src_py}" ]] && continue
217 einfo "Purging ${src_py}[co]" 377 einfo "Purging ${src_py}[co]"
218 rm -f ${src_py}[co] 378 rm -f "${src_py}"[co]
219 fi
220 done 379 done
380
221 # attempt to remove directories that maybe empty 381 # attempt to remove directories that maybe empty
222 for dir in $(find ${path} -type d | sort -r); do 382 find "${path}" -type d | sort -r | while read -r dir; do
223 rmdir ${dir} 2>/dev/null 383 rmdir "${dir}" 2>/dev/null
224 done 384 done
225 done 385 done
226} 386}

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.55

  ViewVC Help
Powered by ViewVC 1.1.20