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

Diff of /eclass/python.eclass

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

Revision 1.41 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.41 2008/05/30 09:58:28 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#
34 __python_version_extract 2.5b3 34 __python_version_extract 2.5b3
35 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 35 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
36 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" 36 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
37} 37}
38 38
39# @FUNCTION: python_disable_pyc
40# @DESCRIPTION:
41# Tells python not to automatically recompile modules to .pyc/.pyo
42# even if the timestamps/version stamps don't match. This is done
43# to protect sandbox.
44#
45# note: supported by >=dev-lang/python-2.2.3-r3 only.
46#
47python_disable_pyc() {
48 export PYTHON_DONTCOMPILE=1
49}
50
51# @FUNCTION: python_enable_pyc
52# @DESCRIPTION:
53# Tells python to automatically recompile modules to .pyc/.pyo if the
54# timestamps/version stamps change
55python_enable_pyc() {
56 unset PYTHON_DONTCOMPILE
57}
58
59python_disable_pyc
60
61# @FUNCTION: python_version 39# @FUNCTION: python_version
62# @DESCRIPTION: 40# @DESCRIPTION:
63# Run without arguments and it will export the version of python 41# Run without arguments and it will export the version of python
64# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 42# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
65__python_version_extract() { 43__python_version_extract() {
66 verstr=$1 44 local verstr=$1
67 export PYVER_MAJOR=${verstr:0:1} 45 export PYVER_MAJOR=${verstr:0:1}
68 export PYVER_MINOR=${verstr:2:1} 46 export PYVER_MINOR=${verstr:2:1}
69 if [ "${verstr:3}x" = ".x" ]; then 47 if [[ ${verstr:3:1} == . ]]; then
70 export PYVER_MICRO=${verstr:4} 48 export PYVER_MICRO=${verstr:4}
71 fi 49 fi
72 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}" 50 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
73} 51}
74 52
75python_version() { 53python_version() {
54 [[ -n "${PYVER}" ]] && return 0
76 local tmpstr 55 local tmpstr
77 python=${python:-/usr/bin/python} 56 python=${python:-/usr/bin/python}
78 tmpstr="$(${python} -V 2>&1 )" 57 tmpstr="$(${python} -V 2>&1 )"
79 export PYVER_ALL="${tmpstr#Python }" 58 export PYVER_ALL="${tmpstr#Python }"
80 __python_version_extract $PYVER_ALL 59 __python_version_extract $PYVER_ALL
60}
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.
67#
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"
81} 107}
82 108
83# @FUNCTION: python_makesym 109# @FUNCTION: python_makesym
84# @DESCRIPTION: 110# @DESCRIPTION:
85# Run without arguments, it will create the /usr/bin/python symlinks 111# Run without arguments, it will create the /usr/bin/python symlinks
114# Example: 140# Example:
115# if python_mod_exists gtk; then 141# if python_mod_exists gtk; then
116# echo "gtk support enabled" 142# echo "gtk support enabled"
117# fi 143# fi
118python_mod_exists() { 144python_mod_exists() {
119 [ -z "$1" ] && die "${FUNCTION} requires an argument!" 145 [[ "$1" ]] && die "${FUNCNAME} requires an argument!"
120 if ! python -c "import $1" >/dev/null 2>&1; then 146 python -c "import $1" >/dev/null 2>&1
121 return 1
122 fi
123 return 0
124} 147}
125 148
126# @FUNCTION: python_mod_compile 149# @FUNCTION: python_mod_compile
127# @USAGE: < file > [more files ...] 150# @USAGE: < file > [more files ...]
128# @DESCRIPTION: 151# @DESCRIPTION:
131# 154#
132# Example: 155# Example:
133# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 156# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
134# 157#
135python_mod_compile() { 158python_mod_compile() {
136 local f myroot 159 local f myroot myfiles=()
137 160
138 # Check if phase is pkg_postinst() 161 # Check if phase is pkg_postinst()
139 [[ ${EBUILD_PHASE} != postinst ]] &&\ 162 [[ ${EBUILD_PHASE} != postinst ]] &&\
140 die "${FUNCNAME} should only be run in pkg_postinst()" 163 die "${FUNCNAME} should only be run in pkg_postinst()"
141 164
142 # allow compiling for older python versions 165 # allow compiling for older python versions
143 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 166 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
144 PYVER=${PYTHON_OVERRIDE_PYVER} 167 PYVER=${PYTHON_OVERRIDE_PYVER}
145 else 168 else
146 python_version 169 python_version
147 fi 170 fi
148 171
149 # strip trailing slash 172 # strip trailing slash
150 myroot="${ROOT%/}" 173 myroot="${ROOT%/}"
151 174
152 # respect ROOT 175 # respect ROOT
153 for f in $@; do 176 for f in "$@"; do
154 [ -f "${myroot}/${f}" ] && myfiles="${myfiles} ${myroot}/${f}" 177 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
155 done 178 done
156 179
157 if [ -n "${myfiles}" ]; then 180 if ((${#myfiles[@]})); then
158 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[@]}"
159 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[@]}"
160 else 183 else
161 ewarn "No files to compile!" 184 ewarn "No files to compile!"
162 fi 185 fi
163} 186}
164 187
176# Options passed to this function are passed to compileall.py 199# Options passed to this function are passed to compileall.py
177# 200#
178# Example: 201# Example:
179# python_mod_optimize /usr/share/codegen 202# python_mod_optimize /usr/share/codegen
180python_mod_optimize() { 203python_mod_optimize() {
181 local mydirs myfiles myroot myopts 204 local myroot mydirs=() myfiles=() myopts=()
182 205
183 # Check if phase is pkg_postinst() 206 # Check if phase is pkg_postinst()
184 [[ ${EBUILD_PHASE} != postinst ]] &&\ 207 [[ ${EBUILD_PHASE} != postinst ]] &&\
185 die "${FUNCNAME} should only be run in pkg_postinst()" 208 die "${FUNCNAME} should only be run in pkg_postinst()"
186 209
187 # strip trailing slash 210 # strip trailing slash
188 myroot="${ROOT%/}" 211 myroot="${ROOT%/}"
189 212
190 # respect ROOT and options passed to compileall.py 213 # respect ROOT and options passed to compileall.py
191 while [ $# -gt 0 ]; do 214 while (($#)); do
192 case $1 in 215 case $1 in
193 -l|-f|-q) 216 -l|-f|-q)
194 myopts="${myopts} $1" 217 myopts+=("$1")
195 ;; 218 ;;
196 -d|-x) 219 -d|-x)
197 myopts="${myopts} $1 $2" 220 myopts+=("$1" "$2")
198 shift 221 shift
199 ;; 222 ;;
200 -*) 223 -*)
201 ewarn "${FUNCNAME}: Ignoring compile option $1" 224 ewarn "${FUNCNAME}: Ignoring compile option $1"
202 ;; 225 ;;
203 *) 226 *)
204 [ ! -e "${myroot}/${1}" ] && ewarn "${myroot}/${1} doesn't exist!" 227 if [[ -d "${myroot}"/$1 ]]; then
205 [ -d "${myroot}/${1#/}" ] && mydirs="${mydirs} ${myroot}/${1#/}" 228 mydirs+=("${myroot}/$1")
229 elif [[ -f "${myroot}"/$1 ]]; then
206 # Files are passed to python_mod_compile which is ROOT-aware 230 # Files are passed to python_mod_compile which is ROOT-aware
207 [ -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
208 ;; 237 ;;
209 esac 238 esac
210 shift 239 shift
211 done 240 done
212 241
216 else 245 else
217 python_version 246 python_version
218 fi 247 fi
219 248
220 # set additional opts 249 # set additional opts
221 myopts="${myopts} -q" 250 myopts+=(-q)
222 251
223 ebegin "Byte compiling python modules for python-${PYVER} .." 252 ebegin "Byte compiling python modules for python-${PYVER} .."
224 if [ -n "${mydirs}" ]; then 253 if ((${#mydirs[@]})); then
225 python${PYVER} \ 254 python${PYVER} \
226 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ 255 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
227 ${myopts} ${mydirs} 256 "${myopts[@]}" "${mydirs[@]}"
228 python${PYVER} -O \ 257 python${PYVER} -O \
229 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ 258 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
230 ${myopts} ${mydirs} 259 "${myopts[@]}" "${mydirs[@]}"
231 fi 260 fi
232 261
233 if [ -n "${myfiles}" ]; then 262 if ((${#myfiles[@]})); then
234 python_mod_compile ${myfiles} 263 python_mod_compile "${myfiles[@]}"
235 fi 264 fi
236 265
237 eend $? 266 eend $?
238} 267}
239 268
247# 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.)
248# 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
249# 278#
250# This function should only be run in pkg_postrm() 279# This function should only be run in pkg_postrm()
251python_mod_cleanup() { 280python_mod_cleanup() {
252 local SEARCH_PATH myroot 281 local SEARCH_PATH=() myroot src_py
253 282
254 # Check if phase is pkg_postrm() 283 # Check if phase is pkg_postrm()
255 [[ ${EBUILD_PHASE} != postrm ]] &&\ 284 [[ ${EBUILD_PHASE} != postrm ]] &&\
256 die "${FUNCNAME} should only be run in pkg_postrm()" 285 die "${FUNCNAME} should only be run in pkg_postrm()"
257 286
258 # strip trailing slash 287 # strip trailing slash
259 myroot="${ROOT%/}" 288 myroot="${ROOT%/}"
260 289
261 if [ $# -gt 0 ]; then 290 if (($#)); then
262 for path in $@; do 291 SEARCH_PATH=("${@#/}")
263 SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}" 292 SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}")
264 done
265 else 293 else
266 for path in ${myroot}/usr/lib*/python*/site-packages; do 294 SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages)
267 SEARCH_PATH="${SEARCH_PATH} ${path}"
268 done
269 fi 295 fi
270 296
271 for path in ${SEARCH_PATH}; do 297 for path in "${SEARCH_PATH[@]}"; do
272 einfo "Cleaning orphaned Python bytecode from ${path} .." 298 einfo "Cleaning orphaned Python bytecode from ${path} .."
273 for obj in $(find ${path} -name '*.py[co]'); do 299 while read -rd ''; do
274 src_py="${obj%[co]}" 300 src_py="${REPLY%[co]}"
275 if [ ! -f "${src_py}" ]; then 301 [[ -f "${src_py}" ]] && continue
276 einfo "Purging ${src_py}[co]" 302 einfo "Purging ${src_py}[co]"
277 rm -f ${src_py}[co] 303 rm -f "${src_py}"[co]
278 fi 304 done < <(find "${path}" -name '*.py[co]' -print0)
279 done 305
280 # attempt to remove directories that maybe empty 306 # attempt to remove directories that maybe empty
281 for dir in $(find ${path} -type d | sort -r); do 307 while read -r dir; do
282 rmdir ${dir} 2>/dev/null 308 rmdir "${dir}" 2>/dev/null
283 done 309 done < <(find "${path}" -type d | sort -r)
284 done 310 done
285} 311}

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

  ViewVC Help
Powered by ViewVC 1.1.20