/[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.18 - (hide annotations) (download)
Sat May 5 18:03:33 2012 UTC (23 months, 2 weeks ago) by floppym
Branch: MAIN
Changes since 1.17: +2 -2 lines
Eliminate another duplicate slash.

1 nelchael 1.1 # Copyright 1999-2012 Gentoo Foundation
2     # Distributed under the terms of the GNU General Public License v2
3 floppym 1.18 # $Header: /var/cvsroot/gentoo-x86/eclass/python-distutils-ng.eclass,v 1.17 2012/05/05 18:00:21 floppym 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 floppym 1.15 local S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}"
138 nelchael 1.1 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 nelchael 1.16 local compile_flags="--compile -O2"
175 nelchael 1.1
176     case "${1}" in
177 nelchael 1.16 jython*)
178     # Jython does not support optimizations
179     compile_flags="--compile" ;;
180 nelchael 1.1 esac
181    
182 nelchael 1.16 unset PYTHONDONTWRITEBYTECODE
183     [[ -n "${PYTHON_DISABLE_COMPILATION}" ]] && compile_flags="--no-compile"
184 floppym 1.17 "${PYTHON}" setup.py install ${compile_flags} --root="${D}" || die
185 nelchael 1.1 }
186    
187 nelchael 1.11 # @FUNCTION: python-distutils-ng_redoscript
188     # @USAGE: script_file_path [destination_directory]
189     # @DESCRIPTION:
190     # Reinstall script installed already by setup.py. This works by first moving the
191     # script to ${T} directory and later running python-distutils-ng_doscript on it.
192     # script_file_path has to be a full path relative to ${D}.
193     python-distutils-ng_redoscript() {
194     local sbn="$(basename "${1}")"
195     mkdir -p "${T}/_${sbn}/" || die "failed to create directory"
196 floppym 1.18 mv "${D}${1}" "${T}/_${sbn}/${sbn}" || die "failed to move file"
197 nelchael 1.11 python-distutils-ng_doscript "${T}/_${sbn}/${sbn}" "${2}"
198     }
199    
200 nelchael 1.1 # @FUNCTION: python-distutils-ng_doscript
201     # @USAGE: script_file_name [destination_directory]
202     # @DESCRIPTION:
203     # Install given script file in destination directory (for default value check
204     # python-distutils-ng_newscript) for all enabled implementations using original
205     # script name as a base name.
206     #
207     # See also python-distutils-ng_newscript for more details.
208     python-distutils-ng_doscript() {
209     python-distutils-ng_newscript "${1}" "$(basename "${1}")" "${2}"
210     }
211    
212     # @FUNCTION: python-distutils-ng_newscript
213     # @USAGE: script_file_name new_file_name [destination_directory]
214     # @DESCRIPTION:
215     # Install given script file in destination directory for all enabled
216     # implementations using new_file_name as a base name.
217     #
218     # Destination directory defaults to /usr/bin.
219     #
220     # If only one Python implementation is enabled the script will be installed
221     # as-is. Otherwise each script copy will have the name mangled to
222     # "new_file_name-IMPLEMENTATION". For every installed script new hash-bang line
223     # will be inserted to reference specific Python interpreter.
224     #
225     # In case of multiple implementations there will be also a symlink with name
226     # equal to new_file_name that will be a symlink to default implementation, which
227     # defaults to value of PYTHON_DEFAULT_IMPLEMENTATION, if not specified the
228     # function will pick default implementation: it will the be first enabled one
229     # from the following list:
230     # python2_7, python2_6, python2_5, python3_2, python3_1, pypy1_8, pypy1_7, jython2_5
231     python-distutils-ng_newscript() {
232     [[ -n "${1}" ]] || die "Missing source file name"
233     [[ -n "${2}" ]] || die "Missing destination file name"
234     local source_file="${1}"
235     local destination_file="${2}"
236     local default_impl="${PYTHON_DEFAULT_IMPLEMENTATION}"
237     local enabled_impls=0
238     local destination_directory="/usr/bin"
239     [[ -n "${3}" ]] && destination_directory="${3}"
240    
241     for impl in ${PYTHON_COMPAT}; do
242     use "python_targets_${impl}" || continue
243     enabled_impls=$((enabled_impls + 1))
244     done
245    
246     if [[ -z "${default_impl}" ]]; then
247     for impl in python{2_7,2_6,2_5,3_2,3_1} pypy{1_8,1_7} jython2_5; do
248     use "python_targets_${impl}" || continue
249     default_impl="${impl}"
250     break
251     done
252     else
253 nelchael 1.9 use "python_targets_${default_impl}" || \
254 nelchael 1.1 die "default implementation ${default_impl} not enabled"
255     fi
256    
257     [[ -n "${default_impl}" ]] || die "Could not select default implementation"
258    
259     dodir "${destination_directory}"
260     insinto "${destination_directory}"
261     if [[ "${enabled_impls}" = "1" ]]; then
262     einfo "Installing ${source_file} for single implementation (${default_impl}) in ${destination_directory}"
263     newins "${source_file}" "${destination_file}"
264     fperms 755 "${destination_directory}/${destination_file}"
265     sed -i \
266     -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
267     "${D}${destination_directory}/${destination_file}" || die
268     else
269     einfo "Installing ${source_file} for multiple implementations (default: ${default_impl}) in ${destination_directory}"
270     for impl in ${PYTHON_COMPAT}; do
271     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
272    
273     newins "${source_file}" "${destination_file}-${impl}"
274     fperms 755 "${destination_directory}/${destination_file}-${impl}"
275     sed -i \
276     -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
277     "${D}${destination_directory}/${destination_file}-${impl}" || die
278     done
279    
280     dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}"
281     fi
282     }
283    
284 nelchael 1.10 # Phase function: pkg_pretend
285     python-distutils-ng_pkg_pretend() {
286     if has "collision-protect" ${FEATURES}; then
287 nelchael 1.13 ewarn "Due to previous eclass compiling Python files outside of src_install"
288     ewarn "(and not recording resulting .pyc and .pyo files as owned by any package)"
289     ewarn "merging this package with \"collision-protect\" in FEATURES may result"
290     ewarn "in an error, please switch to using \"protect-owned\" instead."
291 nelchael 1.10 fi
292     }
293    
294 nelchael 1.1 # Phase function: src_prepare
295     python-distutils-ng_src_prepare() {
296     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
297    
298     # Try to run binary for each implementation:
299     for impl in ${PYTHON_COMPAT}; do
300     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
301     $(_python-distutils-ng_get_binary_for_implementation "${impl}") \
302     -c "import sys" || die
303     done
304    
305     # Run prepare shared by all implementations:
306     if type python_prepare_all &> /dev/null; then
307     einfo "Running python_prepare_all in ${S} for all"
308     python_prepare_all
309     fi
310    
311     # Create a copy of S for each implementation:
312     for impl in ${PYTHON_COMPAT}; do
313     use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
314    
315     einfo "Creating copy for ${impl} in ${WORKDIR}/impl_${impl}"
316     mkdir -p "${WORKDIR}/impl_${impl}" || die
317     cp -pr "${S}" "${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}" || die
318     done
319    
320     # Run python_prepare for each implementation:
321     if type python_prepare &> /dev/null; then
322     _python-distutils-ng_run_for_each_impl python_prepare
323     fi
324     }
325    
326     # Phase function: src_configure
327     python-distutils-ng_src_configure() {
328     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
329    
330     if type python_configure &> /dev/null; then
331     _python-distutils-ng_run_for_each_impl python_configure
332     fi
333     }
334    
335     # Phase function: src_compile
336     python-distutils-ng_src_compile() {
337     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
338    
339     if type python_compile &> /dev/null; then
340     _python-distutils-ng_run_for_each_impl python_compile
341     else
342     _python-distutils-ng_run_for_each_impl \
343     _python-distutils-ng_default_distutils_compile
344     fi
345     }
346    
347     # Phase function: src_test
348     python-distutils-ng_src_test() {
349     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
350    
351     if type python_test &> /dev/null; then
352     _python-distutils-ng_run_for_each_impl python_test
353     fi
354     }
355    
356     # Phase function: src_install
357     python-distutils-ng_src_install() {
358     [[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; }
359    
360     if type python_install &> /dev/null; then
361     _python-distutils-ng_run_for_each_impl python_install
362     else
363     _python-distutils-ng_run_for_each_impl \
364     _python-distutils-ng_default_distutils_install
365     fi
366    
367     if type python_install_all &> /dev/null; then
368     einfo "Running python_install_all in ${S} for all"
369 nelchael 1.12 pushd "${S}" &> /dev/null
370 nelchael 1.1 python_install_all
371 nelchael 1.12 popd &> /dev/null
372 nelchael 1.1 fi
373     }

  ViewVC Help
Powered by ViewVC 1.1.20