/[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.60 - (show annotations) (download)
Sun Jul 6 11:45:20 2014 UTC (3 months, 2 weeks ago) by mgorny
Branch: MAIN
Changes since 1.59: +13 -31 lines
Disable python2.6 support and clean up the related code.

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.59 2014/06/29 14:24:22 floppym 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_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_7|python3_[234]|jython2_[57])
70 return 0
71 ;;
72 pypy1_[89]|pypy2_0|python2_[56]|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.7
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.7
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.7/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.7
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.7.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 python*)
311 # python-2.7, python-3.2, etc.
312 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-})
313 ;;
314 *)
315 die "${impl}: obtaining ${var} not supported"
316 ;;
317 esac
318
319 export PYTHON_CFLAGS=${val}
320 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
321 ;;
322 PYTHON_LIBS)
323 local val
324
325 case "${impl}" in
326 python*)
327 # python-2.7, python-3.2, etc.
328 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-})
329 ;;
330 *)
331 die "${impl}: obtaining ${var} not supported"
332 ;;
333 esac
334
335 export PYTHON_LIBS=${val}
336 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
337 ;;
338 PYTHON_PKG_DEP)
339 local d
340 case ${impl} in
341 python2.7)
342 PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
343 python3.2)
344 PYTHON_PKG_DEP='>=dev-lang/python-3.2.5-r2:3.2';;
345 python3.3)
346 PYTHON_PKG_DEP='>=dev-lang/python-3.3.2-r2:3.3';;
347 python*)
348 PYTHON_PKG_DEP="dev-lang/python:${impl#python}";;
349 pypy)
350 PYTHON_PKG_DEP='virtual/pypy:0=';;
351 jython2.5)
352 PYTHON_PKG_DEP='>=dev-java/jython-2.5.3-r2:2.5';;
353 jython2.7)
354 PYTHON_PKG_DEP='dev-java/jython:2.7';;
355 *)
356 die "Invalid implementation: ${impl}"
357 esac
358
359 # use-dep
360 if [[ ${PYTHON_REQ_USE} ]]; then
361 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
362 fi
363
364 export PYTHON_PKG_DEP
365 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
366 ;;
367 PYTHON_SCRIPTDIR)
368 local dir
369 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
370 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
371 ;;
372 *)
373 die "python_export: unknown variable ${var}"
374 esac
375 done
376 }
377
378 # @FUNCTION: python_get_sitedir
379 # @USAGE: [<impl>]
380 # @DESCRIPTION:
381 # Obtain and print the 'site-packages' path for the given
382 # implementation. If no implementation is provided, ${EPYTHON} will
383 # be used.
384 #
385 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
386 # better to use python_export() directly instead.
387 python_get_sitedir() {
388 debug-print-function ${FUNCNAME} "${@}"
389
390 python_export "${@}" PYTHON_SITEDIR
391 echo "${PYTHON_SITEDIR}"
392 }
393
394 # @FUNCTION: python_get_includedir
395 # @USAGE: [<impl>]
396 # @DESCRIPTION:
397 # Obtain and print the include path for the given implementation. If no
398 # implementation is provided, ${EPYTHON} will be used.
399 #
400 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
401 # is better to use python_export() directly instead.
402 python_get_includedir() {
403 debug-print-function ${FUNCNAME} "${@}"
404
405 python_export "${@}" PYTHON_INCLUDEDIR
406 echo "${PYTHON_INCLUDEDIR}"
407 }
408
409 # @FUNCTION: python_get_library_path
410 # @USAGE: [<impl>]
411 # @DESCRIPTION:
412 # Obtain and print the Python library path for the given implementation.
413 # If no implementation is provided, ${EPYTHON} will be used.
414 #
415 # Please note that this function can be used with CPython only. Use
416 # in another implementation will result in a fatal failure.
417 python_get_library_path() {
418 debug-print-function ${FUNCNAME} "${@}"
419
420 python_export "${@}" PYTHON_LIBPATH
421 echo "${PYTHON_LIBPATH}"
422 }
423
424 # @FUNCTION: python_get_CFLAGS
425 # @USAGE: [<impl>]
426 # @DESCRIPTION:
427 # Obtain and print the compiler flags for building against Python,
428 # for the given implementation. If no implementation is provided,
429 # ${EPYTHON} will be used.
430 #
431 # Please note that this function can be used with CPython only.
432 # It requires Python and pkg-config installed, and therefore proper
433 # build-time dependencies need be added to the ebuild.
434 python_get_CFLAGS() {
435 debug-print-function ${FUNCNAME} "${@}"
436
437 python_export "${@}" PYTHON_CFLAGS
438 echo "${PYTHON_CFLAGS}"
439 }
440
441 # @FUNCTION: python_get_LIBS
442 # @USAGE: [<impl>]
443 # @DESCRIPTION:
444 # Obtain and print the compiler flags for linking against Python,
445 # for the given implementation. If no implementation is provided,
446 # ${EPYTHON} will be used.
447 #
448 # Please note that this function can be used with CPython only.
449 # It requires Python and pkg-config installed, and therefore proper
450 # build-time dependencies need be added to the ebuild.
451 python_get_LIBS() {
452 debug-print-function ${FUNCNAME} "${@}"
453
454 python_export "${@}" PYTHON_LIBS
455 echo "${PYTHON_LIBS}"
456 }
457
458 # @FUNCTION: python_get_scriptdir
459 # @USAGE: [<impl>]
460 # @DESCRIPTION:
461 # Obtain and print the script install path for the given
462 # implementation. If no implementation is provided, ${EPYTHON} will
463 # be used.
464 python_get_scriptdir() {
465 debug-print-function ${FUNCNAME} "${@}"
466
467 python_export "${@}" PYTHON_SCRIPTDIR
468 echo "${PYTHON_SCRIPTDIR}"
469 }
470
471 # @FUNCTION: _python_ln_rel
472 # @USAGE: <from> <to>
473 # @INTERNAL
474 # @DESCRIPTION:
475 # Create a relative symlink.
476 _python_ln_rel() {
477 debug-print-function ${FUNCNAME} "${@}"
478
479 local target=${1}
480 local symname=${2}
481
482 local tgpath=${target%/*}/
483 local sympath=${symname%/*}/
484 local rel_target=
485
486 while [[ ${sympath} ]]; do
487 local tgseg= symseg=
488
489 while [[ ! ${tgseg} && ${tgpath} ]]; do
490 tgseg=${tgpath%%/*}
491 tgpath=${tgpath#${tgseg}/}
492 done
493
494 while [[ ! ${symseg} && ${sympath} ]]; do
495 symseg=${sympath%%/*}
496 sympath=${sympath#${symseg}/}
497 done
498
499 if [[ ${tgseg} != ${symseg} ]]; then
500 rel_target=../${rel_target}${tgseg:+${tgseg}/}
501 fi
502 done
503 rel_target+=${tgpath}${target##*/}
504
505 debug-print "${FUNCNAME}: ${symname} -> ${target}"
506 debug-print "${FUNCNAME}: rel_target = ${rel_target}"
507
508 ln -fs "${rel_target}" "${symname}"
509 }
510
511 # @FUNCTION: python_optimize
512 # @USAGE: [<directory>...]
513 # @DESCRIPTION:
514 # Compile and optimize Python modules in specified directories (absolute
515 # paths). If no directories are provided, the default system paths
516 # are used (prepended with ${D}).
517 python_optimize() {
518 debug-print-function ${FUNCNAME} "${@}"
519
520 if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
521 eerror "The new Python eclasses expect the compiled Python files to"
522 eerror "be controlled by the Package Manager. For this reason,"
523 eerror "the python_optimize function can be used only during src_* phases"
524 eerror "(src_install most commonly) and not during pkg_* phases."
525 echo
526 die "python_optimize is not to be used in pre/post* phases"
527 fi
528
529 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
530
531 local PYTHON=${PYTHON}
532 [[ ${PYTHON} ]] || python_export PYTHON
533
534 # Note: python2.6 can't handle passing files to compileall...
535 # TODO: we do not support 2.6 any longer
536
537 # default to sys.path
538 if [[ ${#} -eq 0 ]]; then
539 local f
540 while IFS= read -r -d '' f; do
541 # 1) accept only absolute paths
542 # (i.e. skip '', '.' or anything like that)
543 # 2) skip paths which do not exist
544 # (python2.6 complains about them verbosely)
545
546 if [[ ${f} == /* && -d ${D}${f} ]]; then
547 set -- "${D}${f}" "${@}"
548 fi
549 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))')
550
551 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
552 fi
553
554 local d
555 for d; do
556 # make sure to get a nice path without //
557 local instpath=${d#${D}}
558 instpath=/${instpath##/}
559
560 case "${EPYTHON}" in
561 python*)
562 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
563 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
564 ;;
565 *)
566 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
567 ;;
568 esac
569 done
570 }
571
572 # @ECLASS-VARIABLE: python_scriptroot
573 # @DEFAULT_UNSET
574 # @DESCRIPTION:
575 # The current script destination for python_doscript(). The path
576 # is relative to the installation root (${ED}).
577 #
578 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
579 #
580 # Can be set indirectly through the python_scriptinto() function.
581 #
582 # Example:
583 # @CODE
584 # src_install() {
585 # local python_scriptroot=${GAMES_BINDIR}
586 # python_foreach_impl python_doscript foo
587 # }
588 # @CODE
589
590 # @FUNCTION: python_scriptinto
591 # @USAGE: <new-path>
592 # @DESCRIPTION:
593 # Set the current scriptroot. The new value will be stored
594 # in the 'python_scriptroot' environment variable. The new value need
595 # be relative to the installation root (${ED}).
596 #
597 # Alternatively, you can set the variable directly.
598 python_scriptinto() {
599 debug-print-function ${FUNCNAME} "${@}"
600
601 python_scriptroot=${1}
602 }
603
604 # @FUNCTION: python_doexe
605 # @USAGE: <files>...
606 # @DESCRIPTION:
607 # Install the given executables into current python_scriptroot,
608 # for the current Python implementation (${EPYTHON}).
609 #
610 # The executable will be wrapped properly for the Python implementation,
611 # though no shebang mangling will be performed.
612 python_doexe() {
613 debug-print-function ${FUNCNAME} "${@}"
614
615 local f
616 for f; do
617 python_newexe "${f}" "${f##*/}"
618 done
619 }
620
621 # @FUNCTION: python_newexe
622 # @USAGE: <path> <new-name>
623 # @DESCRIPTION:
624 # Install the given executable into current python_scriptroot,
625 # for the current Python implementation (${EPYTHON}).
626 #
627 # The executable will be wrapped properly for the Python implementation,
628 # though no shebang mangling will be performed. It will be renamed
629 # to <new-name>.
630 python_newexe() {
631 debug-print-function ${FUNCNAME} "${@}"
632
633 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
634 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
635
636 local d=${python_scriptroot:-${DESTTREE}/bin}
637 local wrapd=${d}
638
639 local f=${1}
640 local barefn=${2}
641 local newfn
642
643 if _python_want_python_exec2; then
644 local PYTHON_SCRIPTDIR
645 python_export PYTHON_SCRIPTDIR
646 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
647 newfn=${barefn}
648 else
649 newfn=${barefn}-${EPYTHON}
650 fi
651
652 (
653 dodir "${wrapd}"
654 exeinto "${d}"
655 newexe "${f}" "${newfn}" || die
656 )
657
658 # install the wrapper
659 _python_ln_rel "${ED%/}"$(_python_get_wrapper_path) \
660 "${ED%/}/${wrapd}/${barefn}" || die
661
662 # don't use this at home, just call python_doscript() instead
663 if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
664 python_fix_shebang -q "${ED%/}/${d}/${newfn}"
665 fi
666 }
667
668 # @FUNCTION: python_doscript
669 # @USAGE: <files>...
670 # @DESCRIPTION:
671 # Install the given scripts into current python_scriptroot,
672 # for the current Python implementation (${EPYTHON}).
673 #
674 # All specified files must start with a 'python' shebang. The shebang
675 # will be converted, and the files will be wrapped properly
676 # for the Python implementation.
677 #
678 # Example:
679 # @CODE
680 # src_install() {
681 # python_foreach_impl python_doscript ${PN}
682 # }
683 # @CODE
684 python_doscript() {
685 debug-print-function ${FUNCNAME} "${@}"
686
687 local _PYTHON_REWRITE_SHEBANG=1
688 python_doexe "${@}"
689 }
690
691 # @FUNCTION: python_newscript
692 # @USAGE: <path> <new-name>
693 # @DESCRIPTION:
694 # Install the given script into current python_scriptroot
695 # for the current Python implementation (${EPYTHON}), and name it
696 # <new-name>.
697 #
698 # The file must start with a 'python' shebang. The shebang will be
699 # converted, and the file will be wrapped properly for the Python
700 # implementation. It will be renamed to <new-name>.
701 #
702 # Example:
703 # @CODE
704 # src_install() {
705 # python_foreach_impl python_newscript foo.py foo
706 # }
707 # @CODE
708 python_newscript() {
709 debug-print-function ${FUNCNAME} "${@}"
710
711 local _PYTHON_REWRITE_SHEBANG=1
712 python_newexe "${@}"
713 }
714
715 # @ECLASS-VARIABLE: python_moduleroot
716 # @DEFAULT_UNSET
717 # @DESCRIPTION:
718 # The current module root for python_domodule(). The path can be either
719 # an absolute system path (it must start with a slash, and ${ED} will be
720 # prepended to it) or relative to the implementation's site-packages directory
721 # (then it must start with a non-slash character).
722 #
723 # When unset, the modules will be installed in the site-packages root.
724 #
725 # Can be set indirectly through the python_moduleinto() function.
726 #
727 # Example:
728 # @CODE
729 # src_install() {
730 # local python_moduleroot=bar
731 # # installs ${PYTHON_SITEDIR}/bar/baz.py
732 # python_foreach_impl python_domodule baz.py
733 # }
734 # @CODE
735
736 # @FUNCTION: python_moduleinto
737 # @USAGE: <new-path>
738 # @DESCRIPTION:
739 # Set the current module root. The new value will be stored
740 # in the 'python_moduleroot' environment variable. The new value need
741 # be relative to the site-packages root.
742 #
743 # Alternatively, you can set the variable directly.
744 python_moduleinto() {
745 debug-print-function ${FUNCNAME} "${@}"
746
747 python_moduleroot=${1}
748 }
749
750 # @FUNCTION: python_domodule
751 # @USAGE: <files>...
752 # @DESCRIPTION:
753 # Install the given modules (or packages) into the current
754 # python_moduleroot. The list can mention both modules (files)
755 # and packages (directories). All listed files will be installed
756 # for all enabled implementations, and compiled afterwards.
757 #
758 # Example:
759 # @CODE
760 # src_install() {
761 # # (${PN} being a directory)
762 # python_foreach_impl python_domodule ${PN}
763 # }
764 # @CODE
765 python_domodule() {
766 debug-print-function ${FUNCNAME} "${@}"
767
768 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
769
770 local d
771 if [[ ${python_moduleroot} == /* ]]; then
772 # absolute path
773 d=${python_moduleroot}
774 else
775 # relative to site-packages
776 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
777 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
778
779 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
780 fi
781
782 local INSDESTTREE
783
784 insinto "${d}"
785 doins -r "${@}" || die
786
787 python_optimize "${ED}/${d}"
788 }
789
790 # @FUNCTION: python_doheader
791 # @USAGE: <files>...
792 # @DESCRIPTION:
793 # Install the given headers into the implementation-specific include
794 # directory. This function is unconditionally recursive, i.e. you can
795 # pass directories instead of files.
796 #
797 # Example:
798 # @CODE
799 # src_install() {
800 # python_foreach_impl python_doheader foo.h bar.h
801 # }
802 # @CODE
803 python_doheader() {
804 debug-print-function ${FUNCNAME} "${@}"
805
806 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
807
808 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
809 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
810
811 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
812
813 local INSDESTTREE
814
815 insinto "${d}"
816 doins -r "${@}" || die
817 }
818
819 # @FUNCTION: python_wrapper_setup
820 # @USAGE: [<path> [<impl>]]
821 # @DESCRIPTION:
822 # Create proper 'python' executable and pkg-config wrappers
823 # (if available) in the directory named by <path>. Set up PATH
824 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
825 #
826 # The wrappers will be created for implementation named by <impl>,
827 # or for one named by ${EPYTHON} if no <impl> passed.
828 #
829 # If the named directory contains a python symlink already, it will
830 # be assumed to contain proper wrappers already and only environment
831 # setup will be done. If wrapper update is requested, the directory
832 # shall be removed first.
833 python_wrapper_setup() {
834 debug-print-function ${FUNCNAME} "${@}"
835
836 local workdir=${1:-${T}/${EPYTHON}}
837 local impl=${2:-${EPYTHON}}
838
839 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
840 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
841
842 if [[ ! -x ${workdir}/bin/python ]]; then
843 mkdir -p "${workdir}"/{bin,pkgconfig} || die
844
845 # Clean up, in case we were supposed to do a cheap update.
846 rm -f "${workdir}"/bin/python{,2,3,-config}
847 rm -f "${workdir}"/bin/2to3
848 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc
849
850 local EPYTHON PYTHON
851 python_export "${impl}" EPYTHON PYTHON
852
853 local pyver
854 if python_is_python3; then
855 pyver=3
856 else
857 pyver=2
858 fi
859
860 # Python interpreter
861 ln -s "${PYTHON}" "${workdir}"/bin/python || die
862 ln -s python "${workdir}"/bin/python${pyver} || die
863
864 local nonsupp=()
865
866 # CPython-specific
867 if [[ ${EPYTHON} == python* ]]; then
868 ln -s "${PYTHON}-config" "${workdir}"/bin/python-config || die
869
870 # Python 2.6+.
871 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
872
873 # Python 2.7+.
874 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
875 "${workdir}"/pkgconfig/python.pc || die
876 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
877 else
878 nonsupp+=( 2to3 python-config )
879 fi
880
881 local x
882 for x in "${nonsupp[@]}"; do
883 cat >"${workdir}"/bin/${x} <<__EOF__
884 #!/bin/sh
885 echo "${x} is not supported by ${EPYTHON}" >&2
886 exit 1
887 __EOF__
888 chmod +x "${workdir}"/bin/${x} || die
889 done
890
891 # Now, set the environment.
892 # But note that ${workdir} may be shared with something else,
893 # and thus already on top of PATH.
894 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
895 PATH=${workdir}/bin${PATH:+:${PATH}}
896 fi
897 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
898 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
899 fi
900 export PATH PKG_CONFIG_PATH
901 fi
902 }
903
904 # @FUNCTION: python_is_python3
905 # @USAGE: [<impl>]
906 # @DESCRIPTION:
907 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
908 # (i.e. uses syntax and stdlib of Python 3.*).
909 #
910 # Returns 0 (true) if it is, 1 (false) otherwise.
911 python_is_python3() {
912 local impl=${1:-${EPYTHON}}
913 [[ ${impl} ]] || die "python_is_python3: no impl nor EPYTHON"
914
915 [[ ${impl} == python3* ]]
916 }
917
918 # @FUNCTION: python_fix_shebang
919 # @USAGE: [-f|--force] [-q|--quiet] <path>...
920 # @DESCRIPTION:
921 # Replace the shebang in Python scripts with the current Python
922 # implementation (EPYTHON). If a directory is passed, works recursively
923 # on all Python scripts.
924 #
925 # Only files having a 'python*' shebang will be modified. Files with
926 # other shebang will either be skipped when working recursively
927 # on a directory or treated as error when specified explicitly.
928 #
929 # Shebangs matching explicitly current Python version will be left
930 # unmodified. Shebangs requesting another Python version will be treated
931 # as fatal error, unless --force is given.
932 #
933 # --force causes the function to replace even shebangs that require
934 # incompatible Python version. --quiet causes the function not to list
935 # modified files verbosely.
936 python_fix_shebang() {
937 debug-print-function ${FUNCNAME} "${@}"
938
939 [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
940
941 local force quiet
942 while [[ ${@} ]]; do
943 case "${1}" in
944 -f|--force) force=1; shift;;
945 -q|--quiet) quiet=1; shift;;
946 --) shift; break;;
947 *) break;;
948 esac
949 done
950
951 [[ ${1} ]] || die "${FUNCNAME}: no paths given"
952
953 local path f
954 for path; do
955 local any_correct any_fixed is_recursive
956
957 [[ -d ${path} ]] && is_recursive=1
958
959 while IFS= read -r -d '' f; do
960 local shebang i
961 local error= from=
962
963 read shebang <"${f}"
964
965 # First, check if it's shebang at all...
966 if [[ ${shebang} == '#!'* ]]; then
967 # Match left-to-right in a loop, to avoid matching random
968 # repetitions like 'python2.7 python2'.
969 for i in ${shebang}; do
970 case "${i}" in
971 *"${EPYTHON}")
972 debug-print "${FUNCNAME}: in file ${f#${D}}"
973 debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
974
975 # Nothing to do, move along.
976 any_correct=1
977 from=${EPYTHON}
978 break
979 ;;
980 *python|*python[23])
981 debug-print "${FUNCNAME}: in file ${f#${D}}"
982 debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
983
984 if [[ ${i} == *python2 ]]; then
985 from=python2
986 if [[ ! ${force} ]]; then
987 python_is_python3 "${EPYTHON}" && error=1
988 fi
989 elif [[ ${i} == *python3 ]]; then
990 from=python3
991 if [[ ! ${force} ]]; then
992 python_is_python3 "${EPYTHON}" || error=1
993 fi
994 else
995 from=python
996 fi
997 break
998 ;;
999 *python[23].[0123456789]|*pypy|*jython[23].[0123456789])
1000 # Explicit mismatch.
1001 if [[ ! ${force} ]]; then
1002 error=1
1003 else
1004 case "${i}" in
1005 *python[23].[0123456789])
1006 from="python[23].[0123456789]";;
1007 *pypy)
1008 from="pypy";;
1009 *jython[23].[0123456789])
1010 from="jython[23].[0123456789]";;
1011 *)
1012 die "${FUNCNAME}: internal error in 2nd pattern match";;
1013 esac
1014 fi
1015 break
1016 ;;
1017 esac
1018 done
1019 fi
1020
1021 if [[ ! ${error} && ! ${from} ]]; then
1022 # Non-Python shebang. Allowed in recursive mode,
1023 # disallowed when specifying file explicitly.
1024 [[ ${is_recursive} ]] && continue
1025 error=1
1026 fi
1027
1028 if [[ ! ${quiet} ]]; then
1029 einfo "Fixing shebang in ${f#${D}}."
1030 fi
1031
1032 if [[ ! ${error} ]]; then
1033 # We either want to match ${from} followed by space
1034 # or at end-of-string.
1035 if [[ ${shebang} == *${from}" "* ]]; then
1036 sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die
1037 else
1038 sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
1039 fi
1040 any_fixed=1
1041 else
1042 eerror "The file has incompatible shebang:"
1043 eerror " file: ${f#${D}}"
1044 eerror " current shebang: ${shebang}"
1045 eerror " requested impl: ${EPYTHON}"
1046 die "${FUNCNAME}: conversion of incompatible shebang requested"
1047 fi
1048 done < <(find "${path}" -type f -print0)
1049
1050 if [[ ! ${any_fixed} ]]; then
1051 eqawarn "QA warning: ${FUNCNAME}, ${path#${D}} did not match any fixable files."
1052 if [[ ${any_correct} ]]; then
1053 eqawarn "All files have ${EPYTHON} shebang already."
1054 else
1055 eqawarn "There are no Python files in specified directory."
1056 fi
1057 fi
1058 done
1059 }
1060
1061 # @FUNCTION: _python_want_python_exec2
1062 # @INTERNAL
1063 # @DESCRIPTION:
1064 # Check whether we should be using python-exec:2.
1065 _python_want_python_exec2() {
1066 debug-print-function ${FUNCNAME} "${@}"
1067
1068 # EAPI 4 lacks slot operators, so just fix it on python-exec:2.
1069 [[ ${EAPI} == 4 ]] && return 0
1070
1071 # Check if we cached the result, or someone put an override.
1072 if [[ ! ${_PYTHON_WANT_PYTHON_EXEC2+1} ]]; then
1073 has_version 'dev-lang/python-exec:2'
1074 _PYTHON_WANT_PYTHON_EXEC2=$(( ! ${?} ))
1075 fi
1076
1077 # Non-zero means 'yes', zero means 'no'.
1078 [[ ${_PYTHON_WANT_PYTHON_EXEC2} != 0 ]]
1079 }
1080
1081 # @FUNCTION: _python_get_wrapper_path
1082 # @INTERNAL
1083 # @DESCRIPTION:
1084 # Output path to proper python-exec slot.
1085 _python_get_wrapper_path() {
1086 debug-print-function ${FUNCNAME} "${@}"
1087
1088 if _python_want_python_exec2; then
1089 echo /usr/lib/python-exec/python-exec2
1090 else
1091 echo /usr/bin/python-exec
1092 fi
1093 }
1094
1095 # @FUNCTION: python_export_utf8_locale
1096 # @RETURN: 0 on success, 1 on failure.
1097 # @DESCRIPTION:
1098 # Attempts to export a usable UTF-8 locale in the LC_CTYPE variable. Does
1099 # nothing if LC_ALL is defined, or if the current locale uses a UTF-8 charmap.
1100 # This may be used to work around the quirky open() behavior of python3.
1101 python_export_utf8_locale() {
1102 debug-print-function ${FUNCNAME} "${@}"
1103
1104 if [[ $(locale charmap) != UTF-8 ]]; then
1105 if [[ -n ${LC_ALL} ]]; then
1106 ewarn "LC_ALL is set to a locale with a charmap other than UTF-8."
1107 ewarn "This may trigger build failures in some python packages."
1108 return 1
1109 fi
1110
1111 # Try English first, then everything else.
1112 local lang locales="en_US.UTF-8 $(locale -a)"
1113
1114 for lang in ${locales}; do
1115 if [[ $(LC_CTYPE=${lang} locale charmap 2>/dev/null) == UTF-8 ]]; then
1116 export LC_CTYPE=${lang}
1117 return 0
1118 fi
1119 done
1120
1121 ewarn "Could not find a UTF-8 locale. This may trigger build failures in"
1122 ewarn "some python packages. Please ensure that a UTF-8 locale is listed in"
1123 ewarn "/etc/locale.gen and run locale-gen."
1124 return 1
1125 fi
1126
1127 return 0
1128 }
1129
1130 _PYTHON_UTILS_R1=1
1131 fi

  ViewVC Help
Powered by ViewVC 1.1.20