/[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.25 - (hide annotations) (download)
Sat May 26 09:46:23 2012 UTC (22 months, 3 weeks ago) by mgorny
Branch: MAIN
Changes since 1.24: +17 -7 lines
Fix double hashbang in installed scripts. Patch by Krzysztof Pawlik, modified by me.

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

  ViewVC Help
Powered by ViewVC 1.1.20