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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.55 - (show annotations) (download)
Wed May 27 22:49:32 2009 UTC (5 years, 3 months ago) by betelgeuse
Branch: MAIN
Changes since 1.54: +77 -2 lines
Add support for PYTHON_USE_WITH* variables

1 # Copyright 1999-2008 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.54 2008/10/30 05:21:46 zmedico Exp $
4
5 # @ECLASS: python.eclass
6 # @MAINTAINER:
7 # python@gentoo.org
8 #
9 # original author: Alastair Tse <liquidx@gentoo.org>
10 # @BLURB: A Utility Eclass that should be inherited by anything that deals with Python or Python modules.
11 # @DESCRIPTION:
12 # Some useful functions for dealing with python.
13 inherit alternatives multilib
14
15
16 if [[ -n "${NEED_PYTHON}" ]] ; then
17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
18 DEPEND="${PYTHON_ATOM}"
19 RDEPEND="${DEPEND}"
20 else
21 PYTHON_ATOM="dev-lang/python"
22 fi
23
24 __python_eclass_test() {
25 __python_version_extract 2.3
26 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
27 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
28 __python_version_extract 2.3.4
29 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
30 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
31 __python_version_extract 2.3.5
32 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
33 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
34 __python_version_extract 2.4
35 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
36 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
37 __python_version_extract 2.5b3
38 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
39 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
40 }
41
42 # @FUNCTION: python_version
43 # @DESCRIPTION:
44 # Run without arguments and it will export the version of python
45 # currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
46 __python_version_extract() {
47 local verstr=$1
48 export PYVER_MAJOR=${verstr:0:1}
49 export PYVER_MINOR=${verstr:2:1}
50 if [[ ${verstr:3:1} == . ]]; then
51 export PYVER_MICRO=${verstr:4}
52 fi
53 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
54 }
55
56 python_version() {
57 [[ -n "${PYVER}" ]] && return 0
58 local tmpstr
59 python=${python:-/usr/bin/python}
60 tmpstr="$(${python} -V 2>&1 )"
61 export PYVER_ALL="${tmpstr#Python }"
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.
85 if ! 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}"
135 fi
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 #
145 python_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
154 python_enable_pyc() {
155 unset PYTHONDONTWRITEBYTECODE
156 unset PYTHON_DONTCOMPILE
157 }
158
159 python_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.
164 python_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
172 python_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
180 python_get_sitedir() {
181 echo "$(python_get_libdir)/site-packages"
182 }
183
184 # @FUNCTION: python_makesym
185 # @DESCRIPTION:
186 # Run without arguments, it will create the /usr/bin/python symlinks
187 # to the latest installed version
188 python_makesym() {
189 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
190 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
191 }
192
193 # @FUNCTION: python_tkinter_exists
194 # @DESCRIPTION:
195 # Run without arguments, checks if python was compiled with Tkinter
196 # support. If not, prints an error message and dies.
197 python_tkinter_exists() {
198 if ! python -c "import Tkinter" >/dev/null 2>&1; then
199 eerror "You need to recompile python with Tkinter support."
200 eerror "Try adding: 'dev-lang/python tk'"
201 eerror "in to /etc/portage/package.use"
202 echo
203 die "missing tkinter support with installed python"
204 fi
205 }
206
207 # @FUNCTION: python_mod_exists
208 # @USAGE: < module >
209 # @DESCRIPTION:
210 # Run with the module name as an argument. it will check if a
211 # python module is installed and loadable. it will return
212 # TRUE(0) if the module exists, and FALSE(1) if the module does
213 # not exist.
214 #
215 # Example:
216 # if python_mod_exists gtk; then
217 # echo "gtk support enabled"
218 # fi
219 python_mod_exists() {
220 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
221 python -c "import $1" >/dev/null 2>&1
222 }
223
224 # @FUNCTION: python_mod_compile
225 # @USAGE: < file > [more files ...]
226 # @DESCRIPTION:
227 # Given filenames, it will pre-compile the module's .pyc and .pyo.
228 # This function should only be run in pkg_postinst()
229 #
230 # Example:
231 # python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
232 #
233 python_mod_compile() {
234 local f myroot myfiles=()
235
236 # Check if phase is pkg_postinst()
237 [[ ${EBUILD_PHASE} != postinst ]] &&\
238 die "${FUNCNAME} should only be run in pkg_postinst()"
239
240 # allow compiling for older python versions
241 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
242 PYVER=${PYTHON_OVERRIDE_PYVER}
243 else
244 python_version
245 fi
246
247 # strip trailing slash
248 myroot="${ROOT%/}"
249
250 # respect ROOT
251 for f in "$@"; do
252 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
253 done
254
255 if ((${#myfiles[@]})); then
256 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
257 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
258 else
259 ewarn "No files to compile!"
260 fi
261 }
262
263 # @FUNCTION: python_mod_optimize
264 # @USAGE: [ path ]
265 # @DESCRIPTION:
266 # If no arguments supplied, it will recompile all modules under
267 # sys.path (eg. /usr/lib/python2.3, /usr/lib/python2.3/site-packages/ ..)
268 # no recursively
269 #
270 # If supplied with arguments, it will recompile all modules recursively
271 # in the supplied directory
272 # This function should only be run in pkg_postinst()
273 #
274 # Options passed to this function are passed to compileall.py
275 #
276 # Example:
277 # python_mod_optimize /usr/share/codegen
278 python_mod_optimize() {
279 local myroot mydirs=() myfiles=() myopts=()
280
281 # Check if phase is pkg_postinst()
282 [[ ${EBUILD_PHASE} != postinst ]] &&\
283 die "${FUNCNAME} should only be run in pkg_postinst()"
284
285 # strip trailing slash
286 myroot="${ROOT%/}"
287
288 # respect ROOT and options passed to compileall.py
289 while (($#)); do
290 case $1 in
291 -l|-f|-q)
292 myopts+=("$1")
293 ;;
294 -d|-x)
295 myopts+=("$1" "$2")
296 shift
297 ;;
298 -*)
299 ewarn "${FUNCNAME}: Ignoring compile option $1"
300 ;;
301 *)
302 if [[ -d "${myroot}"/$1 ]]; then
303 mydirs+=("${myroot}/$1")
304 elif [[ -f "${myroot}"/$1 ]]; then
305 # Files are passed to python_mod_compile which is ROOT-aware
306 myfiles+=("$1")
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
312 ;;
313 esac
314 shift
315 done
316
317 # allow compiling for older python versions
318 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
319 PYVER=${PYTHON_OVERRIDE_PYVER}
320 else
321 python_version
322 fi
323
324 # set additional opts
325 myopts+=(-q)
326
327 ebegin "Byte compiling python modules for python-${PYVER} .."
328 if ((${#mydirs[@]})); then
329 python${PYVER} \
330 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
331 "${myopts[@]}" "${mydirs[@]}"
332 python${PYVER} -O \
333 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
334 "${myopts[@]}" "${mydirs[@]}"
335 fi
336
337 if ((${#myfiles[@]})); then
338 python_mod_compile "${myfiles[@]}"
339 fi
340
341 eend $?
342 }
343
344 # @FUNCTION: python_mod_cleanup
345 # @USAGE: [ dir ]
346 # @DESCRIPTION:
347 # Run with optional arguments, where arguments are directories of
348 # python modules. if none given, it will look in /usr/lib/python[0-9].[0-9]
349 #
350 # It will recursively scan all compiled python modules in the directories
351 # and determine if they are orphaned (eg. their corresponding .py is missing.)
352 # if they are, then it will remove their corresponding .pyc and .pyo
353 #
354 # This function should only be run in pkg_postrm()
355 python_mod_cleanup() {
356 local SEARCH_PATH=() myroot src_py
357
358 # Check if phase is pkg_postrm()
359 [[ ${EBUILD_PHASE} != postrm ]] &&\
360 die "${FUNCNAME} should only be run in pkg_postrm()"
361
362 # strip trailing slash
363 myroot="${ROOT%/}"
364
365 if (($#)); then
366 SEARCH_PATH=("${@#/}")
367 SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}")
368 else
369 SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages)
370 fi
371
372 for path in "${SEARCH_PATH[@]}"; do
373 einfo "Cleaning orphaned Python bytecode from ${path} .."
374 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
375 src_py="${REPLY%[co]}"
376 [[ -f "${src_py}" ]] && continue
377 einfo "Purging ${src_py}[co]"
378 rm -f "${src_py}"[co]
379 done
380
381 # attempt to remove directories that maybe empty
382 find "${path}" -type d | sort -r | while read -r dir; do
383 rmdir "${dir}" 2>/dev/null
384 done
385 done
386 }

  ViewVC Help
Powered by ViewVC 1.1.20