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

Diff of /eclass/python.eclass

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

Revision 1.46 Revision 1.53
1# Copyright 1999-2008 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.46 2008/10/26 17:11:51 hawking Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.53 2008/10/27 12:23:50 hawking 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#
42# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 42# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
43__python_version_extract() { 43__python_version_extract() {
44 local verstr=$1 44 local verstr=$1
45 export PYVER_MAJOR=${verstr:0:1} 45 export PYVER_MAJOR=${verstr:0:1}
46 export PYVER_MINOR=${verstr:2:1} 46 export PYVER_MINOR=${verstr:2:1}
47 if [ "${verstr:3}x" = ".x" ]; then 47 if [[ ${verstr:3:1} == . ]]; then
48 export PYVER_MICRO=${verstr:4} 48 export PYVER_MICRO=${verstr:4}
49 fi 49 fi
50 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}" 50 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
51} 51}
52 52
66# to protect sandbox. 66# to protect sandbox.
67# 67#
68# note: supported by >=dev-lang/python-2.2.3-r3 only. 68# note: supported by >=dev-lang/python-2.2.3-r3 only.
69# 69#
70python_disable_pyc() { 70python_disable_pyc() {
71 python_version
72 if [[ ${PYVER/./,} -ge 2,6 ]]; then
73 export PYTHONDONTWRITEBYTECODE=1 71 export PYTHONDONTWRITEBYTECODE=1 # For 2.6 and above
74 else 72 export PYTHON_DONTCOMPILE=1 # For 2.5 and below
75 export PYTHON_DONTCOMPILE=1
76 fi
77} 73}
78 74
79# @FUNCTION: python_enable_pyc 75# @FUNCTION: python_enable_pyc
80# @DESCRIPTION: 76# @DESCRIPTION:
81# Tells python to automatically recompile modules to .pyc/.pyo if the 77# Tells python to automatically recompile modules to .pyc/.pyo if the
82# timestamps/version stamps change 78# timestamps/version stamps change
83python_enable_pyc() { 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() {
84 python_version 90 python_version
85 if [[ ${PYVER/./,} -ge 2,6 ]]; then 91 export PYTHON_NEED_REBUILD=${PYVER}
86 unset PYTHONDONTWRITEBYTECODE
87 else
88 unset PYTHON_DONTCOMPILE
89 fi
90} 92}
91
92python_disable_pyc
93 93
94# @FUNCTION: python_get_libdir 94# @FUNCTION: python_get_libdir
95# @DESCRIPTION: 95# @DESCRIPTION:
96# Run without arguments, returns the python library dir 96# Run without arguments, returns the python library dir
97python_get_libdir() { 97python_get_libdir() {
140# Example: 140# Example:
141# if python_mod_exists gtk; then 141# if python_mod_exists gtk; then
142# echo "gtk support enabled" 142# echo "gtk support enabled"
143# fi 143# fi
144python_mod_exists() { 144python_mod_exists() {
145 [ -z "$1" ] && die "${FUNCTION} requires an argument!" 145 [[ "$1" ]] && die "${FUNCNAME} requires an argument!"
146 if ! python -c "import $1" >/dev/null 2>&1; then 146 python -c "import $1" >/dev/null 2>&1
147 return 1
148 fi
149 return 0
150} 147}
151 148
152# @FUNCTION: python_mod_compile 149# @FUNCTION: python_mod_compile
153# @USAGE: < file > [more files ...] 150# @USAGE: < file > [more files ...]
154# @DESCRIPTION: 151# @DESCRIPTION:
157# 154#
158# Example: 155# Example:
159# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 156# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
160# 157#
161python_mod_compile() { 158python_mod_compile() {
162 local f myroot 159 local f myroot myfiles=()
163 160
164 # Check if phase is pkg_postinst() 161 # Check if phase is pkg_postinst()
165 [[ ${EBUILD_PHASE} != postinst ]] &&\ 162 [[ ${EBUILD_PHASE} != postinst ]] &&\
166 die "${FUNCNAME} should only be run in pkg_postinst()" 163 die "${FUNCNAME} should only be run in pkg_postinst()"
167 164
168 # allow compiling for older python versions 165 # allow compiling for older python versions
169 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 166 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
170 PYVER=${PYTHON_OVERRIDE_PYVER} 167 PYVER=${PYTHON_OVERRIDE_PYVER}
171 else 168 else
172 python_version 169 python_version
173 fi 170 fi
174 171
175 # strip trailing slash 172 # strip trailing slash
176 myroot="${ROOT%/}" 173 myroot="${ROOT%/}"
177 174
178 # respect ROOT 175 # respect ROOT
179 for f in $@; do 176 for f in "$@"; do
180 [ -f "${myroot}/${f}" ] && myfiles="${myfiles} ${myroot}/${f}" 177 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
181 done 178 done
182 179
183 if [ -n "${myfiles}" ]; then 180 if ((${#myfiles[@]})); then
184 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py ${myfiles} 181 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
185 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py ${myfiles} 182 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
186 else 183 else
187 ewarn "No files to compile!" 184 ewarn "No files to compile!"
188 fi 185 fi
189} 186}
190 187
202# Options passed to this function are passed to compileall.py 199# Options passed to this function are passed to compileall.py
203# 200#
204# Example: 201# Example:
205# python_mod_optimize /usr/share/codegen 202# python_mod_optimize /usr/share/codegen
206python_mod_optimize() { 203python_mod_optimize() {
207 local mydirs myfiles myroot myopts 204 local myroot mydirs=() myfiles=() myopts=()
208 205
209 # Check if phase is pkg_postinst() 206 # Check if phase is pkg_postinst()
210 [[ ${EBUILD_PHASE} != postinst ]] &&\ 207 [[ ${EBUILD_PHASE} != postinst ]] &&\
211 die "${FUNCNAME} should only be run in pkg_postinst()" 208 die "${FUNCNAME} should only be run in pkg_postinst()"
212 209
213 # strip trailing slash 210 # strip trailing slash
214 myroot="${ROOT%/}" 211 myroot="${ROOT%/}"
215 212
216 # respect ROOT and options passed to compileall.py 213 # respect ROOT and options passed to compileall.py
217 while [ $# -gt 0 ]; do 214 while (($#)); do
218 case $1 in 215 case $1 in
219 -l|-f|-q) 216 -l|-f|-q)
220 myopts="${myopts} $1" 217 myopts+=("$1")
221 ;; 218 ;;
222 -d|-x) 219 -d|-x)
223 myopts="${myopts} $1 $2" 220 myopts+=("$1" "$2")
224 shift 221 shift
225 ;; 222 ;;
226 -*) 223 -*)
227 ewarn "${FUNCNAME}: Ignoring compile option $1" 224 ewarn "${FUNCNAME}: Ignoring compile option $1"
228 ;; 225 ;;
229 *) 226 *)
230 [ ! -e "${myroot}/${1}" ] && ewarn "${myroot}/${1} doesn't exist!" 227 if [[ -d "${myroot}"/$1 ]]; then
231 [ -d "${myroot}/${1#/}" ] && mydirs="${mydirs} ${myroot}/${1#/}" 228 mydirs+=("${myroot}/$1")
229 elif [[ -f "${myroot}"/$1 ]]; then
232 # Files are passed to python_mod_compile which is ROOT-aware 230 # Files are passed to python_mod_compile which is ROOT-aware
233 [ -f "${myroot}/${1}" ] && myfiles="${myfiles} ${1}" 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
234 ;; 237 ;;
235 esac 238 esac
236 shift 239 shift
237 done 240 done
238 241
242 else 245 else
243 python_version 246 python_version
244 fi 247 fi
245 248
246 # set additional opts 249 # set additional opts
247 myopts="${myopts} -q" 250 myopts+=(-q)
248 251
249 ebegin "Byte compiling python modules for python-${PYVER} .." 252 ebegin "Byte compiling python modules for python-${PYVER} .."
250 if [ -n "${mydirs}" ]; then 253 if ((${#mydirs[@]})); then
251 python${PYVER} \ 254 python${PYVER} \
252 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ 255 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
253 ${myopts} ${mydirs} 256 "${myopts[@]}" "${mydirs[@]}"
254 python${PYVER} -O \ 257 python${PYVER} -O \
255 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ 258 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
256 ${myopts} ${mydirs} 259 "${myopts[@]}" "${mydirs[@]}"
257 fi 260 fi
258 261
259 if [ -n "${myfiles}" ]; then 262 if ((${#myfiles[@]})); then
260 python_mod_compile ${myfiles} 263 python_mod_compile "${myfiles[@]}"
261 fi 264 fi
262 265
263 eend $? 266 eend $?
264} 267}
265 268
273# and determine if they are orphaned (eg. their corresponding .py is missing.) 276# and determine if they are orphaned (eg. their corresponding .py is missing.)
274# if they are, then it will remove their corresponding .pyc and .pyo 277# if they are, then it will remove their corresponding .pyc and .pyo
275# 278#
276# This function should only be run in pkg_postrm() 279# This function should only be run in pkg_postrm()
277python_mod_cleanup() { 280python_mod_cleanup() {
278 local SEARCH_PATH myroot 281 local SEARCH_PATH=() myroot src_py
279 282
280 # Check if phase is pkg_postrm() 283 # Check if phase is pkg_postrm()
281 [[ ${EBUILD_PHASE} != postrm ]] &&\ 284 [[ ${EBUILD_PHASE} != postrm ]] &&\
282 die "${FUNCNAME} should only be run in pkg_postrm()" 285 die "${FUNCNAME} should only be run in pkg_postrm()"
283 286
284 # strip trailing slash 287 # strip trailing slash
285 myroot="${ROOT%/}" 288 myroot="${ROOT%/}"
286 289
287 if [ $# -gt 0 ]; then 290 if (($#)); then
288 for path in $@; do 291 SEARCH_PATH=("${@#/}")
289 SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}" 292 SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}")
290 done
291 else 293 else
292 for path in ${myroot}/usr/lib*/python*/site-packages; do 294 SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages)
293 SEARCH_PATH="${SEARCH_PATH} ${path}"
294 done
295 fi 295 fi
296 296
297 for path in ${SEARCH_PATH}; do 297 for path in "${SEARCH_PATH[@]}"; do
298 einfo "Cleaning orphaned Python bytecode from ${path} .." 298 einfo "Cleaning orphaned Python bytecode from ${path} .."
299 for obj in $(find ${path} -name '*.py[co]'); do 299 while read -rd ''; do
300 src_py="${obj%[co]}" 300 src_py="${REPLY%[co]}"
301 if [ ! -f "${src_py}" ]; then 301 [[ -f "${src_py}" ]] && continue
302 einfo "Purging ${src_py}[co]" 302 einfo "Purging ${src_py}[co]"
303 rm -f ${src_py}[co] 303 rm -f "${src_py}"[co]
304 fi 304 done < <(find "${path}" -name '*.py[co]' -print0)
305 done 305
306 # attempt to remove directories that maybe empty 306 # attempt to remove directories that maybe empty
307 for dir in $(find ${path} -type d | sort -r); do 307 while read -r dir; do
308 rmdir ${dir} 2>/dev/null 308 rmdir "${dir}" 2>/dev/null
309 done 309 done < <(find "${path}" -type d | sort -r)
310 done 310 done
311} 311}

Legend:
Removed from v.1.46  
changed lines
  Added in v.1.53

  ViewVC Help
Powered by ViewVC 1.1.20