/[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.21 - (hide annotations) (download)
Mon May 21 17:30:35 2012 UTC (2 years, 6 months ago) by nelchael
Branch: MAIN
Changes since 1.20: +7 -1 lines
Fix #! line for installed scripts and install them for enabled implementations, see bug #416131.

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

  ViewVC Help
Powered by ViewVC 1.1.20