| 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.44 2008/08/29 19:28:08 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 | # |
| … | |
… | |
| 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 | # |
|
|
| 47 | python_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 |
|
|
| 55 | python_enable_pyc() { |
|
|
| 56 | unset PYTHON_DONTCOMPILE |
|
|
| 57 | } |
|
|
| 58 | |
|
|
| 59 | python_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 | |
| … | |
… | |
| 77 | local tmpstr |
55 | local tmpstr |
| 78 | python=${python:-/usr/bin/python} |
56 | python=${python:-/usr/bin/python} |
| 79 | tmpstr="$(${python} -V 2>&1 )" |
57 | tmpstr="$(${python} -V 2>&1 )" |
| 80 | export PYVER_ALL="${tmpstr#Python }" |
58 | export PYVER_ALL="${tmpstr#Python }" |
| 81 | __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 | # |
|
|
70 | python_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 |
|
|
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() { |
|
|
90 | python_version |
|
|
91 | export PYTHON_NEED_REBUILD=${PYVER} |
| 82 | } |
92 | } |
| 83 | |
93 | |
| 84 | # @FUNCTION: python_get_libdir |
94 | # @FUNCTION: python_get_libdir |
| 85 | # @DESCRIPTION: |
95 | # @DESCRIPTION: |
| 86 | # Run without arguments, returns the python library dir |
96 | # Run without arguments, returns the python library dir |
| … | |
… | |
| 130 | # Example: |
140 | # Example: |
| 131 | # if python_mod_exists gtk; then |
141 | # if python_mod_exists gtk; then |
| 132 | # echo "gtk support enabled" |
142 | # echo "gtk support enabled" |
| 133 | # fi |
143 | # fi |
| 134 | python_mod_exists() { |
144 | python_mod_exists() { |
| 135 | [ -z "$1" ] && die "${FUNCTION} requires an argument!" |
145 | [[ "$1" ]] || die "${FUNCNAME} requires an argument!" |
| 136 | if ! python -c "import $1" >/dev/null 2>&1; then |
146 | python -c "import $1" >/dev/null 2>&1 |
| 137 | return 1 |
|
|
| 138 | fi |
|
|
| 139 | return 0 |
|
|
| 140 | } |
147 | } |
| 141 | |
148 | |
| 142 | # @FUNCTION: python_mod_compile |
149 | # @FUNCTION: python_mod_compile |
| 143 | # @USAGE: < file > [more files ...] |
150 | # @USAGE: < file > [more files ...] |
| 144 | # @DESCRIPTION: |
151 | # @DESCRIPTION: |
| … | |
… | |
| 147 | # |
154 | # |
| 148 | # Example: |
155 | # Example: |
| 149 | # python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py |
156 | # python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py |
| 150 | # |
157 | # |
| 151 | python_mod_compile() { |
158 | python_mod_compile() { |
| 152 | local f myroot |
159 | local f myroot myfiles=() |
| 153 | |
160 | |
| 154 | # Check if phase is pkg_postinst() |
161 | # Check if phase is pkg_postinst() |
| 155 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
162 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
| 156 | die "${FUNCNAME} should only be run in pkg_postinst()" |
163 | die "${FUNCNAME} should only be run in pkg_postinst()" |
| 157 | |
164 | |
| 158 | # allow compiling for older python versions |
165 | # allow compiling for older python versions |
| 159 | if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then |
166 | if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then |
| 160 | PYVER=${PYTHON_OVERRIDE_PYVER} |
167 | PYVER=${PYTHON_OVERRIDE_PYVER} |
| 161 | else |
168 | else |
| 162 | python_version |
169 | python_version |
| 163 | fi |
170 | fi |
| 164 | |
171 | |
| 165 | # strip trailing slash |
172 | # strip trailing slash |
| 166 | myroot="${ROOT%/}" |
173 | myroot="${ROOT%/}" |
| 167 | |
174 | |
| 168 | # respect ROOT |
175 | # respect ROOT |
| 169 | for f in $@; do |
176 | for f in "$@"; do |
| 170 | [ -f "${myroot}/${f}" ] && myfiles="${myfiles} ${myroot}/${f}" |
177 | [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") |
| 171 | done |
178 | done |
| 172 | |
179 | |
| 173 | if [ -n "${myfiles}" ]; then |
180 | if ((${#myfiles[@]})); then |
| 174 | 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[@]}" |
| 175 | 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[@]}" |
| 176 | else |
183 | else |
| 177 | ewarn "No files to compile!" |
184 | ewarn "No files to compile!" |
| 178 | fi |
185 | fi |
| 179 | } |
186 | } |
| 180 | |
187 | |
| … | |
… | |
| 192 | # Options passed to this function are passed to compileall.py |
199 | # Options passed to this function are passed to compileall.py |
| 193 | # |
200 | # |
| 194 | # Example: |
201 | # Example: |
| 195 | # python_mod_optimize /usr/share/codegen |
202 | # python_mod_optimize /usr/share/codegen |
| 196 | python_mod_optimize() { |
203 | python_mod_optimize() { |
| 197 | local mydirs myfiles myroot myopts |
204 | local myroot mydirs=() myfiles=() myopts=() |
| 198 | |
205 | |
| 199 | # Check if phase is pkg_postinst() |
206 | # Check if phase is pkg_postinst() |
| 200 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
207 | [[ ${EBUILD_PHASE} != postinst ]] &&\ |
| 201 | die "${FUNCNAME} should only be run in pkg_postinst()" |
208 | die "${FUNCNAME} should only be run in pkg_postinst()" |
| 202 | |
209 | |
| 203 | # strip trailing slash |
210 | # strip trailing slash |
| 204 | myroot="${ROOT%/}" |
211 | myroot="${ROOT%/}" |
| 205 | |
212 | |
| 206 | # respect ROOT and options passed to compileall.py |
213 | # respect ROOT and options passed to compileall.py |
| 207 | while [ $# -gt 0 ]; do |
214 | while (($#)); do |
| 208 | case $1 in |
215 | case $1 in |
| 209 | -l|-f|-q) |
216 | -l|-f|-q) |
| 210 | myopts="${myopts} $1" |
217 | myopts+=("$1") |
| 211 | ;; |
218 | ;; |
| 212 | -d|-x) |
219 | -d|-x) |
| 213 | myopts="${myopts} $1 $2" |
220 | myopts+=("$1" "$2") |
| 214 | shift |
221 | shift |
| 215 | ;; |
222 | ;; |
| 216 | -*) |
223 | -*) |
| 217 | ewarn "${FUNCNAME}: Ignoring compile option $1" |
224 | ewarn "${FUNCNAME}: Ignoring compile option $1" |
| 218 | ;; |
225 | ;; |
| 219 | *) |
226 | *) |
| 220 | [ ! -e "${myroot}/${1}" ] && ewarn "${myroot}/${1} doesn't exist!" |
227 | if [[ -d "${myroot}"/$1 ]]; then |
| 221 | [ -d "${myroot}/${1#/}" ] && mydirs="${mydirs} ${myroot}/${1#/}" |
228 | mydirs+=("${myroot}/$1") |
|
|
229 | elif [[ -f "${myroot}"/$1 ]]; then |
| 222 | # Files are passed to python_mod_compile which is ROOT-aware |
230 | # Files are passed to python_mod_compile which is ROOT-aware |
| 223 | [ -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 |
| 224 | ;; |
237 | ;; |
| 225 | esac |
238 | esac |
| 226 | shift |
239 | shift |
| 227 | done |
240 | done |
| 228 | |
241 | |
| … | |
… | |
| 232 | else |
245 | else |
| 233 | python_version |
246 | python_version |
| 234 | fi |
247 | fi |
| 235 | |
248 | |
| 236 | # set additional opts |
249 | # set additional opts |
| 237 | myopts="${myopts} -q" |
250 | myopts+=(-q) |
| 238 | |
251 | |
| 239 | ebegin "Byte compiling python modules for python-${PYVER} .." |
252 | ebegin "Byte compiling python modules for python-${PYVER} .." |
| 240 | if [ -n "${mydirs}" ]; then |
253 | if ((${#mydirs[@]})); then |
| 241 | python${PYVER} \ |
254 | python${PYVER} \ |
| 242 | ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
255 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
| 243 | ${myopts} ${mydirs} |
256 | "${myopts[@]}" "${mydirs[@]}" |
| 244 | python${PYVER} -O \ |
257 | python${PYVER} -O \ |
| 245 | ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
258 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
| 246 | ${myopts} ${mydirs} |
259 | "${myopts[@]}" "${mydirs[@]}" |
| 247 | fi |
260 | fi |
| 248 | |
261 | |
| 249 | if [ -n "${myfiles}" ]; then |
262 | if ((${#myfiles[@]})); then |
| 250 | python_mod_compile ${myfiles} |
263 | python_mod_compile "${myfiles[@]}" |
| 251 | fi |
264 | fi |
| 252 | |
265 | |
| 253 | eend $? |
266 | eend $? |
| 254 | } |
267 | } |
| 255 | |
268 | |
| … | |
… | |
| 263 | # 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.) |
| 264 | # 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 |
| 265 | # |
278 | # |
| 266 | # This function should only be run in pkg_postrm() |
279 | # This function should only be run in pkg_postrm() |
| 267 | python_mod_cleanup() { |
280 | python_mod_cleanup() { |
| 268 | local SEARCH_PATH myroot |
281 | local SEARCH_PATH=() myroot src_py |
| 269 | |
282 | |
| 270 | # Check if phase is pkg_postrm() |
283 | # Check if phase is pkg_postrm() |
| 271 | [[ ${EBUILD_PHASE} != postrm ]] &&\ |
284 | [[ ${EBUILD_PHASE} != postrm ]] &&\ |
| 272 | die "${FUNCNAME} should only be run in pkg_postrm()" |
285 | die "${FUNCNAME} should only be run in pkg_postrm()" |
| 273 | |
286 | |
| 274 | # strip trailing slash |
287 | # strip trailing slash |
| 275 | myroot="${ROOT%/}" |
288 | myroot="${ROOT%/}" |
| 276 | |
289 | |
| 277 | if [ $# -gt 0 ]; then |
290 | if (($#)); then |
| 278 | for path in $@; do |
291 | SEARCH_PATH=("${@#/}") |
| 279 | 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] |
| 280 | done |
304 | done |
| 281 | else |
|
|
| 282 | for path in ${myroot}/usr/lib*/python*/site-packages; do |
|
|
| 283 | SEARCH_PATH="${SEARCH_PATH} ${path}" |
|
|
| 284 | done |
|
|
| 285 | fi |
|
|
| 286 | |
305 | |
| 287 | for path in ${SEARCH_PATH}; do |
|
|
| 288 | einfo "Cleaning orphaned Python bytecode from ${path} .." |
|
|
| 289 | for obj in $(find ${path} -name '*.py[co]'); do |
|
|
| 290 | src_py="${obj%[co]}" |
|
|
| 291 | if [ ! -f "${src_py}" ]; then |
|
|
| 292 | einfo "Purging ${src_py}[co]" |
|
|
| 293 | rm -f ${src_py}[co] |
|
|
| 294 | fi |
|
|
| 295 | done |
|
|
| 296 | # attempt to remove directories that maybe empty |
306 | # attempt to remove directories that maybe empty |
| 297 | for dir in $(find ${path} -type d | sort -r); do |
307 | find "${path}" -type d | sort -r | while read -r dir; do |
| 298 | rmdir ${dir} 2>/dev/null |
308 | rmdir "${dir}" 2>/dev/null |
| 299 | done |
309 | done |
| 300 | done |
310 | done |
| 301 | } |
311 | } |