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

Diff of /eclass/python.eclass

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

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

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.54

  ViewVC Help
Powered by ViewVC 1.1.20