/[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.53 - (show annotations) (download)
Tue Apr 8 16:05:30 2014 UTC (3 months, 3 weeks ago) by mgorny
Branch: MAIN
Changes since 1.52: +8 -22 lines
Disable pypy2_0 and clean up after it.

1 # Copyright 1999-2014 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.52 2014/03/13 08:10:46 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 # A 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 pypy
45 python3_2 python3_3 python3_4
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_[234]|jython2_[57])
70 return 0
71 ;;
72 pypy1_[89]|pypy2_0|python2_5|python3_1)
73 return 1
74 ;;
75 pypy)
76 if [[ ${EAPI:-0} == [01234] ]]; then
77 die "PyPy is supported in EAPI 5 and newer only."
78 fi
79 ;;
80 *)
81 die "Invalid implementation in PYTHON_COMPAT: ${impl}"
82 esac
83 }
84
85 # @ECLASS-VARIABLE: PYTHON
86 # @DEFAULT_UNSET
87 # @DESCRIPTION:
88 # The absolute path to the current Python interpreter.
89 #
90 # This variable is set automatically in the following contexts:
91 #
92 # python-r1: Set in functions called by python_foreach_impl() or after
93 # calling python_export_best().
94 #
95 # python-single-r1: Set after calling python-single-r1_pkg_setup().
96 #
97 # distutils-r1: Set within any of the python sub-phase functions.
98 #
99 # Example value:
100 # @CODE
101 # /usr/bin/python2.6
102 # @CODE
103
104 # @ECLASS-VARIABLE: EPYTHON
105 # @DEFAULT_UNSET
106 # @DESCRIPTION:
107 # The executable name of the current Python interpreter.
108 #
109 # This variable is set automatically in the following contexts:
110 #
111 # python-r1: Set in functions called by python_foreach_impl() or after
112 # calling python_export_best().
113 #
114 # python-single-r1: Set after calling python-single-r1_pkg_setup().
115 #
116 # distutils-r1: Set within any of the python sub-phase functions.
117 #
118 # Example value:
119 # @CODE
120 # python2.6
121 # @CODE
122
123 # @ECLASS-VARIABLE: PYTHON_SITEDIR
124 # @DEFAULT_UNSET
125 # @DESCRIPTION:
126 # The path to Python site-packages directory.
127 #
128 # Set and exported on request using python_export().
129 #
130 # Example value:
131 # @CODE
132 # /usr/lib64/python2.6/site-packages
133 # @CODE
134
135 # @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
136 # @DEFAULT_UNSET
137 # @DESCRIPTION:
138 # The path to Python include directory.
139 #
140 # Set and exported on request using python_export().
141 #
142 # Example value:
143 # @CODE
144 # /usr/include/python2.6
145 # @CODE
146
147 # @ECLASS-VARIABLE: PYTHON_LIBPATH
148 # @DEFAULT_UNSET
149 # @DESCRIPTION:
150 # The path to Python library.
151 #
152 # Set and exported on request using python_export().
153 # Valid only for CPython.
154 #
155 # Example value:
156 # @CODE
157 # /usr/lib64/libpython2.6.so
158 # @CODE
159
160 # @ECLASS-VARIABLE: PYTHON_CFLAGS
161 # @DEFAULT_UNSET
162 # @DESCRIPTION:
163 # Proper C compiler flags for building against Python. Obtained from
164 # pkg-config or python-config.
165 #
166 # Set and exported on request using python_export().
167 # Valid only for CPython. Requires a proper build-time dependency
168 # on the Python implementation and on pkg-config.
169 #
170 # Example value:
171 # @CODE
172 # -I/usr/include/python2.7
173 # @CODE
174
175 # @ECLASS-VARIABLE: PYTHON_LIBS
176 # @DEFAULT_UNSET
177 # @DESCRIPTION:
178 # Proper C compiler flags for linking against Python. Obtained from
179 # pkg-config or python-config.
180 #
181 # Set and exported on request using python_export().
182 # Valid only for CPython. Requires a proper build-time dependency
183 # on the Python implementation and on pkg-config.
184 #
185 # Example value:
186 # @CODE
187 # -lpython2.7
188 # @CODE
189
190 # @ECLASS-VARIABLE: PYTHON_PKG_DEP
191 # @DEFAULT_UNSET
192 # @DESCRIPTION:
193 # The complete dependency on a particular Python package as a string.
194 #
195 # Set and exported on request using python_export().
196 #
197 # Example value:
198 # @CODE
199 # dev-lang/python:2.7[xml]
200 # @CODE
201
202 # @ECLASS-VARIABLE: PYTHON_SCRIPTDIR
203 # @DEFAULT_UNSET
204 # @DESCRIPTION:
205 # The location where Python scripts must be installed for current impl.
206 #
207 # Set and exported on request using python_export().
208 #
209 # Example value:
210 # @CODE
211 # /usr/lib/python-exec/python2.7
212 # @CODE
213
214 # @FUNCTION: python_export
215 # @USAGE: [<impl>] <variables>...
216 # @DESCRIPTION:
217 # Set and export the Python implementation-relevant variables passed
218 # as parameters.
219 #
220 # The optional first parameter may specify the requested Python
221 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
222 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
223 # the current one will be obtained from ${EPYTHON}.
224 #
225 # The variables which can be exported are: PYTHON, EPYTHON,
226 # PYTHON_SITEDIR. They are described more completely in the eclass
227 # variable documentation.
228 python_export() {
229 debug-print-function ${FUNCNAME} "${@}"
230
231 local impl var
232
233 case "${1}" in
234 python*|jython*)
235 impl=${1/_/.}
236 shift
237 ;;
238 pypy)
239 impl=${1}
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*|pypy)
263 dir=/usr/$(get_libdir)/${impl}
264 ;;
265 jython*)
266 dir=/usr/share/${impl/n/n-}/Lib
267 ;;
268 esac
269
270 export PYTHON_SITEDIR=${EPREFIX}${dir}/site-packages
271 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
272 ;;
273 PYTHON_INCLUDEDIR)
274 local dir
275 case "${impl}" in
276 python*)
277 dir=/usr/include/${impl}
278 ;;
279 pypy)
280 dir=/usr/$(get_libdir)/${impl}/include
281 ;;
282 *)
283 die "${impl} lacks header files"
284 ;;
285 esac
286
287 export PYTHON_INCLUDEDIR=${EPREFIX}${dir}
288 debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
289 ;;
290 PYTHON_LIBPATH)
291 local libname
292 case "${impl}" in
293 python*)
294 libname=lib${impl}
295 ;;
296 *)
297 die "${impl} lacks a dynamic library"
298 ;;
299 esac
300
301 local path=${EPREFIX}/usr/$(get_libdir)
302
303 export PYTHON_LIBPATH=${path}/${libname}$(get_libname)
304 debug-print "${FUNCNAME}: PYTHON_LIBPATH = ${PYTHON_LIBPATH}"
305 ;;
306 PYTHON_CFLAGS)
307 local val
308
309 case "${impl}" in
310 python2.5|python2.6)
311 # old versions support python-config only
312 val=$("${impl}-config" --includes)
313 ;;
314 python*)
315 # python-2.7, python-3.2, etc.
316 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-})
317 ;;
318 *)
319 die "${impl}: obtaining ${var} not supported"
320 ;;
321 esac
322
323 export PYTHON_CFLAGS=${val}
324 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
325 ;;
326 PYTHON_LIBS)
327 local val
328
329 case "${impl}" in
330 python2.5|python2.6)
331 # old versions support python-config only
332 val=$("${impl}-config" --libs)
333 ;;
334 python*)
335 # python-2.7, python-3.2, etc.
336 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-})
337 ;;
338 *)
339 die "${impl}: obtaining ${var} not supported"
340 ;;
341 esac
342
343 export PYTHON_LIBS=${val}
344 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
345 ;;
346 PYTHON_PKG_DEP)
347 local d
348 case ${impl} in
349 python2.6)
350 PYTHON_PKG_DEP='>=dev-lang/python-2.6.8-r3:2.6';;
351 python2.7)
352 PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
353 python3.2)
354 PYTHON_PKG_DEP='>=dev-lang/python-3.2.5-r2:3.2';;
355 python3.3)
356 PYTHON_PKG_DEP='>=dev-lang/python-3.3.2-r2:3.3';;
357 python*)
358 PYTHON_PKG_DEP="dev-lang/python:${impl#python}";;
359 pypy)
360 PYTHON_PKG_DEP='virtual/pypy:0=';;
361 jython2.5)
362 PYTHON_PKG_DEP='>=dev-java/jython-2.5.3-r2:2.5';;
363 jython2.7)
364 PYTHON_PKG_DEP='dev-java/jython:2.7';;
365 *)
366 die "Invalid implementation: ${impl}"
367 esac
368
369 # use-dep
370 if [[ ${PYTHON_REQ_USE} ]]; then
371 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
372 fi
373
374 export PYTHON_PKG_DEP
375 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
376 ;;
377 PYTHON_SCRIPTDIR)
378 local dir
379 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
380 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
381 ;;
382 *)
383 die "python_export: unknown variable ${var}"
384 esac
385 done
386 }
387
388 # @FUNCTION: python_get_sitedir
389 # @USAGE: [<impl>]
390 # @DESCRIPTION:
391 # Obtain and print the 'site-packages' path for the given
392 # implementation. If no implementation is provided, ${EPYTHON} will
393 # be used.
394 #
395 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
396 # better to use python_export() directly instead.
397 python_get_sitedir() {
398 debug-print-function ${FUNCNAME} "${@}"
399
400 python_export "${@}" PYTHON_SITEDIR
401 echo "${PYTHON_SITEDIR}"
402 }
403
404 # @FUNCTION: python_get_includedir
405 # @USAGE: [<impl>]
406 # @DESCRIPTION:
407 # Obtain and print the include path for the given implementation. If no
408 # implementation is provided, ${EPYTHON} will be used.
409 #
410 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
411 # is better to use python_export() directly instead.
412 python_get_includedir() {
413 debug-print-function ${FUNCNAME} "${@}"
414
415 python_export "${@}" PYTHON_INCLUDEDIR
416 echo "${PYTHON_INCLUDEDIR}"
417 }
418
419 # @FUNCTION: python_get_library_path
420 # @USAGE: [<impl>]
421 # @DESCRIPTION:
422 # Obtain and print the Python library path for the given implementation.
423 # If no implementation is provided, ${EPYTHON} will be used.
424 #
425 # Please note that this function can be used with CPython only. Use
426 # in another implementation will result in a fatal failure.
427 python_get_library_path() {
428 debug-print-function ${FUNCNAME} "${@}"
429
430 python_export "${@}" PYTHON_LIBPATH
431 echo "${PYTHON_LIBPATH}"
432 }
433
434 # @FUNCTION: python_get_CFLAGS
435 # @USAGE: [<impl>]
436 # @DESCRIPTION:
437 # Obtain and print the compiler flags for building against Python,
438 # for the given implementation. If no implementation is provided,
439 # ${EPYTHON} will be used.
440 #
441 # Please note that this function can be used with CPython only.
442 # It requires Python and pkg-config installed, and therefore proper
443 # build-time dependencies need be added to the ebuild.
444 python_get_CFLAGS() {
445 debug-print-function ${FUNCNAME} "${@}"
446
447 python_export "${@}" PYTHON_CFLAGS
448 echo "${PYTHON_CFLAGS}"
449 }
450
451 # @FUNCTION: python_get_LIBS
452 # @USAGE: [<impl>]
453 # @DESCRIPTION:
454 # Obtain and print the compiler flags for linking against Python,
455 # for the given implementation. If no implementation is provided,
456 # ${EPYTHON} will be used.
457 #
458 # Please note that this function can be used with CPython only.
459 # It requires Python and pkg-config installed, and therefore proper
460 # build-time dependencies need be added to the ebuild.
461 python_get_LIBS() {
462 debug-print-function ${FUNCNAME} "${@}"
463
464 python_export "${@}" PYTHON_LIBS
465 echo "${PYTHON_LIBS}"
466 }
467
468 # @FUNCTION: python_get_scriptdir
469 # @USAGE: [<impl>]
470 # @DESCRIPTION:
471 # Obtain and print the script install path for the given
472 # implementation. If no implementation is provided, ${EPYTHON} will
473 # be used.
474 python_get_scriptdir() {
475 debug-print-function ${FUNCNAME} "${@}"
476
477 python_export "${@}" PYTHON_SCRIPTDIR
478 echo "${PYTHON_SCRIPTDIR}"
479 }
480
481 # @FUNCTION: _python_rewrite_shebang
482 # @USAGE: [<EPYTHON>] <path>...
483 # @INTERNAL
484 # @DESCRIPTION:
485 # Replaces 'python' executable in the shebang with the executable name
486 # of the specified interpreter. If no EPYTHON value (implementation) is
487 # used, the current ${EPYTHON} will be used.
488 #
489 # All specified files must start with a 'python' shebang. A file not
490 # having a matching shebang will be refused. The exact shebang style
491 # will be preserved in order not to break anything.
492 #
493 # Example conversions:
494 # @CODE
495 # From: #!/usr/bin/python -R
496 # To: #!/usr/bin/python2.7 -R
497 #
498 # From: #!/usr/bin/env FOO=bar python
499 # To: #!/usr/bin/env FOO=bar python2.7
500 # @CODE
501 _python_rewrite_shebang() {
502 debug-print-function ${FUNCNAME} "${@}"
503
504 local impl
505 case "${1}" in
506 python*|jython*|pypy*)
507 impl=${1}
508 shift
509 ;;
510 *)
511 impl=${EPYTHON}
512 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
513 ;;
514 esac
515 debug-print "${FUNCNAME}: implementation: ${impl}"
516
517 local f
518 for f; do
519 local from shebang
520 read -r shebang < "${f}"
521 shebang=${shebang%$'\r'}
522 debug-print "${FUNCNAME}: path = ${f}"
523 debug-print "${FUNCNAME}: shebang = ${shebang}"
524
525 if [[ "${shebang} " == *'python '* ]]; then
526 from=python
527 elif [[ "${shebang} " == *'python2 '* ]]; then
528 from=python2
529 elif [[ "${shebang} " == *'python3 '* ]]; then
530 from=python3
531 else
532 eerror "A file does not seem to have a supported shebang:"
533 eerror " file: ${f}"
534 eerror " shebang: ${shebang}"
535 die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
536 fi
537
538 if { [[ ${from} == python2 ]] && python_is_python3 "${impl}"; } \
539 || { [[ ${from} == python3 ]] && ! python_is_python3 "${impl}"; } then
540 eerror "A file does have shebang not supporting requested impl:"
541 eerror " file: ${f}"
542 eerror " shebang: ${shebang}"
543 eerror " impl: ${impl}"
544 die "${FUNCNAME}: ${f} does have shebang not supporting ${EPYTHON}"
545 fi
546
547 sed -i -e "1s:${from}:${impl}:" "${f}" || die
548 done
549 }
550
551 # @FUNCTION: _python_ln_rel
552 # @USAGE: <from> <to>
553 # @INTERNAL
554 # @DESCRIPTION:
555 # Create a relative symlink.
556 _python_ln_rel() {
557 debug-print-function ${FUNCNAME} "${@}"
558
559 local target=${1}
560 local symname=${2}
561
562 local tgpath=${target%/*}/
563 local sympath=${symname%/*}/
564 local rel_target=
565
566 while [[ ${sympath} ]]; do
567 local tgseg= symseg=
568
569 while [[ ! ${tgseg} && ${tgpath} ]]; do
570 tgseg=${tgpath%%/*}
571 tgpath=${tgpath#${tgseg}/}
572 done
573
574 while [[ ! ${symseg} && ${sympath} ]]; do
575 symseg=${sympath%%/*}
576 sympath=${sympath#${symseg}/}
577 done
578
579 if [[ ${tgseg} != ${symseg} ]]; then
580 rel_target=../${rel_target}${tgseg:+${tgseg}/}
581 fi
582 done
583 rel_target+=${tgpath}${target##*/}
584
585 debug-print "${FUNCNAME}: ${symname} -> ${target}"
586 debug-print "${FUNCNAME}: rel_target = ${rel_target}"
587
588 ln -fs "${rel_target}" "${symname}"
589 }
590
591 # @FUNCTION: python_optimize
592 # @USAGE: [<directory>...]
593 # @DESCRIPTION:
594 # Compile and optimize Python modules in specified directories (absolute
595 # paths). If no directories are provided, the default system paths
596 # are used (prepended with ${D}).
597 python_optimize() {
598 debug-print-function ${FUNCNAME} "${@}"
599
600 if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
601 eerror "The new Python eclasses expect the compiled Python files to"
602 eerror "be controlled by the Package Manager. For this reason,"
603 eerror "the python_optimize function can be used only during src_* phases"
604 eerror "(src_install most commonly) and not during pkg_* phases."
605 echo
606 die "python_optimize is not to be used in pre/post* phases"
607 fi
608
609 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
610
611 local PYTHON=${PYTHON}
612 [[ ${PYTHON} ]] || python_export PYTHON
613
614 # Note: python2.6 can't handle passing files to compileall...
615
616 # default to sys.path
617 if [[ ${#} -eq 0 ]]; then
618 local f
619 while IFS= read -r -d '' f; do
620 # 1) accept only absolute paths
621 # (i.e. skip '', '.' or anything like that)
622 # 2) skip paths which do not exist
623 # (python2.6 complains about them verbosely)
624
625 if [[ ${f} == /* && -d ${D}${f} ]]; then
626 set -- "${D}${f}" "${@}"
627 fi
628 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))')
629
630 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
631 fi
632
633 local d
634 for d; do
635 # make sure to get a nice path without //
636 local instpath=${d#${D}}
637 instpath=/${instpath##/}
638
639 case "${EPYTHON}" in
640 python*)
641 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
642 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
643 ;;
644 *)
645 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
646 ;;
647 esac
648 done
649 }
650
651 # @ECLASS-VARIABLE: python_scriptroot
652 # @DEFAULT_UNSET
653 # @DESCRIPTION:
654 # The current script destination for python_doscript(). The path
655 # is relative to the installation root (${ED}).
656 #
657 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
658 #
659 # Can be set indirectly through the python_scriptinto() function.
660 #
661 # Example:
662 # @CODE
663 # src_install() {
664 # local python_scriptroot=${GAMES_BINDIR}
665 # python_foreach_impl python_doscript foo
666 # }
667 # @CODE
668
669 # @FUNCTION: python_scriptinto
670 # @USAGE: <new-path>
671 # @DESCRIPTION:
672 # Set the current scriptroot. The new value will be stored
673 # in the 'python_scriptroot' environment variable. The new value need
674 # be relative to the installation root (${ED}).
675 #
676 # Alternatively, you can set the variable directly.
677 python_scriptinto() {
678 debug-print-function ${FUNCNAME} "${@}"
679
680 python_scriptroot=${1}
681 }
682
683 # @FUNCTION: python_doexe
684 # @USAGE: <files>...
685 # @DESCRIPTION:
686 # Install the given executables into current python_scriptroot,
687 # for the current Python implementation (${EPYTHON}).
688 #
689 # The executable will be wrapped properly for the Python implementation,
690 # though no shebang mangling will be performed.
691 python_doexe() {
692 debug-print-function ${FUNCNAME} "${@}"
693
694 local f
695 for f; do
696 python_newexe "${f}" "${f##*/}"
697 done
698 }
699
700 # @FUNCTION: python_newexe
701 # @USAGE: <path> <new-name>
702 # Install the given executable into current python_scriptroot,
703 # for the current Python implementation (${EPYTHON}).
704 #
705 # The executable will be wrapped properly for the Python implementation,
706 # though no shebang mangling will be performed. It will be renamed
707 # to <new-name>.
708 python_newexe() {
709 debug-print-function ${FUNCNAME} "${@}"
710
711 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
712 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
713
714 local d=${python_scriptroot:-${DESTTREE}/bin}
715 local wrapd=${d}
716
717 local f=${1}
718 local barefn=${2}
719 local newfn
720
721 if _python_want_python_exec2; then
722 local PYTHON_SCRIPTDIR
723 python_export PYTHON_SCRIPTDIR
724 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
725 newfn=${barefn}
726 else
727 newfn=${barefn}-${EPYTHON}
728 fi
729
730 (
731 dodir "${wrapd}"
732 exeinto "${d}"
733 newexe "${f}" "${newfn}" || die
734 )
735
736 # install the wrapper
737 _python_ln_rel "${ED%/}"$(_python_get_wrapper_path) \
738 "${ED%/}/${wrapd}/${barefn}" || die
739
740 # don't use this at home, just call python_doscript() instead
741 if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
742 _python_rewrite_shebang "${ED%/}/${d}/${newfn}"
743 fi
744 }
745
746 # @FUNCTION: python_doscript
747 # @USAGE: <files>...
748 # @DESCRIPTION:
749 # Install the given scripts into current python_scriptroot,
750 # for the current Python implementation (${EPYTHON}).
751 #
752 # All specified files must start with a 'python' shebang. The shebang
753 # will be converted, and the files will be wrapped properly
754 # for the Python implementation.
755 #
756 # Example:
757 # @CODE
758 # src_install() {
759 # python_foreach_impl python_doscript ${PN}
760 # }
761 # @CODE
762 python_doscript() {
763 debug-print-function ${FUNCNAME} "${@}"
764
765 local _PYTHON_REWRITE_SHEBANG=1
766 python_doexe "${@}"
767 }
768
769 # @FUNCTION: python_newscript
770 # @USAGE: <path> <new-name>
771 # @DESCRIPTION:
772 # Install the given script into current python_scriptroot
773 # for the current Python implementation (${EPYTHON}), and name it
774 # <new-name>.
775 #
776 # The file must start with a 'python' shebang. The shebang will be
777 # converted, and the file will be wrapped properly for the Python
778 # implementation. It will be renamed to <new-name>.
779 #
780 # Example:
781 # @CODE
782 # src_install() {
783 # python_foreach_impl python_newscript foo.py foo
784 # }
785 # @CODE
786 python_newscript() {
787 debug-print-function ${FUNCNAME} "${@}"
788
789 local _PYTHON_REWRITE_SHEBANG=1
790 python_newexe "${@}"
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-lang/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