/[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.30 - (show annotations) (download)
Sat Jul 27 11:17:44 2013 UTC (11 months, 4 weeks ago) by mgorny
Branch: MAIN
Changes since 1.29: +2 -2 lines
Switch eclasses to use virtual/pypy (and therefore support pypy-bin).

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

  ViewVC Help
Powered by ViewVC 1.1.20