| 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.48 2008/10/26 17:34:44 hawking Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.54 2008/10/30 05:21:46 zmedico 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 | # |
| … | |
… | |
| 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 | # |
| 70 | python_disable_pyc() { |
70 | python_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 |
| 83 | python_enable_pyc() { |
79 | python_enable_pyc() { |
|
|
80 | unset PYTHONDONTWRITEBYTECODE |
|
|
81 | unset PYTHON_DONTCOMPILE |
|
|
82 | } |
|
|
83 | |
|
|
84 | python_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. |
|
|
89 | python_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 | |
|
|
| 92 | python_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 |
| 97 | python_get_libdir() { |
97 | python_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 |
| 144 | python_mod_exists() { |
144 | python_mod_exists() { |
| 145 | [[ "$1" ]] && die "${FUNCNAME} requires an argument!" |
145 | [[ "$1" ]] || die "${FUNCNAME} requires an argument!" |
| 146 | python -c "import $1" >/dev/null 2>&1 |
146 | python -c "import $1" >/dev/null 2>&1 |
| 147 | } |
147 | } |
| 148 | |
148 | |
| 149 | # @FUNCTION: python_mod_compile |
149 | # @FUNCTION: python_mod_compile |
| 150 | # @USAGE: < file > [more files ...] |
150 | # @USAGE: < file > [more files ...] |
| … | |
… | |
| 171 | |
171 | |
| 172 | # strip trailing slash |
172 | # strip trailing slash |
| 173 | myroot="${ROOT%/}" |
173 | myroot="${ROOT%/}" |
| 174 | |
174 | |
| 175 | # respect ROOT |
175 | # respect ROOT |
| 176 | for f in $@; do |
176 | for f in "$@"; do |
| 177 | [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") |
177 | [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") |
| 178 | done |
178 | done |
| 179 | |
179 | |
| 180 | if ((${#myfiles[@]})); then |
180 | if ((${#myfiles[@]})); then |
| 181 | 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[@]}" |
| … | |
… | |
| 199 | # Options passed to this function are passed to compileall.py |
199 | # Options passed to this function are passed to compileall.py |
| 200 | # |
200 | # |
| 201 | # Example: |
201 | # Example: |
| 202 | # python_mod_optimize /usr/share/codegen |
202 | # python_mod_optimize /usr/share/codegen |
| 203 | python_mod_optimize() { |
203 | python_mod_optimize() { |
| 204 | local mydirs myfiles myroot myopts |
204 | local myroot mydirs=() myfiles=() myopts=() |
| 205 | |
205 | |
| 206 | # Check if phase is pkg_postinst() |
206 | # Check if phase is pkg_postinst() |
| 207 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
207 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
| 208 | die "${FUNCNAME} should only be run in pkg_postinst()" |
208 | die "${FUNCNAME} should only be run in pkg_postinst()" |
| 209 | |
209 | |
| 210 | # strip trailing slash |
210 | # strip trailing slash |
| 211 | myroot="${ROOT%/}" |
211 | myroot="${ROOT%/}" |
| 212 | |
212 | |
| 213 | # respect ROOT and options passed to compileall.py |
213 | # respect ROOT and options passed to compileall.py |
| 214 | while [ $# -gt 0 ]; do |
214 | while (($#)); do |
| 215 | case $1 in |
215 | case $1 in |
| 216 | -l|-f|-q) |
216 | -l|-f|-q) |
| 217 | myopts="${myopts} $1" |
217 | myopts+=("$1") |
| 218 | ;; |
218 | ;; |
| 219 | -d|-x) |
219 | -d|-x) |
| 220 | myopts="${myopts} $1 $2" |
220 | myopts+=("$1" "$2") |
| 221 | shift |
221 | shift |
| 222 | ;; |
222 | ;; |
| 223 | -*) |
223 | -*) |
| 224 | ewarn "${FUNCNAME}: Ignoring compile option $1" |
224 | ewarn "${FUNCNAME}: Ignoring compile option $1" |
| 225 | ;; |
225 | ;; |
| 226 | *) |
226 | *) |
| 227 | [ ! -e "${myroot}/${1}" ] && ewarn "${myroot}/${1} doesn't exist!" |
227 | if [[ -d "${myroot}"/$1 ]]; then |
| 228 | [ -d "${myroot}/${1#/}" ] && mydirs="${mydirs} ${myroot}/${1#/}" |
228 | mydirs+=("${myroot}/$1") |
|
|
229 | elif [[ -f "${myroot}"/$1 ]]; then |
| 229 | # Files are passed to python_mod_compile which is ROOT-aware |
230 | # Files are passed to python_mod_compile which is ROOT-aware |
| 230 | [ -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 |
| 231 | ;; |
237 | ;; |
| 232 | esac |
238 | esac |
| 233 | shift |
239 | shift |
| 234 | done |
240 | done |
| 235 | |
241 | |
| … | |
… | |
| 239 | else |
245 | else |
| 240 | python_version |
246 | python_version |
| 241 | fi |
247 | fi |
| 242 | |
248 | |
| 243 | # set additional opts |
249 | # set additional opts |
| 244 | myopts="${myopts} -q" |
250 | myopts+=(-q) |
| 245 | |
251 | |
| 246 | ebegin "Byte compiling python modules for python-${PYVER} .." |
252 | ebegin "Byte compiling python modules for python-${PYVER} .." |
| 247 | if [ -n "${mydirs}" ]; then |
253 | if ((${#mydirs[@]})); then |
| 248 | python${PYVER} \ |
254 | python${PYVER} \ |
| 249 | ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
255 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
| 250 | ${myopts} ${mydirs} |
256 | "${myopts[@]}" "${mydirs[@]}" |
| 251 | python${PYVER} -O \ |
257 | python${PYVER} -O \ |
| 252 | ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
258 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
| 253 | ${myopts} ${mydirs} |
259 | "${myopts[@]}" "${mydirs[@]}" |
| 254 | fi |
260 | fi |
| 255 | |
261 | |
| 256 | if [ -n "${myfiles}" ]; then |
262 | if ((${#myfiles[@]})); then |
| 257 | python_mod_compile ${myfiles} |
263 | python_mod_compile "${myfiles[@]}" |
| 258 | fi |
264 | fi |
| 259 | |
265 | |
| 260 | eend $? |
266 | eend $? |
| 261 | } |
267 | } |
| 262 | |
268 | |
| … | |
… | |
| 270 | # 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.) |
| 271 | # 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 |
| 272 | # |
278 | # |
| 273 | # This function should only be run in pkg_postrm() |
279 | # This function should only be run in pkg_postrm() |
| 274 | python_mod_cleanup() { |
280 | python_mod_cleanup() { |
| 275 | local SEARCH_PATH myroot |
281 | local SEARCH_PATH=() myroot src_py |
| 276 | |
282 | |
| 277 | # Check if phase is pkg_postrm() |
283 | # Check if phase is pkg_postrm() |
| 278 | [[ ${EBUILD_PHASE} != postrm ]] &&\ |
284 | [[ ${EBUILD_PHASE} != postrm ]] &&\ |
| 279 | die "${FUNCNAME} should only be run in pkg_postrm()" |
285 | die "${FUNCNAME} should only be run in pkg_postrm()" |
| 280 | |
286 | |
| 281 | # strip trailing slash |
287 | # strip trailing slash |
| 282 | myroot="${ROOT%/}" |
288 | myroot="${ROOT%/}" |
| 283 | |
289 | |
| 284 | if [ $# -gt 0 ]; then |
290 | if (($#)); then |
| 285 | for path in $@; do |
291 | SEARCH_PATH=("${@#/}") |
| 286 | SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}" |
292 | SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}") |
|
|
293 | else |
|
|
294 | SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages) |
|
|
295 | fi |
|
|
296 | |
|
|
297 | for path in "${SEARCH_PATH[@]}"; do |
|
|
298 | einfo "Cleaning orphaned Python bytecode from ${path} .." |
|
|
299 | find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do |
|
|
300 | src_py="${REPLY%[co]}" |
|
|
301 | [[ -f "${src_py}" ]] && continue |
|
|
302 | einfo "Purging ${src_py}[co]" |
|
|
303 | rm -f "${src_py}"[co] |
| 287 | done |
304 | done |
| 288 | else |
|
|
| 289 | for path in ${myroot}/usr/lib*/python*/site-packages; do |
|
|
| 290 | SEARCH_PATH="${SEARCH_PATH} ${path}" |
|
|
| 291 | done |
|
|
| 292 | fi |
|
|
| 293 | |
305 | |
| 294 | for path in ${SEARCH_PATH}; do |
|
|
| 295 | einfo "Cleaning orphaned Python bytecode from ${path} .." |
|
|
| 296 | for obj in $(find ${path} -name '*.py[co]'); do |
|
|
| 297 | src_py="${obj%[co]}" |
|
|
| 298 | if [ ! -f "${src_py}" ]; then |
|
|
| 299 | einfo "Purging ${src_py}[co]" |
|
|
| 300 | rm -f ${src_py}[co] |
|
|
| 301 | fi |
|
|
| 302 | done |
|
|
| 303 | # attempt to remove directories that maybe empty |
306 | # attempt to remove directories that maybe empty |
| 304 | for dir in $(find ${path} -type d | sort -r); do |
307 | find "${path}" -type d | sort -r | while read -r dir; do |
| 305 | rmdir ${dir} 2>/dev/null |
308 | rmdir "${dir}" 2>/dev/null |
| 306 | done |
309 | done |
| 307 | done |
310 | done |
| 308 | } |
311 | } |