/[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.45 - (show annotations) (download)
Wed Oct 30 19:14:02 2013 UTC (10 months, 3 weeks ago) by mgorny
Branch: MAIN
Changes since 1.44: +2 -2 lines
Switch the eclasses to use dev-lang/python-exec.

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.44 2013/10/27 07:27:52 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 python2.6)
353 PYTHON_PKG_DEP='>=dev-lang/python-2.6.8-r3:2.6';;
354 python2.7)
355 PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
356 python3.2)
357 PYTHON_PKG_DEP='>=dev-lang/python-3.2.5-r2:3.2';;
358 python3.3)
359 PYTHON_PKG_DEP='>=dev-lang/python-3.3.2-r2:3.3';;
360 pypy-c2.0)
361 PYTHON_PKG_DEP='>=virtual/pypy-2.0.2:2.0';;
362 jython2.5)
363 PYTHON_PKG_DEP='>=dev-java/jython-2.5.3-r2:2.5';;
364 jython2.7)
365 PYTHON_PKG_DEP='dev-java/jython:2.7';;
366 *)
367 die "Invalid implementation: ${impl}"
368 esac
369
370 # use-dep
371 if [[ ${PYTHON_REQ_USE} ]]; then
372 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
373 fi
374
375 export PYTHON_PKG_DEP
376 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
377 ;;
378 PYTHON_SCRIPTDIR)
379 local dir
380 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
381 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
382 ;;
383 *)
384 die "python_export: unknown variable ${var}"
385 esac
386 done
387 }
388
389 # @FUNCTION: python_get_sitedir
390 # @USAGE: [<impl>]
391 # @DESCRIPTION:
392 # Obtain and print the 'site-packages' path for the given
393 # implementation. If no implementation is provided, ${EPYTHON} will
394 # be used.
395 #
396 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
397 # better to use python_export() directly instead.
398 python_get_sitedir() {
399 debug-print-function ${FUNCNAME} "${@}"
400
401 python_export "${@}" PYTHON_SITEDIR
402 echo "${PYTHON_SITEDIR}"
403 }
404
405 # @FUNCTION: python_get_includedir
406 # @USAGE: [<impl>]
407 # @DESCRIPTION:
408 # Obtain and print the include path for the given implementation. If no
409 # implementation is provided, ${EPYTHON} will be used.
410 #
411 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
412 # is better to use python_export() directly instead.
413 python_get_includedir() {
414 debug-print-function ${FUNCNAME} "${@}"
415
416 python_export "${@}" PYTHON_INCLUDEDIR
417 echo "${PYTHON_INCLUDEDIR}"
418 }
419
420 # @FUNCTION: python_get_library_path
421 # @USAGE: [<impl>]
422 # @DESCRIPTION:
423 # Obtain and print the Python library path for the given implementation.
424 # If no implementation is provided, ${EPYTHON} will be used.
425 #
426 # Please note that this function can be used with CPython only. Use
427 # in another implementation will result in a fatal failure.
428 python_get_library_path() {
429 debug-print-function ${FUNCNAME} "${@}"
430
431 python_export "${@}" PYTHON_LIBPATH
432 echo "${PYTHON_LIBPATH}"
433 }
434
435 # @FUNCTION: python_get_CFLAGS
436 # @USAGE: [<impl>]
437 # @DESCRIPTION:
438 # Obtain and print the compiler flags for building against Python,
439 # for the given implementation. If no implementation is provided,
440 # ${EPYTHON} will be used.
441 #
442 # Please note that this function can be used with CPython only.
443 # It requires Python and pkg-config installed, and therefore proper
444 # build-time dependencies need be added to the ebuild.
445 python_get_CFLAGS() {
446 debug-print-function ${FUNCNAME} "${@}"
447
448 python_export "${@}" PYTHON_CFLAGS
449 echo "${PYTHON_CFLAGS}"
450 }
451
452 # @FUNCTION: python_get_LIBS
453 # @USAGE: [<impl>]
454 # @DESCRIPTION:
455 # Obtain and print the compiler flags for linking against Python,
456 # for the given implementation. If no implementation is provided,
457 # ${EPYTHON} will be used.
458 #
459 # Please note that this function can be used with CPython only.
460 # It requires Python and pkg-config installed, and therefore proper
461 # build-time dependencies need be added to the ebuild.
462 python_get_LIBS() {
463 debug-print-function ${FUNCNAME} "${@}"
464
465 python_export "${@}" PYTHON_LIBS
466 echo "${PYTHON_LIBS}"
467 }
468
469 # @FUNCTION: python_get_scriptdir
470 # @USAGE: [<impl>]
471 # @DESCRIPTION:
472 # Obtain and print the script install path for the given
473 # implementation. If no implementation is provided, ${EPYTHON} will
474 # be used.
475 python_get_scriptdir() {
476 debug-print-function ${FUNCNAME} "${@}"
477
478 python_export "${@}" PYTHON_SCRIPTDIR
479 echo "${PYTHON_SCRIPTDIR}"
480 }
481
482 # @FUNCTION: _python_rewrite_shebang
483 # @USAGE: [<EPYTHON>] <path>...
484 # @INTERNAL
485 # @DESCRIPTION:
486 # Replaces 'python' executable in the shebang with the executable name
487 # of the specified interpreter. If no EPYTHON value (implementation) is
488 # used, the current ${EPYTHON} will be used.
489 #
490 # All specified files must start with a 'python' shebang. A file not
491 # having a matching shebang will be refused. The exact shebang style
492 # will be preserved in order not to break anything.
493 #
494 # Example conversions:
495 # @CODE
496 # From: #!/usr/bin/python -R
497 # To: #!/usr/bin/python2.7 -R
498 #
499 # From: #!/usr/bin/env FOO=bar python
500 # To: #!/usr/bin/env FOO=bar python2.7
501 # @CODE
502 _python_rewrite_shebang() {
503 debug-print-function ${FUNCNAME} "${@}"
504
505 local impl
506 case "${1}" in
507 python*|jython*|pypy-c*)
508 impl=${1}
509 shift
510 ;;
511 *)
512 impl=${EPYTHON}
513 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
514 ;;
515 esac
516 debug-print "${FUNCNAME}: implementation: ${impl}"
517
518 local f
519 for f; do
520 local from shebang
521 read -r shebang < "${f}"
522 shebang=${shebang%$'\r'}
523 debug-print "${FUNCNAME}: path = ${f}"
524 debug-print "${FUNCNAME}: shebang = ${shebang}"
525
526 if [[ "${shebang} " == *'python '* ]]; then
527 from=python
528 elif [[ "${shebang} " == *'python2 '* ]]; then
529 from=python2
530 elif [[ "${shebang} " == *'python3 '* ]]; then
531 from=python3
532 else
533 eerror "A file does not seem to have a supported shebang:"
534 eerror " file: ${f}"
535 eerror " shebang: ${shebang}"
536 die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
537 fi
538
539 if { [[ ${from} == python2 ]] && python_is_python3 "${impl}"; } \
540 || { [[ ${from} == python3 ]] && ! python_is_python3 "${impl}"; } then
541 eerror "A file does have shebang not supporting requested impl:"
542 eerror " file: ${f}"
543 eerror " shebang: ${shebang}"
544 eerror " impl: ${impl}"
545 die "${FUNCNAME}: ${f} does have shebang not supporting ${EPYTHON}"
546 fi
547
548 sed -i -e "1s:${from}:${impl}:" "${f}" || die
549 done
550 }
551
552 # @FUNCTION: _python_ln_rel
553 # @USAGE: <from> <to>
554 # @INTERNAL
555 # @DESCRIPTION:
556 # Create a relative symlink.
557 _python_ln_rel() {
558 debug-print-function ${FUNCNAME} "${@}"
559
560 local target=${1}
561 local symname=${2}
562
563 local tgpath=${target%/*}/
564 local sympath=${symname%/*}/
565 local rel_target=
566
567 while [[ ${sympath} ]]; do
568 local tgseg= symseg=
569
570 while [[ ! ${tgseg} && ${tgpath} ]]; do
571 tgseg=${tgpath%%/*}
572 tgpath=${tgpath#${tgseg}/}
573 done
574
575 while [[ ! ${symseg} && ${sympath} ]]; do
576 symseg=${sympath%%/*}
577 sympath=${sympath#${symseg}/}
578 done
579
580 if [[ ${tgseg} != ${symseg} ]]; then
581 rel_target=../${rel_target}${tgseg:+${tgseg}/}
582 fi
583 done
584 rel_target+=${tgpath}${target##*/}
585
586 debug-print "${FUNCNAME}: ${symname} -> ${target}"
587 debug-print "${FUNCNAME}: rel_target = ${rel_target}"
588
589 ln -fs "${rel_target}" "${symname}"
590 }
591
592 # @FUNCTION: python_optimize
593 # @USAGE: [<directory>...]
594 # @DESCRIPTION:
595 # Compile and optimize Python modules in specified directories (absolute
596 # paths). If no directories are provided, the default system paths
597 # are used (prepended with ${D}).
598 python_optimize() {
599 debug-print-function ${FUNCNAME} "${@}"
600
601 if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
602 eerror "The new Python eclasses expect the compiled Python files to"
603 eerror "be controlled by the Package Manager. For this reason,"
604 eerror "the python_optimize function can be used only during src_* phases"
605 eerror "(src_install most commonly) and not during pkg_* phases."
606 echo
607 die "python_optimize is not to be used in pre/post* phases"
608 fi
609
610 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
611
612 local PYTHON=${PYTHON}
613 [[ ${PYTHON} ]] || python_export PYTHON
614
615 # Note: python2.6 can't handle passing files to compileall...
616
617 # default to sys.path
618 if [[ ${#} -eq 0 ]]; then
619 local f
620 while IFS= read -r -d '' f; do
621 # 1) accept only absolute paths
622 # (i.e. skip '', '.' or anything like that)
623 # 2) skip paths which do not exist
624 # (python2.6 complains about them verbosely)
625
626 if [[ ${f} == /* && -d ${D}${f} ]]; then
627 set -- "${D}${f}" "${@}"
628 fi
629 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))')
630
631 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
632 fi
633
634 local d
635 for d; do
636 # make sure to get a nice path without //
637 local instpath=${d#${D}}
638 instpath=/${instpath##/}
639
640 case "${EPYTHON}" in
641 python*)
642 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
643 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
644 ;;
645 *)
646 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
647 ;;
648 esac
649 done
650 }
651
652 # @ECLASS-VARIABLE: python_scriptroot
653 # @DEFAULT_UNSET
654 # @DESCRIPTION:
655 # The current script destination for python_doscript(). The path
656 # is relative to the installation root (${ED}).
657 #
658 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
659 #
660 # Can be set indirectly through the python_scriptinto() function.
661 #
662 # Example:
663 # @CODE
664 # src_install() {
665 # local python_scriptroot=${GAMES_BINDIR}
666 # python_foreach_impl python_doscript foo
667 # }
668 # @CODE
669
670 # @FUNCTION: python_scriptinto
671 # @USAGE: <new-path>
672 # @DESCRIPTION:
673 # Set the current scriptroot. The new value will be stored
674 # in the 'python_scriptroot' environment variable. The new value need
675 # be relative to the installation root (${ED}).
676 #
677 # Alternatively, you can set the variable directly.
678 python_scriptinto() {
679 debug-print-function ${FUNCNAME} "${@}"
680
681 python_scriptroot=${1}
682 }
683
684 # @FUNCTION: python_doscript
685 # @USAGE: <files>...
686 # @DESCRIPTION:
687 # Install the given scripts into current python_scriptroot,
688 # for the current Python implementation (${EPYTHON}).
689 #
690 # All specified files must start with a 'python' shebang. The shebang
691 # will be converted, the file will be renamed to be EPYTHON-suffixed
692 # and a wrapper will be installed in place of the original name.
693 #
694 # Example:
695 # @CODE
696 # src_install() {
697 # python_foreach_impl python_doscript ${PN}
698 # }
699 # @CODE
700 python_doscript() {
701 debug-print-function ${FUNCNAME} "${@}"
702
703 local f
704 for f; do
705 python_newscript "${f}" "${f##*/}"
706 done
707 }
708
709 # @FUNCTION: python_newscript
710 # @USAGE: <path> <new-name>
711 # @DESCRIPTION:
712 # Install the given script into current python_scriptroot
713 # for the current Python implementation (${EPYTHON}), and name it
714 # <new-name>.
715 #
716 # The file must start with a 'python' shebang. The shebang will be
717 # converted, the file will be renamed to be EPYTHON-suffixed
718 # and a wrapper will be installed in place of the <new-name>.
719 #
720 # Example:
721 # @CODE
722 # src_install() {
723 # python_foreach_impl python_newscript foo.py foo
724 # }
725 # @CODE
726 python_newscript() {
727 debug-print-function ${FUNCNAME} "${@}"
728
729 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
730 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
731
732 local d=${python_scriptroot:-${DESTTREE}/bin}
733 local wrapd=${d}
734
735 local f=${1}
736 local barefn=${2}
737 local newfn
738
739 if _python_want_python_exec2; then
740 local PYTHON_SCRIPTDIR
741 python_export PYTHON_SCRIPTDIR
742 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
743 newfn=${barefn}
744 else
745 newfn=${barefn}-${EPYTHON}
746 fi
747
748 (
749 dodir "${wrapd}"
750 exeinto "${d}"
751 newexe "${f}" "${newfn}" || die
752 )
753 _python_rewrite_shebang "${ED%/}/${d}/${newfn}"
754
755 # install the wrapper
756 _python_ln_rel "${ED%/}"$(_python_get_wrapper_path) \
757 "${ED%/}/${wrapd}/${barefn}" || die
758 }
759
760 # @ECLASS-VARIABLE: python_moduleroot
761 # @DEFAULT_UNSET
762 # @DESCRIPTION:
763 # The current module root for python_domodule(). The path can be either
764 # an absolute system path (it must start with a slash, and ${ED} will be
765 # prepended to it) or relative to the implementation's site-packages directory
766 # (then it must start with a non-slash character).
767 #
768 # When unset, the modules will be installed in the site-packages root.
769 #
770 # Can be set indirectly through the python_moduleinto() function.
771 #
772 # Example:
773 # @CODE
774 # src_install() {
775 # local python_moduleroot=bar
776 # # installs ${PYTHON_SITEDIR}/bar/baz.py
777 # python_foreach_impl python_domodule baz.py
778 # }
779 # @CODE
780
781 # @FUNCTION: python_moduleinto
782 # @USAGE: <new-path>
783 # @DESCRIPTION:
784 # Set the current module root. The new value will be stored
785 # in the 'python_moduleroot' environment variable. The new value need
786 # be relative to the site-packages root.
787 #
788 # Alternatively, you can set the variable directly.
789 python_moduleinto() {
790 debug-print-function ${FUNCNAME} "${@}"
791
792 python_moduleroot=${1}
793 }
794
795 # @FUNCTION: python_domodule
796 # @USAGE: <files>...
797 # @DESCRIPTION:
798 # Install the given modules (or packages) into the current
799 # python_moduleroot. The list can mention both modules (files)
800 # and packages (directories). All listed files will be installed
801 # for all enabled implementations, and compiled afterwards.
802 #
803 # Example:
804 # @CODE
805 # src_install() {
806 # # (${PN} being a directory)
807 # python_foreach_impl python_domodule ${PN}
808 # }
809 # @CODE
810 python_domodule() {
811 debug-print-function ${FUNCNAME} "${@}"
812
813 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
814
815 local d
816 if [[ ${python_moduleroot} == /* ]]; then
817 # absolute path
818 d=${python_moduleroot}
819 else
820 # relative to site-packages
821 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
822 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
823
824 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
825 fi
826
827 local INSDESTTREE
828
829 insinto "${d}"
830 doins -r "${@}" || die
831
832 python_optimize "${ED}/${d}"
833 }
834
835 # @FUNCTION: python_doheader
836 # @USAGE: <files>...
837 # @DESCRIPTION:
838 # Install the given headers into the implementation-specific include
839 # directory. This function is unconditionally recursive, i.e. you can
840 # pass directories instead of files.
841 #
842 # Example:
843 # @CODE
844 # src_install() {
845 # python_foreach_impl python_doheader foo.h bar.h
846 # }
847 # @CODE
848 python_doheader() {
849 debug-print-function ${FUNCNAME} "${@}"
850
851 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
852
853 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
854 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
855
856 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
857
858 local INSDESTTREE
859
860 insinto "${d}"
861 doins -r "${@}" || die
862 }
863
864 # @FUNCTION: python_wrapper_setup
865 # @USAGE: [<path> [<impl>]]
866 # @DESCRIPTION:
867 # Create proper 'python' executable and pkg-config wrappers
868 # (if available) in the directory named by <path>. Set up PATH
869 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
870 #
871 # The wrappers will be created for implementation named by <impl>,
872 # or for one named by ${EPYTHON} if no <impl> passed.
873 #
874 # If the named directory contains a python symlink already, it will
875 # be assumed to contain proper wrappers already and only environment
876 # setup will be done. If wrapper update is requested, the directory
877 # shall be removed first.
878 python_wrapper_setup() {
879 debug-print-function ${FUNCNAME} "${@}"
880
881 local workdir=${1:-${T}/${EPYTHON}}
882 local impl=${2:-${EPYTHON}}
883
884 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
885 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
886
887 if [[ ! -x ${workdir}/bin/python ]]; then
888 mkdir -p "${workdir}"/{bin,pkgconfig} || die
889
890 # Clean up, in case we were supposed to do a cheap update.
891 rm -f "${workdir}"/bin/python{,2,3,-config}
892 rm -f "${workdir}"/bin/2to3
893 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc
894
895 local EPYTHON PYTHON
896 python_export "${impl}" EPYTHON PYTHON
897
898 local pyver
899 if python_is_python3; then
900 pyver=3
901 else
902 pyver=2
903 fi
904
905 # Python interpreter
906 ln -s "${PYTHON}" "${workdir}"/bin/python || die
907 ln -s python "${workdir}"/bin/python${pyver} || die
908
909 local nonsupp=()
910
911 # CPython-specific
912 if [[ ${EPYTHON} == python* ]]; then
913 ln -s "${PYTHON}-config" "${workdir}"/bin/python-config || die
914
915 # Python 2.6+.
916 if [[ ${EPYTHON} != python2.5 ]]; then
917 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
918 else
919 nonsupp+=( 2to3 )
920 fi
921
922 # Python 2.7+.
923 if [[ ${EPYTHON} != python2.[56] ]]; then
924 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
925 "${workdir}"/pkgconfig/python.pc || die
926 else
927 # XXX?
928 ln -s /dev/null "${workdir}"/pkgconfig/python.pc || die
929 fi
930 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
931 else
932 nonsupp+=( 2to3 python-config )
933 fi
934
935 local x
936 for x in "${nonsupp[@]}"; do
937 cat >"${workdir}"/bin/${x} <<__EOF__
938 #!/bin/sh
939 echo "${x} is not supported by ${EPYTHON}" >&2
940 exit 1
941 __EOF__
942 chmod +x "${workdir}"/bin/${x} || die
943 done
944
945 # Now, set the environment.
946 # But note that ${workdir} may be shared with something else,
947 # and thus already on top of PATH.
948 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
949 PATH=${workdir}/bin${PATH:+:${PATH}}
950 fi
951 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
952 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
953 fi
954 export PATH PKG_CONFIG_PATH
955 fi
956 }
957
958 # @FUNCTION: python_is_python3
959 # @USAGE: [<impl>]
960 # @DESCRIPTION:
961 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
962 # (i.e. uses syntax and stdlib of Python 3.*).
963 #
964 # Returns 0 (true) if it is, 1 (false) otherwise.
965 python_is_python3() {
966 local impl=${1:-${EPYTHON}}
967 [[ ${impl} ]] || die "python_is_python3: no impl nor EPYTHON"
968
969 [[ ${impl} == python3* ]]
970 }
971
972 # @FUNCTION: _python_want_python_exec2
973 # @INTERNAL
974 # @DESCRIPTION:
975 # Check whether we should be using python-exec:2.
976 _python_want_python_exec2() {
977 debug-print-function ${FUNCNAME} "${@}"
978
979 # EAPI 4 lacks slot operators, so just fix it on python-exec:2.
980 [[ ${EAPI} == 4 ]] && return 0
981
982 # Check if we cached the result, or someone put an override.
983 if [[ ! ${_PYTHON_WANT_PYTHON_EXEC2+1} ]]; then
984 has_version 'dev-lang/python-exec:2'
985 _PYTHON_WANT_PYTHON_EXEC2=$(( ! ${?} ))
986 fi
987
988 # Non-zero means 'yes', zero means 'no'.
989 [[ ${_PYTHON_WANT_PYTHON_EXEC2} != 0 ]]
990 }
991
992 # @FUNCTION: _python_get_wrapper_path
993 # @INTERNAL
994 # @DESCRIPTION:
995 # Output path to proper python-exec slot.
996 _python_get_wrapper_path() {
997 debug-print-function ${FUNCNAME} "${@}"
998
999 if _python_want_python_exec2; then
1000 echo /usr/lib/python-exec/python-exec2
1001 else
1002 echo /usr/bin/python-exec
1003 fi
1004 }
1005
1006 _PYTHON_UTILS_R1=1
1007 fi

  ViewVC Help
Powered by ViewVC 1.1.20