Gentoo Archives: gentoo-commits

From: "Michal Gorny (mgorny)" <mgorny@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-x86 commit in eclass: python-r1.eclass python-utils-r1.eclass
Date: Sat, 24 Nov 2012 20:51:25
Message-Id: 20121124205114.4BB1020C65@flycatcher.gentoo.org
1 mgorny 12/11/24 20:51:14
2
3 Modified: python-r1.eclass
4 Added: python-utils-r1.eclass
5 Log:
6 Move common Python functions to python-utils-r1.
7
8 Revision Changes Path
9 1.21 eclass/python-r1.eclass
10
11 file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-r1.eclass?rev=1.21&view=markup
12 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-r1.eclass?rev=1.21&content-type=text/plain
13 diff : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-r1.eclass?r1=1.20&r2=1.21
14
15 Index: python-r1.eclass
16 ===================================================================
17 RCS file: /var/cvsroot/gentoo-x86/eclass/python-r1.eclass,v
18 retrieving revision 1.20
19 retrieving revision 1.21
20 diff -u -r1.20 -r1.21
21 --- python-r1.eclass 21 Nov 2012 09:04:14 -0000 1.20
22 +++ python-r1.eclass 24 Nov 2012 20:51:14 -0000 1.21
23 @@ -1,6 +1,6 @@
24 # Copyright 1999-2012 Gentoo Foundation
25 # Distributed under the terms of the GNU General Public License v2
26 -# $Header: /var/cvsroot/gentoo-x86/eclass/python-r1.eclass,v 1.20 2012/11/21 09:04:14 mgorny Exp $
27 +# $Header: /var/cvsroot/gentoo-x86/eclass/python-r1.eclass,v 1.21 2012/11/24 20:51:14 mgorny Exp $
28
29 # @ECLASS: python-r1
30 # @MAINTAINER:
31 @@ -20,9 +20,13 @@
32 # package easily. It also provides methods to easily run a command for
33 # each enabled Python implementation and duplicate the sources for them.
34 #
35 -# Please note that this eclass is mostly intended to be extended
36 -# on-request. If you find something you used in other eclasses missing,
37 -# please don't hack it around and request an enhancement instead.
38 +# Please note that python-r1 will always inherit python-utils-r1 as
39 +# well. Thus, all the functions defined there can be used
40 +# in the packages using python-r1, and there is no need ever to inherit
41 +# both.
42 +#
43 +# For more information, please see the python-r1 Developer's Guide:
44 +# http://www.gentoo.org/proj/en/Python/python-r1/dev-guide.xml
45
46 case "${EAPI}" in
47 0|1|2|3)
48 @@ -36,7 +40,7 @@
49 ;;
50 esac
51
52 -inherit multilib
53 +inherit python-utils-r1
54
55 # @ECLASS-VARIABLE: _PYTHON_ALL_IMPLS
56 # @INTERNAL
57 @@ -181,160 +185,6 @@
58 # ${WORKDIR}/foo-1.3-python2_6
59 # @CODE
60
61 -# @ECLASS-VARIABLE: PYTHON
62 -# @DESCRIPTION:
63 -# The absolute path to the current Python interpreter.
64 -#
65 -# Set and exported only in commands run by python_foreach_impl().
66 -#
67 -# Example value:
68 -# @CODE
69 -# /usr/bin/python2.6
70 -# @CODE
71 -
72 -# @ECLASS-VARIABLE: EPYTHON
73 -# @DESCRIPTION:
74 -# The executable name of the current Python interpreter.
75 -#
76 -# This variable is used consistently with python.eclass.
77 -#
78 -# Set and exported only in commands run by python_foreach_impl().
79 -#
80 -# Example value:
81 -# @CODE
82 -# python2.6
83 -# @CODE
84 -
85 -# @ECLASS-VARIABLE: PYTHON_SITEDIR
86 -# @DESCRIPTION:
87 -# The path to Python site-packages directory.
88 -#
89 -# Set and exported on request using python_export().
90 -#
91 -# Example value:
92 -# @CODE
93 -# /usr/lib64/python2.6/site-packages
94 -# @CODE
95 -
96 -# @FUNCTION: python_export
97 -# @USAGE: [<impl>] <variables>...
98 -# @DESCRIPTION:
99 -# Set and export the Python implementation-relevant variables passed
100 -# as parameters.
101 -#
102 -# The optional first parameter may specify the requested Python
103 -# implementation (either as PYTHON_TARGETS value, e.g. python2_7,
104 -# or an EPYTHON one, e.g. python2.7). If no implementation passed,
105 -# the current one will be obtained from ${EPYTHON}.
106 -#
107 -# The variables which can be exported are: PYTHON, EPYTHON,
108 -# PYTHON_SITEDIR. They are described more completely in the eclass
109 -# variable documentation.
110 -python_export() {
111 - debug-print-function ${FUNCNAME} "${@}"
112 -
113 - local impl var
114 -
115 - case "${1}" in
116 - python*|jython*)
117 - impl=${1/_/.}
118 - shift
119 - ;;
120 - pypy-c*)
121 - impl=${1}
122 - shift
123 - ;;
124 - pypy*)
125 - local v=${1#pypy}
126 - impl=pypy-c${v/_/.}
127 - shift
128 - ;;
129 - *)
130 - impl=${EPYTHON}
131 - [[ ${impl} ]] || die "python_export: no impl nor EPYTHON"
132 - ;;
133 - esac
134 - debug-print "${FUNCNAME}: implementation: ${impl}"
135 -
136 - for var; do
137 - case "${var}" in
138 - EPYTHON)
139 - export EPYTHON=${impl}
140 - debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
141 - ;;
142 - PYTHON)
143 - export PYTHON=${EPREFIX}/usr/bin/${impl}
144 - debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
145 - ;;
146 - PYTHON_SITEDIR)
147 - local dir
148 - case "${impl}" in
149 - python*)
150 - dir=/usr/$(get_libdir)/${impl}
151 - ;;
152 - jython*)
153 - dir=/usr/share/${impl}/Lib
154 - ;;
155 - pypy*)
156 - dir=/usr/$(get_libdir)/${impl/-c/}
157 - ;;
158 - esac
159 -
160 - export PYTHON_SITEDIR=${EPREFIX}${dir}/site-packages
161 - debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
162 - ;;
163 - *)
164 - die "python_export: unknown variable ${var}"
165 - esac
166 - done
167 -}
168 -
169 -# @FUNCTION: python_get_PYTHON
170 -# @USAGE: [<impl>]
171 -# @DESCRIPTION:
172 -# Obtain and print the path to the Python interpreter for the given
173 -# implementation. If no implementation is provided, ${EPYTHON} will
174 -# be used.
175 -#
176 -# If you just need to have PYTHON set (and exported), then it is better
177 -# to use python_export() directly instead.
178 -python_get_PYTHON() {
179 - debug-print-function ${FUNCNAME} "${@}"
180 -
181 - python_export "${@}" PYTHON
182 - echo "${PYTHON}"
183 -}
184 -
185 -# @FUNCTION: python_get_EPYTHON
186 -# @USAGE: <impl>
187 -# @DESCRIPTION:
188 -# Obtain and print the EPYTHON value for the given implementation.
189 -#
190 -# If you just need to have EPYTHON set (and exported), then it is better
191 -# to use python_export() directly instead.
192 -python_get_EPYTHON() {
193 - debug-print-function ${FUNCNAME} "${@}"
194 -
195 - python_export "${@}" EPYTHON
196 - echo "${EPYTHON}"
197 -}
198 -
199 -# @FUNCTION: python_get_sitedir
200 -# @USAGE: [<impl>]
201 -# @DESCRIPTION:
202 -# Obtain and print the 'site-packages' path for the given
203 -# implementation. If no implementation is provided, ${EPYTHON} will
204 -# be used.
205 -#
206 -# If you just need to have PYTHON_SITEDIR set (and exported), then it is
207 -# better to use python_export() directly instead.
208 -python_get_sitedir() {
209 - debug-print-function ${FUNCNAME} "${@}"
210 -
211 - python_export "${@}" PYTHON_SITEDIR
212 - echo "${PYTHON_SITEDIR}"
213 -}
214 -
215 # @FUNCTION: python_copy_sources
216 # @DESCRIPTION:
217 # Create a single copy of the package sources (${S}) for each enabled
218 @@ -617,97 +467,6 @@
219 python_export "${impl}" "${@}"
220 }
221
222 -# @FUNCTION: _python_rewrite_shebang
223 -# @INTERNAL
224 -# @USAGE: [<EPYTHON>] <path>...
225 -# @DESCRIPTION:
226 -# Replaces 'python' executable in the shebang with the executable name
227 -# of the specified interpreter. If no EPYTHON value (implementation) is
228 -# used, the current ${EPYTHON} will be used.
229 -#
230 -# All specified files must start with a 'python' shebang. A file not
231 -# having a matching shebang will be refused. The exact shebang style
232 -# will be preserved in order not to break anything.
233 -#
234 -# Example conversions:
235 -# @CODE
236 -# From: #!/usr/bin/python -R
237 -# To: #!/usr/bin/python2.7 -R
238 -#
239 -# From: #!/usr/bin/env FOO=bar python
240 -# To: #!/usr/bin/env FOO=bar python2.7
241 -# @CODE
242 -_python_rewrite_shebang() {
243 - debug-print-function ${FUNCNAME} "${@}"
244 -
245 - local impl
246 - case "${1}" in
247 - python*|jython*|pypy-c*)
248 - impl=${1}
249 - shift
250 - ;;
251 - *)
252 - impl=${EPYTHON}
253 - [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
254 - ;;
255 - esac
256 - debug-print "${FUNCNAME}: implementation: ${impl}"
257 -
258 - local f
259 - for f; do
260 - local shebang=$(head -n 1 "${f}")
261 - debug-print "${FUNCNAME}: path = ${f}"
262 - debug-print "${FUNCNAME}: shebang = ${shebang}"
263 -
264 - if [[ "${shebang} " != *'python '* ]]; then
265 - eerror "A file does not seem to have a supported shebang:"
266 - eerror " file: ${f}"
267 - eerror " shebang: ${shebang}"
268 - die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
269 - fi
270 -
271 - sed -i -e "1s:python:${impl}:" "${f}" || die
272 - done
273 -}
274 -
275 -# @FUNCTION: _python_ln_rel
276 -# @INTERNAL
277 -# @USAGE: <from> <to>
278 -# @DESCRIPTION:
279 -# Create a relative symlink.
280 -_python_ln_rel() {
281 - debug-print-function ${FUNCNAME} "${@}"
282 -
283 - local from=${1}
284 - local to=${2}
285 -
286 - local frpath=${from%/*}/
287 - local topath=${to%/*}/
288 - local rel_path=
289 -
290 - # remove double slashes
291 - frpath=${frpath/\/\///}
292 - topath=${topath/\/\///}
293 -
294 - while [[ ${topath} ]]; do
295 - local frseg=${frpath%%/*}
296 - local toseg=${topath%%/*}
297 -
298 - if [[ ${frseg} != ${toseg} ]]; then
299 - rel_path=../${rel_path}${frseg:+${frseg}/}
300 - fi
301 -
302 - frpath=${frpath#${frseg}/}
303 - topath=${topath#${toseg}/}
304 - done
305 - rel_path+=${frpath}${1##*/}
306 -
307 - debug-print "${FUNCNAME}: ${from} -> ${to}"
308 - debug-print "${FUNCNAME}: rel_path = ${rel_path}"
309 -
310 - ln -fs "${rel_path}" "${to}"
311 -}
312 -
313 # @FUNCTION: python_replicate_script
314 # @USAGE: <path>...
315 # @DESCRIPTION:
316 @@ -743,160 +502,3 @@
317 _python_ln_rel "${ED}"/usr/bin/python-exec "${f}" || die
318 done
319 }
320 -
321 -# @ECLASS-VARIABLE: python_scriptroot
322 -# @DEFAULT_UNSET
323 -# @DESCRIPTION:
324 -# The current script destination for python_doscript(). The path
325 -# is relative to the installation root (${ED}).
326 -#
327 -# When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
328 -#
329 -# Can be set indirectly through the python_scriptinto() function.
330 -#
331 -# Example:
332 -# @CODE
333 -# src_install() {
334 -# local python_scriptroot=${GAMES_BINDIR}
335 -# python_foreach_impl python_doscript foo
336 -# }
337 -# @CODE
338 -
339 -# @FUNCTION: python_scriptinto
340 -# @USAGE: <new-path>
341 -# @DESCRIPTION:
342 -# Set the current scriptroot. The new value will be stored
343 -# in the 'python_scriptroot' environment variable. The new value need
344 -# be relative to the installation root (${ED}).
345 -#
346 -# Alternatively, you can set the variable directly.
347 -python_scriptinto() {
348 - debug-print-function ${FUNCNAME} "${@}"
349 -
350 - python_scriptroot=${1}
351 -}
352 -
353 -# @FUNCTION: python_doscript
354 -# @USAGE: <files>...
355 -# @DESCRIPTION:
356 -# Install the given scripts into current python_scriptroot,
357 -# for the current Python implementation (${EPYTHON}).
358 -#
359 -# All specified files must start with a 'python' shebang. The shebang
360 -# will be converted, the file will be renamed to be EPYTHON-suffixed
361 -# and a wrapper will be installed in place of the original name.
362 -#
363 -# Example:
364 -# @CODE
365 -# src_install() {
366 -# python_foreach_impl python_doscript ${PN}
367 -# }
368 -# @CODE
369 -python_doscript() {
370 - debug-print-function ${FUNCNAME} "${@}"
371 -
372 - [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
373 -
374 - local d=${python_scriptroot:-${DESTTREE}/bin}
375 - local INSDESTTREE INSOPTIONS
376 -
377 - insinto "${d}"
378 - insopts -m755
379 -
380 - local f
381 - for f; do
382 - local oldfn=${f##*/}
383 - local newfn=${oldfn}-${EPYTHON}
384 -
385 - debug-print "${FUNCNAME}: ${oldfn} -> ${newfn}"
386 - newins "${f}" "${newfn}"
387 - _python_rewrite_shebang "${D}/${d}/${newfn}"
388 -
389 - # install the wrapper
390 - _python_ln_rel "${ED}"/usr/bin/python-exec "${D}/${d}/${oldfn}" || die
391 - done
392 -}
393 -
394 -# @ECLASS-VARIABLE: python_moduleroot
395 -# @DEFAULT_UNSET
396 -# @DESCRIPTION:
397 -# The current module root for python_domodule(). The path can be either
398 -# an absolute system path (it must start with a slash, and ${D} will be
399 -# prepended to it) or relative to the implementation's site-packages directory
400 -# (then it must start with a non-slash character).
401 -#
402 -# When unset, the modules will be installed in the site-packages root.
403 -#
404 -# Can be set indirectly through the python_moduleinto() function.
405 -#
406 -# Example:
407 -# @CODE
408 -# src_install() {
409 -# local python_moduleroot=bar
410 -# # installs ${PYTHON_SITEDIR}/bar/baz.py
411 -# python_foreach_impl python_domodule baz.py
412 -# }
413 -# @CODE
414 -
415 -# @FUNCTION: python_moduleinto
416 -# @USAGE: <new-path>
417 -# @DESCRIPTION:
418 -# Set the current module root. The new value will be stored
419 -# in the 'python_moduleroot' environment variable. The new value need
420 -# be relative to the site-packages root.
421 -#
422 -# Alternatively, you can set the variable directly.
423 -python_moduleinto() {
424 - debug-print-function ${FUNCNAME} "${@}"
425 -
426 - python_moduleroot=${1}
427 -}
428 -
429 -# @FUNCTION: python_domodule
430 -# @USAGE: <files>...
431 -# @DESCRIPTION:
432 -# Install the given modules (or packages) into the current
433 -# python_moduleroot. The list can mention both modules (files)
434 -# and packages (directories). All listed files will be installed
435 -# for all enabled implementations, and compiled afterwards.
436 -#
437 -# Example:
438 -# @CODE
439 -# src_install() {
440 -# # (${PN} being a directory)
441 -# python_foreach_impl python_domodule ${PN}
442 -# }
443 -# @CODE
444 -python_domodule() {
445 - debug-print-function ${FUNCNAME} "${@}"
446 -
447 - [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
448 -
449 - local d
450 - if [[ ${python_moduleroot} == /* ]]; then
451 - # absolute path
452 - d=${python_moduleroot}
453 - else
454 - # relative to site-packages
455 - local PYTHON_SITEDIR=${PYTHON_SITEDIR}
456 - [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
457 -
458 - d=${PYTHON_SITEDIR}/${python_moduleroot}
459 - fi
460 -
461 - local INSDESTTREE
462 -
463 - insinto "${d}"
464 - doins -r "${@}"
465 -
466 - # erm, python2.6 can't handle passing files to compileall...
467 - case "${EPYTHON}" in
468 - python*)
469 - "${PYTHON}" -m compileall -q "${D}/${d}"
470 - "${PYTHON}" -OO -m compileall -q -f "${D}/${d}"
471 - ;;
472 - *)
473 - "${PYTHON}" -m compileall -q "${D}/${d}"
474 - ;;
475 - esac
476 -}
477
478
479
480 1.1 eclass/python-utils-r1.eclass
481
482 file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-utils-r1.eclass?rev=1.1&view=markup
483 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-utils-r1.eclass?rev=1.1&content-type=text/plain
484
485 Index: python-utils-r1.eclass
486 ===================================================================
487 # Copyright 1999-2012 Gentoo Foundation
488 # Distributed under the terms of the GNU General Public License v2
489 # $Header: /var/cvsroot/gentoo-x86/eclass/python-utils-r1.eclass,v 1.1 2012/11/24 20:51:14 mgorny Exp $
490
491 # @ECLASS: python-utils-r1
492 # @MAINTAINER:
493 # Michał Górny <mgorny@g.o>
494 # Python herd <python@g.o>
495 # @AUTHOR:
496 # Author: Michał Górny <mgorny@g.o>
497 # Based on work of: Krzysztof Pawlik <nelchael@g.o>
498 # @BLURB: Utility functions for packages with Python parts.
499 # @DESCRIPTION:
500 # An utility eclass providing functions to query Python implementations,
501 # install Python modules and scripts.
502 #
503 # This eclass does not set any metadata variables nor export any phase
504 # functions. It can be inherited safely.
505 #
506 # For more information, please see the python-r1 Developer's Guide:
507 # http://www.gentoo.org/proj/en/Python/python-r1/dev-guide.xml
508
509 case "${EAPI}" in
510 0|1|2|3)
511 die "Unsupported EAPI=${EAPI} (too old) for ${ECLASS}"
512 ;;
513 4|5)
514 # EAPI=4 makes die behavior clear
515 ;;
516 *)
517 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
518 ;;
519 esac
520
521 inherit multilib
522
523 # @ECLASS-VARIABLE: PYTHON
524 # @DESCRIPTION:
525 # The absolute path to the current Python interpreter.
526 #
527 # Set and exported only in commands run by python_foreach_impl().
528 #
529 # Example value:
530 # @CODE
531 # /usr/bin/python2.6
532 # @CODE
533
534 # @ECLASS-VARIABLE: EPYTHON
535 # @DESCRIPTION:
536 # The executable name of the current Python interpreter.
537 #
538 # This variable is used consistently with python.eclass.
539 #
540 # Set and exported only in commands run by python_foreach_impl().
541 #
542 # Example value:
543 # @CODE
544 # python2.6
545 # @CODE
546
547 # @ECLASS-VARIABLE: PYTHON_SITEDIR
548 # @DESCRIPTION:
549 # The path to Python site-packages directory.
550 #
551 # Set and exported on request using python_export().
552 #
553 # Example value:
554 # @CODE
555 # /usr/lib64/python2.6/site-packages
556 # @CODE
557
558 # @FUNCTION: python_export
559 # @USAGE: [<impl>] <variables>...
560 # @DESCRIPTION:
561 # Set and export the Python implementation-relevant variables passed
562 # as parameters.
563 #
564 # The optional first parameter may specify the requested Python
565 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
566 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
567 # the current one will be obtained from ${EPYTHON}.
568 #
569 # The variables which can be exported are: PYTHON, EPYTHON,
570 # PYTHON_SITEDIR. They are described more completely in the eclass
571 # variable documentation.
572 python_export() {
573 debug-print-function ${FUNCNAME} "${@}"
574
575 local impl var
576
577 case "${1}" in
578 python*|jython*)
579 impl=${1/_/.}
580 shift
581 ;;
582 pypy-c*)
583 impl=${1}
584 shift
585 ;;
586 pypy*)
587 local v=${1#pypy}
588 impl=pypy-c${v/_/.}
589 shift
590 ;;
591 *)
592 impl=${EPYTHON}
593 [[ ${impl} ]] || die "python_export: no impl nor EPYTHON"
594 ;;
595 esac
596 debug-print "${FUNCNAME}: implementation: ${impl}"
597
598 for var; do
599 case "${var}" in
600 EPYTHON)
601 export EPYTHON=${impl}
602 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
603 ;;
604 PYTHON)
605 export PYTHON=${EPREFIX}/usr/bin/${impl}
606 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
607 ;;
608 PYTHON_SITEDIR)
609 local dir
610 case "${impl}" in
611 python*)
612 dir=/usr/$(get_libdir)/${impl}
613 ;;
614 jython*)
615 dir=/usr/share/${impl}/Lib
616 ;;
617 pypy*)
618 dir=/usr/$(get_libdir)/${impl/-c/}
619 ;;
620 esac
621
622 export PYTHON_SITEDIR=${EPREFIX}${dir}/site-packages
623 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
624 ;;
625 *)
626 die "python_export: unknown variable ${var}"
627 esac
628 done
629 }
630
631 # @FUNCTION: python_get_PYTHON
632 # @USAGE: [<impl>]
633 # @DESCRIPTION:
634 # Obtain and print the path to the Python interpreter for the given
635 # implementation. If no implementation is provided, ${EPYTHON} will
636 # be used.
637 #
638 # If you just need to have PYTHON set (and exported), then it is better
639 # to use python_export() directly instead.
640 python_get_PYTHON() {
641 debug-print-function ${FUNCNAME} "${@}"
642
643 python_export "${@}" PYTHON
644 echo "${PYTHON}"
645 }
646
647 # @FUNCTION: python_get_EPYTHON
648 # @USAGE: <impl>
649 # @DESCRIPTION:
650 # Obtain and print the EPYTHON value for the given implementation.
651 #
652 # If you just need to have EPYTHON set (and exported), then it is better
653 # to use python_export() directly instead.
654 python_get_EPYTHON() {
655 debug-print-function ${FUNCNAME} "${@}"
656
657 python_export "${@}" EPYTHON
658 echo "${EPYTHON}"
659 }
660
661 # @FUNCTION: python_get_sitedir
662 # @USAGE: [<impl>]
663 # @DESCRIPTION:
664 # Obtain and print the 'site-packages' path for the given
665 # implementation. If no implementation is provided, ${EPYTHON} will
666 # be used.
667 #
668 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
669 # better to use python_export() directly instead.
670 python_get_sitedir() {
671 debug-print-function ${FUNCNAME} "${@}"
672
673 python_export "${@}" PYTHON_SITEDIR
674 echo "${PYTHON_SITEDIR}"
675 }
676
677 # @FUNCTION: _python_rewrite_shebang
678 # @INTERNAL
679 # @USAGE: [<EPYTHON>] <path>...
680 # @DESCRIPTION:
681 # Replaces 'python' executable in the shebang with the executable name
682 # of the specified interpreter. If no EPYTHON value (implementation) is
683 # used, the current ${EPYTHON} will be used.
684 #
685 # All specified files must start with a 'python' shebang. A file not
686 # having a matching shebang will be refused. The exact shebang style
687 # will be preserved in order not to break anything.
688 #
689 # Example conversions:
690 # @CODE
691 # From: #!/usr/bin/python -R
692 # To: #!/usr/bin/python2.7 -R
693 #
694 # From: #!/usr/bin/env FOO=bar python
695 # To: #!/usr/bin/env FOO=bar python2.7
696 # @CODE
697 _python_rewrite_shebang() {
698 debug-print-function ${FUNCNAME} "${@}"
699
700 local impl
701 case "${1}" in
702 python*|jython*|pypy-c*)
703 impl=${1}
704 shift
705 ;;
706 *)
707 impl=${EPYTHON}
708 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
709 ;;
710 esac
711 debug-print "${FUNCNAME}: implementation: ${impl}"
712
713 local f
714 for f; do
715 local shebang=$(head -n 1 "${f}")
716 debug-print "${FUNCNAME}: path = ${f}"
717 debug-print "${FUNCNAME}: shebang = ${shebang}"
718
719 if [[ "${shebang} " != *'python '* ]]; then
720 eerror "A file does not seem to have a supported shebang:"
721 eerror " file: ${f}"
722 eerror " shebang: ${shebang}"
723 die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
724 fi
725
726 sed -i -e "1s:python:${impl}:" "${f}" || die
727 done
728 }
729
730 # @FUNCTION: _python_ln_rel
731 # @INTERNAL
732 # @USAGE: <from> <to>
733 # @DESCRIPTION:
734 # Create a relative symlink.
735 _python_ln_rel() {
736 debug-print-function ${FUNCNAME} "${@}"
737
738 local from=${1}
739 local to=${2}
740
741 local frpath=${from%/*}/
742 local topath=${to%/*}/
743 local rel_path=
744
745 # remove double slashes
746 frpath=${frpath/\/\///}
747 topath=${topath/\/\///}
748
749 while [[ ${topath} ]]; do
750 local frseg=${frpath%%/*}
751 local toseg=${topath%%/*}
752
753 if [[ ${frseg} != ${toseg} ]]; then
754 rel_path=../${rel_path}${frseg:+${frseg}/}
755 fi
756
757 frpath=${frpath#${frseg}/}
758 topath=${topath#${toseg}/}
759 done
760 rel_path+=${frpath}${1##*/}
761
762 debug-print "${FUNCNAME}: ${from} -> ${to}"
763 debug-print "${FUNCNAME}: rel_path = ${rel_path}"
764
765 ln -fs "${rel_path}" "${to}"
766 }
767
768 # @ECLASS-VARIABLE: python_scriptroot
769 # @DEFAULT_UNSET
770 # @DESCRIPTION:
771 # The current script destination for python_doscript(). The path
772 # is relative to the installation root (${ED}).
773 #
774 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
775 #
776 # Can be set indirectly through the python_scriptinto() function.
777 #
778 # Example:
779 # @CODE
780 # src_install() {
781 # local python_scriptroot=${GAMES_BINDIR}
782 # python_foreach_impl python_doscript foo
783 # }
784 # @CODE
785
786 # @FUNCTION: python_scriptinto
787 # @USAGE: <new-path>
788 # @DESCRIPTION:
789 # Set the current scriptroot. The new value will be stored
790 # in the 'python_scriptroot' environment variable. The new value need
791 # be relative to the installation root (${ED}).
792 #
793 # Alternatively, you can set the variable directly.
794 python_scriptinto() {
795 debug-print-function ${FUNCNAME} "${@}"
796
797 python_scriptroot=${1}
798 }
799
800 # @FUNCTION: python_doscript
801 # @USAGE: <files>...
802 # @DESCRIPTION:
803 # Install the given scripts into current python_scriptroot,
804 # for the current Python implementation (${EPYTHON}).
805 #
806 # All specified files must start with a 'python' shebang. The shebang
807 # will be converted, the file will be renamed to be EPYTHON-suffixed
808 # and a wrapper will be installed in place of the original name.
809 #
810 # Example:
811 # @CODE
812 # src_install() {
813 # python_foreach_impl python_doscript ${PN}
814 # }
815 # @CODE
816 python_doscript() {
817 debug-print-function ${FUNCNAME} "${@}"
818
819 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
820
821 local d=${python_scriptroot:-${DESTTREE}/bin}
822 local INSDESTTREE INSOPTIONS
823
824 insinto "${d}"
825 insopts -m755
826
827 local f
828 for f; do
829 local oldfn=${f##*/}
830 local newfn=${oldfn}-${EPYTHON}
831
832 debug-print "${FUNCNAME}: ${oldfn} -> ${newfn}"
833 newins "${f}" "${newfn}"
834 _python_rewrite_shebang "${D}/${d}/${newfn}"
835
836 # install the wrapper
837 _python_ln_rel "${ED}"/usr/bin/python-exec "${D}/${d}/${oldfn}" || die
838 done
839 }
840
841 # @ECLASS-VARIABLE: python_moduleroot
842 # @DEFAULT_UNSET
843 # @DESCRIPTION:
844 # The current module root for python_domodule(). The path can be either
845 # an absolute system path (it must start with a slash, and ${D} will be
846 # prepended to it) or relative to the implementation's site-packages directory
847 # (then it must start with a non-slash character).
848 #
849 # When unset, the modules will be installed in the site-packages root.
850 #
851 # Can be set indirectly through the python_moduleinto() function.
852 #
853 # Example:
854 # @CODE
855 # src_install() {
856 # local python_moduleroot=bar
857 # # installs ${PYTHON_SITEDIR}/bar/baz.py
858 # python_foreach_impl python_domodule baz.py
859 # }
860 # @CODE
861
862 # @FUNCTION: python_moduleinto
863 # @USAGE: <new-path>
864 # @DESCRIPTION:
865 # Set the current module root. The new value will be stored
866 # in the 'python_moduleroot' environment variable. The new value need
867 # be relative to the site-packages root.
868 #
869 # Alternatively, you can set the variable directly.
870 python_moduleinto() {
871 debug-print-function ${FUNCNAME} "${@}"
872
873 python_moduleroot=${1}
874 }
875
876 # @FUNCTION: python_domodule
877 # @USAGE: <files>...
878 # @DESCRIPTION:
879 # Install the given modules (or packages) into the current
880 # python_moduleroot. The list can mention both modules (files)
881 # and packages (directories). All listed files will be installed
882 # for all enabled implementations, and compiled afterwards.
883 #
884 # Example:
885 # @CODE
886 # src_install() {
887 # # (${PN} being a directory)
888 # python_foreach_impl python_domodule ${PN}
889 # }
890 # @CODE
891 python_domodule() {
892 debug-print-function ${FUNCNAME} "${@}"
893
894 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
895
896 local d
897 if [[ ${python_moduleroot} == /* ]]; then
898 # absolute path
899 d=${python_moduleroot}
900 else
901 # relative to site-packages
902 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
903 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
904
905 d=${PYTHON_SITEDIR}/${python_moduleroot}
906 fi
907
908 local INSDESTTREE
909
910 insinto "${d}"
911 doins -r "${@}"
912
913 local PYTHON=${PYTHON}
914 [[ ${PYTHON} ]] || python_export PYTHON
915
916 # erm, python2.6 can't handle passing files to compileall...
917 case "${EPYTHON}" in
918 python*)
919 "${PYTHON}" -m compileall -q "${D}/${d}"
920 "${PYTHON}" -OO -m compileall -q -f "${D}/${d}"
921 ;;
922 *)
923 "${PYTHON}" -m compileall -q "${D}/${d}"
924 ;;
925 esac
926 }