/[gentoo-x86]/eclass/python-distutils-ng.eclass
Gentoo

Contents of /eclass/python-distutils-ng.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations) (download)
Tue Apr 3 19:21:45 2012 UTC (2 years, 4 months ago) by nelchael
Branch: MAIN
Changes since 1.10: +14 -1 lines
Add function that makes it easier to properly install Python scripts that get installed automatically by distutils.

1 nelchael 1.1 # Copyright 1999-2012 Gentoo Foundation
2     # Distributed under the terms of the GNU General Public License v2
3 nelchael 1.11 # $Header: /var/cvsroot/gentoo-x86/eclass/python-distutils-ng.eclass,v 1.10 2012/04/03 19:12:46 nelchael Exp $
4 nelchael 1.1
5     # @ECLASS: python-distutils-ng
6     # @MAINTAINER:
7     # Python herd <python@gentoo.org>
8     # @AUTHOR:
9     # Author: Krzysztof Pawlik <nelchael@gentoo.org>
10     # @BLURB: An eclass for installing Python packages using distutils with proper
11     # support for multiple Python slots.
12     # @DESCRIPTION:
13     # The Python eclass is designed to allow an easier installation of Python
14     # packages and their incorporation into the Gentoo Linux system.
15     #
16     # This eclass provides functions for following phases:
17     # - src_prepare - you can define python_prepare_all function that will be run
18     # before creating implementation-specific directory and python_prepare
19     # function that will be run for each implementation
20     # - src_configure - you can define python_configure function that will be run
21     # for each implementation
22     # - src_compile - you can define python_compile function that will be run for
23     # each implementation, default function will run `setup.py build'
24     # - src_test - you can define python_test function that will be run for each
25     # implementation
26     # - src_install - you can define python_install function that will be run for
27     # each implementation and python_install_all that will be run in original
28 nelchael 1.9 # directory (so it will not contain any implementation-specific files)
29 nelchael 1.1
30     # @ECLASS-VARIABLE: PYTHON_COMPAT
31     # @DESCRIPTION:
32     # This variable contains a space separated list of implementations (see above) a
33     # package is compatible to. It must be set before the `inherit' call. The
34     # default is to enable all implementations.
35    
36     if [[ -z "${PYTHON_COMPAT}" ]]; then
37     # Default: pure python, support all implementations
38     PYTHON_COMPAT=" python2_5 python2_6 python2_7"
39     PYTHON_COMPAT+=" python3_1 python3_2"
40     PYTHON_COMPAT+=" jython2_5"
41     PYTHON_COMPAT+=" pypy1_7 pypy1_8"
42     fi
43    
44     # @ECLASS-VARIABLE: PYTHON_OPTIONAL
45     # @DESCRIPTION:
46     # Set the value to "yes" to make the dependency on a Python interpreter
47     # optional.
48    
49     # @ECLASS-VARIABLE: PYTHON_DISABLE_COMPILATION
50     # @DESCRIPTION:
51     # Set the value to "yes" to skip compilation and/or optimization of Python
52     # modules.
53    
54 nelchael 1.10 EXPORT_FUNCTIONS pkg_pretend src_prepare src_configure src_compile src_test src_install
55 nelchael 1.1
56     case "${EAPI}" in
57     0|1|2|3)
58     die "Unsupported EAPI=${EAPI} (too old) for python-distutils-ng.eclass" ;;
59     4)
60     # EAPI=4 needed for REQUIRED_USE
61     S="${S:-${WORKDIR}/${P}}"
62     ;;
63     *)
64     die "Unsupported EAPI=${EAPI} (unknown) for python-distutils-ng.eclass" ;;
65     esac
66    
67     # @FUNCTION: _python-distutils-ng_get_binary_for_implementation
68     # @USAGE: implementation
69     # @RETURN: Full path to Python binary for given implementation.
70     # @DESCRIPTION:
71     # This function returns full path for Python binary for given implementation.
72     #
73     # Binary returned by this function should be used instead of simply calling
74     # `python'.
75     _python-distutils-ng_get_binary_for_implementation() {
76     local impl="${1/_/.}"
77     case "${impl}" in
78     python?.?|jython?.?)
79     echo "/usr/bin/${impl}" ;;
80     pypy?.?)
81     echo "/usr/bin/pypy-c${impl: -3}" ;;
82     *)
83     die "Unsupported implementation: ${1}" ;;
84     esac
85     }
86    
87 nelchael 1.3 required_use_str=""
88     for impl in ${PYTHON_COMPAT}; do
89 nelchael 1.4 required_use_str+=" python_targets_${impl}"
90 nelchael 1.3 done
91     required_use_str=" || ( ${required_use_str} )"
92 nelchael 1.1 if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then
93 nelchael 1.4 IUSE+=" python"
94 nelchael 1.1 REQUIRED_USE+=" python? ( ${required_use_str} )"
95     else
96 jlec 1.2 REQUIRED_USE+=" ${required_use_str}"
97 nelchael 1.1 fi
98 nelchael 1.4 unset required_use_str
99 nelchael 1.1
100     for impl in ${PYTHON_COMPAT}; do
101 nelchael 1.4 IUSE+=" python_targets_${impl}"
102 nelchael 1.6 dep_str="${impl/_/.}"
103     case "${dep_str}" in
104     python?.?)
105 marienz 1.8 dep_str="dev-lang/python:${dep_str: -3}" ;;
106 nelchael 1.6 jython?.?)
107 marienz 1.8 dep_str="dev-java/jython:${dep_str: -3}" ;;
108 nelchael 1.6 pypy?.?)
109 marienz 1.8 dep_str="dev-python/pypy:${dep_str: -3}" ;;
110 nelchael 1.6 *)
111     die "Unsupported implementation: ${impl}" ;;
112     esac
113     dep_str="python_targets_${impl}? ( ${dep_str} )"
114 nelchael 1.1
115     if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then
116     RDEPEND="${RDEPEND} python? ( ${dep_str} )"
117     DEPEND="${DEPEND} python? ( ${dep_str} )"
118     else
119     RDEPEND="${RDEPEND} ${dep_str}"
120     DEPEND="${DEPEND} ${dep_str}"
121     fi
122 nelchael 1.4 unset dep_str
123 nelchael 1.1 done
124    
125     _PACKAGE_SPECIFIC_S="${S#${WORKDIR}/}"
126    
127     # @FUNCTION: _python-distutils-ng_run_for_impl
128     # @USAGE: implementation command_to_run
129     # @DESCRIPTION:
130     # Run command_to_run using specified Python implementation.
131     #
132     # This will run the command_to_run in implementation-specific working directory.
133     _python-distutils-ng_run_for_impl() {
134     local impl="${1}"
135     local command="${2}"
136    
137     S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}"
138     PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
139     EPYTHON="${impl/_/.}"
140    
141     einfo "Running ${command} in ${S} for ${impl}"
142    
143     pushd "${S}" &> /dev/null
144     "${command}" "${impl}" "${PYTHON}"
145     popd &> /dev/null
146     }
147    
148     # @FUNCTION: _python-distutils-ng_run_for_each_impl
149     # @USAGE: command_to_run
150     # @DESCRIPTION:
151     # Run command_to_run for all enabled Python implementations.
152     #
153     # See also _python-distutils-ng_run_for_impl
154     _python-distutils-ng_run_for_each_impl() {
155     local command="${1}"
156    
157     for impl in ${PYTHON_COMPAT}; do
158     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
159     _python-distutils-ng_run_for_impl "${impl}" "${command}"
160     done
161     }
162    
163     # @FUNCTION: _python-distutils-ng_default_distutils_compile
164     # @DESCRIPTION:
165     # Default src_compile for distutils-based packages.
166     _python-distutils-ng_default_distutils_compile() {
167     "${PYTHON}" setup.py build || die
168     }
169    
170     # @FUNCTION: _python-distutils-ng_default_distutils_install
171     # @DESCRIPTION:
172     # Default src_install for distutils-based packages.
173     _python-distutils-ng_default_distutils_install() {
174     "${PYTHON}" setup.py install --no-compile --root="${D}/" || die
175     }
176    
177     # @FUNCTION: _python-distutils-ng_has_compileall
178     # @USAGE: implementation
179     # @RETURN: 0 if given implementation has compileall module
180     # @DESCRIPTION:
181     # This function is used to decide whenever to compile Python modules for given
182     # implementation.
183     _python-distutils-ng_has_compileall() {
184     case "${1}" in
185     python?_?|jython?_?)
186     return 0 ;;
187     *)
188     return 1 ;;
189     esac
190     }
191    
192     # @FUNCTION: _python-distutils-ng_has_compileall_opt
193     # @USAGE: implementation
194     # @RETURN: 0 if given implementation has compileall module and supports # optimizations
195     # @DESCRIPTION:
196     # This function is used to decide whenever to compile and optimize Python
197     # modules for given implementation.
198     _python-distutils-ng_has_compileall_opt() {
199     case "${1}" in
200     python?_?)
201     return 0 ;;
202     *)
203     return 1 ;;
204     esac
205     }
206    
207 nelchael 1.11 # @FUNCTION: python-distutils-ng_redoscript
208     # @USAGE: script_file_path [destination_directory]
209     # @DESCRIPTION:
210     # Reinstall script installed already by setup.py. This works by first moving the
211     # script to ${T} directory and later running python-distutils-ng_doscript on it.
212     # script_file_path has to be a full path relative to ${D}.
213     python-distutils-ng_redoscript() {
214     local sbn="$(basename "${1}")"
215     mkdir -p "${T}/_${sbn}/" || die "failed to create directory"
216     mv "${D}/${1}" "${T}/_${sbn}/${sbn}" || die "failed to move file"
217     python-distutils-ng_doscript "${T}/_${sbn}/${sbn}" "${2}"
218     }
219    
220 nelchael 1.1 # @FUNCTION: python-distutils-ng_doscript
221     # @USAGE: script_file_name [destination_directory]
222     # @DESCRIPTION:
223     # Install given script file in destination directory (for default value check
224     # python-distutils-ng_newscript) for all enabled implementations using original
225     # script name as a base name.
226     #
227     # See also python-distutils-ng_newscript for more details.
228     python-distutils-ng_doscript() {
229     python-distutils-ng_newscript "${1}" "$(basename "${1}")" "${2}"
230     }
231    
232     # @FUNCTION: python-distutils-ng_newscript
233     # @USAGE: script_file_name new_file_name [destination_directory]
234     # @DESCRIPTION:
235     # Install given script file in destination directory for all enabled
236     # implementations using new_file_name as a base name.
237     #
238     # Destination directory defaults to /usr/bin.
239     #
240     # If only one Python implementation is enabled the script will be installed
241     # as-is. Otherwise each script copy will have the name mangled to
242     # "new_file_name-IMPLEMENTATION". For every installed script new hash-bang line
243     # will be inserted to reference specific Python interpreter.
244     #
245     # In case of multiple implementations there will be also a symlink with name
246     # equal to new_file_name that will be a symlink to default implementation, which
247     # defaults to value of PYTHON_DEFAULT_IMPLEMENTATION, if not specified the
248     # function will pick default implementation: it will the be first enabled one
249     # from the following list:
250     # python2_7, python2_6, python2_5, python3_2, python3_1, pypy1_8, pypy1_7, jython2_5
251     python-distutils-ng_newscript() {
252     [[ -n "${1}" ]] || die "Missing source file name"
253     [[ -n "${2}" ]] || die "Missing destination file name"
254     local source_file="${1}"
255     local destination_file="${2}"
256     local default_impl="${PYTHON_DEFAULT_IMPLEMENTATION}"
257     local enabled_impls=0
258     local destination_directory="/usr/bin"
259     [[ -n "${3}" ]] && destination_directory="${3}"
260    
261     for impl in ${PYTHON_COMPAT}; do
262     use "python_targets_${impl}" || continue
263     enabled_impls=$((enabled_impls + 1))
264     done
265    
266     if [[ -z "${default_impl}" ]]; then
267     for impl in python{2_7,2_6,2_5,3_2,3_1} pypy{1_8,1_7} jython2_5; do
268     use "python_targets_${impl}" || continue
269     default_impl="${impl}"
270     break
271     done
272     else
273 nelchael 1.9 use "python_targets_${default_impl}" || \
274 nelchael 1.1 die "default implementation ${default_impl} not enabled"
275     fi
276    
277     [[ -n "${default_impl}" ]] || die "Could not select default implementation"
278    
279     dodir "${destination_directory}"
280     insinto "${destination_directory}"
281     if [[ "${enabled_impls}" = "1" ]]; then
282     einfo "Installing ${source_file} for single implementation (${default_impl}) in ${destination_directory}"
283     newins "${source_file}" "${destination_file}"
284     fperms 755 "${destination_directory}/${destination_file}"
285     sed -i \
286     -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
287     "${D}${destination_directory}/${destination_file}" || die
288     else
289     einfo "Installing ${source_file} for multiple implementations (default: ${default_impl}) in ${destination_directory}"
290     for impl in ${PYTHON_COMPAT}; do
291     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
292    
293     newins "${source_file}" "${destination_file}-${impl}"
294     fperms 755 "${destination_directory}/${destination_file}-${impl}"
295     sed -i \
296     -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
297     "${D}${destination_directory}/${destination_file}-${impl}" || die
298     done
299    
300     dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}"
301     fi
302     }
303    
304 nelchael 1.10 # Phase function: pkg_pretend
305     python-distutils-ng_pkg_pretend() {
306     if has "collision-protect" ${FEATURES}; then
307     eerror "Due to previous eclass compiling Python files outside of src_install"
308     eerror "(and not recording resulting .pyc and .pyo files as owned by any package)"
309     eerror "merging this package with \"collision-protect\" in FEATURES will result"
310     eerror "in an error, please switch to using \"protect-owned\" instead."
311     die "\"collision-protect\" in FEATURES detected"
312     fi
313     }
314    
315 nelchael 1.1 # Phase function: src_prepare
316     python-distutils-ng_src_prepare() {
317     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
318    
319     # Try to run binary for each implementation:
320     for impl in ${PYTHON_COMPAT}; do
321     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
322     $(_python-distutils-ng_get_binary_for_implementation "${impl}") \
323     -c "import sys" || die
324     done
325    
326     # Run prepare shared by all implementations:
327     if type python_prepare_all &> /dev/null; then
328     einfo "Running python_prepare_all in ${S} for all"
329     python_prepare_all
330     fi
331    
332     # Create a copy of S for each implementation:
333     for impl in ${PYTHON_COMPAT}; do
334     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
335    
336     einfo "Creating copy for ${impl} in ${WORKDIR}/impl_${impl}"
337     mkdir -p "${WORKDIR}/impl_${impl}" || die
338     cp -pr "${S}" "${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}" || die
339     done
340    
341     # Run python_prepare for each implementation:
342     if type python_prepare &> /dev/null; then
343     _python-distutils-ng_run_for_each_impl python_prepare
344     fi
345     }
346    
347     # Phase function: src_configure
348     python-distutils-ng_src_configure() {
349     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
350    
351     if type python_configure &> /dev/null; then
352     _python-distutils-ng_run_for_each_impl python_configure
353     fi
354     }
355    
356     # Phase function: src_compile
357     python-distutils-ng_src_compile() {
358     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
359    
360     if type python_compile &> /dev/null; then
361     _python-distutils-ng_run_for_each_impl python_compile
362     else
363     _python-distutils-ng_run_for_each_impl \
364     _python-distutils-ng_default_distutils_compile
365     fi
366     }
367    
368     # Phase function: src_test
369     python-distutils-ng_src_test() {
370     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
371    
372     if type python_test &> /dev/null; then
373     _python-distutils-ng_run_for_each_impl python_test
374     fi
375     }
376    
377     # Phase function: src_install
378     python-distutils-ng_src_install() {
379     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
380    
381     if type python_install &> /dev/null; then
382     _python-distutils-ng_run_for_each_impl python_install
383     else
384     _python-distutils-ng_run_for_each_impl \
385     _python-distutils-ng_default_distutils_install
386     fi
387    
388     S="${WORKDIR}/${_PACKAGE_SPECIFIC_S}"
389     if type python_install_all &> /dev/null; then
390     einfo "Running python_install_all in ${S} for all"
391     python_install_all
392     fi
393    
394     for impl in ${PYTHON_COMPAT}; do
395     [[ "${PYTHON_DISABLE_COMPILATION}" = "yes" ]] && continue
396     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
397    
398     PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
399     for accessible_path in $(${PYTHON} -c 'import sys; print " ".join(sys.path)'); do
400     [[ -d "${D}/${accessible_path}" ]] || continue
401    
402     _python-distutils-ng_has_compileall "${impl}" || continue
403     ebegin "Compiling ${accessible_path} for ${impl}"
404     ${PYTHON} \
405     -m compileall -q -f "${D}/${accessible_path}" || die
406     eend $?
407    
408     _python-distutils-ng_has_compileall_opt "${impl}" || continue
409     ebegin "Optimizing ${accessible_path} for ${impl}"
410     PYTHONOPTIMIZE=1 ${PYTHON} \
411     -m compileall -q -f "${D}/${accessible_path}" || die
412     eend $?
413     done;
414     done
415     }

  ViewVC Help
Powered by ViewVC 1.1.20