/[gentoo-x86]/eclass/python-distutils-ng.eclass
Gentoo

Diff of /eclass/python-distutils-ng.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 Revision 1.27
1# Copyright 1999-2012 Gentoo Foundation 1# Copyright 1999-2012 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/Attic/python-distutils-ng.eclass,v 1.2 2012/03/26 06:12:53 jlec Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/Attic/python-distutils-ng.eclass,v 1.27 2012/09/26 12:07:45 hasufell Exp $
4 4
5# @ECLASS: python-distutils-ng 5# @ECLASS: python-distutils-ng
6# @MAINTAINER: 6# @MAINTAINER:
7# Python herd <python@gentoo.org> 7# Python herd <python@gentoo.org>
8# @AUTHOR: 8# @AUTHOR:
9# Author: Krzysztof Pawlik <nelchael@gentoo.org> 9# Author: Krzysztof Pawlik <nelchael@gentoo.org>
10# @BLURB: An eclass for installing Python packages using distutils with proper 10# @BLURB: Install Python packages using distutils.
11# support for multiple Python slots.
12# @DESCRIPTION: 11# @DESCRIPTION:
13# The Python eclass is designed to allow an easier installation of Python 12# The Python eclass is designed to allow an easier installation of Python
14# packages and their incorporation into the Gentoo Linux system. 13# packages and their incorporation into the Gentoo Linux system.
15# 14#
16# This eclass provides functions for following phases: 15# This eclass provides functions for following phases:
23# each implementation, default function will run `setup.py build' 22# each implementation, default function will run `setup.py build'
24# - src_test - you can define python_test function that will be run for each 23# - src_test - you can define python_test function that will be run for each
25# implementation 24# implementation
26# - src_install - you can define python_install function that will be run for 25# - 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 26# each implementation and python_install_all that will be run in original
28# directory (so it wil lnot contain any implementation-specific files) 27# directory (so it will not contain any implementation-specific files)
29 28
30# @ECLASS-VARIABLE: PYTHON_COMPAT 29# @ECLASS-VARIABLE: PYTHON_COMPAT
30# @DEFAULT_UNSET
31# @DESCRIPTION: 31# @DESCRIPTION:
32# This variable contains a space separated list of implementations (see above) a 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 33# package is compatible to. It must be set before the `inherit' call. The
34# default is to enable all implementations. 34# default is to enable all implementations.
35 35
36if [[ -z "${PYTHON_COMPAT}" ]]; then 36if [[ -z "${PYTHON_COMPAT}" ]]; then
37 # Default: pure python, support all implementations 37 # Default: pure python, support all implementations
38 PYTHON_COMPAT=" python2_5 python2_6 python2_7" 38 PYTHON_COMPAT=" python2_5 python2_6 python2_7"
39 PYTHON_COMPAT+=" python3_1 python3_2" 39 PYTHON_COMPAT+=" python3_1 python3_2"
40 PYTHON_COMPAT+=" jython2_5" 40 PYTHON_COMPAT+=" jython2_5"
41 PYTHON_COMPAT+=" pypy1_7 pypy1_8" 41 PYTHON_COMPAT+=" pypy1_8 pypy1_9"
42fi 42fi
43 43
44# @ECLASS-VARIABLE: PYTHON_OPTIONAL 44# @ECLASS-VARIABLE: PYTHON_OPTIONAL
45# @DEFAULT_UNSET
45# @DESCRIPTION: 46# @DESCRIPTION:
46# Set the value to "yes" to make the dependency on a Python interpreter 47# Set the value to "yes" to make the dependency on a Python interpreter
47# optional. 48# optional.
48 49
49# @ECLASS-VARIABLE: PYTHON_DISABLE_COMPILATION 50# @ECLASS-VARIABLE: PYTHON_DISABLE_COMPILATION
51# @DEFAULT_UNSET
50# @DESCRIPTION: 52# @DESCRIPTION:
51# Set the value to "yes" to skip compilation and/or optimization of Python 53# Set the value to "yes" to skip compilation and/or optimization of Python
52# modules. 54# modules.
55
56# @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# @ECLASS-VARIABLE: PYTHON_USE
63# @DEFAULT_UNSET
64# @DESCRIPTION:
65# Comma-separated list of useflags needed for all(!) allowed
66# implementations. This is directly substituted into one or more of
67# dev-lang/python[${PYTHON_USE}], dev-python/pypy[${PYTHON_USE}] and
68# dev-java/jython[${PYTHON_USE}].
69# @CODE
70# example 1: PYTHON_USE="xml,sqlite"
71# example 2: PYTHON_USE="xml?,threads?,-foo"
72# @CODE
53 73
54EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install 74EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
55 75
56case "${EAPI}" in 76case "${EAPI}" in
57 0|1|2|3) 77 0|1|2|3)
62 ;; 82 ;;
63 *) 83 *)
64 die "Unsupported EAPI=${EAPI} (unknown) for python-distutils-ng.eclass" ;; 84 die "Unsupported EAPI=${EAPI} (unknown) for python-distutils-ng.eclass" ;;
65esac 85esac
66 86
67# @FUNCTION: _python-distutils-ng_generate_depend 87DEPEND="${DEPEND} !<sys-apps/portage-2.1.10.58"
68# @USAGE: implementation
69# @RETURN: Package atom of a Python implementation for *DEPEND.
70# @DESCRIPTION:
71# This function returns the full package atom of a Python implementation.
72#
73# `implementation' has to be one of the valid values for PYTHON_COMPAT.
74_python-distutils-ng_generate_depend() {
75 local impl="${1/_/.}"
76 case "${impl}" in
77 python?.?)
78 echo "dev-lang/${impl::-3}:${impl: -3}" ;;
79 jython?.?)
80 echo "dev-java/${impl::-3}:${impl: -3}" ;;
81 pypy?.?)
82 echo "dev-python/${impl::-3}:${impl: -3}" ;;
83 *)
84 die "Unsupported implementation: ${1}" ;;
85 esac
86}
87 88
88# @FUNCTION: _python-distutils-ng_get_binary_for_implementation 89# @FUNCTION: _python-distutils-ng_get_binary_for_implementation
89# @USAGE: implementation 90# @USAGE: implementation
90# @RETURN: Full path to Python binary for given implementation. 91# @RETURN: Full path to Python binary for given implementation.
91# @DESCRIPTION: 92# @DESCRIPTION:
103 *) 104 *)
104 die "Unsupported implementation: ${1}" ;; 105 die "Unsupported implementation: ${1}" ;;
105 esac 106 esac
106} 107}
107 108
108required_use_str=" || ( 109required_use_str=""
109 python_targets_python2_5 python_targets_python2_6 python_targets_python2_7 110for impl in ${PYTHON_COMPAT}; do
110 python_targets_python3_1 python_targets_python3_2 111 required_use_str+=" python_targets_${impl}"
111 python_targets_jython2_5 112done
112 python_targets_pypy1_7 python_targets_pypy1_8 )" 113required_use_str=" || ( ${required_use_str} )"
113if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then 114if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then
114 IUSE+="python" 115 IUSE+=" python"
115 REQUIRED_USE+=" python? ( ${required_use_str} )" 116 REQUIRED_USE+=" python? ( ${required_use_str} )"
116else 117else
117 REQUIRED_USE+=" ${required_use_str}" 118 REQUIRED_USE+=" ${required_use_str}"
118fi 119fi
120unset required_use_str
119 121
122# avoid empty use deps
123_PYTHON_USE="${PYTHON_USE:+[${PYTHON_USE}]}"
124
125# set python DEPEND and RDEPEND
120for impl in ${PYTHON_COMPAT}; do 126for impl in ${PYTHON_COMPAT}; do
121 IUSE+=" python_targets_${impl} " 127 IUSE+=" python_targets_${impl}"
122 local dep_str="python_targets_${impl}? ( $(_python-distutils-ng_generate_depend "${impl}") )" 128 dep_str="${impl/_/.}"
129 case "${dep_str}" in
130 python?.?)
131 dep_str="dev-lang/python:${dep_str: -3}${_PYTHON_USE}" ;;
132 jython?.?)
133 dep_str="dev-java/jython:${dep_str: -3}${_PYTHON_USE}" ;;
134 pypy?.?)
135 dep_str="dev-python/pypy:${dep_str: -3}${_PYTHON_USE}" ;;
136 *)
137 die "Unsupported implementation: ${impl}" ;;
138 esac
139 dep_str="python_targets_${impl}? ( ${dep_str} )"
123 140
124 if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then 141 if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then
125 RDEPEND="${RDEPEND} python? ( ${dep_str} )" 142 RDEPEND="${RDEPEND} python? ( ${dep_str} )"
126 DEPEND="${DEPEND} python? ( ${dep_str} )" 143 DEPEND="${DEPEND} python? ( ${dep_str} )"
127 else 144 else
128 RDEPEND="${RDEPEND} ${dep_str}" 145 RDEPEND="${RDEPEND} ${dep_str}"
129 DEPEND="${DEPEND} ${dep_str}" 146 DEPEND="${DEPEND} ${dep_str}"
130 fi 147 fi
148 unset dep_str
131done 149done
132 150
133_PACKAGE_SPECIFIC_S="${S#${WORKDIR}/}" 151_PACKAGE_SPECIFIC_S="${S#${WORKDIR}/}"
134 152
135# @FUNCTION: _python-distutils-ng_run_for_impl 153# @FUNCTION: _python-distutils-ng_run_for_impl
140# This will run the command_to_run in implementation-specific working directory. 158# This will run the command_to_run in implementation-specific working directory.
141_python-distutils-ng_run_for_impl() { 159_python-distutils-ng_run_for_impl() {
142 local impl="${1}" 160 local impl="${1}"
143 local command="${2}" 161 local command="${2}"
144 162
145 S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}" 163 local S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}"
146 PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")" 164 PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
147 EPYTHON="${impl/_/.}" 165 EPYTHON="${impl/_/.}"
148 166
149 einfo "Running ${command} in ${S} for ${impl}" 167 einfo "Running ${command} in ${S} for ${impl}"
150 168
177 195
178# @FUNCTION: _python-distutils-ng_default_distutils_install 196# @FUNCTION: _python-distutils-ng_default_distutils_install
179# @DESCRIPTION: 197# @DESCRIPTION:
180# Default src_install for distutils-based packages. 198# Default src_install for distutils-based packages.
181_python-distutils-ng_default_distutils_install() { 199_python-distutils-ng_default_distutils_install() {
182 "${PYTHON}" setup.py install --no-compile --root="${D}/" || die 200 local compile_flags="--compile -O2"
183}
184 201
185# @FUNCTION: _python-distutils-ng_has_compileall
186# @USAGE: implementation
187# @RETURN: 0 if given implementation has compileall module
188# @DESCRIPTION:
189# This function is used to decide whenever to compile Python modules for given
190# implementation.
191_python-distutils-ng_has_compileall() {
192 case "${1}" in 202 case "${1}" in
193 python?_?|jython?_?) 203 jython*)
194 return 0 ;; 204 # Jython does not support optimizations
195 *) 205 compile_flags="--compile" ;;
196 return 1 ;;
197 esac 206 esac
198}
199 207
208 unset PYTHONDONTWRITEBYTECODE
209 [[ -n "${PYTHON_DISABLE_COMPILATION}" ]] && compile_flags="--no-compile"
210 "${PYTHON}" setup.py install ${compile_flags} --root="${D}" || die
211}
212
213# @FUNCTION: python-distutils-ng_rewrite_hashbang
214# @USAGE: script_file_name implementation
215# @DESCRIPTION:
216# Rewrite #! line in named script, dies if #! line is not for Python or missing.
217python-distutils-ng_rewrite_hashbang() {
218 [[ -n "${1}" ]] || die "Missing file name"
219 [[ -n "${2}" ]] || die "Missing implementation"
220 local file_name="${1}"
221 local binary="$(_python-distutils-ng_get_binary_for_implementation "${2}")"
222 [[ $(head -n 1 "${file_name}") == '#!'*(python|jython|pypy-c)* ]] || \
223 die "Missing or invalid #! line in ${file_name}"
224 sed -i -e "1c#!${binary}" "${file_name}" || die
225}
226
200# @FUNCTION: _python-distutils-ng_has_compileall_opt 227# @FUNCTION: python-distutils-ng_redoscript
201# @USAGE: implementation 228# @USAGE: script_file_path [destination_directory]
202# @RETURN: 0 if given implementation has compileall module and supports # optimizations
203# @DESCRIPTION: 229# @DESCRIPTION:
204# This function is used to decide whenever to compile and optimize Python 230# Reinstall script installed already by setup.py. This works by first moving the
205# modules for given implementation. 231# script to ${T} directory and later running python-distutils-ng_doscript on it.
232# script_file_path has to be a full path relative to ${D}.
233# Warning: this function can be run automatically by the eclass in src_install,
234# see python-distutils-ng_src_install and PYTHON_DISABLE_SCRIPT_REDOS variable.
206_python-distutils-ng_has_compileall_opt() { 235python-distutils-ng_redoscript() {
207 case "${1}" in 236 local sbn="$(basename "${1}")"
208 python?_?) 237 mkdir -p "${T}/_${sbn}/" || die "failed to create directory"
209 return 0 ;; 238 mv "${D}${1}" "${T}/_${sbn}/${sbn}" || die "failed to move file"
210 *) 239 python-distutils-ng_doscript "${T}/_${sbn}/${sbn}" "${2}"
211 return 1 ;;
212 esac
213} 240}
214 241
215# @FUNCTION: python-distutils-ng_doscript 242# @FUNCTION: python-distutils-ng_doscript
216# @USAGE: script_file_name [destination_directory] 243# @USAGE: script_file_name [destination_directory]
217# @DESCRIPTION: 244# @DESCRIPTION:
257 use "python_targets_${impl}" || continue 284 use "python_targets_${impl}" || continue
258 enabled_impls=$((enabled_impls + 1)) 285 enabled_impls=$((enabled_impls + 1))
259 done 286 done
260 287
261 if [[ -z "${default_impl}" ]]; then 288 if [[ -z "${default_impl}" ]]; then
262 for impl in python{2_7,2_6,2_5,3_2,3_1} pypy{1_8,1_7} jython2_5; do 289 for impl in python{2_7,2_6,2_5,3_2,3_1} pypy{1_9,1_8,1_7} jython2_5; do
263 use "python_targets_${impl}" || continue 290 use "python_targets_${impl}" || continue
264 default_impl="${impl}" 291 default_impl="${impl}"
265 break 292 break
266 done 293 done
267 else 294 else
268 use "python_targets_${impl}" || \ 295 use "python_targets_${default_impl}" || \
269 die "default implementation ${default_impl} not enabled" 296 die "default implementation ${default_impl} not enabled"
270 fi 297 fi
271 298
272 [[ -n "${default_impl}" ]] || die "Could not select default implementation" 299 [[ -n "${default_impl}" ]] || die "Could not select default implementation"
273 300
275 insinto "${destination_directory}" 302 insinto "${destination_directory}"
276 if [[ "${enabled_impls}" = "1" ]]; then 303 if [[ "${enabled_impls}" = "1" ]]; then
277 einfo "Installing ${source_file} for single implementation (${default_impl}) in ${destination_directory}" 304 einfo "Installing ${source_file} for single implementation (${default_impl}) in ${destination_directory}"
278 newins "${source_file}" "${destination_file}" 305 newins "${source_file}" "${destination_file}"
279 fperms 755 "${destination_directory}/${destination_file}" 306 fperms 755 "${destination_directory}/${destination_file}"
280 sed -i \ 307 python-distutils-ng_rewrite_hashbang "${D}${destination_directory}/${destination_file}" "${default_impl}"
281 -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
282 "${D}${destination_directory}/${destination_file}" || die
283 else 308 else
284 einfo "Installing ${source_file} for multiple implementations (default: ${default_impl}) in ${destination_directory}" 309 einfo "Installing ${source_file} for multiple implementations (default: ${default_impl}) in ${destination_directory}"
285 for impl in ${PYTHON_COMPAT}; do 310 for impl in ${PYTHON_COMPAT}; do
286 use "python_targets_${impl}" ${PYTHON_COMPAT} || continue 311 use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
287 312
288 newins "${source_file}" "${destination_file}-${impl}" 313 newins "${source_file}" "${destination_file}-${impl}"
289 fperms 755 "${destination_directory}/${destination_file}-${impl}" 314 fperms 755 "${destination_directory}/${destination_file}-${impl}"
290 sed -i \ 315 python-distutils-ng_rewrite_hashbang "${D}${destination_directory}/${destination_file}-${impl}" "${impl}"
291 -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
292 "${D}${destination_directory}/${destination_file}-${impl}" || die
293 done 316 done
294 317
295 dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}" 318 dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}"
296 fi 319 fi
297} 320}
367 else 390 else
368 _python-distutils-ng_run_for_each_impl \ 391 _python-distutils-ng_run_for_each_impl \
369 _python-distutils-ng_default_distutils_install 392 _python-distutils-ng_default_distutils_install
370 fi 393 fi
371 394
372 S="${WORKDIR}/${_PACKAGE_SPECIFIC_S}"
373 if type python_install_all &> /dev/null; then 395 if type python_install_all &> /dev/null; then
374 einfo "Running python_install_all in ${S} for all" 396 einfo "Running python_install_all in ${S} for all"
397 pushd "${S}" &> /dev/null
375 python_install_all 398 python_install_all
399 popd &> /dev/null
376 fi 400 fi
377 401
378 for impl in ${PYTHON_COMPAT}; do 402 if [[ -z "${PYTHON_DISABLE_SCRIPT_REDOS}" ]]; then
379 [[ "${PYTHON_DISABLE_COMPILATION}" = "yes" ]] && continue 403 for script_file in $(find "${D}"{,usr/}{,s}bin/ -type f -executable 2> /dev/null); do
380 use "python_targets_${impl}" ${PYTHON_COMPAT} || continue 404 python-distutils-ng_redoscript "/${script_file#${D}}"
381
382 PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
383 for accessible_path in $(${PYTHON} -c 'import sys; print " ".join(sys.path)'); do
384 [[ -d "${D}/${accessible_path}" ]] || continue
385
386 _python-distutils-ng_has_compileall "${impl}" || continue
387 ebegin "Compiling ${accessible_path} for ${impl}"
388 ${PYTHON} \
389 -m compileall -q -f "${D}/${accessible_path}" || die
390 eend $?
391
392 _python-distutils-ng_has_compileall_opt "${impl}" || continue
393 ebegin "Optimizing ${accessible_path} for ${impl}"
394 PYTHONOPTIMIZE=1 ${PYTHON} \
395 -m compileall -q -f "${D}/${accessible_path}" || die
396 eend $?
397 done; 405 done
398 done 406 fi
399} 407}

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.27

  ViewVC Help
Powered by ViewVC 1.1.20