/[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.3 - (hide annotations) (download)
Mon Mar 26 16:24:31 2012 UTC (2 years, 3 months ago) by nelchael
Branch: MAIN
Changes since 1.2: +6 -6 lines
Include only valid values in REQUIRED_USE.

1 nelchael 1.1 # Copyright 1999-2012 Gentoo Foundation
2     # Distributed under the terms of the GNU General Public License v2
3 nelchael 1.3 # $Header: /var/cvsroot/gentoo-x86/eclass/python-distutils-ng.eclass,v 1.2 2012/03/26 06:12:53 jlec 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     # directory (so it wil lnot contain any implementation-specific files)
29    
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     EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
55    
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_generate_depend
68     # @USAGE: implementation
69     # @RETURN: Package atom of a Python implementation for *DEPEND.
70     # @DESCRIPTION:
71     # This function returns the full package atom of a Python implementation.
72     #
73     # `implementation' has to be one of the valid values for PYTHON_COMPAT.
74     _python-distutils-ng_generate_depend() {
75     local impl="${1/_/.}"
76     case "${impl}" in
77     python?.?)
78     echo "dev-lang/${impl::-3}:${impl: -3}" ;;
79     jython?.?)
80     echo "dev-java/${impl::-3}:${impl: -3}" ;;
81     pypy?.?)
82     echo "dev-python/${impl::-3}:${impl: -3}" ;;
83     *)
84     die "Unsupported implementation: ${1}" ;;
85     esac
86     }
87    
88     # @FUNCTION: _python-distutils-ng_get_binary_for_implementation
89     # @USAGE: implementation
90     # @RETURN: Full path to Python binary for given implementation.
91     # @DESCRIPTION:
92     # This function returns full path for Python binary for given implementation.
93     #
94     # Binary returned by this function should be used instead of simply calling
95     # `python'.
96     _python-distutils-ng_get_binary_for_implementation() {
97     local impl="${1/_/.}"
98     case "${impl}" in
99     python?.?|jython?.?)
100     echo "/usr/bin/${impl}" ;;
101     pypy?.?)
102     echo "/usr/bin/pypy-c${impl: -3}" ;;
103     *)
104     die "Unsupported implementation: ${1}" ;;
105     esac
106     }
107    
108 nelchael 1.3 required_use_str=""
109     for impl in ${PYTHON_COMPAT}; do
110     required_use_str="${required_use_str} python_targets_${impl}"
111     done
112     required_use_str=" || ( ${required_use_str} )"
113 nelchael 1.1 if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then
114     IUSE+="python"
115     REQUIRED_USE+=" python? ( ${required_use_str} )"
116     else
117 jlec 1.2 REQUIRED_USE+=" ${required_use_str}"
118 nelchael 1.1 fi
119    
120     for impl in ${PYTHON_COMPAT}; do
121     IUSE+=" python_targets_${impl} "
122     local dep_str="python_targets_${impl}? ( $(_python-distutils-ng_generate_depend "${impl}") )"
123    
124     if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then
125     RDEPEND="${RDEPEND} python? ( ${dep_str} )"
126     DEPEND="${DEPEND} python? ( ${dep_str} )"
127     else
128     RDEPEND="${RDEPEND} ${dep_str}"
129     DEPEND="${DEPEND} ${dep_str}"
130     fi
131     done
132    
133     _PACKAGE_SPECIFIC_S="${S#${WORKDIR}/}"
134    
135     # @FUNCTION: _python-distutils-ng_run_for_impl
136     # @USAGE: implementation command_to_run
137     # @DESCRIPTION:
138     # Run command_to_run using specified Python implementation.
139     #
140     # This will run the command_to_run in implementation-specific working directory.
141     _python-distutils-ng_run_for_impl() {
142     local impl="${1}"
143     local command="${2}"
144    
145     S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}"
146     PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
147     EPYTHON="${impl/_/.}"
148    
149     einfo "Running ${command} in ${S} for ${impl}"
150    
151     pushd "${S}" &> /dev/null
152     "${command}" "${impl}" "${PYTHON}"
153     popd &> /dev/null
154     }
155    
156     # @FUNCTION: _python-distutils-ng_run_for_each_impl
157     # @USAGE: command_to_run
158     # @DESCRIPTION:
159     # Run command_to_run for all enabled Python implementations.
160     #
161     # See also _python-distutils-ng_run_for_impl
162     _python-distutils-ng_run_for_each_impl() {
163     local command="${1}"
164    
165     for impl in ${PYTHON_COMPAT}; do
166     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
167     _python-distutils-ng_run_for_impl "${impl}" "${command}"
168     done
169     }
170    
171     # @FUNCTION: _python-distutils-ng_default_distutils_compile
172     # @DESCRIPTION:
173     # Default src_compile for distutils-based packages.
174     _python-distutils-ng_default_distutils_compile() {
175     "${PYTHON}" setup.py build || die
176     }
177    
178     # @FUNCTION: _python-distutils-ng_default_distutils_install
179     # @DESCRIPTION:
180     # Default src_install for distutils-based packages.
181     _python-distutils-ng_default_distutils_install() {
182     "${PYTHON}" setup.py install --no-compile --root="${D}/" || die
183     }
184    
185     # @FUNCTION: _python-distutils-ng_has_compileall
186     # @USAGE: implementation
187     # @RETURN: 0 if given implementation has compileall module
188     # @DESCRIPTION:
189     # This function is used to decide whenever to compile Python modules for given
190     # implementation.
191     _python-distutils-ng_has_compileall() {
192     case "${1}" in
193     python?_?|jython?_?)
194     return 0 ;;
195     *)
196     return 1 ;;
197     esac
198     }
199    
200     # @FUNCTION: _python-distutils-ng_has_compileall_opt
201     # @USAGE: implementation
202     # @RETURN: 0 if given implementation has compileall module and supports # optimizations
203     # @DESCRIPTION:
204     # This function is used to decide whenever to compile and optimize Python
205     # modules for given implementation.
206     _python-distutils-ng_has_compileall_opt() {
207     case "${1}" in
208     python?_?)
209     return 0 ;;
210     *)
211     return 1 ;;
212     esac
213     }
214    
215     # @FUNCTION: python-distutils-ng_doscript
216     # @USAGE: script_file_name [destination_directory]
217     # @DESCRIPTION:
218     # Install given script file in destination directory (for default value check
219     # python-distutils-ng_newscript) for all enabled implementations using original
220     # script name as a base name.
221     #
222     # See also python-distutils-ng_newscript for more details.
223     python-distutils-ng_doscript() {
224     python-distutils-ng_newscript "${1}" "$(basename "${1}")" "${2}"
225     }
226    
227     # @FUNCTION: python-distutils-ng_newscript
228     # @USAGE: script_file_name new_file_name [destination_directory]
229     # @DESCRIPTION:
230     # Install given script file in destination directory for all enabled
231     # implementations using new_file_name as a base name.
232     #
233     # Destination directory defaults to /usr/bin.
234     #
235     # If only one Python implementation is enabled the script will be installed
236     # as-is. Otherwise each script copy will have the name mangled to
237     # "new_file_name-IMPLEMENTATION". For every installed script new hash-bang line
238     # will be inserted to reference specific Python interpreter.
239     #
240     # In case of multiple implementations there will be also a symlink with name
241     # equal to new_file_name that will be a symlink to default implementation, which
242     # defaults to value of PYTHON_DEFAULT_IMPLEMENTATION, if not specified the
243     # function will pick default implementation: it will the be first enabled one
244     # from the following list:
245     # python2_7, python2_6, python2_5, python3_2, python3_1, pypy1_8, pypy1_7, jython2_5
246     python-distutils-ng_newscript() {
247     [[ -n "${1}" ]] || die "Missing source file name"
248     [[ -n "${2}" ]] || die "Missing destination file name"
249     local source_file="${1}"
250     local destination_file="${2}"
251     local default_impl="${PYTHON_DEFAULT_IMPLEMENTATION}"
252     local enabled_impls=0
253     local destination_directory="/usr/bin"
254     [[ -n "${3}" ]] && destination_directory="${3}"
255    
256     for impl in ${PYTHON_COMPAT}; do
257     use "python_targets_${impl}" || continue
258     enabled_impls=$((enabled_impls + 1))
259     done
260    
261     if [[ -z "${default_impl}" ]]; then
262     for impl in python{2_7,2_6,2_5,3_2,3_1} pypy{1_8,1_7} jython2_5; do
263     use "python_targets_${impl}" || continue
264     default_impl="${impl}"
265     break
266     done
267     else
268     use "python_targets_${impl}" || \
269     die "default implementation ${default_impl} not enabled"
270     fi
271    
272     [[ -n "${default_impl}" ]] || die "Could not select default implementation"
273    
274     dodir "${destination_directory}"
275     insinto "${destination_directory}"
276     if [[ "${enabled_impls}" = "1" ]]; then
277     einfo "Installing ${source_file} for single implementation (${default_impl}) in ${destination_directory}"
278     newins "${source_file}" "${destination_file}"
279     fperms 755 "${destination_directory}/${destination_file}"
280     sed -i \
281     -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
282     "${D}${destination_directory}/${destination_file}" || die
283     else
284     einfo "Installing ${source_file} for multiple implementations (default: ${default_impl}) in ${destination_directory}"
285     for impl in ${PYTHON_COMPAT}; do
286     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
287    
288     newins "${source_file}" "${destination_file}-${impl}"
289     fperms 755 "${destination_directory}/${destination_file}-${impl}"
290     sed -i \
291     -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
292     "${D}${destination_directory}/${destination_file}-${impl}" || die
293     done
294    
295     dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}"
296     fi
297     }
298    
299     # Phase function: src_prepare
300     python-distutils-ng_src_prepare() {
301     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
302    
303     # Try to run binary for each implementation:
304     for impl in ${PYTHON_COMPAT}; do
305     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
306     $(_python-distutils-ng_get_binary_for_implementation "${impl}") \
307     -c "import sys" || die
308     done
309    
310     # Run prepare shared by all implementations:
311     if type python_prepare_all &> /dev/null; then
312     einfo "Running python_prepare_all in ${S} for all"
313     python_prepare_all
314     fi
315    
316     # Create a copy of S for each implementation:
317     for impl in ${PYTHON_COMPAT}; do
318     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
319    
320     einfo "Creating copy for ${impl} in ${WORKDIR}/impl_${impl}"
321     mkdir -p "${WORKDIR}/impl_${impl}" || die
322     cp -pr "${S}" "${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}" || die
323     done
324    
325     # Run python_prepare for each implementation:
326     if type python_prepare &> /dev/null; then
327     _python-distutils-ng_run_for_each_impl python_prepare
328     fi
329     }
330    
331     # Phase function: src_configure
332     python-distutils-ng_src_configure() {
333     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
334    
335     if type python_configure &> /dev/null; then
336     _python-distutils-ng_run_for_each_impl python_configure
337     fi
338     }
339    
340     # Phase function: src_compile
341     python-distutils-ng_src_compile() {
342     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
343    
344     if type python_compile &> /dev/null; then
345     _python-distutils-ng_run_for_each_impl python_compile
346     else
347     _python-distutils-ng_run_for_each_impl \
348     _python-distutils-ng_default_distutils_compile
349     fi
350     }
351    
352     # Phase function: src_test
353     python-distutils-ng_src_test() {
354     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
355    
356     if type python_test &> /dev/null; then
357     _python-distutils-ng_run_for_each_impl python_test
358     fi
359     }
360    
361     # Phase function: src_install
362     python-distutils-ng_src_install() {
363     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
364    
365     if type python_install &> /dev/null; then
366     _python-distutils-ng_run_for_each_impl python_install
367     else
368     _python-distutils-ng_run_for_each_impl \
369     _python-distutils-ng_default_distutils_install
370     fi
371    
372     S="${WORKDIR}/${_PACKAGE_SPECIFIC_S}"
373     if type python_install_all &> /dev/null; then
374     einfo "Running python_install_all in ${S} for all"
375     python_install_all
376     fi
377    
378     for impl in ${PYTHON_COMPAT}; do
379     [[ "${PYTHON_DISABLE_COMPILATION}" = "yes" ]] && continue
380     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
381    
382     PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
383     for accessible_path in $(${PYTHON} -c 'import sys; print " ".join(sys.path)'); do
384     [[ -d "${D}/${accessible_path}" ]] || continue
385    
386     _python-distutils-ng_has_compileall "${impl}" || continue
387     ebegin "Compiling ${accessible_path} for ${impl}"
388     ${PYTHON} \
389     -m compileall -q -f "${D}/${accessible_path}" || die
390     eend $?
391    
392     _python-distutils-ng_has_compileall_opt "${impl}" || continue
393     ebegin "Optimizing ${accessible_path} for ${impl}"
394     PYTHONOPTIMIZE=1 ${PYTHON} \
395     -m compileall -q -f "${D}/${accessible_path}" || die
396     eend $?
397     done;
398     done
399     }

  ViewVC Help
Powered by ViewVC 1.1.20