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

Contents of /eclass/python-r1.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (show annotations) (download)
Sun Nov 4 15:16:34 2012 UTC (21 months, 3 weeks ago) by mgorny
Branch: MAIN
Changes since 1.16: +7 -6 lines
Always require PYTHON_COMPAT. Add an exception for python-exec which is a special case.

1 # Copyright 1999-2012 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/python-r1.eclass,v 1.16 2012/11/01 21:49:34 mgorny Exp $
4
5 # @ECLASS: python-r1
6 # @MAINTAINER:
7 # Michał Górny <mgorny@gentoo.org>
8 # Python herd <python@gentoo.org>
9 # @AUTHOR:
10 # Author: Michał Górny <mgorny@gentoo.org>
11 # Based on work of: Krzysztof Pawlik <nelchael@gentoo.org>
12 # @BLURB: A common, simple eclass for Python packages.
13 # @DESCRIPTION:
14 # A common eclass providing helper functions to build and install
15 # packages supporting being installed for multiple Python
16 # implementations.
17 #
18 # This eclass sets correct IUSE and REQUIRED_USE. It exports PYTHON_DEPS
19 # and PYTHON_USEDEP so you can create correct dependencies for your
20 # package easily. It also provides methods to easily run a command for
21 # each enabled Python implementation and duplicate the sources for them.
22 #
23 # Please note that this eclass is mostly intended to be extended
24 # on-request. If you find something you used in other eclasses missing,
25 # please don't hack it around and request an enhancement instead.
26
27 case "${EAPI}" in
28 0|1|2|3)
29 die "Unsupported EAPI=${EAPI} (too old) for ${ECLASS}"
30 ;;
31 4|5)
32 # EAPI=4 needed for REQUIRED_USE
33 ;;
34 *)
35 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
36 ;;
37 esac
38
39 inherit multilib
40
41 # @ECLASS-VARIABLE: _PYTHON_ALL_IMPLS
42 # @INTERNAL
43 # @DESCRIPTION:
44 # All supported Python implementations, most preferred last.
45 _PYTHON_ALL_IMPLS=(
46 jython2_5
47 pypy1_8 pypy1_9
48 python3_1 python3_2 python3_3
49 python2_5 python2_6 python2_7
50 )
51
52 # @ECLASS-VARIABLE: PYTHON_COMPAT
53 # @REQUIRED
54 # @DESCRIPTION:
55 # This variable contains a list of Python implementations the package
56 # supports. It must be set before the `inherit' call. It has to be
57 # an array.
58 #
59 # Example:
60 # @CODE
61 # PYTHON_COMPAT=( python2_5 python2_6 python2_7 )
62 # @CODE
63 #
64 # Please note that you can also use bash brace expansion if you like:
65 # @CODE
66 # PYTHON_COMPAT=( python{2_5,2_6,2_7} )
67 # @CODE
68 if ! declare -p PYTHON_COMPAT &>/dev/null; then
69 if [[ ${CATEGORY}/${PN} == dev-python/python-exec ]]; then
70 PYTHON_COMPAT=( "${_PYTHON_ALL_IMPLS[@]}" )
71 else
72 die 'PYTHON_COMPAT not declared.'
73 fi
74 fi
75
76 # @ECLASS-VARIABLE: PYTHON_REQ_USE
77 # @DEFAULT_UNSET
78 # @DESCRIPTION:
79 # The list of USEflags required to be enabled on the chosen Python
80 # implementations, formed as a USE-dependency string. It should be valid
81 # for all implementations in PYTHON_COMPAT, so it may be necessary to
82 # use USE defaults.
83 #
84 # Example:
85 # @CODE
86 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
87 # @CODE
88 #
89 # It will cause the Python dependencies to look like:
90 # @CODE
91 # python_targets_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
92 # @CODE
93
94 # @ECLASS-VARIABLE: PYTHON_DEPS
95 # @DESCRIPTION:
96 # This is an eclass-generated Python dependency string for all
97 # implementations listed in PYTHON_COMPAT.
98 #
99 # Example use:
100 # @CODE
101 # RDEPEND="${PYTHON_DEPS}
102 # dev-foo/mydep"
103 # DEPEND="${RDEPEND}"
104 # @CODE
105 #
106 # Example value:
107 # @CODE
108 # dev-python/python-exec
109 # python_targets_python2_6? ( dev-lang/python:2.6[gdbm] )
110 # python_targets_python2_7? ( dev-lang/python:2.7[gdbm] )
111 # @CODE
112
113 # @ECLASS-VARIABLE: PYTHON_USEDEP
114 # @DESCRIPTION:
115 # This is an eclass-generated USE-dependency string which can be used to
116 # depend on another Python package being built for the same Python
117 # implementations.
118 #
119 # The generate USE-flag list is compatible with packages using python-r1
120 # and python-distutils-ng eclasses. It must not be used on packages
121 # using python.eclass.
122 #
123 # Example use:
124 # @CODE
125 # RDEPEND="dev-python/foo[${PYTHON_USEDEP}]"
126 # @CODE
127 #
128 # Example value:
129 # @CODE
130 # python_targets_python2_6?,python_targets_python2_7?
131 # @CODE
132
133 _python_set_globals() {
134 local flags=( "${PYTHON_COMPAT[@]/#/python_targets_}" )
135 local optflags=${flags[@]/%/?}
136
137 IUSE=${flags[*]}
138 REQUIRED_USE="|| ( ${flags[*]} )"
139 PYTHON_USEDEP=${optflags// /,}
140
141 local usestr
142 [[ ${PYTHON_REQ_USE} ]] && usestr="[${PYTHON_REQ_USE}]"
143
144 # 1) well, python-exec would suffice as an RDEP
145 # but no point in making this overcomplex, BDEP doesn't hurt anyone
146 # 2) python-exec should be built with all targets forced anyway
147 # but if new targets were added, we may need to force a rebuild
148 PYTHON_DEPS="dev-python/python-exec[${PYTHON_USEDEP}]"
149 local i
150 for i in "${PYTHON_COMPAT[@]}"; do
151 local d
152 case ${i} in
153 python*)
154 d='dev-lang/python';;
155 jython*)
156 d='dev-java/jython';;
157 pypy*)
158 d='dev-python/pypy';;
159 *)
160 die "Invalid implementation: ${i}"
161 esac
162
163 local v=${i##*[a-z]}
164 PYTHON_DEPS+=" python_targets_${i}? ( ${d}:${v/_/.}${usestr} )"
165 done
166 }
167 _python_set_globals
168
169 # @ECLASS-VARIABLE: BUILD_DIR
170 # @DESCRIPTION:
171 # The current build directory. In global scope, it is supposed to
172 # contain an initial build directory; if unset, it defaults to ${S}.
173 #
174 # In functions run by python_foreach_impl(), the BUILD_DIR is locally
175 # set to an implementation-specific build directory. That path is
176 # created through appending a hyphen and the implementation name
177 # to the final component of the initial BUILD_DIR.
178 #
179 # Example value:
180 # @CODE
181 # ${WORKDIR}/foo-1.3-python2_6
182 # @CODE
183
184 # @ECLASS-VARIABLE: PYTHON
185 # @DESCRIPTION:
186 # The absolute path to the current Python interpreter.
187 #
188 # Set and exported only in commands run by python_foreach_impl().
189 #
190 # Example value:
191 # @CODE
192 # /usr/bin/python2.6
193 # @CODE
194
195 # @ECLASS-VARIABLE: EPYTHON
196 # @DESCRIPTION:
197 # The executable name of the current Python interpreter.
198 #
199 # This variable is used consistently with python.eclass.
200 #
201 # Set and exported only in commands run by python_foreach_impl().
202 #
203 # Example value:
204 # @CODE
205 # python2.6
206 # @CODE
207
208 # @ECLASS-VARIABLE: PYTHON_SITEDIR
209 # @DESCRIPTION:
210 # The path to Python site-packages directory.
211 #
212 # Set and exported on request using python_export().
213 #
214 # Example value:
215 # @CODE
216 # /usr/lib64/python2.6/site-packages
217 # @CODE
218
219 # @FUNCTION: python_export
220 # @USAGE: [<impl>] <variables>...
221 # @DESCRIPTION:
222 # Set and export the Python implementation-relevant variables passed
223 # as parameters.
224 #
225 # The optional first parameter may specify the requested Python
226 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
227 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
228 # the current one will be obtained from ${EPYTHON}.
229 #
230 # The variables which can be exported are: PYTHON, EPYTHON,
231 # PYTHON_SITEDIR. They are described more completely in the eclass
232 # variable documentation.
233 python_export() {
234 debug-print-function ${FUNCNAME} "${@}"
235
236 local impl var
237
238 case "${1}" in
239 python*|jython*)
240 impl=${1/_/.}
241 shift
242 ;;
243 pypy-c*)
244 impl=${1}
245 shift
246 ;;
247 pypy*)
248 local v=${1#pypy}
249 impl=pypy-c${v/_/.}
250 shift
251 ;;
252 *)
253 impl=${EPYTHON}
254 [[ ${impl} ]] || die "python_export: no impl nor EPYTHON"
255 ;;
256 esac
257 debug-print "${FUNCNAME}: implementation: ${impl}"
258
259 for var; do
260 case "${var}" in
261 EPYTHON)
262 export EPYTHON=${impl}
263 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
264 ;;
265 PYTHON)
266 export PYTHON=${EPREFIX}/usr/bin/${impl}
267 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
268 ;;
269 PYTHON_SITEDIR)
270 local dir
271 case "${impl}" in
272 python*)
273 dir=/usr/$(get_libdir)/${impl}
274 ;;
275 jython*)
276 dir=/usr/share/${impl}/Lib
277 ;;
278 pypy*)
279 dir=/usr/$(get_libdir)/${impl/-c/}
280 ;;
281 esac
282
283 export PYTHON_SITEDIR=${EPREFIX}${dir}/site-packages
284 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
285 ;;
286 *)
287 die "python_export: unknown variable ${var}"
288 esac
289 done
290 }
291
292 # @FUNCTION: python_get_PYTHON
293 # @USAGE: [<impl>]
294 # @DESCRIPTION:
295 # Obtain and print the path to the Python interpreter for the given
296 # implementation. If no implementation is provided, ${EPYTHON} will
297 # be used.
298 #
299 # If you just need to have PYTHON set (and exported), then it is better
300 # to use python_export() directly instead.
301 python_get_PYTHON() {
302 debug-print-function ${FUNCNAME} "${@}"
303
304 python_export "${@}" PYTHON
305 echo "${PYTHON}"
306 }
307
308 # @FUNCTION: python_get_EPYTHON
309 # @USAGE: <impl>
310 # @DESCRIPTION:
311 # Obtain and print the EPYTHON value for the given implementation.
312 #
313 # If you just need to have EPYTHON set (and exported), then it is better
314 # to use python_export() directly instead.
315 python_get_EPYTHON() {
316 debug-print-function ${FUNCNAME} "${@}"
317
318 python_export "${@}" EPYTHON
319 echo "${EPYTHON}"
320 }
321
322 # @FUNCTION: python_get_sitedir
323 # @USAGE: [<impl>]
324 # @DESCRIPTION:
325 # Obtain and print the 'site-packages' path for the given
326 # implementation. If no implementation is provided, ${EPYTHON} will
327 # be used.
328 #
329 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
330 # better to use python_export() directly instead.
331 python_get_sitedir() {
332 debug-print-function ${FUNCNAME} "${@}"
333
334 python_export "${@}" PYTHON_SITEDIR
335 echo "${PYTHON_SITEDIR}"
336 }
337
338 # @FUNCTION: python_copy_sources
339 # @DESCRIPTION:
340 # Create a single copy of the package sources (${S}) for each enabled
341 # Python implementation.
342 #
343 # The sources are always copied from S to implementation-specific build
344 # directories respecting BUILD_DIR.
345 python_copy_sources() {
346 debug-print-function ${FUNCNAME} "${@}"
347
348 local impl
349 local bdir=${BUILD_DIR:-${S}}
350
351 debug-print "${FUNCNAME}: bdir = ${bdir}"
352 einfo "Will copy sources from ${S}"
353 # the order is irrelevant here
354 for impl in "${PYTHON_COMPAT[@]}"; do
355 if use "python_targets_${impl}"
356 then
357 local BUILD_DIR=${bdir%%/}-${impl}
358
359 einfo "${impl}: copying to ${BUILD_DIR}"
360 debug-print "${FUNCNAME}: [${impl}] cp ${S} => ${BUILD_DIR}"
361 cp -pr "${S}" "${BUILD_DIR}" || die
362 fi
363 done
364 }
365
366 # @FUNCTION: python_foreach_impl
367 # @USAGE: <command> [<args>...]
368 # @DESCRIPTION:
369 # Run the given command for each of the enabled Python implementations.
370 # If additional parameters are passed, they will be passed through
371 # to the command. If the command fails, python_foreach_impl dies.
372 # If necessary, use ':' to force a successful return.
373 #
374 # For each command being run, EPYTHON, PYTHON and BUILD_DIR are set
375 # locally, and the former two are exported to the command environment.
376 python_foreach_impl() {
377 debug-print-function ${FUNCNAME} "${@}"
378
379 local impl
380 local bdir=${BUILD_DIR:-${S}}
381
382 debug-print "${FUNCNAME}: bdir = ${bdir}"
383 for impl in "${_PYTHON_ALL_IMPLS[@]}"; do
384 if has "${impl}" "${PYTHON_COMPAT[@]}" && use "python_targets_${impl}"
385 then
386 local EPYTHON PYTHON
387 python_export "${impl}" EPYTHON PYTHON
388 local BUILD_DIR=${bdir%%/}-${impl}
389 export EPYTHON PYTHON
390
391 einfo "${EPYTHON}: running ${@}"
392 "${@}" || die "${EPYTHON}: ${1} failed"
393 fi
394 done
395 }
396
397 # @FUNCTION: python_export_best
398 # @USAGE: [<variable>...]
399 # @DESCRIPTION:
400 # Find the best (most preferred) Python implementation enabled
401 # and export given variables for it. If no variables are provided,
402 # EPYTHON & PYTHON will be exported.
403 python_export_best() {
404 debug-print-function ${FUNCNAME} "${@}"
405
406 [[ ${#} -gt 0 ]] || set -- EPYTHON PYTHON
407
408 local impl best
409 for impl in "${_PYTHON_ALL_IMPLS[@]}"; do
410 if has "${impl}" "${PYTHON_COMPAT[@]}" && use "python_targets_${impl}"
411 then
412 best=${impl}
413 fi
414 done
415
416 [[ ${best+1} ]] || die "python_export_best(): no implementation found!"
417
418 debug-print "${FUNCNAME}: Best implementation is: ${impl}"
419 python_export "${impl}" "${@}"
420 }
421
422 # @FUNCTION: _python_rewrite_shebang
423 # @INTERNAL
424 # @USAGE: [<EPYTHON>] <path>...
425 # @DESCRIPTION:
426 # Replaces 'python' executable in the shebang with the executable name
427 # of the specified interpreter. If no EPYTHON value (implementation) is
428 # used, the current ${EPYTHON} will be used.
429 #
430 # All specified files must start with a 'python' shebang. A file not
431 # having a matching shebang will be refused. The exact shebang style
432 # will be preserved in order not to break anything.
433 #
434 # Example conversions:
435 # @CODE
436 # From: #!/usr/bin/python -R
437 # To: #!/usr/bin/python2.7 -R
438 #
439 # From: #!/usr/bin/env FOO=bar python
440 # To: #!/usr/bin/env FOO=bar python2.7
441 # @CODE
442 _python_rewrite_shebang() {
443 debug-print-function ${FUNCNAME} "${@}"
444
445 local impl
446 case "${1}" in
447 python*|jython*|pypy-c*)
448 impl=${1}
449 shift
450 ;;
451 *)
452 impl=${EPYTHON}
453 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
454 ;;
455 esac
456 debug-print "${FUNCNAME}: implementation: ${impl}"
457
458 local f
459 for f; do
460 local shebang=$(head -n 1 "${f}")
461 debug-print "${FUNCNAME}: path = ${f}"
462 debug-print "${FUNCNAME}: shebang = ${shebang}"
463
464 if [[ "${shebang} " != *'python '* ]]; then
465 eerror "A file does not seem to have a supported shebang:"
466 eerror " file: ${f}"
467 eerror " shebang: ${shebang}"
468 die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
469 fi
470
471 sed -i -e "1s:python:${impl}:" "${f}" || die
472 done
473 }
474
475 # @FUNCTION: _python_ln_rel
476 # @INTERNAL
477 # @USAGE: <from> <to>
478 # @DESCRIPTION:
479 # Create a relative symlink.
480 _python_ln_rel() {
481 debug-print-function ${FUNCNAME} "${@}"
482
483 local from=${1}
484 local to=${2}
485
486 local frpath=${from%/*}/
487 local topath=${to%/*}/
488 local rel_path=
489
490 # remove double slashes
491 frpath=${frpath/\/\///}
492 topath=${topath/\/\///}
493
494 while [[ ${topath} ]]; do
495 local frseg=${frpath%%/*}
496 local toseg=${topath%%/*}
497
498 if [[ ${frseg} != ${toseg} ]]; then
499 rel_path=../${rel_path}${frseg:+${frseg}/}
500 fi
501
502 frpath=${frpath#${frseg}/}
503 topath=${topath#${toseg}/}
504 done
505 rel_path+=${frpath}${1##*/}
506
507 debug-print "${FUNCNAME}: ${from} -> ${to}"
508 debug-print "${FUNCNAME}: rel_path = ${rel_path}"
509
510 ln -fs "${rel_path}" "${to}"
511 }
512
513 # @FUNCTION: python_replicate_script
514 # @USAGE: <path>...
515 # @DESCRIPTION:
516 # Copy the given script to variants for all enabled Python
517 # implementations, then replace it with a symlink to the wrapper.
518 #
519 # All specified files must start with a 'python' shebang. A file not
520 # having a matching shebang will be refused.
521 python_replicate_script() {
522 debug-print-function ${FUNCNAME} "${@}"
523
524 local suffixes=()
525
526 _add_suffix() {
527 suffixes+=( "${EPYTHON}" )
528 }
529 python_foreach_impl _add_suffix
530 debug-print "${FUNCNAME}: suffixes = ( ${suffixes[@]} )"
531
532 local f suffix
533 for suffix in "${suffixes[@]}"; do
534 for f; do
535 local newf=${f}-${suffix}
536
537 debug-print "${FUNCNAME}: ${f} -> ${newf}"
538 cp "${f}" "${newf}" || die
539 done
540
541 _python_rewrite_shebang "${suffix}" "${@/%/-${suffix}}"
542 done
543
544 for f; do
545 _python_ln_rel "${ED}"/usr/bin/python-exec "${f}" || die
546 done
547 }

  ViewVC Help
Powered by ViewVC 1.1.20