/[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.22 - (hide annotations) (download)
Mon May 21 17:33:09 2012 UTC (2 years, 5 months ago) by nelchael
Branch: MAIN
Changes since 1.21: +7 -1 lines
Document PYTHON_DISABLE_SCRIPT_REDOS.

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

  ViewVC Help
Powered by ViewVC 1.1.20