/[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.12 - (show annotations) (download)
Wed Jan 2 21:12:44 2013 UTC (19 months, 2 weeks ago) by mgorny
Branch: MAIN
Changes since 1.11: +3 -4 lines
Remove myself from explicit maintainers, it is enough to assign the bugs to Python team.

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.11 2012/12/30 14:17:29 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
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
45 pypy1_8 pypy1_9 pypy2_0
46 python3_1 python3_2 python3_3
47 python2_5 python2_6 python2_7
48 )
49
50 # @ECLASS-VARIABLE: PYTHON
51 # @DESCRIPTION:
52 # The absolute path to the current Python interpreter.
53 #
54 # Set and exported only in commands run by python_foreach_impl().
55 #
56 # Example value:
57 # @CODE
58 # /usr/bin/python2.6
59 # @CODE
60
61 # @ECLASS-VARIABLE: EPYTHON
62 # @DESCRIPTION:
63 # The executable name of the current Python interpreter.
64 #
65 # This variable is used consistently with python.eclass.
66 #
67 # Set and exported only in commands run by python_foreach_impl().
68 #
69 # Example value:
70 # @CODE
71 # python2.6
72 # @CODE
73
74 # @ECLASS-VARIABLE: PYTHON_SITEDIR
75 # @DESCRIPTION:
76 # The path to Python site-packages directory.
77 #
78 # Set and exported on request using python_export().
79 #
80 # Example value:
81 # @CODE
82 # /usr/lib64/python2.6/site-packages
83 # @CODE
84
85 # @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
86 # @DESCRIPTION:
87 # The path to Python include directory.
88 #
89 # Set and exported on request using python_export().
90 #
91 # Example value:
92 # @CODE
93 # /usr/include/python2.6
94 # @CODE
95
96 # @ECLASS-VARIABLE: PYTHON_PKG_DEP
97 # @DESCRIPTION:
98 # The complete dependency on a particular Python package as a string.
99 #
100 # Set and exported on request using python_export().
101 #
102 # Example value:
103 # @CODE
104 # dev-lang/python:2.7[xml]
105 # @CODE
106
107 # @FUNCTION: python_export
108 # @USAGE: [<impl>] <variables>...
109 # @DESCRIPTION:
110 # Set and export the Python implementation-relevant variables passed
111 # as parameters.
112 #
113 # The optional first parameter may specify the requested Python
114 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
115 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
116 # the current one will be obtained from ${EPYTHON}.
117 #
118 # The variables which can be exported are: PYTHON, EPYTHON,
119 # PYTHON_SITEDIR. They are described more completely in the eclass
120 # variable documentation.
121 python_export() {
122 debug-print-function ${FUNCNAME} "${@}"
123
124 local impl var
125
126 case "${1}" in
127 python*|jython*)
128 impl=${1/_/.}
129 shift
130 ;;
131 pypy-c*)
132 impl=${1}
133 shift
134 ;;
135 pypy*)
136 local v=${1#pypy}
137 impl=pypy-c${v/_/.}
138 shift
139 ;;
140 *)
141 impl=${EPYTHON}
142 [[ ${impl} ]] || die "python_export: no impl nor EPYTHON"
143 ;;
144 esac
145 debug-print "${FUNCNAME}: implementation: ${impl}"
146
147 for var; do
148 case "${var}" in
149 EPYTHON)
150 export EPYTHON=${impl}
151 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
152 ;;
153 PYTHON)
154 export PYTHON=${EPREFIX}/usr/bin/${impl}
155 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
156 ;;
157 PYTHON_SITEDIR)
158 local dir
159 case "${impl}" in
160 python*)
161 dir=/usr/$(get_libdir)/${impl}
162 ;;
163 jython*)
164 dir=/usr/share/${impl}/Lib
165 ;;
166 pypy*)
167 dir=/usr/$(get_libdir)/${impl/-c/}
168 ;;
169 esac
170
171 export PYTHON_SITEDIR=${EPREFIX}${dir}/site-packages
172 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
173 ;;
174 PYTHON_INCLUDEDIR)
175 local dir
176 case "${impl}" in
177 python*)
178 dir=/usr/include/${impl}
179 ;;
180 jython*)
181 dir=/usr/share/${impl}/Include
182 ;;
183 pypy*)
184 dir=/usr/$(get_libdir)/${impl/-c/}/include
185 ;;
186 esac
187
188 export PYTHON_INCLUDEDIR=${EPREFIX}${dir}
189 debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
190 ;;
191 PYTHON_PKG_DEP)
192 local d
193 case ${impl} in
194 python*)
195 PYTHON_PKG_DEP='dev-lang/python';;
196 jython*)
197 PYTHON_PKG_DEP='dev-java/jython';;
198 pypy*)
199 PYTHON_PKG_DEP='dev-python/pypy';;
200 *)
201 die "Invalid implementation: ${impl}"
202 esac
203
204 # slot
205 PYTHON_PKG_DEP+=:${impl##*[a-z-]}
206
207 # use-dep
208 if [[ ${PYTHON_REQ_USE} ]]; then
209 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
210 fi
211
212 export PYTHON_PKG_DEP
213 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
214 ;;
215 *)
216 die "python_export: unknown variable ${var}"
217 esac
218 done
219 }
220
221 # @FUNCTION: python_get_PYTHON
222 # @USAGE: [<impl>]
223 # @DESCRIPTION:
224 # Obtain and print the path to the Python interpreter for the given
225 # implementation. If no implementation is provided, ${EPYTHON} will
226 # be used.
227 #
228 # If you just need to have PYTHON set (and exported), then it is better
229 # to use python_export() directly instead.
230 python_get_PYTHON() {
231 debug-print-function ${FUNCNAME} "${@}"
232
233 python_export "${@}" PYTHON
234 echo "${PYTHON}"
235 }
236
237 # @FUNCTION: python_get_EPYTHON
238 # @USAGE: <impl>
239 # @DESCRIPTION:
240 # Obtain and print the EPYTHON value for the given implementation.
241 #
242 # If you just need to have EPYTHON set (and exported), then it is better
243 # to use python_export() directly instead.
244 python_get_EPYTHON() {
245 debug-print-function ${FUNCNAME} "${@}"
246
247 python_export "${@}" EPYTHON
248 echo "${EPYTHON}"
249 }
250
251 # @FUNCTION: python_get_sitedir
252 # @USAGE: [<impl>]
253 # @DESCRIPTION:
254 # Obtain and print the 'site-packages' path for the given
255 # implementation. If no implementation is provided, ${EPYTHON} will
256 # be used.
257 #
258 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
259 # better to use python_export() directly instead.
260 python_get_sitedir() {
261 debug-print-function ${FUNCNAME} "${@}"
262
263 python_export "${@}" PYTHON_SITEDIR
264 echo "${PYTHON_SITEDIR}"
265 }
266
267 # @FUNCTION: python_get_includedir
268 # @USAGE: [<impl>]
269 # @DESCRIPTION:
270 # Obtain and print the include path for the given implementation. If no
271 # implementation is provided, ${EPYTHON} will be used.
272 #
273 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
274 # is better to use python_export() directly instead.
275 python_get_includedir() {
276 debug-print-function ${FUNCNAME} "${@}"
277
278 python_export "${@}" PYTHON_INCLUDEDIR
279 echo "${PYTHON_INCLUDEDIR}"
280 }
281
282 # @FUNCTION: _python_rewrite_shebang
283 # @INTERNAL
284 # @USAGE: [<EPYTHON>] <path>...
285 # @DESCRIPTION:
286 # Replaces 'python' executable in the shebang with the executable name
287 # of the specified interpreter. If no EPYTHON value (implementation) is
288 # used, the current ${EPYTHON} will be used.
289 #
290 # All specified files must start with a 'python' shebang. A file not
291 # having a matching shebang will be refused. The exact shebang style
292 # will be preserved in order not to break anything.
293 #
294 # Example conversions:
295 # @CODE
296 # From: #!/usr/bin/python -R
297 # To: #!/usr/bin/python2.7 -R
298 #
299 # From: #!/usr/bin/env FOO=bar python
300 # To: #!/usr/bin/env FOO=bar python2.7
301 # @CODE
302 _python_rewrite_shebang() {
303 debug-print-function ${FUNCNAME} "${@}"
304
305 local impl
306 case "${1}" in
307 python*|jython*|pypy-c*)
308 impl=${1}
309 shift
310 ;;
311 *)
312 impl=${EPYTHON}
313 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
314 ;;
315 esac
316 debug-print "${FUNCNAME}: implementation: ${impl}"
317
318 local f
319 for f; do
320 local shebang=$(head -n 1 "${f}")
321 debug-print "${FUNCNAME}: path = ${f}"
322 debug-print "${FUNCNAME}: shebang = ${shebang}"
323
324 if [[ "${shebang} " != *'python '* ]]; then
325 eerror "A file does not seem to have a supported shebang:"
326 eerror " file: ${f}"
327 eerror " shebang: ${shebang}"
328 die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
329 fi
330
331 sed -i -e "1s:python:${impl}:" "${f}" || die
332 done
333 }
334
335 # @FUNCTION: _python_ln_rel
336 # @INTERNAL
337 # @USAGE: <from> <to>
338 # @DESCRIPTION:
339 # Create a relative symlink.
340 _python_ln_rel() {
341 debug-print-function ${FUNCNAME} "${@}"
342
343 local from=${1}
344 local to=${2}
345
346 local frpath=${from%/*}/
347 local topath=${to%/*}/
348 local rel_path=
349
350 while [[ ${topath} ]]; do
351 local frseg= toseg=
352
353 while [[ ! ${frseg} && ${frpath} ]]; do
354 frseg=${frpath%%/*}
355 frpath=${frpath#${frseg}/}
356 done
357
358 while [[ ! ${toseg} && ${topath} ]]; do
359 toseg=${topath%%/*}
360 topath=${topath#${toseg}/}
361 done
362
363 if [[ ${frseg} != ${toseg} ]]; then
364 rel_path=../${rel_path}${frseg:+${frseg}/}
365 fi
366 done
367 rel_path+=${frpath}${1##*/}
368
369 debug-print "${FUNCNAME}: ${from} -> ${to}"
370 debug-print "${FUNCNAME}: rel_path = ${rel_path}"
371
372 ln -fs "${rel_path}" "${to}"
373 }
374
375 # @FUNCTION: python_optimize
376 # @USAGE: [<directory>...]
377 # @DESCRIPTION:
378 # Compile and optimize Python modules in specified directories (absolute
379 # paths). If no directories are provided, the default system paths
380 # are used (prepended with ${D}).
381 python_optimize() {
382 debug-print-function ${FUNCNAME} "${@}"
383
384 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
385
386 local PYTHON=${PYTHON}
387 [[ ${PYTHON} ]] || python_export PYTHON
388
389 # Note: python2.6 can't handle passing files to compileall...
390
391 # default to sys.path
392 if [[ ${#} -eq 0 ]]; then
393 local f
394 while IFS= read -r -d '' f; do
395 # 1) accept only absolute paths
396 # (i.e. skip '', '.' or anything like that)
397 # 2) skip paths which do not exist
398 # (python2.6 complains about them verbosely)
399
400 if [[ ${f} == /* && -d ${D}${f} ]]; then
401 set -- "${D}${f}" "${@}"
402 fi
403 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))')
404
405 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
406 fi
407
408 local d
409 for d; do
410 # make sure to get a nice path without //
411 local instpath=${d#${D}}
412 instpath=/${instpath##/}
413
414 case "${EPYTHON}" in
415 python*)
416 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
417 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
418 ;;
419 *)
420 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${@}"
421 ;;
422 esac
423 done
424 }
425
426 # @ECLASS-VARIABLE: python_scriptroot
427 # @DEFAULT_UNSET
428 # @DESCRIPTION:
429 # The current script destination for python_doscript(). The path
430 # is relative to the installation root (${ED}).
431 #
432 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
433 #
434 # Can be set indirectly through the python_scriptinto() function.
435 #
436 # Example:
437 # @CODE
438 # src_install() {
439 # local python_scriptroot=${GAMES_BINDIR}
440 # python_foreach_impl python_doscript foo
441 # }
442 # @CODE
443
444 # @FUNCTION: python_scriptinto
445 # @USAGE: <new-path>
446 # @DESCRIPTION:
447 # Set the current scriptroot. The new value will be stored
448 # in the 'python_scriptroot' environment variable. The new value need
449 # be relative to the installation root (${ED}).
450 #
451 # Alternatively, you can set the variable directly.
452 python_scriptinto() {
453 debug-print-function ${FUNCNAME} "${@}"
454
455 python_scriptroot=${1}
456 }
457
458 # @FUNCTION: python_doscript
459 # @USAGE: <files>...
460 # @DESCRIPTION:
461 # Install the given scripts into current python_scriptroot,
462 # for the current Python implementation (${EPYTHON}).
463 #
464 # All specified files must start with a 'python' shebang. The shebang
465 # will be converted, the file will be renamed to be EPYTHON-suffixed
466 # and a wrapper will be installed in place of the original name.
467 #
468 # Example:
469 # @CODE
470 # src_install() {
471 # python_foreach_impl python_doscript ${PN}
472 # }
473 # @CODE
474 python_doscript() {
475 debug-print-function ${FUNCNAME} "${@}"
476
477 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
478
479 local d=${python_scriptroot:-${DESTTREE}/bin}
480 local INSDESTTREE INSOPTIONS
481
482 insinto "${d}"
483 insopts -m755
484
485 local f
486 for f; do
487 local oldfn=${f##*/}
488 local newfn=${oldfn}-${EPYTHON}
489
490 debug-print "${FUNCNAME}: ${oldfn} -> ${newfn}"
491 newins "${f}" "${newfn}" || die
492 _python_rewrite_shebang "${ED}/${d}/${newfn}"
493
494 # install the wrapper
495 _python_ln_rel "${ED}"/usr/bin/python-exec "${ED}/${d}/${oldfn}" || die
496 done
497 }
498
499 # @ECLASS-VARIABLE: python_moduleroot
500 # @DEFAULT_UNSET
501 # @DESCRIPTION:
502 # The current module root for python_domodule(). The path can be either
503 # an absolute system path (it must start with a slash, and ${ED} will be
504 # prepended to it) or relative to the implementation's site-packages directory
505 # (then it must start with a non-slash character).
506 #
507 # When unset, the modules will be installed in the site-packages root.
508 #
509 # Can be set indirectly through the python_moduleinto() function.
510 #
511 # Example:
512 # @CODE
513 # src_install() {
514 # local python_moduleroot=bar
515 # # installs ${PYTHON_SITEDIR}/bar/baz.py
516 # python_foreach_impl python_domodule baz.py
517 # }
518 # @CODE
519
520 # @FUNCTION: python_moduleinto
521 # @USAGE: <new-path>
522 # @DESCRIPTION:
523 # Set the current module root. The new value will be stored
524 # in the 'python_moduleroot' environment variable. The new value need
525 # be relative to the site-packages root.
526 #
527 # Alternatively, you can set the variable directly.
528 python_moduleinto() {
529 debug-print-function ${FUNCNAME} "${@}"
530
531 python_moduleroot=${1}
532 }
533
534 # @FUNCTION: python_domodule
535 # @USAGE: <files>...
536 # @DESCRIPTION:
537 # Install the given modules (or packages) into the current
538 # python_moduleroot. The list can mention both modules (files)
539 # and packages (directories). All listed files will be installed
540 # for all enabled implementations, and compiled afterwards.
541 #
542 # Example:
543 # @CODE
544 # src_install() {
545 # # (${PN} being a directory)
546 # python_foreach_impl python_domodule ${PN}
547 # }
548 # @CODE
549 python_domodule() {
550 debug-print-function ${FUNCNAME} "${@}"
551
552 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
553
554 local d
555 if [[ ${python_moduleroot} == /* ]]; then
556 # absolute path
557 d=${python_moduleroot}
558 else
559 # relative to site-packages
560 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
561 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
562
563 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
564 fi
565
566 local INSDESTTREE
567
568 insinto "${d}"
569 doins -r "${@}" || die
570
571 python_optimize "${ED}/${d}"
572 }
573
574 _PYTHON_UTILS_R1=1
575 fi

  ViewVC Help
Powered by ViewVC 1.1.20