/[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.6 - (show annotations) (download)
Sat Dec 1 22:10:34 2012 UTC (19 months, 1 week ago) by mgorny
Branch: MAIN
Changes since 1.5: +3 -1 lines
Add a bit of debug.

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

  ViewVC Help
Powered by ViewVC 1.1.20