/[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.7 - (hide annotations) (download)
Tue Mar 27 15:08:00 2012 UTC (2 years, 5 months ago) by mr_bones_
Branch: MAIN
Changes since 1.6: +2 -2 lines
fix typo: dv -> dev

1 nelchael 1.1 # Copyright 1999-2012 Gentoo Foundation
2     # Distributed under the terms of the GNU General Public License v2
3 mr_bones_ 1.7 # $Header: /var/cvsroot/gentoo-x86/eclass/python-distutils-ng.eclass,v 1.6 2012/03/26 20:33:42 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     # 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_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     dep_str="dev-lang/python:${impl: -3}" ;;
106     jython?.?)
107     dep_str="dev-java/jython:${impl: -3}" ;;
108     pypy?.?)
109 mr_bones_ 1.7 dep_str="dev-python/pypy:${impl: -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     # @FUNCTION: python-distutils-ng_doscript
208     # @USAGE: script_file_name [destination_directory]
209     # @DESCRIPTION:
210     # Install given script file in destination directory (for default value check
211     # python-distutils-ng_newscript) for all enabled implementations using original
212     # script name as a base name.
213     #
214     # See also python-distutils-ng_newscript for more details.
215     python-distutils-ng_doscript() {
216     python-distutils-ng_newscript "${1}" "$(basename "${1}")" "${2}"
217     }
218    
219     # @FUNCTION: python-distutils-ng_newscript
220     # @USAGE: script_file_name new_file_name [destination_directory]
221     # @DESCRIPTION:
222     # Install given script file in destination directory for all enabled
223     # implementations using new_file_name as a base name.
224     #
225     # Destination directory defaults to /usr/bin.
226     #
227     # If only one Python implementation is enabled the script will be installed
228     # as-is. Otherwise each script copy will have the name mangled to
229     # "new_file_name-IMPLEMENTATION". For every installed script new hash-bang line
230     # will be inserted to reference specific Python interpreter.
231     #
232     # In case of multiple implementations there will be also a symlink with name
233     # equal to new_file_name that will be a symlink to default implementation, which
234     # defaults to value of PYTHON_DEFAULT_IMPLEMENTATION, if not specified the
235     # function will pick default implementation: it will the be first enabled one
236     # from the following list:
237     # python2_7, python2_6, python2_5, python3_2, python3_1, pypy1_8, pypy1_7, jython2_5
238     python-distutils-ng_newscript() {
239     [[ -n "${1}" ]] || die "Missing source file name"
240     [[ -n "${2}" ]] || die "Missing destination file name"
241     local source_file="${1}"
242     local destination_file="${2}"
243     local default_impl="${PYTHON_DEFAULT_IMPLEMENTATION}"
244     local enabled_impls=0
245     local destination_directory="/usr/bin"
246     [[ -n "${3}" ]] && destination_directory="${3}"
247    
248     for impl in ${PYTHON_COMPAT}; do
249     use "python_targets_${impl}" || continue
250     enabled_impls=$((enabled_impls + 1))
251     done
252    
253     if [[ -z "${default_impl}" ]]; then
254     for impl in python{2_7,2_6,2_5,3_2,3_1} pypy{1_8,1_7} jython2_5; do
255     use "python_targets_${impl}" || continue
256     default_impl="${impl}"
257     break
258     done
259     else
260     use "python_targets_${impl}" || \
261     die "default implementation ${default_impl} not enabled"
262     fi
263    
264     [[ -n "${default_impl}" ]] || die "Could not select default implementation"
265    
266     dodir "${destination_directory}"
267     insinto "${destination_directory}"
268     if [[ "${enabled_impls}" = "1" ]]; then
269     einfo "Installing ${source_file} for single implementation (${default_impl}) in ${destination_directory}"
270     newins "${source_file}" "${destination_file}"
271     fperms 755 "${destination_directory}/${destination_file}"
272     sed -i \
273     -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
274     "${D}${destination_directory}/${destination_file}" || die
275     else
276     einfo "Installing ${source_file} for multiple implementations (default: ${default_impl}) in ${destination_directory}"
277     for impl in ${PYTHON_COMPAT}; do
278     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
279    
280     newins "${source_file}" "${destination_file}-${impl}"
281     fperms 755 "${destination_directory}/${destination_file}-${impl}"
282     sed -i \
283     -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
284     "${D}${destination_directory}/${destination_file}-${impl}" || die
285     done
286    
287     dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}"
288     fi
289     }
290    
291     # Phase function: src_prepare
292     python-distutils-ng_src_prepare() {
293     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
294    
295     # Try to run binary for each implementation:
296     for impl in ${PYTHON_COMPAT}; do
297     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
298     $(_python-distutils-ng_get_binary_for_implementation "${impl}") \
299     -c "import sys" || die
300     done
301    
302     # Run prepare shared by all implementations:
303     if type python_prepare_all &> /dev/null; then
304     einfo "Running python_prepare_all in ${S} for all"
305     python_prepare_all
306     fi
307    
308     # Create a copy of S for each implementation:
309     for impl in ${PYTHON_COMPAT}; do
310     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
311    
312     einfo "Creating copy for ${impl} in ${WORKDIR}/impl_${impl}"
313     mkdir -p "${WORKDIR}/impl_${impl}" || die
314     cp -pr "${S}" "${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}" || die
315     done
316    
317     # Run python_prepare for each implementation:
318     if type python_prepare &> /dev/null; then
319     _python-distutils-ng_run_for_each_impl python_prepare
320     fi
321     }
322    
323     # Phase function: src_configure
324     python-distutils-ng_src_configure() {
325     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
326    
327     if type python_configure &> /dev/null; then
328     _python-distutils-ng_run_for_each_impl python_configure
329     fi
330     }
331    
332     # Phase function: src_compile
333     python-distutils-ng_src_compile() {
334     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
335    
336     if type python_compile &> /dev/null; then
337     _python-distutils-ng_run_for_each_impl python_compile
338     else
339     _python-distutils-ng_run_for_each_impl \
340     _python-distutils-ng_default_distutils_compile
341     fi
342     }
343    
344     # Phase function: src_test
345     python-distutils-ng_src_test() {
346     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
347    
348     if type python_test &> /dev/null; then
349     _python-distutils-ng_run_for_each_impl python_test
350     fi
351     }
352    
353     # Phase function: src_install
354     python-distutils-ng_src_install() {
355     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
356    
357     if type python_install &> /dev/null; then
358     _python-distutils-ng_run_for_each_impl python_install
359     else
360     _python-distutils-ng_run_for_each_impl \
361     _python-distutils-ng_default_distutils_install
362     fi
363    
364     S="${WORKDIR}/${_PACKAGE_SPECIFIC_S}"
365     if type python_install_all &> /dev/null; then
366     einfo "Running python_install_all in ${S} for all"
367     python_install_all
368     fi
369    
370     for impl in ${PYTHON_COMPAT}; do
371     [[ "${PYTHON_DISABLE_COMPILATION}" = "yes" ]] && continue
372     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
373    
374     PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
375     for accessible_path in $(${PYTHON} -c 'import sys; print " ".join(sys.path)'); do
376     [[ -d "${D}/${accessible_path}" ]] || continue
377    
378     _python-distutils-ng_has_compileall "${impl}" || continue
379     ebegin "Compiling ${accessible_path} for ${impl}"
380     ${PYTHON} \
381     -m compileall -q -f "${D}/${accessible_path}" || die
382     eend $?
383    
384     _python-distutils-ng_has_compileall_opt "${impl}" || continue
385     ebegin "Optimizing ${accessible_path} for ${impl}"
386     PYTHONOPTIMIZE=1 ${PYTHON} \
387     -m compileall -q -f "${D}/${accessible_path}" || die
388     eend $?
389     done;
390     done
391     }

  ViewVC Help
Powered by ViewVC 1.1.20