/[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.55
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.55 2009/05/27 22:49:32 betelgeuse 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#
12# Some useful functions for dealing with python. 12# Some useful functions for dealing with python.
13inherit alternatives multilib 13inherit alternatives multilib
14 14
15 15
16if [[ -n "${NEED_PYTHON}" ]] ; then 16if [[ -n "${NEED_PYTHON}" ]] ; then
17 DEPEND=">=dev-lang/python-${NEED_PYTHON}" 17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
18 DEPEND="${PYTHON_ATOM}"
18 RDEPEND="${DEPEND}" 19 RDEPEND="${DEPEND}"
20else
21 PYTHON_ATOM="dev-lang/python"
19fi 22fi
20 23
21__python_eclass_test() { 24__python_eclass_test() {
22 __python_version_extract 2.3 25 __python_version_extract 2.3
23 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 26 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
34 __python_version_extract 2.5b3 37 __python_version_extract 2.5b3
35 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 38 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
36 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" 39 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
37} 40}
38 41
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 42# @FUNCTION: python_version
62# @DESCRIPTION: 43# @DESCRIPTION:
63# Run without arguments and it will export the version of python 44# Run without arguments and it will export the version of python
64# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 45# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
65__python_version_extract() { 46__python_version_extract() {
66 verstr=$1 47 local verstr=$1
67 export PYVER_MAJOR=${verstr:0:1} 48 export PYVER_MAJOR=${verstr:0:1}
68 export PYVER_MINOR=${verstr:2:1} 49 export PYVER_MINOR=${verstr:2:1}
69 if [ "${verstr:3}x" = ".x" ]; then 50 if [[ ${verstr:3:1} == . ]]; then
70 export PYVER_MICRO=${verstr:4} 51 export PYVER_MICRO=${verstr:4}
71 fi 52 fi
72 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}" 53 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
73} 54}
74 55
75python_version() { 56python_version() {
57 [[ -n "${PYVER}" ]] && return 0
76 local tmpstr 58 local tmpstr
77 python=${python:-/usr/bin/python} 59 python=${python:-/usr/bin/python}
78 tmpstr="$(${python} -V 2>&1 )" 60 tmpstr="$(${python} -V 2>&1 )"
79 export PYVER_ALL="${tmpstr#Python }" 61 export PYVER_ALL="${tmpstr#Python }"
80 __python_version_extract $PYVER_ALL 62 __python_version_extract $PYVER_ALL
63}
64
65# @ECLASS-VARIABLE: PYTHON_USE_WITH
66# @DESCRIPTION:
67# Set this to a space separated list of use flags
68# the python slot in use must be built with.
69
70# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
71# @DESCRIPTION:
72# Set this to a space separated list of use flags
73# of which one must be turned on for the slot of
74# in use.
75
76# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
77# @DESCRIPTION:
78# Set this if you need to make either PYTHON_USE_WITH or
79# PYTHON_USE_WITH_OR atoms conditional under a use flag.
80
81# @FUNCTION: python_pkg_setup
82# @DESCRIPTION:
83# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
84# are respected. Only exported if one of those variables is set.
85if ! has ${EAPI} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
86 python_pkg_setup_fail() {
87 eerror "${1}"
88 die "${1}"
89 }
90
91 python_pkg_setup() {
92 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
93
94 python_version
95 local failed
96 local pyatom="dev-lang/python:${PYVER}"
97
98 for use in ${PYTHON_USE_WITH}; do
99 if ! has_version "${pyatom}[${use}]"; then
100 python_pkg_setup_fail \
101 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}"
102 fi
103 done
104
105 for use in ${PYTHON_USE_WITH_OR}; do
106 if has_version "${pyatom}[${use}]"; then
107 return
108 fi
109 done
110
111 if [[ ${PYTHON_USE_WITH_OR} ]]; then
112 python_pkg_setup_fail \
113 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}"
114 fi
115 }
116
117 EXPORT_FUNCTIONS pkg_setup
118
119 if [[ ${PYTHON_USE_WITH} ]]; then
120 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
121 elif [[ ${PYTHON_USE_WITH_OR} ]]; then
122 PYTHON_USE_WITH_ATOM="|| ( "
123 for use in ${PYTHON_USE_WITH_OR}; do
124 PYTHON_USE_WITH_ATOM="
125 ${PYTHON_USE_WITH_ATOM}
126 ${PYTHON_ATOM}[${use}]"
127 done
128 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )"
129 fi
130 if [[ ${PYTHON_USE_WITH_OPT} ]]; then
131 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
132 fi
133 DEPEND="${PYTHON_USE_WITH_ATOM}"
134 RDEPEND="${PYTHON_USE_WITH_ATOM}"
135fi
136
137# @FUNCTION: python_disable_pyc
138# @DESCRIPTION:
139# Tells python not to automatically recompile modules to .pyc/.pyo
140# even if the timestamps/version stamps don't match. This is done
141# to protect sandbox.
142#
143# note: supported by >=dev-lang/python-2.2.3-r3 only.
144#
145python_disable_pyc() {
146 export PYTHONDONTWRITEBYTECODE=1 # For 2.6 and above
147 export PYTHON_DONTCOMPILE=1 # For 2.5 and below
148}
149
150# @FUNCTION: python_enable_pyc
151# @DESCRIPTION:
152# Tells python to automatically recompile modules to .pyc/.pyo if the
153# timestamps/version stamps change
154python_enable_pyc() {
155 unset PYTHONDONTWRITEBYTECODE
156 unset PYTHON_DONTCOMPILE
157}
158
159python_disable_pyc
160
161# @FUNCTION: python_need_rebuild
162# @DESCRIPTION: Run without arguments, specifies that the package should be
163# rebuilt after a python upgrade.
164python_need_rebuild() {
165 python_version
166 export PYTHON_NEED_REBUILD=${PYVER}
167}
168
169# @FUNCTION: python_get_libdir
170# @DESCRIPTION:
171# Run without arguments, returns the python library dir
172python_get_libdir() {
173 python_version
174 echo "/usr/$(get_libdir)/python${PYVER}"
175}
176
177# @FUNCTION: python_get_sitedir
178# @DESCRIPTION:
179# Run without arguments, returns the python site-packages dir
180python_get_sitedir() {
181 echo "$(python_get_libdir)/site-packages"
81} 182}
82 183
83# @FUNCTION: python_makesym 184# @FUNCTION: python_makesym
84# @DESCRIPTION: 185# @DESCRIPTION:
85# Run without arguments, it will create the /usr/bin/python symlinks 186# Run without arguments, it will create the /usr/bin/python symlinks
114# Example: 215# Example:
115# if python_mod_exists gtk; then 216# if python_mod_exists gtk; then
116# echo "gtk support enabled" 217# echo "gtk support enabled"
117# fi 218# fi
118python_mod_exists() { 219python_mod_exists() {
119 [ -z "$1" ] && die "${FUNCTION} requires an argument!" 220 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
120 if ! python -c "import $1" >/dev/null 2>&1; then 221 python -c "import $1" >/dev/null 2>&1
121 return 1
122 fi
123 return 0
124} 222}
125 223
126# @FUNCTION: python_mod_compile 224# @FUNCTION: python_mod_compile
127# @USAGE: < file > [more files ...] 225# @USAGE: < file > [more files ...]
128# @DESCRIPTION: 226# @DESCRIPTION:
131# 229#
132# Example: 230# Example:
133# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 231# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
134# 232#
135python_mod_compile() { 233python_mod_compile() {
136 local f myroot 234 local f myroot myfiles=()
137 235
138 # Check if phase is pkg_postinst() 236 # Check if phase is pkg_postinst()
139 [[ ${EBUILD_PHASE} != postinst ]] &&\ 237 [[ ${EBUILD_PHASE} != postinst ]] &&\
140 die "${FUNCNAME} should only be run in pkg_postinst()" 238 die "${FUNCNAME} should only be run in pkg_postinst()"
141 239
142 # allow compiling for older python versions 240 # allow compiling for older python versions
143 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then 241 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
144 PYVER=${PYTHON_OVERRIDE_PYVER} 242 PYVER=${PYTHON_OVERRIDE_PYVER}
145 else 243 else
146 python_version 244 python_version
147 fi 245 fi
148 246
149 # strip trailing slash 247 # strip trailing slash
150 myroot="${ROOT%/}" 248 myroot="${ROOT%/}"
151 249
152 # respect ROOT 250 # respect ROOT
153 for f in $@; do 251 for f in "$@"; do
154 [ -f "${myroot}/${f}" ] && myfiles="${myfiles} ${myroot}/${f}" 252 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
155 done 253 done
156 254
157 if [ -n "${myfiles}" ]; then 255 if ((${#myfiles[@]})); then
158 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py ${myfiles} 256 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} 257 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
160 else 258 else
161 ewarn "No files to compile!" 259 ewarn "No files to compile!"
162 fi 260 fi
163} 261}
164 262
176# Options passed to this function are passed to compileall.py 274# Options passed to this function are passed to compileall.py
177# 275#
178# Example: 276# Example:
179# python_mod_optimize /usr/share/codegen 277# python_mod_optimize /usr/share/codegen
180python_mod_optimize() { 278python_mod_optimize() {
181 local mydirs myfiles myroot myopts path 279 local myroot mydirs=() myfiles=() myopts=()
182 280
183 # Check if phase is pkg_postinst() 281 # Check if phase is pkg_postinst()
184 [[ ${EBUILD_PHASE} != postinst ]] &&\ 282 [[ ${EBUILD_PHASE} != postinst ]] &&\
185 die "${FUNCNAME} should only be run in pkg_postinst()" 283 die "${FUNCNAME} should only be run in pkg_postinst()"
186 284
187 # strip trailing slash 285 # strip trailing slash
188 myroot="${ROOT%/}" 286 myroot="${ROOT%/}"
189 287
190 # respect ROOT and options passed to compileall.py 288 # respect ROOT and options passed to compileall.py
191 while [ $# -gt 0 ]; do 289 while (($#)); do
192 case $1 in 290 case $1 in
193 -l|-f|-q) 291 -l|-f|-q)
194 myopts="${myopts} $1" 292 myopts+=("$1")
195 ;; 293 ;;
196 -d|-x) 294 -d|-x)
197 # -x takes regexp as argument so quoting is necessary. 295 myopts+=("$1" "$2")
198 myopts="${myopts} $1 \"$2\""
199 shift 296 shift
200 ;; 297 ;;
201 -*) 298 -*)
202 ewarn "${FUNCNAME}: Ignoring compile option $1" 299 ewarn "${FUNCNAME}: Ignoring compile option $1"
203 ;; 300 ;;
204 *) 301 *)
205 for path in $@; do 302 if [[ -d "${myroot}"/$1 ]]; then
206 [ ! -e "${myroot}/${path}" ] && ewarn "${myroot}/${path} doesn't exist!" 303 mydirs+=("${myroot}/$1")
207 [ -d "${myroot}/${path#/}" ] && mydirs="${mydirs} ${myroot}/${path#/}" 304 elif [[ -f "${myroot}"/$1 ]]; then
208 # Files are passed to python_mod_compile which is ROOT-aware 305 # Files are passed to python_mod_compile which is ROOT-aware
209 [ -f "${myroot}/${path}" ] && myfiles="${myfiles} ${path}" 306 myfiles+=("$1")
210 done 307 elif [[ -e "${myroot}/$1" ]]; then
308 ewarn "${myroot}/$1 is not a file or directory!"
309 else
310 ewarn "${myroot}/$1 doesn't exist!"
311 fi
211 ;; 312 ;;
212 esac 313 esac
213 shift 314 shift
214 done 315 done
215 316
218 PYVER=${PYTHON_OVERRIDE_PYVER} 319 PYVER=${PYTHON_OVERRIDE_PYVER}
219 else 320 else
220 python_version 321 python_version
221 fi 322 fi
222 323
223 # set opts 324 # set additional opts
224 if [ "${PYVER}" = "2.2" ]; then 325 myopts+=(-q)
225 compileopts=""
226 else
227 compileopts="-q"
228 fi
229 326
230 ebegin "Byte compiling python modules for python-${PYVER} .." 327 ebegin "Byte compiling python modules for python-${PYVER} .."
231 if [ -n "${mydirs}" ]; then 328 if ((${#mydirs[@]})); then
232 python${PYVER} \ 329 python${PYVER} \
233 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ 330 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
234 ${compileopts} ${myopts} ${mydirs} 331 "${myopts[@]}" "${mydirs[@]}"
235 python${PYVER} -O \ 332 python${PYVER} -O \
236 ${myroot}/usr/$(get_libdir)/python${PYVER}/compileall.py \ 333 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
237 ${compileopts} ${myopts} ${mydirs} 334 "${myopts[@]}" "${mydirs[@]}"
238 fi 335 fi
239 336
240 if [ -n "${myfiles}" ]; then 337 if ((${#myfiles[@]})); then
241 python_mod_compile ${myfiles} 338 python_mod_compile "${myfiles[@]}"
242 fi 339 fi
243 340
244 eend $? 341 eend $?
245} 342}
246 343
254# and determine if they are orphaned (eg. their corresponding .py is missing.) 351# 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 352# if they are, then it will remove their corresponding .pyc and .pyo
256# 353#
257# This function should only be run in pkg_postrm() 354# This function should only be run in pkg_postrm()
258python_mod_cleanup() { 355python_mod_cleanup() {
259 local SEARCH_PATH myroot 356 local SEARCH_PATH=() myroot src_py
260 357
261 # Check if phase is pkg_postrm() 358 # Check if phase is pkg_postrm()
262 [[ ${EBUILD_PHASE} != postrm ]] &&\ 359 [[ ${EBUILD_PHASE} != postrm ]] &&\
263 die "${FUNCNAME} should only be run in pkg_postrm()" 360 die "${FUNCNAME} should only be run in pkg_postrm()"
264 361
265 # strip trailing slash 362 # strip trailing slash
266 myroot="${ROOT%/}" 363 myroot="${ROOT%/}"
267 364
268 if [ $# -gt 0 ]; then 365 if (($#)); then
269 for path in $@; do 366 SEARCH_PATH=("${@#/}")
270 SEARCH_PATH="${SEARCH_PATH} ${myroot}/${path#/}" 367 SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}")
271 done
272 else 368 else
273 for path in ${myroot}/usr/lib*/python*/site-packages; do 369 SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages)
274 SEARCH_PATH="${SEARCH_PATH} ${path}"
275 done
276 fi 370 fi
277 371
278 for path in ${SEARCH_PATH}; do 372 for path in "${SEARCH_PATH[@]}"; do
279 einfo "Cleaning orphaned Python bytecode from ${path} .." 373 einfo "Cleaning orphaned Python bytecode from ${path} .."
280 for obj in $(find ${path} -name '*.py[co]'); do 374 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
281 src_py="${obj%[co]}" 375 src_py="${REPLY%[co]}"
282 if [ ! -f "${src_py}" ]; then 376 [[ -f "${src_py}" ]] && continue
283 einfo "Purging ${src_py}[co]" 377 einfo "Purging ${src_py}[co]"
284 rm -f ${src_py}[co] 378 rm -f "${src_py}"[co]
285 fi
286 done 379 done
380
287 # attempt to remove directories that maybe empty 381 # attempt to remove directories that maybe empty
288 for dir in $(find ${path} -type d | sort -r); do 382 find "${path}" -type d | sort -r | while read -r dir; do
289 rmdir ${dir} 2>/dev/null 383 rmdir "${dir}" 2>/dev/null
290 done 384 done
291 done 385 done
292} 386}

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

  ViewVC Help
Powered by ViewVC 1.1.20