/[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.19 - (hide annotations) (download)
Sun May 6 03:20:45 2012 UTC (23 months, 1 week ago) by floppym
Branch: MAIN
Changes since 1.18: +5 -3 lines
Silence eclass-to-manpage warnings.

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

  ViewVC Help
Powered by ViewVC 1.1.20