/[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.89 - (show annotations) (download)
Mon Aug 3 18:31:41 2015 UTC (3 years, 2 months ago) by mr_bones_
Branch: MAIN
CVS Tags: HEAD
Changes since 1.88: +2 -2 lines
Add quotes to support reading from files with spaces in the filename.

1 # Copyright 1999-2015 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.88 2015/07/27 16:35:19 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 wiki:
20 # https://wiki.gentoo.org/wiki/Project:Python/python-utils-r1
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 pypy3
45 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_[34]|jython2_[57])
70 return 0
71 ;;
72 pypy1_[89]|pypy2_0|python2_[56]|python3_[12])
73 return 1
74 ;;
75 pypy|pypy3)
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|pypy3)
239 impl=${1}
240 shift
241 ;;
242 *)
243 impl=${EPYTHON}
244 if [[ -z ${impl} ]]; then
245 die "python_export called without a python implementation and EPYTHON is unset"
246 fi
247 ;;
248 esac
249 debug-print "${FUNCNAME}: implementation: ${impl}"
250
251 for var; do
252 case "${var}" in
253 EPYTHON)
254 export EPYTHON=${impl}
255 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
256 ;;
257 PYTHON)
258 export PYTHON=${EPREFIX}/usr/bin/${impl}
259 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
260 ;;
261 PYTHON_SITEDIR)
262 local dir
263 case "${impl}" in
264 python*|pypy|pypy3)
265 dir=/usr/$(get_libdir)/${impl}
266 ;;
267 jython*)
268 dir=/usr/share/${impl/n/n-}/Lib
269 ;;
270 esac
271
272 export PYTHON_SITEDIR=${EPREFIX}${dir}/site-packages
273 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
274 ;;
275 PYTHON_INCLUDEDIR)
276 local dir
277 case "${impl}" in
278 python*)
279 dir=/usr/include/${impl}
280 ;;
281 pypy|pypy3)
282 dir=/usr/$(get_libdir)/${impl}/include
283 ;;
284 *)
285 die "${impl} lacks header files"
286 ;;
287 esac
288
289 export PYTHON_INCLUDEDIR=${EPREFIX}${dir}
290 debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
291 ;;
292 PYTHON_LIBPATH)
293 local libname
294 case "${impl}" in
295 python*)
296 libname=lib${impl}
297 ;;
298 *)
299 die "${impl} lacks a dynamic library"
300 ;;
301 esac
302
303 local path=${EPREFIX}/usr/$(get_libdir)
304
305 export PYTHON_LIBPATH=${path}/${libname}$(get_libname)
306 debug-print "${FUNCNAME}: PYTHON_LIBPATH = ${PYTHON_LIBPATH}"
307 ;;
308 PYTHON_CFLAGS)
309 local val
310
311 case "${impl}" in
312 python*)
313 # python-2.7, python-3.2, etc.
314 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-})
315 ;;
316 *)
317 die "${impl}: obtaining ${var} not supported"
318 ;;
319 esac
320
321 export PYTHON_CFLAGS=${val}
322 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
323 ;;
324 PYTHON_LIBS)
325 local val
326
327 case "${impl}" in
328 python*)
329 # python-2.7, python-3.2, etc.
330 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-})
331 ;;
332 *)
333 die "${impl}: obtaining ${var} not supported"
334 ;;
335 esac
336
337 export PYTHON_LIBS=${val}
338 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
339 ;;
340 PYTHON_PKG_DEP)
341 local d
342 case ${impl} in
343 python2.7)
344 PYTHON_PKG_DEP='>=dev-lang/python-2.7.5-r2:2.7';;
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 pypy3)
352 PYTHON_PKG_DEP='virtual/pypy3:0=';;
353 jython2.5)
354 PYTHON_PKG_DEP='>=dev-java/jython-2.5.3-r2:2.5';;
355 jython2.7)
356 PYTHON_PKG_DEP='dev-java/jython:2.7';;
357 *)
358 die "Invalid implementation: ${impl}"
359 esac
360
361 # use-dep
362 if [[ ${PYTHON_REQ_USE} ]]; then
363 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
364 fi
365
366 export PYTHON_PKG_DEP
367 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
368 ;;
369 PYTHON_SCRIPTDIR)
370 local dir
371 export PYTHON_SCRIPTDIR=${EPREFIX}/usr/lib/python-exec/${impl}
372 debug-print "${FUNCNAME}: PYTHON_SCRIPTDIR = ${PYTHON_SCRIPTDIR}"
373 ;;
374 *)
375 die "python_export: unknown variable ${var}"
376 esac
377 done
378 }
379
380 # @FUNCTION: python_get_sitedir
381 # @USAGE: [<impl>]
382 # @DESCRIPTION:
383 # Obtain and print the 'site-packages' path for the given
384 # implementation. If no implementation is provided, ${EPYTHON} will
385 # be used.
386 #
387 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
388 # better to use python_export() directly instead.
389 python_get_sitedir() {
390 debug-print-function ${FUNCNAME} "${@}"
391
392 python_export "${@}" PYTHON_SITEDIR
393 echo "${PYTHON_SITEDIR}"
394 }
395
396 # @FUNCTION: python_get_includedir
397 # @USAGE: [<impl>]
398 # @DESCRIPTION:
399 # Obtain and print the include path for the given implementation. If no
400 # implementation is provided, ${EPYTHON} will be used.
401 #
402 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
403 # is better to use python_export() directly instead.
404 python_get_includedir() {
405 debug-print-function ${FUNCNAME} "${@}"
406
407 python_export "${@}" PYTHON_INCLUDEDIR
408 echo "${PYTHON_INCLUDEDIR}"
409 }
410
411 # @FUNCTION: python_get_library_path
412 # @USAGE: [<impl>]
413 # @DESCRIPTION:
414 # Obtain and print the Python library path for the given implementation.
415 # If no implementation is provided, ${EPYTHON} will be used.
416 #
417 # Please note that this function can be used with CPython only. Use
418 # in another implementation will result in a fatal failure.
419 python_get_library_path() {
420 debug-print-function ${FUNCNAME} "${@}"
421
422 python_export "${@}" PYTHON_LIBPATH
423 echo "${PYTHON_LIBPATH}"
424 }
425
426 # @FUNCTION: python_get_CFLAGS
427 # @USAGE: [<impl>]
428 # @DESCRIPTION:
429 # Obtain and print the compiler flags for building against Python,
430 # for the given implementation. If no implementation is provided,
431 # ${EPYTHON} will be used.
432 #
433 # Please note that this function can be used with CPython only.
434 # It requires Python and pkg-config installed, and therefore proper
435 # build-time dependencies need be added to the ebuild.
436 python_get_CFLAGS() {
437 debug-print-function ${FUNCNAME} "${@}"
438
439 python_export "${@}" PYTHON_CFLAGS
440 echo "${PYTHON_CFLAGS}"
441 }
442
443 # @FUNCTION: python_get_LIBS
444 # @USAGE: [<impl>]
445 # @DESCRIPTION:
446 # Obtain and print the compiler flags for linking against Python,
447 # for the given implementation. If no implementation is provided,
448 # ${EPYTHON} will be used.
449 #
450 # Please note that this function can be used with CPython only.
451 # It requires Python and pkg-config installed, and therefore proper
452 # build-time dependencies need be added to the ebuild.
453 python_get_LIBS() {
454 debug-print-function ${FUNCNAME} "${@}"
455
456 python_export "${@}" PYTHON_LIBS
457 echo "${PYTHON_LIBS}"
458 }
459
460 # @FUNCTION: python_get_scriptdir
461 # @USAGE: [<impl>]
462 # @DESCRIPTION:
463 # Obtain and print the script install path for the given
464 # implementation. If no implementation is provided, ${EPYTHON} will
465 # be used.
466 python_get_scriptdir() {
467 debug-print-function ${FUNCNAME} "${@}"
468
469 python_export "${@}" PYTHON_SCRIPTDIR
470 echo "${PYTHON_SCRIPTDIR}"
471 }
472
473 # @FUNCTION: _python_ln_rel
474 # @USAGE: <from> <to>
475 # @INTERNAL
476 # @DESCRIPTION:
477 # Create a relative symlink.
478 _python_ln_rel() {
479 debug-print-function ${FUNCNAME} "${@}"
480
481 local target=${1}
482 local symname=${2}
483
484 local tgpath=${target%/*}/
485 local sympath=${symname%/*}/
486 local rel_target=
487
488 while [[ ${sympath} ]]; do
489 local tgseg= symseg=
490
491 while [[ ! ${tgseg} && ${tgpath} ]]; do
492 tgseg=${tgpath%%/*}
493 tgpath=${tgpath#${tgseg}/}
494 done
495
496 while [[ ! ${symseg} && ${sympath} ]]; do
497 symseg=${sympath%%/*}
498 sympath=${sympath#${symseg}/}
499 done
500
501 if [[ ${tgseg} != ${symseg} ]]; then
502 rel_target=../${rel_target}${tgseg:+${tgseg}/}
503 fi
504 done
505 rel_target+=${tgpath}${target##*/}
506
507 debug-print "${FUNCNAME}: ${symname} -> ${target}"
508 debug-print "${FUNCNAME}: rel_target = ${rel_target}"
509
510 ln -fs "${rel_target}" "${symname}"
511 }
512
513 # @FUNCTION: python_optimize
514 # @USAGE: [<directory>...]
515 # @DESCRIPTION:
516 # Compile and optimize Python modules in specified directories (absolute
517 # paths). If no directories are provided, the default system paths
518 # are used (prepended with ${D}).
519 python_optimize() {
520 debug-print-function ${FUNCNAME} "${@}"
521
522 if [[ ${EBUILD_PHASE} == pre* || ${EBUILD_PHASE} == post* ]]; then
523 eerror "The new Python eclasses expect the compiled Python files to"
524 eerror "be controlled by the Package Manager. For this reason,"
525 eerror "the python_optimize function can be used only during src_* phases"
526 eerror "(src_install most commonly) and not during pkg_* phases."
527 echo
528 die "python_optimize is not to be used in pre/post* phases"
529 fi
530
531 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
532
533 local PYTHON=${PYTHON}
534 [[ ${PYTHON} ]] || python_export PYTHON
535
536 # Note: python2.6 can't handle passing files to compileall...
537 # TODO: we do not support 2.6 any longer
538
539 # default to sys.path
540 if [[ ${#} -eq 0 ]]; then
541 local f
542 while IFS= read -r -d '' f; do
543 # 1) accept only absolute paths
544 # (i.e. skip '', '.' or anything like that)
545 # 2) skip paths which do not exist
546 # (python2.6 complains about them verbosely)
547
548 if [[ ${f} == /* && -d ${D}${f} ]]; then
549 set -- "${D}${f}" "${@}"
550 fi
551 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))')
552
553 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
554 fi
555
556 local d
557 for d; do
558 # make sure to get a nice path without //
559 local instpath=${d#${D}}
560 instpath=/${instpath##/}
561
562 case "${EPYTHON}" in
563 python*)
564 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
565 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
566 ;;
567 *)
568 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
569 ;;
570 esac
571 done
572 }
573
574 # @ECLASS-VARIABLE: python_scriptroot
575 # @DEFAULT_UNSET
576 # @DESCRIPTION:
577 # The current script destination for python_doscript(). The path
578 # is relative to the installation root (${ED}).
579 #
580 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
581 #
582 # Can be set indirectly through the python_scriptinto() function.
583 #
584 # Example:
585 # @CODE
586 # src_install() {
587 # local python_scriptroot=${GAMES_BINDIR}
588 # python_foreach_impl python_doscript foo
589 # }
590 # @CODE
591
592 # @FUNCTION: python_scriptinto
593 # @USAGE: <new-path>
594 # @DESCRIPTION:
595 # Set the current scriptroot. The new value will be stored
596 # in the 'python_scriptroot' environment variable. The new value need
597 # be relative to the installation root (${ED}).
598 #
599 # Alternatively, you can set the variable directly.
600 python_scriptinto() {
601 debug-print-function ${FUNCNAME} "${@}"
602
603 python_scriptroot=${1}
604 }
605
606 # @FUNCTION: python_doexe
607 # @USAGE: <files>...
608 # @DESCRIPTION:
609 # Install the given executables into current python_scriptroot,
610 # for the current Python implementation (${EPYTHON}).
611 #
612 # The executable will be wrapped properly for the Python implementation,
613 # though no shebang mangling will be performed.
614 python_doexe() {
615 debug-print-function ${FUNCNAME} "${@}"
616
617 local f
618 for f; do
619 python_newexe "${f}" "${f##*/}"
620 done
621 }
622
623 # @FUNCTION: python_newexe
624 # @USAGE: <path> <new-name>
625 # @DESCRIPTION:
626 # Install the given executable into current python_scriptroot,
627 # for the current Python implementation (${EPYTHON}).
628 #
629 # The executable will be wrapped properly for the Python implementation,
630 # though no shebang mangling will be performed. It will be renamed
631 # to <new-name>.
632 python_newexe() {
633 debug-print-function ${FUNCNAME} "${@}"
634
635 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
636 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
637
638 local wrapd=${python_scriptroot:-${DESTTREE}/bin}
639
640 local f=${1}
641 local newfn=${2}
642
643 local PYTHON_SCRIPTDIR d
644 python_export PYTHON_SCRIPTDIR
645 d=${PYTHON_SCRIPTDIR#${EPREFIX}}
646
647 (
648 dodir "${wrapd}"
649 exeinto "${d}"
650 newexe "${f}" "${newfn}" || die
651 )
652
653 # install the wrapper
654 _python_ln_rel "${ED%/}"/usr/lib/python-exec/python-exec2 \
655 "${ED%/}/${wrapd}/${newfn}" || die
656
657 # don't use this at home, just call python_doscript() instead
658 if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
659 python_fix_shebang -q "${ED%/}/${d}/${newfn}"
660 fi
661 }
662
663 # @FUNCTION: python_doscript
664 # @USAGE: <files>...
665 # @DESCRIPTION:
666 # Install the given scripts into current python_scriptroot,
667 # for the current Python implementation (${EPYTHON}).
668 #
669 # All specified files must start with a 'python' shebang. The shebang
670 # will be converted, and the files will be wrapped properly
671 # for the Python implementation.
672 #
673 # Example:
674 # @CODE
675 # src_install() {
676 # python_foreach_impl python_doscript ${PN}
677 # }
678 # @CODE
679 python_doscript() {
680 debug-print-function ${FUNCNAME} "${@}"
681
682 local _PYTHON_REWRITE_SHEBANG=1
683 python_doexe "${@}"
684 }
685
686 # @FUNCTION: python_newscript
687 # @USAGE: <path> <new-name>
688 # @DESCRIPTION:
689 # Install the given script into current python_scriptroot
690 # for the current Python implementation (${EPYTHON}), and name it
691 # <new-name>.
692 #
693 # The file must start with a 'python' shebang. The shebang will be
694 # converted, and the file will be wrapped properly for the Python
695 # implementation. It will be renamed to <new-name>.
696 #
697 # Example:
698 # @CODE
699 # src_install() {
700 # python_foreach_impl python_newscript foo.py foo
701 # }
702 # @CODE
703 python_newscript() {
704 debug-print-function ${FUNCNAME} "${@}"
705
706 local _PYTHON_REWRITE_SHEBANG=1
707 python_newexe "${@}"
708 }
709
710 # @ECLASS-VARIABLE: python_moduleroot
711 # @DEFAULT_UNSET
712 # @DESCRIPTION:
713 # The current module root for python_domodule(). The path can be either
714 # an absolute system path (it must start with a slash, and ${ED} will be
715 # prepended to it) or relative to the implementation's site-packages directory
716 # (then it must start with a non-slash character).
717 #
718 # When unset, the modules will be installed in the site-packages root.
719 #
720 # Can be set indirectly through the python_moduleinto() function.
721 #
722 # Example:
723 # @CODE
724 # src_install() {
725 # local python_moduleroot=bar
726 # # installs ${PYTHON_SITEDIR}/bar/baz.py
727 # python_foreach_impl python_domodule baz.py
728 # }
729 # @CODE
730
731 # @FUNCTION: python_moduleinto
732 # @USAGE: <new-path>
733 # @DESCRIPTION:
734 # Set the current module root. The new value will be stored
735 # in the 'python_moduleroot' environment variable. The new value need
736 # be relative to the site-packages root.
737 #
738 # Alternatively, you can set the variable directly.
739 python_moduleinto() {
740 debug-print-function ${FUNCNAME} "${@}"
741
742 python_moduleroot=${1}
743 }
744
745 # @FUNCTION: python_domodule
746 # @USAGE: <files>...
747 # @DESCRIPTION:
748 # Install the given modules (or packages) into the current
749 # python_moduleroot. The list can mention both modules (files)
750 # and packages (directories). All listed files will be installed
751 # for all enabled implementations, and compiled afterwards.
752 #
753 # Example:
754 # @CODE
755 # src_install() {
756 # # (${PN} being a directory)
757 # python_foreach_impl python_domodule ${PN}
758 # }
759 # @CODE
760 python_domodule() {
761 debug-print-function ${FUNCNAME} "${@}"
762
763 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
764
765 local d
766 if [[ ${python_moduleroot} == /* ]]; then
767 # absolute path
768 d=${python_moduleroot}
769 else
770 # relative to site-packages
771 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
772 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
773
774 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
775 fi
776
777 local INSDESTTREE
778
779 insinto "${d}"
780 doins -r "${@}" || die
781
782 python_optimize "${ED}/${d}"
783 }
784
785 # @FUNCTION: python_doheader
786 # @USAGE: <files>...
787 # @DESCRIPTION:
788 # Install the given headers into the implementation-specific include
789 # directory. This function is unconditionally recursive, i.e. you can
790 # pass directories instead of files.
791 #
792 # Example:
793 # @CODE
794 # src_install() {
795 # python_foreach_impl python_doheader foo.h bar.h
796 # }
797 # @CODE
798 python_doheader() {
799 debug-print-function ${FUNCNAME} "${@}"
800
801 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
802
803 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
804 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
805
806 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
807
808 local INSDESTTREE
809
810 insinto "${d}"
811 doins -r "${@}" || die
812 }
813
814 # @FUNCTION: python_wrapper_setup
815 # @USAGE: [<path> [<impl>]]
816 # @DESCRIPTION:
817 # Create proper 'python' executable and pkg-config wrappers
818 # (if available) in the directory named by <path>. Set up PATH
819 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
820 #
821 # The wrappers will be created for implementation named by <impl>,
822 # or for one named by ${EPYTHON} if no <impl> passed.
823 #
824 # If the named directory contains a python symlink already, it will
825 # be assumed to contain proper wrappers already and only environment
826 # setup will be done. If wrapper update is requested, the directory
827 # shall be removed first.
828 python_wrapper_setup() {
829 debug-print-function ${FUNCNAME} "${@}"
830
831 local workdir=${1:-${T}/${EPYTHON}}
832 local impl=${2:-${EPYTHON}}
833
834 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
835 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
836
837 if [[ ! -x ${workdir}/bin/python ]]; then
838 _python_check_dead_variables
839
840 mkdir -p "${workdir}"/{bin,pkgconfig} || die
841
842 # Clean up, in case we were supposed to do a cheap update.
843 rm -f "${workdir}"/bin/python{,2,3,-config} || die
844 rm -f "${workdir}"/bin/2to3 || die
845 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc || die
846
847 local EPYTHON PYTHON
848 python_export "${impl}" EPYTHON PYTHON
849
850 local pyver pyother
851 if python_is_python3; then
852 pyver=3
853 pyother=2
854 else
855 pyver=2
856 pyother=3
857 fi
858
859 # Python interpreter
860 # note: we don't use symlinks because python likes to do some
861 # symlink reading magic that breaks stuff
862 # https://bugs.gentoo.org/show_bug.cgi?id=555752
863 cat > "${workdir}/bin/python" <<-_EOF_
864 #!/bin/sh
865 exec "${PYTHON}" "\${@}"
866 _EOF_
867 cp "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
868 chmod +x "${workdir}/bin/python" "${workdir}/bin/python${pyver}" || die
869
870 local nonsupp=( "python${pyother}" "python${pyother}-config" )
871
872 # CPython-specific
873 if [[ ${EPYTHON} == python* ]]; then
874 cat > "${workdir}/bin/python-config" <<-_EOF_
875 #!/bin/sh
876 exec "${PYTHON}-config" "\${@}"
877 _EOF_
878 cp "${workdir}/bin/python-config" \
879 "${workdir}/bin/python${pyver}-config" || die
880 chmod +x "${workdir}/bin/python-config" \
881 "${workdir}/bin/python${pyver}-config" || die
882
883 # Python 2.6+.
884 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
885
886 # Python 2.7+.
887 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
888 "${workdir}"/pkgconfig/python.pc || die
889 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
890 else
891 nonsupp+=( 2to3 python-config "python${pyver}-config" )
892 fi
893
894 local x
895 for x in "${nonsupp[@]}"; do
896 cat >"${workdir}"/bin/${x} <<__EOF__
897 #!/bin/sh
898 echo "${x} is not supported by ${EPYTHON}" >&2
899 exit 127
900 __EOF__
901 chmod +x "${workdir}"/bin/${x} || die
902 done
903
904 # Now, set the environment.
905 # But note that ${workdir} may be shared with something else,
906 # and thus already on top of PATH.
907 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
908 PATH=${workdir}/bin${PATH:+:${PATH}}
909 fi
910 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
911 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
912 fi
913 export PATH PKG_CONFIG_PATH
914 fi
915 }
916
917 # @FUNCTION: python_is_python3
918 # @USAGE: [<impl>]
919 # @DESCRIPTION:
920 # Check whether <impl> (or ${EPYTHON}) is a Python3k variant
921 # (i.e. uses syntax and stdlib of Python 3.*).
922 #
923 # Returns 0 (true) if it is, 1 (false) otherwise.
924 python_is_python3() {
925 local impl=${1:-${EPYTHON}}
926 [[ ${impl} ]] || die "python_is_python3: no impl nor EPYTHON"
927
928 [[ ${impl} == python3* || ${impl} == pypy3 ]]
929 }
930
931 # @FUNCTION: python_is_installed
932 # @USAGE: [<impl>]
933 # @DESCRIPTION:
934 # Check whether the interpreter for <impl> (or ${EPYTHON}) is installed.
935 # Uses has_version with a proper dependency string.
936 #
937 # Returns 0 (true) if it is, 1 (false) otherwise.
938 python_is_installed() {
939 local impl=${1:-${EPYTHON}}
940 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
941
942 # for has_version
943 local -x ROOT=/
944 case "${impl}" in
945 pypy|pypy3)
946 local append=
947 if [[ ${PYTHON_REQ_USE} ]]; then
948 append=[${PYTHON_REQ_USE}]
949 fi
950
951 # be happy with just the interpeter, no need for the virtual
952 has_version "dev-python/${impl}${append}" \
953 || has_version "dev-python/${impl}-bin${append}"
954 ;;
955 *)
956 local PYTHON_PKG_DEP
957 python_export "${impl}" PYTHON_PKG_DEP
958 has_version "${PYTHON_PKG_DEP}"
959 ;;
960 esac
961 }
962
963 # @FUNCTION: python_fix_shebang
964 # @USAGE: [-f|--force] [-q|--quiet] <path>...
965 # @DESCRIPTION:
966 # Replace the shebang in Python scripts with the current Python
967 # implementation (EPYTHON). If a directory is passed, works recursively
968 # on all Python scripts.
969 #
970 # Only files having a 'python*' shebang will be modified. Files with
971 # other shebang will either be skipped when working recursively
972 # on a directory or treated as error when specified explicitly.
973 #
974 # Shebangs matching explicitly current Python version will be left
975 # unmodified. Shebangs requesting another Python version will be treated
976 # as fatal error, unless --force is given.
977 #
978 # --force causes the function to replace even shebangs that require
979 # incompatible Python version. --quiet causes the function not to list
980 # modified files verbosely.
981 python_fix_shebang() {
982 debug-print-function ${FUNCNAME} "${@}"
983
984 [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
985
986 local force quiet
987 while [[ ${@} ]]; do
988 case "${1}" in
989 -f|--force) force=1; shift;;
990 -q|--quiet) quiet=1; shift;;
991 --) shift; break;;
992 *) break;;
993 esac
994 done
995
996 [[ ${1} ]] || die "${FUNCNAME}: no paths given"
997
998 local path f
999 for path; do
1000 local any_correct any_fixed is_recursive
1001
1002 [[ -d ${path} ]] && is_recursive=1
1003
1004 while IFS= read -r -d '' f; do
1005 local shebang i
1006 local error= from=
1007
1008 IFS= read -r shebang <"${f}"
1009
1010 # First, check if it's shebang at all...
1011 if [[ ${shebang} == '#!'* ]]; then
1012 local split_shebang=()
1013 read -r -a split_shebang <<<${shebang}
1014
1015 # Match left-to-right in a loop, to avoid matching random
1016 # repetitions like 'python2.7 python2'.
1017 for i in "${split_shebang[@]}"; do
1018 case "${i}" in
1019 *"${EPYTHON}")
1020 debug-print "${FUNCNAME}: in file ${f#${D}}"
1021 debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
1022
1023 # Nothing to do, move along.
1024 any_correct=1
1025 from=${EPYTHON}
1026 break
1027 ;;
1028 *python|*python[23])
1029 debug-print "${FUNCNAME}: in file ${f#${D}}"
1030 debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
1031
1032 if [[ ${i} == *python2 ]]; then
1033 from=python2
1034 if [[ ! ${force} ]]; then
1035 python_is_python3 "${EPYTHON}" && error=1
1036 fi
1037 elif [[ ${i} == *python3 ]]; then
1038 from=python3
1039 if [[ ! ${force} ]]; then
1040 python_is_python3 "${EPYTHON}" || error=1
1041 fi
1042 else
1043 from=python
1044 fi
1045 break
1046 ;;
1047 *python[23].[0123456789]|*pypy|*pypy3|*jython[23].[0123456789])
1048 # Explicit mismatch.
1049 if [[ ! ${force} ]]; then
1050 error=1
1051 else
1052 case "${i}" in
1053 *python[23].[0123456789])
1054 from="python[23].[0123456789]";;
1055 *pypy)
1056 from="pypy";;
1057 *pypy3)
1058 from="pypy3";;
1059 *jython[23].[0123456789])
1060 from="jython[23].[0123456789]";;
1061 *)
1062 die "${FUNCNAME}: internal error in 2nd pattern match";;
1063 esac
1064 fi
1065 break
1066 ;;
1067 esac
1068 done
1069 fi
1070
1071 if [[ ! ${error} && ! ${from} ]]; then
1072 # Non-Python shebang. Allowed in recursive mode,
1073 # disallowed when specifying file explicitly.
1074 [[ ${is_recursive} ]] && continue
1075 error=1
1076 fi
1077
1078 if [[ ! ${quiet} ]]; then
1079 einfo "Fixing shebang in ${f#${D}}."
1080 fi
1081
1082 if [[ ! ${error} ]]; then
1083 # We either want to match ${from} followed by space
1084 # or at end-of-string.
1085 if [[ ${shebang} == *${from}" "* ]]; then
1086 sed -i -e "1s:${from} :${EPYTHON} :" "${f}" || die
1087 else
1088 sed -i -e "1s:${from}$:${EPYTHON}:" "${f}" || die
1089 fi
1090 any_fixed=1
1091 else
1092 eerror "The file has incompatible shebang:"
1093 eerror " file: ${f#${D}}"
1094 eerror " current shebang: ${shebang}"
1095 eerror " requested impl: ${EPYTHON}"
1096 die "${FUNCNAME}: conversion of incompatible shebang requested"
1097 fi
1098 done < <(find "${path}" -type f -print0)
1099
1100 if [[ ! ${any_fixed} ]]; then
1101 eqawarn "QA warning: ${FUNCNAME}, ${path#${D}} did not match any fixable files."
1102 if [[ ${any_correct} ]]; then
1103 eqawarn "All files have ${EPYTHON} shebang already."
1104 else
1105 eqawarn "There are no Python files in specified directory."
1106 fi
1107 fi
1108 done
1109 }
1110
1111 # @FUNCTION: python_export_utf8_locale
1112 # @RETURN: 0 on success, 1 on failure.
1113 # @DESCRIPTION:
1114 # Attempts to export a usable UTF-8 locale in the LC_CTYPE variable. Does
1115 # nothing if LC_ALL is defined, or if the current locale uses a UTF-8 charmap.
1116 # This may be used to work around the quirky open() behavior of python3.
1117 python_export_utf8_locale() {
1118 debug-print-function ${FUNCNAME} "${@}"
1119
1120 if [[ $(locale charmap) != UTF-8 ]]; then
1121 if [[ -n ${LC_ALL} ]]; then
1122 ewarn "LC_ALL is set to a locale with a charmap other than UTF-8."
1123 ewarn "This may trigger build failures in some python packages."
1124 return 1
1125 fi
1126
1127 # Try English first, then everything else.
1128 local lang locales="en_US.UTF-8 $(locale -a)"
1129
1130 for lang in ${locales}; do
1131 if [[ $(LC_CTYPE=${lang} locale charmap 2>/dev/null) == UTF-8 ]]; then
1132 export LC_CTYPE=${lang}
1133 return 0
1134 fi
1135 done
1136
1137 ewarn "Could not find a UTF-8 locale. This may trigger build failures in"
1138 ewarn "some python packages. Please ensure that a UTF-8 locale is listed in"
1139 ewarn "/etc/locale.gen and run locale-gen."
1140 return 1
1141 fi
1142
1143 return 0
1144 }
1145
1146 # -- python.eclass functions --
1147
1148 _python_check_dead_variables() {
1149 local v
1150
1151 for v in PYTHON_DEPEND PYTHON_USE_WITH{,_OR,_OPT} {RESTRICT,SUPPORT}_PYTHON_ABIS
1152 do
1153 if [[ ${!v} ]]; then
1154 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Ebuild_head"
1155 fi
1156 done
1157
1158 for v in PYTHON_{CPPFLAGS,CFLAGS,CXXFLAGS,LDFLAGS}
1159 do
1160 if [[ ${!v} ]]; then
1161 die "${v} is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#PYTHON_CFLAGS"
1162 fi
1163 done
1164
1165 for v in PYTHON_TESTS_RESTRICTED_ABIS PYTHON_EXPORT_PHASE_FUNCTIONS \
1166 PYTHON_VERSIONED_{SCRIPTS,EXECUTABLES} PYTHON_NONVERSIONED_EXECUTABLES
1167 do
1168 if [[ ${!v} ]]; then
1169 die "${v} is invalid for python-r1 suite"
1170 fi
1171 done
1172
1173 for v in DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES DISTUTILS_SETUP_FILES \
1174 DISTUTILS_GLOBAL_OPTIONS DISTUTILS_SRC_TEST PYTHON_MODNAME
1175 do
1176 if [[ ${!v} ]]; then
1177 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#${v}"
1178 fi
1179 done
1180
1181 if [[ ${DISTUTILS_DISABLE_TEST_DEPENDENCY} ]]; then
1182 die "${v} is invalid for distutils-r1, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#DISTUTILS_SRC_TEST"
1183 fi
1184
1185 # python.eclass::progress
1186 for v in PYTHON_BDEPEND PYTHON_MULTIPLE_ABIS PYTHON_ABI_TYPE \
1187 PYTHON_RESTRICTED_ABIS PYTHON_TESTS_FAILURES_TOLERANT_ABIS \
1188 PYTHON_CFFI_MODULES_GENERATION_COMMANDS
1189 do
1190 if [[ ${!v} ]]; then
1191 die "${v} is invalid for python-r1 suite"
1192 fi
1193 done
1194 }
1195
1196 python_pkg_setup() {
1197 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1198 }
1199
1200 python_convert_shebangs() {
1201 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#python_convert_shebangs"
1202 }
1203
1204 python_clean_py-compile_files() {
1205 die "${FUNCNAME}() is invalid for python-r1 suite"
1206 }
1207
1208 python_clean_installation_image() {
1209 die "${FUNCNAME}() is invalid for python-r1 suite"
1210 }
1211
1212 python_execute_function() {
1213 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#python_execute_function"
1214 }
1215
1216 python_generate_wrapper_scripts() {
1217 die "${FUNCNAME}() is invalid for python-r1 suite"
1218 }
1219
1220 python_merge_intermediate_installation_images() {
1221 die "${FUNCNAME}() is invalid for python-r1 suite"
1222 }
1223
1224 python_set_active_version() {
1225 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#pkg_setup"
1226 }
1227
1228 python_need_rebuild() {
1229 die "${FUNCNAME}() is invalid for python-r1 suite"
1230 }
1231
1232 PYTHON() {
1233 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#.24.28PYTHON.29.2C_.24.7BEPYTHON.7D"
1234 }
1235
1236 python_get_implementation() {
1237 die "${FUNCNAME}() is invalid for python-r1 suite"
1238 }
1239
1240 python_get_implementational_package() {
1241 die "${FUNCNAME}() is invalid for python-r1 suite"
1242 }
1243
1244 python_get_libdir() {
1245 die "${FUNCNAME}() is invalid for python-r1 suite"
1246 }
1247
1248 python_get_library() {
1249 die "${FUNCNAME}() is invalid for python-r1 suite"
1250 }
1251
1252 python_get_version() {
1253 die "${FUNCNAME}() is invalid for python-r1 suite"
1254 }
1255
1256 python_get_implementation_and_version() {
1257 die "${FUNCNAME}() is invalid for python-r1 suite"
1258 }
1259
1260 python_execute_nosetests() {
1261 die "${FUNCNAME}() is invalid for python-r1 suite"
1262 }
1263
1264 python_execute_py.test() {
1265 die "${FUNCNAME}() is invalid for python-r1 suite"
1266 }
1267
1268 python_execute_trial() {
1269 die "${FUNCNAME}() is invalid for python-r1 suite"
1270 }
1271
1272 python_enable_pyc() {
1273 die "${FUNCNAME}() is invalid for python-r1 suite"
1274 }
1275
1276 python_disable_pyc() {
1277 die "${FUNCNAME}() is invalid for python-r1 suite"
1278 }
1279
1280 python_mod_optimize() {
1281 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Python_byte-code_compilation"
1282 }
1283
1284 python_mod_cleanup() {
1285 die "${FUNCNAME}() is invalid for python-r1 suite, please take a look @ https://wiki.gentoo.org/wiki/Project:Python/Python.eclass_conversion#Python_byte-code_compilation"
1286 }
1287
1288 # python.eclass::progress
1289
1290 python_abi_depend() {
1291 die "${FUNCNAME}() is invalid for python-r1 suite"
1292 }
1293
1294 python_install_executables() {
1295 die "${FUNCNAME}() is invalid for python-r1 suite"
1296 }
1297
1298 python_get_extension_module_suffix() {
1299 die "${FUNCNAME}() is invalid for python-r1 suite"
1300 }
1301
1302 python_byte-compile_modules() {
1303 die "${FUNCNAME}() is invalid for python-r1 suite"
1304 }
1305
1306 python_clean_byte-compiled_modules() {
1307 die "${FUNCNAME}() is invalid for python-r1 suite"
1308 }
1309
1310 python_generate_cffi_modules() {
1311 die "${FUNCNAME}() is invalid for python-r1 suite"
1312 }
1313
1314 _PYTHON_UTILS_R1=1
1315 fi

  ViewVC Help
Powered by ViewVC 1.1.20