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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


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

1 dev-zero 1.33 # Copyright 1999-2008 Gentoo Foundation
2 liquidx 1.1 # Distributed under the terms of the GNU General Public License v2
3 betelgeuse 1.55 # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.54 2008/10/30 05:21:46 zmedico Exp $
4 liquidx 1.1
5 dev-zero 1.33 # @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 kloeri 1.32 inherit alternatives multilib
14 liquidx 1.1
15 liquidx 1.2
16 carlo 1.23 if [[ -n "${NEED_PYTHON}" ]] ; then
17 betelgeuse 1.55 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
18     DEPEND="${PYTHON_ATOM}"
19 swegener 1.24 RDEPEND="${DEPEND}"
20 betelgeuse 1.55 else
21     PYTHON_ATOM="dev-lang/python"
22 carlo 1.22 fi
23 liquidx 1.18
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 liquidx 1.28 __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 liquidx 1.18 }
41    
42 dev-zero 1.33 # @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 liquidx 1.18 __python_version_extract() {
47 hawking 1.45 local verstr=$1
48 liquidx 1.18 export PYVER_MAJOR=${verstr:0:1}
49     export PYVER_MINOR=${verstr:2:1}
50 hawking 1.47 if [[ ${verstr:3:1} == . ]]; then
51 swegener 1.29 export PYVER_MICRO=${verstr:4}
52 liquidx 1.28 fi
53 swegener 1.21 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
54 liquidx 1.18 }
55    
56 liquidx 1.1 python_version() {
57 hawking 1.44 [[ -n "${PYVER}" ]] && return 0
58 liquidx 1.1 local tmpstr
59     python=${python:-/usr/bin/python}
60     tmpstr="$(${python} -V 2>&1 )"
61     export PYVER_ALL="${tmpstr#Python }"
62 liquidx 1.18 __python_version_extract $PYVER_ALL
63 liquidx 1.1 }
64    
65 betelgeuse 1.55 # @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 hawking 1.46 # @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 hawking 1.53 export PYTHONDONTWRITEBYTECODE=1 # For 2.6 and above
147     export PYTHON_DONTCOMPILE=1 # For 2.5 and below
148 hawking 1.46 }
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 hawking 1.53 unset PYTHONDONTWRITEBYTECODE
156     unset PYTHON_DONTCOMPILE
157 hawking 1.46 }
158    
159     python_disable_pyc
160    
161 hawking 1.52 # @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 pythonhead 1.43 # @FUNCTION: python_get_libdir
170     # @DESCRIPTION:
171 pythonhead 1.42 # Run without arguments, returns the python library dir
172 pythonhead 1.43 python_get_libdir() {
173 pythonhead 1.42 python_version
174     echo "/usr/$(get_libdir)/python${PYVER}"
175     }
176    
177 pythonhead 1.43 # @FUNCTION: python_get_sitedir
178     # @DESCRIPTION:
179 pythonhead 1.42 # Run without arguments, returns the python site-packages dir
180 pythonhead 1.43 python_get_sitedir() {
181     echo "$(python_get_libdir)/site-packages"
182 pythonhead 1.42 }
183    
184 dev-zero 1.33 # @FUNCTION: python_makesym
185     # @DESCRIPTION:
186     # Run without arguments, it will create the /usr/bin/python symlinks
187     # to the latest installed version
188 liquidx 1.1 python_makesym() {
189 liquidx 1.10 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
190     alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
191 liquidx 1.1 }
192    
193 dev-zero 1.33 # @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 liquidx 1.1 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 kloeri 1.30 eerror "Try adding: 'dev-lang/python tk'"
201 liquidx 1.27 eerror "in to /etc/portage/package.use"
202 liquidx 1.1 echo
203     die "missing tkinter support with installed python"
204     fi
205     }
206    
207 dev-zero 1.33 # @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 liquidx 1.1 #
215 dev-zero 1.33 # Example:
216 liquidx 1.1 # if python_mod_exists gtk; then
217 liquidx 1.28 # echo "gtk support enabled"
218 liquidx 1.1 # fi
219     python_mod_exists() {
220 zmedico 1.54 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
221 hawking 1.47 python -c "import $1" >/dev/null 2>&1
222 liquidx 1.1 }
223    
224 dev-zero 1.33 # @FUNCTION: python_mod_compile
225 hawking 1.35 # @USAGE: < file > [more files ...]
226 dev-zero 1.33 # @DESCRIPTION:
227 hawking 1.35 # Given filenames, it will pre-compile the module's .pyc and .pyo.
228 hawking 1.37 # This function should only be run in pkg_postinst()
229 liquidx 1.1 #
230 dev-zero 1.33 # Example:
231 hawking 1.35 # python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
232 liquidx 1.1 #
233     python_mod_compile() {
234 hawking 1.48 local f myroot myfiles=()
235 hawking 1.37
236     # Check if phase is pkg_postinst()
237     [[ ${EBUILD_PHASE} != postinst ]] &&\
238 hawking 1.39 die "${FUNCNAME} should only be run in pkg_postinst()"
239 hawking 1.37
240 liquidx 1.5 # allow compiling for older python versions
241 hawking 1.48 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
242 liquidx 1.5 PYVER=${PYTHON_OVERRIDE_PYVER}
243 pythonhead 1.7 else
244 liquidx 1.5 python_version
245     fi
246 pythonhead 1.7
247 hawking 1.35 # strip trailing slash
248     myroot="${ROOT%/}"
249    
250     # respect ROOT
251 hawking 1.51 for f in "$@"; do
252 hawking 1.48 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
253 hawking 1.35 done
254    
255 hawking 1.48 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 liquidx 1.1 else
259 hawking 1.35 ewarn "No files to compile!"
260 pythonhead 1.7 fi
261 liquidx 1.1 }
262    
263 dev-zero 1.33 # @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 liquidx 1.1 #
270 dev-zero 1.33 # If supplied with arguments, it will recompile all modules recursively
271     # in the supplied directory
272 hawking 1.37 # This function should only be run in pkg_postinst()
273 dev-zero 1.33 #
274 hawking 1.39 # Options passed to this function are passed to compileall.py
275     #
276 dev-zero 1.33 # Example:
277 hawking 1.36 # python_mod_optimize /usr/share/codegen
278 liquidx 1.1 python_mod_optimize() {
279 hawking 1.49 local myroot mydirs=() myfiles=() myopts=()
280 hawking 1.37
281     # Check if phase is pkg_postinst()
282     [[ ${EBUILD_PHASE} != postinst ]] &&\
283 hawking 1.39 die "${FUNCNAME} should only be run in pkg_postinst()"
284 hawking 1.37
285 liquidx 1.13 # strip trailing slash
286 liquidx 1.16 myroot="${ROOT%/}"
287 liquidx 1.13
288 hawking 1.39 # respect ROOT and options passed to compileall.py
289 hawking 1.49 while (($#)); do
290 hawking 1.39 case $1 in
291     -l|-f|-q)
292 hawking 1.49 myopts+=("$1")
293 hawking 1.39 ;;
294     -d|-x)
295 hawking 1.49 myopts+=("$1" "$2")
296 hawking 1.39 shift
297     ;;
298     -*)
299     ewarn "${FUNCNAME}: Ignoring compile option $1"
300     ;;
301     *)
302 hawking 1.49 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 hawking 1.39 ;;
313     esac
314     shift
315 hawking 1.36 done
316    
317 liquidx 1.5 # allow compiling for older python versions
318     if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
319     PYVER=${PYTHON_OVERRIDE_PYVER}
320 pythonhead 1.7 else
321 liquidx 1.5 python_version
322     fi
323 pythonhead 1.7
324 hawking 1.41 # set additional opts
325 hawking 1.49 myopts+=(-q)
326 pythonhead 1.7
327 liquidx 1.10 ebegin "Byte compiling python modules for python-${PYVER} .."
328 hawking 1.49 if ((${#mydirs[@]})); then
329 hawking 1.36 python${PYVER} \
330 hawking 1.49 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
331     "${myopts[@]}" "${mydirs[@]}"
332 hawking 1.36 python${PYVER} -O \
333 hawking 1.49 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
334     "${myopts[@]}" "${mydirs[@]}"
335 hawking 1.36 fi
336    
337 hawking 1.49 if ((${#myfiles[@]})); then
338     python_mod_compile "${myfiles[@]}"
339 hawking 1.36 fi
340    
341 liquidx 1.5 eend $?
342 liquidx 1.1 }
343    
344 dev-zero 1.33 # @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 hawking 1.37 #
354     # This function should only be run in pkg_postrm()
355 liquidx 1.1 python_mod_cleanup() {
356 hawking 1.50 local SEARCH_PATH=() myroot src_py
357 liquidx 1.16
358 hawking 1.38 # Check if phase is pkg_postrm()
359 hawking 1.37 [[ ${EBUILD_PHASE} != postrm ]] &&\
360 hawking 1.39 die "${FUNCNAME} should only be run in pkg_postrm()"
361 hawking 1.37
362 liquidx 1.13 # strip trailing slash
363 liquidx 1.16 myroot="${ROOT%/}"
364 liquidx 1.1
365 hawking 1.50 if (($#)); then
366     SEARCH_PATH=("${@#/}")
367     SEARCH_PATH=("${SEARCH_PATH[@]/#/$myroot/}")
368 liquidx 1.5 else
369 hawking 1.50 SEARCH_PATH=("${myroot}"/usr/lib*/python*/site-packages)
370 liquidx 1.5 fi
371 pythonhead 1.7
372 hawking 1.50 for path in "${SEARCH_PATH[@]}"; do
373 liquidx 1.11 einfo "Cleaning orphaned Python bytecode from ${path} .."
374 zmedico 1.54 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
375 hawking 1.50 src_py="${REPLY%[co]}"
376     [[ -f "${src_py}" ]] && continue
377     einfo "Purging ${src_py}[co]"
378     rm -f "${src_py}"[co]
379 zmedico 1.54 done
380 hawking 1.50
381 liquidx 1.12 # attempt to remove directories that maybe empty
382 zmedico 1.54 find "${path}" -type d | sort -r | while read -r dir; do
383 hawking 1.50 rmdir "${dir}" 2>/dev/null
384 zmedico 1.54 done
385 pythonhead 1.7 done
386 liquidx 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.20