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

Diff of /eclass/python.eclass

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

Revision 1.39 Revision 1.52
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.39 2008/05/29 21:19:19 hawking Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.52 2008/10/27 00:17:28 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 python_version
72 if [[ ${PYVER/./,} -ge 2,6 ]]; then
73 export PYTHONDONTWRITEBYTECODE=1
74 else
75 export PYTHON_DONTCOMPILE=1
76 fi
77}
78
79# @FUNCTION: python_enable_pyc
80# @DESCRIPTION:
81# Tells python to automatically recompile modules to .pyc/.pyo if the
82# timestamps/version stamps change
83python_enable_pyc() {
84 python_version
85 if [[ ${PYVER/./,} -ge 2,6 ]]; then
86 unset PYTHONDONTWRITEBYTECODE
87 else
88 unset PYTHON_DONTCOMPILE
89 fi
90}
91
92python_disable_pyc
93
94# @FUNCTION: python_need_rebuild
95# @DESCRIPTION: Run without arguments, specifies that the package should be
96# rebuilt after a python upgrade.
97python_need_rebuild() {
98 python_version
99 export PYTHON_NEED_REBUILD=${PYVER}
100}
101
102# @FUNCTION: python_get_libdir
103# @DESCRIPTION:
104# Run without arguments, returns the python library dir
105python_get_libdir() {
106 python_version
107 echo "/usr/$(get_libdir)/python${PYVER}"
108}
109
110# @FUNCTION: python_get_sitedir
111# @DESCRIPTION:
112# Run without arguments, returns the python site-packages dir
113python_get_sitedir() {
114 echo "$(python_get_libdir)/site-packages"
81} 115}
82 116
83# @FUNCTION: python_makesym 117# @FUNCTION: python_makesym
84# @DESCRIPTION: 118# @DESCRIPTION:
85# Run without arguments, it will create the /usr/bin/python symlinks 119# Run without arguments, it will create the /usr/bin/python symlinks
114# Example: 148# Example:
115# if python_mod_exists gtk; then 149# if python_mod_exists gtk; then
116# echo "gtk support enabled" 150# echo "gtk support enabled"
117# fi 151# fi
118python_mod_exists() { 152python_mod_exists() {
119 [ -z "$1" ] && die "${FUNCTION} requires an argument!" 153 [[ "$1" ]] && die "${FUNCNAME} requires an argument!"
120 if ! python -c "import $1" >/dev/null 2>&1; then 154 python -c "import $1" >/dev/null 2>&1
121 return 1
122 fi
123 return 0
124} 155}
125 156
126# @FUNCTION: python_mod_compile 157# @FUNCTION: python_mod_compile
127# @USAGE: < file > [more files ...] 158# @USAGE: < file > [more files ...]
128# @DESCRIPTION: 159# @DESCRIPTION:
131# 162#
132# Example: 163# Example:
133# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 164# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
134# 165#
135python_mod_compile() { 166python_mod_compile() {
136 local f myroot 167 local f myroot myfiles=()
137 168
138 # Check if phase is pkg_postinst() 169 # Check if phase is pkg_postinst()
139 [[ ${EBUILD_PHASE} != postinst ]] &&\ 170 [[ ${EBUILD_PHASE} != postinst ]] &&\
140 die "${FUNCNAME} should only be run in pkg_postinst()" 171 die "${FUNCNAME} should only be run in pkg_postinst()"
141 172
142 # allow compiling for older python versions 173 # allow compiling for older python versions
143 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 174 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
144 PYVER=${PYTHON_OVERRIDE_PYVER} 175 PYVER=${PYTHON_OVERRIDE_PYVER}
145 else 176 else
146 python_version 177 python_version
147 fi 178 fi
148 179
149 # strip trailing slash 180 # strip trailing slash
150 myroot="${ROOT%/}" 181 myroot="${ROOT%/}"
151 182
152 # respect ROOT 183 # respect ROOT
153 for f in $@; do 184 for f in "$@"; do
154 [ -f "${myroot}/${f}" ] && myfiles="${myfiles} ${myroot}/${f}" 185 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
155 done 186 done
156 187
157 if [ -n "${myfiles}" ]; then 188 if ((${#myfiles[@]})); then
158 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py ${myfiles} 189 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} 190 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
160 else 191 else
161 ewarn "No files to compile!" 192 ewarn "No files to compile!"
162 fi 193 fi
163} 194}
164 195
176# Options passed to this function are passed to compileall.py 207# Options passed to this function are passed to compileall.py
177# 208#
178# Example: 209# Example:
179# python_mod_optimize /usr/share/codegen 210# python_mod_optimize /usr/share/codegen
180python_mod_optimize() { 211python_mod_optimize() {
181 local mydirs myfiles myroot myopts path 212 local myroot mydirs=() myfiles=() myopts=()
182 213
183 # Check if phase is pkg_postinst() 214 # Check if phase is pkg_postinst()
184 [[ ${EBUILD_PHASE} != postinst ]] &&\ 215 [[ ${EBUILD_PHASE} != postinst ]] &&\
185 die "${FUNCNAME} should only be run in pkg_postinst()" 216 die "${FUNCNAME} should only be run in pkg_postinst()"
186 217
187 # strip trailing slash 218 # strip trailing slash
188 myroot="${ROOT%/}" 219 myroot="${ROOT%/}"
189 220
190 # respect ROOT and options passed to compileall.py 221 # respect ROOT and options passed to compileall.py
191 while [ $# -gt 0 ]; do 222 while (($#)); do
192 case $1 in 223 case $1 in
193 -l|-f|-q) 224 -l|-f|-q)
194 myopts="${myopts} $1" 225 myopts+=("$1")
195 ;; 226 ;;
196 -d|-x) 227 -d|-x)
197 # -x takes regexp as argument so quoting is necessary. 228 myopts+=("$1" "$2")
198 myopts="${myopts} $1 \"$2\""
199 shift 229 shift
200 ;; 230 ;;
201 -*) 231 -*)
202 ewarn "${FUNCNAME}: Ignoring compile option $1" 232 ewarn "${FUNCNAME}: Ignoring compile option $1"
203 ;; 233 ;;
204 *) 234 *)
205 for path in $@; do 235 if [[ -d "${myroot}"/$1 ]]; then
206 [ ! -e "${myroot}/${path}" ] && ewarn "${myroot}/${path} doesn't exist!" 236 mydirs+=("${myroot}/$1")
207 [ -d "${myroot}/${path#/}" ] && mydirs="${mydirs} ${myroot}/${path#/}" 237 elif [[ -f "${myroot}"/$1 ]]; then
208 # Files are passed to python_mod_compile which is ROOT-aware 238 # Files are passed to python_mod_compile which is ROOT-aware
209 [ -f "${myroot}/${path}" ] && myfiles="${myfiles} ${path}" 239 myfiles+=("$1")
210 done 240 elif [[ -e "${myroot}/$1" ]]; then
241 ewarn "${myroot}/$1 is not a file or directory!"
242 else
243 ewarn "${myroot}/$1 doesn't exist!"
244 fi
211 ;; 245 ;;
212 esac 246 esac
213 shift 247 shift
214 done 248 done
215 249
218 PYVER=${PYTHON_OVERRIDE_PYVER} 252 PYVER=${PYTHON_OVERRIDE_PYVER}
219 else 253 else
220 python_version 254 python_version
221 fi 255 fi
222 256
223 # set opts 257 # set additional opts
224 if [ "${PYVER}" = "2.2" ]; then 258 myopts+=(-q)
225 compileopts=""
226 else
227 compileopts="-q"
228 fi
229 259
230 ebegin "Byte compiling python modules for python-${PYVER} .." 260 ebegin "Byte compiling python modules for python-${PYVER} .."
231 if [ -n "${mydirs}" ]; then 261 if ((${#mydirs[@]})); then
232 python${PYVER} \ 262 python${PYVER} \
233 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ 263 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
234 ${compileopts} ${myopts} ${mydirs} 264 "${myopts[@]}" "${mydirs[@]}"
235 python${PYVER} -O \ 265 python${PYVER} -O \
236 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ 266 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
237 ${compileopts} ${myopts} ${mydirs} 267 "${myopts[@]}" "${mydirs[@]}"
238 fi 268 fi
239 269
240 if [ -n "${myfiles}" ]; then 270 if ((${#myfiles[@]})); then
241 python_mod_compile ${myfiles} 271 python_mod_compile "${myfiles[@]}"
242 fi 272 fi
243 273
244 eend $? 274 eend $?
245} 275}
246 276
254# and determine if they are orphaned (eg. their corresponding .py is missing.) 284# and determine if they are orphaned (eg. their corresponding .py is missing.)
255# if they are, then it will remove their corresponding .pyc and .pyo 285# if they are, then it will remove their corresponding .pyc and .pyo
256# 286#
257# This function should only be run in pkg_postrm() 287# This function should only be run in pkg_postrm()
258python_mod_cleanup() { 288python_mod_cleanup() {
259 local SEARCH_PATH myroot 289 local SEARCH_PATH=() myroot src_py
260 290
261 # Check if phase is pkg_postrm() 291 # Check if phase is pkg_postrm()
262 [[ ${EBUILD_PHASE} != postrm ]] &&\ 292 [[ ${EBUILD_PHASE} != postrm ]] &&\
263 die "${FUNCNAME} should only be run in pkg_postrm()" 293 die "${FUNCNAME} should only be run in pkg_postrm()"
264 294
265 # strip trailing slash 295 # strip trailing slash
266 myroot="${ROOT%/}" 296 myroot="${ROOT%/}"
267 297
268 if [ $# -gt 0 ]; then 298 if (($#)); then
269 for path in $@; do 299 SEARCH_PATH=("${@#/}")
270 SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}" 300 SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}")
271 done
272 else 301 else
273 for path in ${myroot}/usr/lib*/python*/site-packages; do 302 SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages)
274 SEARCH_PATH="${SEARCH_PATH} ${path}"
275 done
276 fi 303 fi
277 304
278 for path in ${SEARCH_PATH}; do 305 for path in "${SEARCH_PATH[@]}"; do
279 einfo "Cleaning orphaned Python bytecode from ${path} .." 306 einfo "Cleaning orphaned Python bytecode from ${path} .."
280 for obj in $(find ${path} -name '*.py[co]'); do 307 while read -rd ''; do
281 src_py="${obj%[co]}" 308 src_py="${REPLY%[co]}"
282 if [ ! -f "${src_py}" ]; then 309 [[ -f "${src_py}" ]] && continue
283 einfo "Purging ${src_py}[co]" 310 einfo "Purging ${src_py}[co]"
284 rm -f ${src_py}[co] 311 rm -f "${src_py}"[co]
285 fi 312 done < <(find "${path}" -name '*.py[co]' -print0)
286 done 313
287 # attempt to remove directories that maybe empty 314 # attempt to remove directories that maybe empty
288 for dir in $(find ${path} -type d | sort -r); do 315 while read -r dir; do
289 rmdir ${dir} 2>/dev/null 316 rmdir "${dir}" 2>/dev/null
290 done 317 done < <(find "${path}" -type d | sort -r)
291 done 318 done
292} 319}

Legend:
Removed from v.1.39  
changed lines
  Added in v.1.52

  ViewVC Help
Powered by ViewVC 1.1.20