/[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.55
1# Copyright 1999-2003 Gentoo Technologies, Inc. 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.7 2003/10/24 07:12:42 pythonhead 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
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() { 24__python_eclass_test() {
38 unset PYTHON_DONTCOMPILE 25 __python_version_extract 2.3
26 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
27 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
28 __python_version_extract 2.3.4
29 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
30 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
31 __python_version_extract 2.3.5
32 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
33 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
34 __python_version_extract 2.4
35 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
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"
39} 40}
40 41
41python_disable_pyc 42# @FUNCTION: python_version
42 43# @DESCRIPTION:
43#
44# name: python_version
45# desc: run without arguments and it will export the version of python 44# Run without arguments and it will export the version of python
46# currently in use as $PYVER 45# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
47# 46__python_version_extract() {
47 local verstr=$1
48 export PYVER_MAJOR=${verstr:0:1}
49 export PYVER_MINOR=${verstr:2:1}
50 if [[ ${verstr:3:1} == . ]]; then
51 export PYVER_MICRO=${verstr:4}
52 fi
53 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
54}
55
48python_version() { 56python_version() {
57 [[ -n "${PYVER}" ]] && return 0
49 local tmpstr 58 local tmpstr
50 python=${python:-/usr/bin/python} 59 python=${python:-/usr/bin/python}
51 tmpstr="$(${python} -V 2>&1 )" 60 tmpstr="$(${python} -V 2>&1 )"
52 export PYVER_ALL="${tmpstr#Python }" 61 export PYVER_ALL="${tmpstr#Python }"
53 62 __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} 63}
59 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.
60# 142#
61# 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:
62# desc: run without arguments, it will create the /usr/bin/python symlinks 186# Run without arguments, it will create the /usr/bin/python symlinks
63# to the latest installed version 187# to the latest installed version
64#
65python_makesym() { 188python_makesym() {
66 alternatives_auto_makesym "/usr/bin/python" "/usr/bin/python[0-9].[0-9]" 189 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]" 190 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
68} 191}
69 192
70#
71# name: python_tkinter_exists 193# @FUNCTION: python_tkinter_exists
72# desc: run without arguments, it will return TRUE(0) if python is compiled 194# @DESCRIPTION:
73# with tkinter or FALSE(1) if python is compiled without tkinter. 195# Run without arguments, checks if python was compiled with Tkinter
74# 196# support. If not, prints an error message and dies.
75python_tkinter_exists() { 197python_tkinter_exists() {
76 if ! python -c "import Tkinter" >/dev/null 2>&1; then 198 if ! python -c "import Tkinter" >/dev/null 2>&1; then
77 eerror "You need to recompile python with Tkinter support." 199 eerror "You need to recompile python with Tkinter support."
78 eerror "That means: USE='tcltk' emerge python" 200 eerror "Try adding: 'dev-lang/python tk'"
201 eerror "in to /etc/portage/package.use"
79 echo 202 echo
80 die "missing tkinter support with installed python" 203 die "missing tkinter support with installed python"
81 fi 204 fi
82} 205}
83 206
84# 207# @FUNCTION: python_mod_exists
85# name: python_mod_exists 208# @USAGE: < module >
209# @DESCRIPTION:
86# 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
87# python module is installed and loadable. it will return 211# python module is installed and loadable. it will return
88# 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
89# not exist. 213# not exist.
90# exam: 214#
215# Example:
91# if python_mod_exists gtk; then 216# if python_mod_exists gtk; then
92# echo "gtk support enabled 217# echo "gtk support enabled"
93# fi 218# fi
94#
95python_mod_exists() { 219python_mod_exists() {
220 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
96 if ! python -c "import $1" >/dev/null 2>&1; then 221 python -c "import $1" >/dev/null 2>&1
97 return 1
98 fi
99 return 0
100} 222}
101 223
102# 224# @FUNCTION: python_mod_compile
103# name: python_mod_compile 225# @USAGE: < file > [more files ...]
226# @DESCRIPTION:
104# 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.
105# should only be run in pkg_postinst() 228# This function should only be run in pkg_postinst()
106# exam: 229#
230# Example:
107# python_mod_compile ${ROOT}usr/lib/python2.3/site-packages/pygoogle.py 231# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
108# 232#
109python_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
110 # allow compiling for older python versions 317 # allow compiling for older python versions
111 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 318 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
112 PYVER=${PYTHON_OVERRIDE_PYVER} 319 PYVER=${PYTHON_OVERRIDE_PYVER}
113 else 320 else
114 python_version 321 python_version
115 fi 322 fi
116 323
117 if [ -f "$1" ]; then 324 # set additional opts
118 python${PYVER} -c "import py_compile; py_compile.compile('${1}')" || \ 325 myopts+=(-q)
119 ewarn "Failed to compile ${1}" 326
120 python${PYVER} -O -c "import py_compile; py_compile.compile('${1}')" || \ 327 ebegin "Byte compiling python modules for python-${PYVER} .."
121 ewarn "Failed to compile ${1}" 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]
349#
350# It will recursively scan all compiled python modules in the directories
351# and determine if they are orphaned (eg. their corresponding .py is missing.)
352# if they are, then it will remove their corresponding .pyc and .pyo
353#
354# This function should only be run in pkg_postrm()
355python_mod_cleanup() {
356 local SEARCH_PATH=() myroot src_py
357
358 # Check if phase is pkg_postrm()
359 [[ ${EBUILD_PHASE} != postrm ]] &&\
360 die "${FUNCNAME} should only be run in pkg_postrm()"
361
362 # strip trailing slash
363 myroot="${ROOT%/}"
364
365 if (($#)); then
366 SEARCH_PATH=("${@#/}")
367 SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}")
122 else 368 else
123 ewarn "Unable to find ${1}" 369 SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages)
124 fi 370 fi
125}
126 371
127#
128# name: python_mod_optimize
129# desc: if no arguments supplied, it will recompile all modules under
130# sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..)
131# no recursively
132#
133# if supplied with arguments, it will recompile all modules recursively
134# in the supplied directory
135# exam:
136# python_mod_optimize ${ROOT}usr/share/codegen
137#
138python_mod_optimize() {
139 # allow compiling for older python versions
140 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
141 PYVER=${PYTHON_OVERRIDE_PYVER}
142 else
143 python_version
144 fi
145
146 # set opts
147 if [ "${PYVER}" = "2.2" ]; then
148 compileopts=""
149 else
150 compileopts="-q"
151 fi
152
153 ebegin "Byte Compiling Python modules for ${PYVER} .."
154 python${PYVER} ${ROOT}usr/lib/python${PYVER}/compileall.py ${compileopts} $@
155 python${PYVER} -O ${ROOT}usr/lib/python${PYVER}/compileall.py ${compileopts} $@
156 eend $?
157}
158
159#
160# name: python_mod_cleanup
161# desc: 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]
163#
164# it will recursively scan all compiled python modules in the directories
165# and determine if they are orphaned (eg. their corresponding .py is missing.)
166# if they are, then it will remove their corresponding .pyc and .pyo
167#
168python_mod_cleanup() {
169 local SEARCH_PATH
170
171 if [ $# -gt 0 ]; then
172 for path in $@; do
173 SEARCH_PATH="${SEARCH_PATH} ${ROOT}${path#/}"
174 done
175 else
176 for path in ${ROOT}usr/lib/python*/site-packages; do
177 SEARCH_PATH="${SEARCH_PATH} ${path}"
178 done
179 fi
180
181 for path in ${SEARCH_PATH}; do 372 for path in "${SEARCH_PATH[@]}"; do
182 einfo "Searching ${path} .." 373 einfo "Cleaning orphaned Python bytecode from ${path} .."
183 for obj in $(find ${path} -name *.pyc); do 374 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
184 src_py="$(echo $obj | sed 's:c$::')" 375 src_py="${REPLY%[co]}"
185 if [ ! -f "${src_py}" ]; then 376 [[ -f "${src_py}" ]] && continue
186 einfo "Purging ${src_py}[co]" 377 einfo "Purging ${src_py}[co]"
187 rm -f ${src_py}[co] 378 rm -f "${src_py}"[co]
188 fi 379 done
380
381 # attempt to remove directories that maybe empty
382 find "${path}" -type d | sort -r | while read -r dir; do
383 rmdir "${dir}" 2>/dev/null
189 done 384 done
190 done 385 done
191} 386}
192
193

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

  ViewVC Help
Powered by ViewVC 1.1.20