/[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.27 - (show annotations) (download)
Fri Jun 7 01:11:31 2013 UTC (10 months, 2 weeks ago) by floppym
Branch: MAIN
Changes since 1.26: +26 -6 lines
Improve docs for PYTHON and EPYTHON.

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.26 2013/05/31 17:50:33 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='dev-python/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 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
598
599 local PYTHON=${PYTHON}
600 [[ ${PYTHON} ]] || python_export PYTHON
601
602 # Note: python2.6 can't handle passing files to compileall...
603
604 # default to sys.path
605 if [[ ${#} -eq 0 ]]; then
606 local f
607 while IFS= read -r -d '' f; do
608 # 1) accept only absolute paths
609 # (i.e. skip '', '.' or anything like that)
610 # 2) skip paths which do not exist
611 # (python2.6 complains about them verbosely)
612
613 if [[ ${f} == /* && -d ${D}${f} ]]; then
614 set -- "${D}${f}" "${@}"
615 fi
616 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))')
617
618 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
619 fi
620
621 local d
622 for d; do
623 # make sure to get a nice path without //
624 local instpath=${d#${D}}
625 instpath=/${instpath##/}
626
627 case "${EPYTHON}" in
628 python*)
629 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
630 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
631 ;;
632 *)
633 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${@}"
634 ;;
635 esac
636 done
637 }
638
639 # @ECLASS-VARIABLE: python_scriptroot
640 # @DEFAULT_UNSET
641 # @DESCRIPTION:
642 # The current script destination for python_doscript(). The path
643 # is relative to the installation root (${ED}).
644 #
645 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
646 #
647 # Can be set indirectly through the python_scriptinto() function.
648 #
649 # Example:
650 # @CODE
651 # src_install() {
652 # local python_scriptroot=${GAMES_BINDIR}
653 # python_foreach_impl python_doscript foo
654 # }
655 # @CODE
656
657 # @FUNCTION: python_scriptinto
658 # @USAGE: <new-path>
659 # @DESCRIPTION:
660 # Set the current scriptroot. The new value will be stored
661 # in the 'python_scriptroot' environment variable. The new value need
662 # be relative to the installation root (${ED}).
663 #
664 # Alternatively, you can set the variable directly.
665 python_scriptinto() {
666 debug-print-function ${FUNCNAME} "${@}"
667
668 python_scriptroot=${1}
669 }
670
671 # @FUNCTION: python_doscript
672 # @USAGE: <files>...
673 # @DESCRIPTION:
674 # Install the given scripts into current python_scriptroot,
675 # for the current Python implementation (${EPYTHON}).
676 #
677 # All specified files must start with a 'python' shebang. The shebang
678 # will be converted, the file will be renamed to be EPYTHON-suffixed
679 # and a wrapper will be installed in place of the original name.
680 #
681 # Example:
682 # @CODE
683 # src_install() {
684 # python_foreach_impl python_doscript ${PN}
685 # }
686 # @CODE
687 python_doscript() {
688 debug-print-function ${FUNCNAME} "${@}"
689
690 local f
691 for f; do
692 python_newscript "${f}" "${f##*/}"
693 done
694 }
695
696 # @FUNCTION: python_newscript
697 # @USAGE: <path> <new-name>
698 # @DESCRIPTION:
699 # Install the given script into current python_scriptroot
700 # for the current Python implementation (${EPYTHON}), and name it
701 # <new-name>.
702 #
703 # The file must start with a 'python' shebang. The shebang will be
704 # converted, the file will be renamed to be EPYTHON-suffixed
705 # and a wrapper will be installed in place of the <new-name>.
706 #
707 # Example:
708 # @CODE
709 # src_install() {
710 # python_foreach_impl python_newscript foo.py foo
711 # }
712 # @CODE
713 python_newscript() {
714 debug-print-function ${FUNCNAME} "${@}"
715
716 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
717 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
718
719 local d=${python_scriptroot:-${DESTTREE}/bin}
720 local INSDESTTREE INSOPTIONS
721
722 insinto "${d}"
723 insopts -m755
724
725 local f=${1}
726 local barefn=${2}
727
728 local newfn=${barefn}-${EPYTHON}
729
730 debug-print "${FUNCNAME}: ${f} -> ${d}/${newfn}"
731 newins "${f}" "${newfn}" || die
732 _python_rewrite_shebang "${ED}/${d}/${newfn}"
733
734 # install the wrapper
735 _python_ln_rel "${ED}"/usr/bin/python-exec "${ED}/${d}/${barefn}" || die
736 }
737
738 # @ECLASS-VARIABLE: python_moduleroot
739 # @DEFAULT_UNSET
740 # @DESCRIPTION:
741 # The current module root for python_domodule(). The path can be either
742 # an absolute system path (it must start with a slash, and ${ED} will be
743 # prepended to it) or relative to the implementation's site-packages directory
744 # (then it must start with a non-slash character).
745 #
746 # When unset, the modules will be installed in the site-packages root.
747 #
748 # Can be set indirectly through the python_moduleinto() function.
749 #
750 # Example:
751 # @CODE
752 # src_install() {
753 # local python_moduleroot=bar
754 # # installs ${PYTHON_SITEDIR}/bar/baz.py
755 # python_foreach_impl python_domodule baz.py
756 # }
757 # @CODE
758
759 # @FUNCTION: python_moduleinto
760 # @USAGE: <new-path>
761 # @DESCRIPTION:
762 # Set the current module root. The new value will be stored
763 # in the 'python_moduleroot' environment variable. The new value need
764 # be relative to the site-packages root.
765 #
766 # Alternatively, you can set the variable directly.
767 python_moduleinto() {
768 debug-print-function ${FUNCNAME} "${@}"
769
770 python_moduleroot=${1}
771 }
772
773 # @FUNCTION: python_domodule
774 # @USAGE: <files>...
775 # @DESCRIPTION:
776 # Install the given modules (or packages) into the current
777 # python_moduleroot. The list can mention both modules (files)
778 # and packages (directories). All listed files will be installed
779 # for all enabled implementations, and compiled afterwards.
780 #
781 # Example:
782 # @CODE
783 # src_install() {
784 # # (${PN} being a directory)
785 # python_foreach_impl python_domodule ${PN}
786 # }
787 # @CODE
788 python_domodule() {
789 debug-print-function ${FUNCNAME} "${@}"
790
791 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
792
793 local d
794 if [[ ${python_moduleroot} == /* ]]; then
795 # absolute path
796 d=${python_moduleroot}
797 else
798 # relative to site-packages
799 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
800 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
801
802 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
803 fi
804
805 local INSDESTTREE
806
807 insinto "${d}"
808 doins -r "${@}" || die
809
810 python_optimize "${ED}/${d}"
811 }
812
813 # @FUNCTION: python_doheader
814 # @USAGE: <files>...
815 # @DESCRIPTION:
816 # Install the given headers into the implementation-specific include
817 # directory. This function is unconditionally recursive, i.e. you can
818 # pass directories instead of files.
819 #
820 # Example:
821 # @CODE
822 # src_install() {
823 # python_foreach_impl python_doheader foo.h bar.h
824 # }
825 # @CODE
826 python_doheader() {
827 debug-print-function ${FUNCNAME} "${@}"
828
829 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
830
831 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
832 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
833
834 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
835
836 local INSDESTTREE
837
838 insinto "${d}"
839 doins -r "${@}" || die
840 }
841
842 # @FUNCTION: python_wrapper_setup
843 # @USAGE: [<path> [<impl>]]
844 # @DESCRIPTION:
845 # Create proper 'python' executable and pkg-config wrappers
846 # (if available) in the directory named by <path>. Set up PATH
847 # and PKG_CONFIG_PATH appropriately. <path> defaults to ${T}/${EPYTHON}.
848 #
849 # The wrappers will be created for implementation named by <impl>,
850 # or for one named by ${EPYTHON} if no <impl> passed.
851 #
852 # If the named directory contains a python symlink already, it will
853 # be assumed to contain proper wrappers already and only environment
854 # setup will be done. If wrapper update is requested, the directory
855 # shall be removed first.
856 python_wrapper_setup() {
857 debug-print-function ${FUNCNAME} "${@}"
858
859 local workdir=${1:-${T}/${EPYTHON}}
860 local impl=${2:-${EPYTHON}}
861
862 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
863 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
864
865 if [[ ! -x ${workdir}/bin/python ]]; then
866 mkdir -p "${workdir}"/{bin,pkgconfig} || die
867
868 # Clean up, in case we were supposed to do a cheap update.
869 rm -f "${workdir}"/bin/python{,2,3,-config}
870 rm -f "${workdir}"/bin/2to3
871 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc
872
873 local EPYTHON PYTHON
874 python_export "${impl}" EPYTHON PYTHON
875
876 local pyver
877 if [[ ${EPYTHON} == python3* ]]; then
878 pyver=3
879 else # includes pypy & jython
880 pyver=2
881 fi
882
883 # Python interpreter
884 ln -s "${PYTHON}" "${workdir}"/bin/python || die
885 ln -s python "${workdir}"/bin/python${pyver} || die
886
887 local nonsupp=()
888
889 # CPython-specific
890 if [[ ${EPYTHON} == python* ]]; then
891 ln -s "${PYTHON}-config" "${workdir}"/bin/python-config || die
892
893 # Python 2.6+.
894 if [[ ${EPYTHON} != python2.5 ]]; then
895 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
896 else
897 nonsupp+=( 2to3 )
898 fi
899
900 # Python 2.7+.
901 if [[ ${EPYTHON} != python2.[56] ]]; then
902 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
903 "${workdir}"/pkgconfig/python.pc || die
904 else
905 # XXX?
906 ln -s /dev/null "${workdir}"/pkgconfig/python.pc || die
907 fi
908 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
909 else
910 nonsupp+=( 2to3 python-config )
911 fi
912
913 local x
914 for x in "${nonsupp[@]}"; do
915 cat >"${workdir}"/bin/${x} <<__EOF__
916 #!/bin/sh
917 echo "${x} is not supported by ${EPYTHON}" >&2
918 exit 1
919 __EOF__
920 chmod +x "${workdir}"/bin/${x} || die
921 done
922
923 # Now, set the environment.
924 # But note that ${workdir} may be shared with something else,
925 # and thus already on top of PATH.
926 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
927 PATH=${workdir}/bin${PATH:+:${PATH}}
928 fi
929 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
930 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
931 fi
932 export PATH PKG_CONFIG_PATH
933 fi
934 }
935
936 _PYTHON_UTILS_R1=1
937 fi

  ViewVC Help
Powered by ViewVC 1.1.20