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

Contents of /eclass/python-utils-r1.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.41 - (show annotations) (download)
Thu Sep 26 11:24:30 2013 UTC (10 months ago) by mgorny
Branch: MAIN
Changes since 1.40: +3 -4 lines
Fix EAPI=4 on python-exec:2 since that is what pkgcore will require (the only EAPI=4 consumer right now).

1 # Copyright 1999-2013 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/python-utils-r1.eclass,v 1.40 2013/09/17 19:40:56 mgorny Exp $
4
5 # @ECLASS: python-utils-r1
6 # @MAINTAINER:
7 # Python team <python@gentoo.org>
8 # @AUTHOR:
9 # Author: Michał Górny <mgorny@gentoo.org>
10 # Based on work of: Krzysztof Pawlik <nelchael@gentoo.org>
11 # @BLURB: Utility functions for packages with Python parts.
12 # @DESCRIPTION:
13 # An utility eclass providing functions to query Python implementations,
14 # install Python modules and scripts.
15 #
16 # This eclass does not set any metadata variables nor export any phase
17 # functions. It can be inherited safely.
18 #
19 # For more information, please see the python-r1 Developer's Guide:
20 # http://www.gentoo.org/proj/en/Python/python-r1/dev-guide.xml
21
22 case "${EAPI:-0}" in
23 0|1|2|3|4|5)
24 ;;
25 *)
26 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
27 ;;
28 esac
29
30 if [[ ${_PYTHON_ECLASS_INHERITED} ]]; then
31 die 'python-r1 suite eclasses can not be used with python.eclass.'
32 fi
33
34 if [[ ! ${_PYTHON_UTILS_R1} ]]; then
35
36 inherit eutils multilib toolchain-funcs
37
38 # @ECLASS-VARIABLE: _PYTHON_ALL_IMPLS
39 # @INTERNAL
40 # @DESCRIPTION:
41 # All supported Python implementations, most preferred last.
42 _PYTHON_ALL_IMPLS=(
43 jython2_5 jython2_7
44 pypy2_0
45 python3_2 python3_3
46 python2_6 python2_7
47 )
48
49 # @FUNCTION: _python_impl_supported
50 # @USAGE: <impl>
51 # @INTERNAL
52 # @DESCRIPTION:
53 # Check whether the implementation <impl> (PYTHON_COMPAT-form)
54 # is still supported.
55 #
56 # Returns 0 if the implementation is valid and supported. If it is
57 # unsupported, returns 1 -- and the caller should ignore the entry.
58 # If it is invalid, dies with an appopriate error messages.
59 _python_impl_supported() {
60 debug-print-function ${FUNCNAME} "${@}"
61
62 [[ ${#} -eq 1 ]] || die "${FUNCNAME}: takes exactly 1 argument (impl)."
63
64 local impl=${1}
65
66 # keep in sync with _PYTHON_ALL_IMPLS!
67 # (not using that list because inline patterns shall be faster)
68 case "${impl}" in
69 python2_[67]|python3_[23]|pypy2_0|jython2_[57])
70 return 0
71 ;;
72 pypy1_[89]|python2_5|python3_1)
73 return 1
74 ;;
75 *)
76 die "Invalid implementation in PYTHON_COMPAT: ${impl}"
77 esac
78 }
79
80 # @ECLASS-VARIABLE: PYTHON
81 # @DEFAULT_UNSET
82 # @DESCRIPTION:
83 # The absolute path to the current Python interpreter.
84 #
85 # This variable is set automatically in the following contexts:
86 #
87 # python-r1: Set in functions called by python_foreach_impl() or after
88 # calling python_export_best().
89 #
90 # python-single-r1: Set after calling python-single-r1_pkg_setup().
91 #
92 # distutils-r1: Set within any of the python sub-phase functions.
93 #
94 # Example value:
95 # @CODE
96 # /usr/bin/python2.6
97 # @CODE
98
99 # @ECLASS-VARIABLE: EPYTHON
100 # @DEFAULT_UNSET
101 # @DESCRIPTION:
102 # The executable name of the current Python interpreter.
103 #
104 # This variable is set automatically in the following contexts:
105 #
106 # python-r1: Set in functions called by python_foreach_impl() or after
107 # calling python_export_best().
108 #
109 # python-single-r1: Set after calling python-single-r1_pkg_setup().
110 #
111 # distutils-r1: Set within any of the python sub-phase functions.
112 #
113 # Example value:
114 # @CODE
115 # python2.6
116 # @CODE
117
118 # @ECLASS-VARIABLE: PYTHON_SITEDIR
119 # @DEFAULT_UNSET
120 # @DESCRIPTION:
121 # The path to Python site-packages directory.
122 #
123 # Set and exported on request using python_export().
124 #
125 # Example value:
126 # @CODE
127 # /usr/lib64/python2.6/site-packages
128 # @CODE
129
130 # @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
131 # @DEFAULT_UNSET
132 # @DESCRIPTION:
133 # The path to Python include directory.
134 #
135 # Set and exported on request using python_export().
136 #
137 # Example value:
138 # @CODE
139 # /usr/include/python2.6
140 # @CODE
141
142 # @ECLASS-VARIABLE: PYTHON_LIBPATH
143 # @DEFAULT_UNSET
144 # @DESCRIPTION:
145 # The path to Python library.
146 #
147 # Set and exported on request using python_export().
148 # Valid only for CPython.
149 #
150 # Example value:
151 # @CODE
152 # /usr/lib64/libpython2.6.so
153 # @CODE
154
155 # @ECLASS-VARIABLE: PYTHON_CFLAGS
156 # @DEFAULT_UNSET
157 # @DESCRIPTION:
158 # Proper C compiler flags for building against Python. Obtained from
159 # pkg-config or python-config.
160 #
161 # Set and exported on request using python_export().
162 # Valid only for CPython. Requires a proper build-time dependency
163 # on the Python implementation and on pkg-config.
164 #
165 # Example value:
166 # @CODE
167 # -I/usr/include/python2.7
168 # @CODE
169
170 # @ECLASS-VARIABLE: PYTHON_LIBS
171 # @DEFAULT_UNSET
172 # @DESCRIPTION:
173 # Proper C compiler flags for linking against Python. Obtained from
174 # pkg-config or python-config.
175 #
176 # Set and exported on request using python_export().
177 # Valid only for CPython. Requires a proper build-time dependency
178 # on the Python implementation and on pkg-config.
179 #
180 # Example value:
181 # @CODE
182 # -lpython2.7
183 # @CODE
184
185 # @ECLASS-VARIABLE: PYTHON_PKG_DEP
186 # @DEFAULT_UNSET
187 # @DESCRIPTION:
188 # The complete dependency on a particular Python package as a string.
189 #
190 # Set and exported on request using python_export().
191 #
192 # Example value:
193 # @CODE
194 # dev-lang/python:2.7[xml]
195 # @CODE
196
197 # @ECLASS-VARIABLE: PYTHON_SCRIPTDIR
198 # @DEFAULT_UNSET
199 # @DESCRIPTION:
200 # The location where Python scripts must be installed for current impl.
201 #
202 # Set and exported on request using python_export().
203 #
204 # Example value:
205 # @CODE
206 # /usr/lib/python-exec/python2.7
207 # @CODE
208
209 # @FUNCTION: python_export
210 # @USAGE: [<impl>] <variables>...
211 # @DESCRIPTION:
212 # Set and export the Python implementation-relevant variables passed
213 # as parameters.
214 #
215 # The optional first parameter may specify the requested Python
216 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
217 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
218 # the current one will be obtained from ${EPYTHON}.
219 #
220 # The variables which can be exported are: PYTHON, EPYTHON,
221 # PYTHON_SITEDIR. They are described more completely in the eclass
222 # variable documentation.
223 python_export() {
224 debug-print-function ${FUNCNAME} "${@}"
225
226 local impl var
227
228 case "${1}" in
229 python*|jython*)
230 impl=${1/_/.}
231 shift
232 ;;
233 pypy-c*)
234 impl=${1}
235 shift
236 ;;
237 pypy*)
238 local v=${1#pypy}
239 impl=pypy-c${v/_/.}
240 shift
241 ;;
242 *)
243 impl=${EPYTHON}
244 [[ ${impl} ]] || die "python_export: no impl nor EPYTHON"
245 ;;
246 esac
247 debug-print "${FUNCNAME}: implementation: ${impl}"
248
249 for var; do
250 case "${var}" in
251 EPYTHON)
252 export EPYTHON=${impl}
253 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
254 ;;
255 PYTHON)
256 export PYTHON=${EPREFIX}/usr/bin/${impl}
257 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
258 ;;
259 PYTHON_SITEDIR)
260 local dir
261 case "${impl}" in
262 python*)
263 dir=/usr/$(get_libdir)/${impl}
264 ;;
265 jython*)
266 dir=/usr/share/${impl/n/n-}/Lib
267 ;;
268 pypy*)
269 dir=/usr/$(get_libdir)/${impl/-c/}
270 ;;
271 esac
272
273 export PYTHON_SITEDIR=${EPREFIX}${dir}/site-packages
274 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
275 ;;
276 PYTHON_INCLUDEDIR)
277 local dir
278 case "${impl}" in
279 python*)
280 dir=/usr/include/${impl}
281 ;;
282 pypy*)
283 dir=/usr/$(get_libdir)/${impl/-c/}/include
284 ;;
285 *)
286 die "${impl} lacks header files"
287 ;;
288 esac
289
290 export PYTHON_INCLUDEDIR=${EPREFIX}${dir}
291 debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
292 ;;
293 PYTHON_LIBPATH)
294 local libname
295 case "${impl}" in
296 python*)
297 libname=lib${impl}
298 ;;
299 *)
300 die "${impl} lacks a dynamic library"
301 ;;
302 esac
303
304 local path=${EPREFIX}/usr/$(get_libdir)
305
306 export PYTHON_LIBPATH=${path}/${libname}$(get_libname)
307 debug-print "${FUNCNAME}: PYTHON_LIBPATH = ${PYTHON_LIBPATH}"
308 ;;
309 PYTHON_CFLAGS)
310 local val
311
312 case "${impl}" in
313 python2.5|python2.6)
314 # old versions support python-config only
315 val=$("${impl}-config" --includes)
316 ;;
317 python*)
318 # python-2.7, python-3.2, etc.
319 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-})
320 ;;
321 *)
322 die "${impl}: obtaining ${var} not supported"
323 ;;
324 esac
325
326 export PYTHON_CFLAGS=${val}
327 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
328 ;;
329 PYTHON_LIBS)
330 local val
331
332 case "${impl}" in
333 python2.5|python2.6)
334 # old versions support python-config only
335 val=$("${impl}-config" --libs)
336 ;;
337 python*)
338 # python-2.7, python-3.2, etc.
339 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-})
340 ;;
341 *)
342 die "${impl}: obtaining ${var} not supported"
343 ;;
344 esac
345
346 export PYTHON_LIBS=${val}
347 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
348 ;;
349 PYTHON_PKG_DEP)
350 local d
351 case ${impl} in
352 python*)
353 PYTHON_PKG_DEP='dev-lang/python';;
354 jython*)
355 PYTHON_PKG_DEP='dev-java/jython';;
356 pypy*)
357 PYTHON_PKG_DEP='virtual/pypy';;
358 *)
359 die "Invalid implementation: ${impl}"
360 esac
361
362 # slot
363 PYTHON_PKG_DEP+=:${impl##*[a-z-]}
364
365 # use-dep
366 if [[ ${PYTHON_REQ_USE} ]]; then
367 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
368 fi
369
370 export PYTHON_PKG_DEP
371 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
372 ;;
373 PYTHON_SCRIPTDIR)
374 local dir
375 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
376 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
377 ;;
378 *)
379 die "python_export: unknown variable ${var}"
380 esac
381 done
382 }
383
384 # @FUNCTION: python_get_PYTHON
385 # @USAGE: [<impl>]
386 # @DESCRIPTION:
387 # Obtain and print the path to the Python interpreter for the given
388 # implementation. If no implementation is provided, ${EPYTHON} will
389 # be used.
390 #
391 # If you just need to have PYTHON set (and exported), then it is better
392 # to use python_export() directly instead.
393 python_get_PYTHON() {
394 debug-print-function ${FUNCNAME} "${@}"
395
396 eqawarn '$(python_get_PYTHON) is discouraged since all standard environments' >&2
397 eqawarn 'have PYTHON exported anyway. Please use ${PYTHON} instead.' >&2
398 eqawarn 'python_get_PYTHON will be removed on 2013-10-16.' >&2
399
400 python_export "${@}" PYTHON
401 echo "${PYTHON}"
402 }
403
404 # @FUNCTION: python_get_EPYTHON
405 # @USAGE: <impl>
406 # @DESCRIPTION:
407 # Obtain and print the EPYTHON value for the given implementation.
408 #
409 # If you just need to have EPYTHON set (and exported), then it is better
410 # to use python_export() directly instead.
411 python_get_EPYTHON() {
412 debug-print-function ${FUNCNAME} "${@}"
413
414 eqawarn '$(python_get_EPYTHON) is discouraged since all standard environments' >&2
415 eqawarn 'have EPYTHON exported anyway. Please use ${EPYTHON} instead.' >&2
416 eqawarn 'python_get_EPYTHON will be removed on 2013-10-16.' >&2
417
418 python_export "${@}" EPYTHON
419 echo "${EPYTHON}"
420 }
421
422 # @FUNCTION: python_get_sitedir
423 # @USAGE: [<impl>]
424 # @DESCRIPTION:
425 # Obtain and print the 'site-packages' path for the given
426 # implementation. If no implementation is provided, ${EPYTHON} will
427 # be used.
428 #
429 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
430 # better to use python_export() directly instead.
431 python_get_sitedir() {
432 debug-print-function ${FUNCNAME} "${@}"
433
434 python_export "${@}" PYTHON_SITEDIR
435 echo "${PYTHON_SITEDIR}"
436 }
437
438 # @FUNCTION: python_get_includedir
439 # @USAGE: [<impl>]
440 # @DESCRIPTION:
441 # Obtain and print the include path for the given implementation. If no
442 # implementation is provided, ${EPYTHON} will be used.
443 #
444 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
445 # is better to use python_export() directly instead.
446 python_get_includedir() {
447 debug-print-function ${FUNCNAME} "${@}"
448
449 python_export "${@}" PYTHON_INCLUDEDIR
450 echo "${PYTHON_INCLUDEDIR}"
451 }
452
453 # @FUNCTION: python_get_library_path
454 # @USAGE: [<impl>]
455 # @DESCRIPTION:
456 # Obtain and print the Python library path for the given implementation.
457 # If no implementation is provided, ${EPYTHON} will be used.
458 #
459 # Please note that this function can be used with CPython only. Use
460 # in another implementation will result in a fatal failure.
461 python_get_library_path() {
462 debug-print-function ${FUNCNAME} "${@}"
463
464 python_export "${@}" PYTHON_LIBPATH
465 echo "${PYTHON_LIBPATH}"
466 }
467
468 # @FUNCTION: python_get_CFLAGS
469 # @USAGE: [<impl>]
470 # @DESCRIPTION:
471 # Obtain and print the compiler flags for building against Python,
472 # for the given implementation. If no implementation is provided,
473 # ${EPYTHON} will be used.
474 #
475 # Please note that this function can be used with CPython only.
476 # It requires Python and pkg-config installed, and therefore proper
477 # build-time dependencies need be added to the ebuild.
478 python_get_CFLAGS() {
479 debug-print-function ${FUNCNAME} "${@}"
480
481 python_export "${@}" PYTHON_CFLAGS
482 echo "${PYTHON_CFLAGS}"
483 }
484
485 # @FUNCTION: python_get_LIBS
486 # @USAGE: [<impl>]
487 # @DESCRIPTION:
488 # Obtain and print the compiler flags for linking against Python,
489 # for the given implementation. If no implementation is provided,
490 # ${EPYTHON} will be used.
491 #
492 # Please note that this function can be used with CPython only.
493 # It requires Python and pkg-config installed, and therefore proper
494 # build-time dependencies need be added to the ebuild.
495 python_get_LIBS() {
496 debug-print-function ${FUNCNAME} "${@}"
497
498 python_export "${@}" PYTHON_LIBS
499 echo "${PYTHON_LIBS}"
500 }
501
502 # @FUNCTION: python_get_scriptdir
503 # @USAGE: [<impl>]
504 # @DESCRIPTION:
505 # Obtain and print the script install path for the given
506 # implementation. If no implementation is provided, ${EPYTHON} will
507 # be used.
508 python_get_scriptdir() {
509 debug-print-function ${FUNCNAME} "${@}"
510
511 python_export "${@}" PYTHON_SCRIPTDIR
512 echo "${PYTHON_SCRIPTDIR}"
513 }
514
515 # @FUNCTION: _python_rewrite_shebang
516 # @USAGE: [<EPYTHON>] <path>...
517 # @INTERNAL
518 # @DESCRIPTION:
519 # Replaces 'python' executable in the shebang with the executable name
520 # of the specified interpreter. If no EPYTHON value (implementation) is
521 # used, the current ${EPYTHON} will be used.
522 #
523 # All specified files must start with a 'python' shebang. A file not
524 # having a matching shebang will be refused. The exact shebang style
525 # will be preserved in order not to break anything.
526 #
527 # Example conversions:
528 # @CODE
529 # From: #!/usr/bin/python -R
530 # To: #!/usr/bin/python2.7 -R
531 #
532 # From: #!/usr/bin/env FOO=bar python
533 # To: #!/usr/bin/env FOO=bar python2.7
534 # @CODE
535 _python_rewrite_shebang() {
536 debug-print-function ${FUNCNAME} "${@}"
537
538 local impl
539 case "${1}" in
540 python*|jython*|pypy-c*)
541 impl=${1}
542 shift
543 ;;
544 *)
545 impl=${EPYTHON}
546 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
547 ;;
548 esac
549 debug-print "${FUNCNAME}: implementation: ${impl}"
550
551 local f
552 for f; do
553 local from shebang
554 read -r shebang < "${f}"
555 shebang=${shebang%$'\r'}
556 debug-print "${FUNCNAME}: path = ${f}"
557 debug-print "${FUNCNAME}: shebang = ${shebang}"
558
559 if [[ "${shebang} " == *'python '* ]]; then
560 from=python
561 elif [[ "${shebang} " == *'python2 '* ]]; then
562 from=python2
563 elif [[ "${shebang} " == *'python3 '* ]]; then
564 from=python3
565 else
566 eerror "A file does not seem to have a supported shebang:"
567 eerror " file: ${f}"
568 eerror " shebang: ${shebang}"
569 die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
570 fi
571
572 if { [[ ${from} == python2 ]] && python_is_python3 "${impl}"; } \
573 || { [[ ${from} == python3 ]] && ! python_is_python3 "${impl}"; } then
574 eerror "A file does have shebang not supporting requested impl:"
575 eerror " file: ${f}"
576 eerror " shebang: ${shebang}"
577 eerror " impl: ${impl}"
578 die "${FUNCNAME}: ${f} does have shebang not supporting ${EPYTHON}"
579 fi
580
581 sed -i -e "1s:${from}:${impl}:" "${f}" || die
582 done
583 }
584
585 # @FUNCTION: _python_ln_rel
586 # @USAGE: <from> <to>
587 # @INTERNAL
588 # @DESCRIPTION:
589 # Create a relative symlink.
590 _python_ln_rel() {
591 debug-print-function ${FUNCNAME} "${@}"
592
593 local target=${1}
594 local symname=${2}
595
596 local tgpath=${target%/*}/
597 local sympath=${symname%/*}/
598 local rel_target=
599
600 while [[ ${sympath} ]]; do
601 local tgseg= symseg=
602
603 while [[ ! ${tgseg} && ${tgpath} ]]; do
604 tgseg=${tgpath%%/*}
605 tgpath=${tgpath#${tgseg}/}
606 done
607
608 while [[ ! ${symseg} && ${sympath} ]]; do
609 symseg=${sympath%%/*}
610 sympath=${sympath#${symseg}/}
611 done
612
613 if [[ ${tgseg} != ${symseg} ]]; then
614 rel_target=../${rel_target}${tgseg:+${tgseg}/}
615 fi
616 done
617 rel_target+=${tgpath}${target##*/}
618
619 debug-print "${FUNCNAME}: ${symname} -> ${target}"
620 debug-print "${FUNCNAME}: rel_target = ${rel_target}"
621
622 ln -fs "${rel_target}" "${symname}"
623 }
624
625 # @FUNCTION: python_optimize
626 # @USAGE: [<directory>...]
627 # @DESCRIPTION:
628 # Compile and optimize Python modules in specified directories (absolute
629 # paths). If no directories are provided, the default system paths
630 # are used (prepended with ${D}).
631 python_optimize() {
632 debug-print-function ${FUNCNAME} "${@}"
633
634 if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
635 eerror "The new Python eclasses expect the compiled Python files to"
636 eerror "be controlled by the Package Manager. For this reason,"
637 eerror "the python_optimize function can be used only during src_* phases"
638 eerror "(src_install most commonly) and not during pkg_* phases."
639 echo
640 die "python_optimize is not to be used in pre/post* phases"
641 fi
642
643 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
644
645 local PYTHON=${PYTHON}
646 [[ ${PYTHON} ]] || python_export PYTHON
647
648 # Note: python2.6 can't handle passing files to compileall...
649
650 # default to sys.path
651 if [[ ${#} -eq 0 ]]; then
652 local f
653 while IFS= read -r -d '' f; do
654 # 1) accept only absolute paths
655 # (i.e. skip '', '.' or anything like that)
656 # 2) skip paths which do not exist
657 # (python2.6 complains about them verbosely)
658
659 if [[ ${f} == /* && -d ${D}${f} ]]; then
660 set -- "${D}${f}" "${@}"
661 fi
662 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))')
663
664 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
665 fi
666
667 local d
668 for d; do
669 # make sure to get a nice path without //
670 local instpath=${d#${D}}
671 instpath=/${instpath##/}
672
673 case "${EPYTHON}" in
674 python*)
675 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
676 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
677 ;;
678 *)
679 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
680 ;;
681 esac
682 done
683 }
684
685 # @ECLASS-VARIABLE: python_scriptroot
686 # @DEFAULT_UNSET
687 # @DESCRIPTION:
688 # The current script destination for python_doscript(). The path
689 # is relative to the installation root (${ED}).
690 #
691 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
692 #
693 # Can be set indirectly through the python_scriptinto() function.
694 #
695 # Example:
696 # @CODE
697 # src_install() {
698 # local python_scriptroot=${GAMES_BINDIR}
699 # python_foreach_impl python_doscript foo
700 # }
701 # @CODE
702
703 # @FUNCTION: python_scriptinto
704 # @USAGE: <new-path>
705 # @DESCRIPTION:
706 # Set the current scriptroot. The new value will be stored
707 # in the 'python_scriptroot' environment variable. The new value need
708 # be relative to the installation root (${ED}).
709 #
710 # Alternatively, you can set the variable directly.
711 python_scriptinto() {
712 debug-print-function ${FUNCNAME} "${@}"
713
714 python_scriptroot=${1}
715 }
716
717 # @FUNCTION: python_doscript
718 # @USAGE: <files>...
719 # @DESCRIPTION:
720 # Install the given scripts into current python_scriptroot,
721 # for the current Python implementation (${EPYTHON}).
722 #
723 # All specified files must start with a 'python' shebang. The shebang
724 # will be converted, the file will be renamed to be EPYTHON-suffixed
725 # and a wrapper will be installed in place of the original name.
726 #
727 # Example:
728 # @CODE
729 # src_install() {
730 # python_foreach_impl python_doscript ${PN}
731 # }
732 # @CODE
733 python_doscript() {
734 debug-print-function ${FUNCNAME} "${@}"
735
736 local f
737 for f; do
738 python_newscript "${f}" "${f##*/}"
739 done
740 }
741
742 # @FUNCTION: python_newscript
743 # @USAGE: <path> <new-name>
744 # @DESCRIPTION:
745 # Install the given script into current python_scriptroot
746 # for the current Python implementation (${EPYTHON}), and name it
747 # <new-name>.
748 #
749 # The file must start with a 'python' shebang. The shebang will be
750 # converted, the file will be renamed to be EPYTHON-suffixed
751 # and a wrapper will be installed in place of the <new-name>.
752 #
753 # Example:
754 # @CODE
755 # src_install() {
756 # python_foreach_impl python_newscript foo.py foo
757 # }
758 # @CODE
759 python_newscript() {
760 debug-print-function ${FUNCNAME} "${@}"
761
762 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
763 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
764
765 local d=${python_scriptroot:-${DESTTREE}/bin}
766 local wrapd=${d}
767
768 local f=${1}
769 local barefn=${2}
770 local newfn
771
772 if _python_want_python_exec2; then
773 local PYTHON_SCRIPTDIR
774 python_export PYTHON_SCRIPTDIR
775 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
776 newfn=${barefn}
777 else
778 newfn=${barefn}-${EPYTHON}
779 fi
780
781 (
782 dodir "${wrapd}"
783 exeinto "${d}"
784 newexe "${f}" "${newfn}" || die
785 )
786 _python_rewrite_shebang "${ED%/}/${d}/${newfn}"
787
788 # install the wrapper
789 _python_ln_rel "${ED%/}"$(_python_get_wrapper_path) \
790 "${ED%/}/${wrapd}/${barefn}" || die
791 }
792
793 # @ECLASS-VARIABLE: python_moduleroot
794 # @DEFAULT_UNSET
795 # @DESCRIPTION:
796 # The current module root for python_domodule(). The path can be either
797 # an absolute system path (it must start with a slash, and ${ED} will be
798 # prepended to it) or relative to the implementation's site-packages directory
799 # (then it must start with a non-slash character).
800 #
801 # When unset, the modules will be installed in the site-packages root.
802 #
803 # Can be set indirectly through the python_moduleinto() function.
804 #
805 # Example:
806 # @CODE
807 # src_install() {
808 # local python_moduleroot=bar
809 # # installs ${PYTHON_SITEDIR}/bar/baz.py
810 # python_foreach_impl python_domodule baz.py
811 # }
812 # @CODE
813
814 # @FUNCTION: python_moduleinto
815 # @USAGE: <new-path>
816 # @DESCRIPTION:
817 # Set the current module root. The new value will be stored
818 # in the 'python_moduleroot' environment variable. The new value need
819 # be relative to the site-packages root.
820 #
821 # Alternatively, you can set the variable directly.
822 python_moduleinto() {
823 debug-print-function ${FUNCNAME} "${@}"
824
825 python_moduleroot=${1}
826 }
827
828 # @FUNCTION: python_domodule
829 # @USAGE: <files>...
830 # @DESCRIPTION:
831 # Install the given modules (or packages) into the current
832 # python_moduleroot. The list can mention both modules (files)
833 # and packages (directories). All listed files will be installed
834 # for all enabled implementations, and compiled afterwards.
835 #
836 # Example:
837 # @CODE
838 # src_install() {
839 # # (${PN} being a directory)
840 # python_foreach_impl python_domodule ${PN}
841 # }
842 # @CODE
843 python_domodule() {
844 debug-print-function ${FUNCNAME} "${@}"
845
846 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
847
848 local d
849 if [[ ${python_moduleroot} == /* ]]; then
850 # absolute path
851 d=${python_moduleroot}
852 else
853 # relative to site-packages
854 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
855 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
856
857 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
858 fi
859
860 local INSDESTTREE
861
862 insinto "${d}"
863 doins -r "${@}" || die
864
865 python_optimize "${ED}/${d}"
866 }
867
868 # @FUNCTION: python_doheader
869 # @USAGE: <files>...
870 # @DESCRIPTION:
871 # Install the given headers into the implementation-specific include
872 # directory. This function is unconditionally recursive, i.e. you can
873 # pass directories instead of files.
874 #
875 # Example:
876 # @CODE
877 # src_install() {
878 # python_foreach_impl python_doheader foo.h bar.h
879 # }
880 # @CODE
881 python_doheader() {
882 debug-print-function ${FUNCNAME} "${@}"
883
884 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
885
886 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
887 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
888
889 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
890
891 local INSDESTTREE
892
893 insinto "${d}"
894 doins -r "${@}" || die
895 }
896
897 # @FUNCTION: python_wrapper_setup
898 # @USAGE: [<path> [<impl>]]
899 # @DESCRIPTION:
900 # Create proper 'python' executable and pkg-config wrappers
901 # (if available) in the directory named by <path>. Set up PATH
902 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
903 #
904 # The wrappers will be created for implementation named by <impl>,
905 # or for one named by ${EPYTHON} if no <impl> passed.
906 #
907 # If the named directory contains a python symlink already, it will
908 # be assumed to contain proper wrappers already and only environment
909 # setup will be done. If wrapper update is requested, the directory
910 # shall be removed first.
911 python_wrapper_setup() {
912 debug-print-function ${FUNCNAME} "${@}"
913
914 local workdir=${1:-${T}/${EPYTHON}}
915 local impl=${2:-${EPYTHON}}
916
917 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
918 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
919
920 if [[ ! -x ${workdir}/bin/python ]]; then
921 mkdir -p "${workdir}"/{bin,pkgconfig} || die
922
923 # Clean up, in case we were supposed to do a cheap update.
924 rm -f "${workdir}"/bin/python{,2,3,-config}
925 rm -f "${workdir}"/bin/2to3
926 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc
927
928 local EPYTHON PYTHON
929 python_export "${impl}" EPYTHON PYTHON
930
931 local pyver
932 if python_is_python3; then
933 pyver=3
934 else
935 pyver=2
936 fi
937
938 # Python interpreter
939 ln -s "${PYTHON}" "${workdir}"/bin/python || die
940 ln -s python "${workdir}"/bin/python${pyver} || die
941
942 local nonsupp=()
943
944 # CPython-specific
945 if [[ ${EPYTHON} == python* ]]; then
946 ln -s "${PYTHON}-config" "${workdir}"/bin/python-config || die
947
948 # Python 2.6+.
949 if [[ ${EPYTHON} != python2.5 ]]; then
950 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
951 else
952 nonsupp+=( 2to3 )
953 fi
954
955 # Python 2.7+.
956 if [[ ${EPYTHON} != python2.[56] ]]; then
957 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
958 "${workdir}"/pkgconfig/python.pc || die
959 else
960 # XXX?
961 ln -s /dev/null "${workdir}"/pkgconfig/python.pc || die
962 fi
963 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
964 else
965 nonsupp+=( 2to3 python-config )
966 fi
967
968 local x
969 for x in "${nonsupp[@]}"; do
970 cat >"${workdir}"/bin/${x} <<__EOF__
971 #!/bin/sh
972 echo "${x} is not supported by ${EPYTHON}" >&2
973 exit 1
974 __EOF__
975 chmod +x "${workdir}"/bin/${x} || die
976 done
977
978 # Now, set the environment.
979 # But note that ${workdir} may be shared with something else,
980 # and thus already on top of PATH.
981 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
982 PATH=${workdir}/bin${PATH:+:${PATH}}
983 fi
984 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
985 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
986 fi
987 export PATH PKG_CONFIG_PATH
988 fi
989 }
990
991 # @FUNCTION: python_is_python3
992 # @USAGE: [<impl>]
993 # @DESCRIPTION:
994 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
995 # (i.e. uses syntax and stdlib of Python 3.*).
996 #
997 # Returns 0 (true) if it is, 1 (false) otherwise.
998 python_is_python3() {
999 local impl=${1:-${EPYTHON}}
1000 [[ ${impl} ]] || die "python_is_python3: no impl nor EPYTHON"
1001
1002 [[ ${impl} == python3* ]]
1003 }
1004
1005 # @FUNCTION: _python_want_python_exec2
1006 # @INTERNAL
1007 # @DESCRIPTION:
1008 # Check whether we should be using python-exec:2.
1009 _python_want_python_exec2() {
1010 debug-print-function ${FUNCNAME} "${@}"
1011
1012 # EAPI 4 lacks slot operators, so just fix it on python-exec:2.
1013 [[ ${EAPI} == 4 ]] && return 0
1014
1015 # Check if we cached the result, or someone put an override.
1016 if [[ ! ${_PYTHON_WANT_PYTHON_EXEC2+1} ]]; then
1017 has_version 'dev-python/python-exec:2'
1018 _PYTHON_WANT_PYTHON_EXEC2=$(( ! ${?} ))
1019 fi
1020
1021 # Non-zero means 'yes', zero means 'no'.
1022 [[ ${_PYTHON_WANT_PYTHON_EXEC2} != 0 ]]
1023 }
1024
1025 # @FUNCTION: _python_get_wrapper_path
1026 # @INTERNAL
1027 # @DESCRIPTION:
1028 # Output path to proper python-exec slot.
1029 _python_get_wrapper_path() {
1030 debug-print-function ${FUNCNAME} "${@}"
1031
1032 if _python_want_python_exec2; then
1033 echo /usr/lib/python-exec/python-exec2
1034 else
1035 echo /usr/bin/python-exec
1036 fi
1037 }
1038
1039 _PYTHON_UTILS_R1=1
1040 fi

  ViewVC Help
Powered by ViewVC 1.1.20