/[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.2 - (hide annotations) (download)
Sat Nov 24 21:07:14 2012 UTC (16 months, 3 weeks ago) by mgorny
Branch: MAIN
Changes since 1.1: +12 -3 lines
Fix EAPI checks, add double- and colliding include guards.

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

  ViewVC Help
Powered by ViewVC 1.1.20