/[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.4 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.4 2012/03/26 18:27:58 nelchael 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:
116else 117else
117 REQUIRED_USE+=" ${required_use_str}" 118 REQUIRED_USE+=" ${required_use_str}"
118fi 119fi
119unset required_use_str 120unset required_use_str
120 121
122# avoid empty use deps
123_PYTHON_USE="${PYTHON_USE:+[${PYTHON_USE}]}"
124
125# set python DEPEND and RDEPEND
121for impl in ${PYTHON_COMPAT}; do 126for impl in ${PYTHON_COMPAT}; do
122 IUSE+=" python_targets_${impl}" 127 IUSE+=" python_targets_${impl}"
123 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} )"
124 140
125 if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then 141 if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then
126 RDEPEND="${RDEPEND} python? ( ${dep_str} )" 142 RDEPEND="${RDEPEND} python? ( ${dep_str} )"
127 DEPEND="${DEPEND} python? ( ${dep_str} )" 143 DEPEND="${DEPEND} python? ( ${dep_str} )"
128 else 144 else
142# 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.
143_python-distutils-ng_run_for_impl() { 159_python-distutils-ng_run_for_impl() {
144 local impl="${1}" 160 local impl="${1}"
145 local command="${2}" 161 local command="${2}"
146 162
147 S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}" 163 local S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}"
148 PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")" 164 PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
149 EPYTHON="${impl/_/.}" 165 EPYTHON="${impl/_/.}"
150 166
151 einfo "Running ${command} in ${S} for ${impl}" 167 einfo "Running ${command} in ${S} for ${impl}"
152 168
179 195
180# @FUNCTION: _python-distutils-ng_default_distutils_install 196# @FUNCTION: _python-distutils-ng_default_distutils_install
181# @DESCRIPTION: 197# @DESCRIPTION:
182# Default src_install for distutils-based packages. 198# Default src_install for distutils-based packages.
183_python-distutils-ng_default_distutils_install() { 199_python-distutils-ng_default_distutils_install() {
184 "${PYTHON}" setup.py install --no-compile --root="${D}/" || die 200 local compile_flags="--compile -O2"
185}
186 201
187# @FUNCTION: _python-distutils-ng_has_compileall
188# @USAGE: implementation
189# @RETURN: 0 if given implementation has compileall module
190# @DESCRIPTION:
191# This function is used to decide whenever to compile Python modules for given
192# implementation.
193_python-distutils-ng_has_compileall() {
194 case "${1}" in 202 case "${1}" in
195 python?_?|jython?_?) 203 jython*)
196 return 0 ;; 204 # Jython does not support optimizations
197 *) 205 compile_flags="--compile" ;;
198 return 1 ;;
199 esac 206 esac
200}
201 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
202# @FUNCTION: _python-distutils-ng_has_compileall_opt 227# @FUNCTION: python-distutils-ng_redoscript
203# @USAGE: implementation 228# @USAGE: script_file_path [destination_directory]
204# @RETURN: 0 if given implementation has compileall module and supports # optimizations
205# @DESCRIPTION: 229# @DESCRIPTION:
206# 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
207# 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.
208_python-distutils-ng_has_compileall_opt() { 235python-distutils-ng_redoscript() {
209 case "${1}" in 236 local sbn="$(basename "${1}")"
210 python?_?) 237 mkdir -p "${T}/_${sbn}/" || die "failed to create directory"
211 return 0 ;; 238 mv "${D}${1}" "${T}/_${sbn}/${sbn}" || die "failed to move file"
212 *) 239 python-distutils-ng_doscript "${T}/_${sbn}/${sbn}" "${2}"
213 return 1 ;;
214 esac
215} 240}
216 241
217# @FUNCTION: python-distutils-ng_doscript 242# @FUNCTION: python-distutils-ng_doscript
218# @USAGE: script_file_name [destination_directory] 243# @USAGE: script_file_name [destination_directory]
219# @DESCRIPTION: 244# @DESCRIPTION:
259 use "python_targets_${impl}" || continue 284 use "python_targets_${impl}" || continue
260 enabled_impls=$((enabled_impls + 1)) 285 enabled_impls=$((enabled_impls + 1))
261 done 286 done
262 287
263 if [[ -z "${default_impl}" ]]; then 288 if [[ -z "${default_impl}" ]]; then
264 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
265 use "python_targets_${impl}" || continue 290 use "python_targets_${impl}" || continue
266 default_impl="${impl}" 291 default_impl="${impl}"
267 break 292 break
268 done 293 done
269 else 294 else
270 use "python_targets_${impl}" || \ 295 use "python_targets_${default_impl}" || \
271 die "default implementation ${default_impl} not enabled" 296 die "default implementation ${default_impl} not enabled"
272 fi 297 fi
273 298
274 [[ -n "${default_impl}" ]] || die "Could not select default implementation" 299 [[ -n "${default_impl}" ]] || die "Could not select default implementation"
275 300
277 insinto "${destination_directory}" 302 insinto "${destination_directory}"
278 if [[ "${enabled_impls}" = "1" ]]; then 303 if [[ "${enabled_impls}" = "1" ]]; then
279 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}"
280 newins "${source_file}" "${destination_file}" 305 newins "${source_file}" "${destination_file}"
281 fperms 755 "${destination_directory}/${destination_file}" 306 fperms 755 "${destination_directory}/${destination_file}"
282 sed -i \ 307 python-distutils-ng_rewrite_hashbang "${D}${destination_directory}/${destination_file}" "${default_impl}"
283 -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
284 "${D}${destination_directory}/${destination_file}" || die
285 else 308 else
286 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}"
287 for impl in ${PYTHON_COMPAT}; do 310 for impl in ${PYTHON_COMPAT}; do
288 use "python_targets_${impl}" ${PYTHON_COMPAT} || continue 311 use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
289 312
290 newins "${source_file}" "${destination_file}-${impl}" 313 newins "${source_file}" "${destination_file}-${impl}"
291 fperms 755 "${destination_directory}/${destination_file}-${impl}" 314 fperms 755 "${destination_directory}/${destination_file}-${impl}"
292 sed -i \ 315 python-distutils-ng_rewrite_hashbang "${D}${destination_directory}/${destination_file}-${impl}" "${impl}"
293 -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
294 "${D}${destination_directory}/${destination_file}-${impl}" || die
295 done 316 done
296 317
297 dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}" 318 dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}"
298 fi 319 fi
299} 320}
369 else 390 else
370 _python-distutils-ng_run_for_each_impl \ 391 _python-distutils-ng_run_for_each_impl \
371 _python-distutils-ng_default_distutils_install 392 _python-distutils-ng_default_distutils_install
372 fi 393 fi
373 394
374 S="${WORKDIR}/${_PACKAGE_SPECIFIC_S}"
375 if type python_install_all &> /dev/null; then 395 if type python_install_all &> /dev/null; then
376 einfo "Running python_install_all in ${S} for all" 396 einfo "Running python_install_all in ${S} for all"
397 pushd "${S}" &> /dev/null
377 python_install_all 398 python_install_all
399 popd &> /dev/null
378 fi 400 fi
379 401
380 for impl in ${PYTHON_COMPAT}; do 402 if [[ -z "${PYTHON_DISABLE_SCRIPT_REDOS}" ]]; then
381 [[ "${PYTHON_DISABLE_COMPILATION}" = "yes" ]] && continue 403 for script_file in $(find "${D}"{,usr/}{,s}bin/ -type f -executable 2> /dev/null); do
382 use "python_targets_${impl}" ${PYTHON_COMPAT} || continue 404 python-distutils-ng_redoscript "/${script_file#${D}}"
383
384 PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
385 for accessible_path in $(${PYTHON} -c 'import sys; print " ".join(sys.path)'); do
386 [[ -d "${D}/${accessible_path}" ]] || continue
387
388 _python-distutils-ng_has_compileall "${impl}" || continue
389 ebegin "Compiling ${accessible_path} for ${impl}"
390 ${PYTHON} \
391 -m compileall -q -f "${D}/${accessible_path}" || die
392 eend $?
393
394 _python-distutils-ng_has_compileall_opt "${impl}" || continue
395 ebegin "Optimizing ${accessible_path} for ${impl}"
396 PYTHONOPTIMIZE=1 ${PYTHON} \
397 -m compileall -q -f "${D}/${accessible_path}" || die
398 eend $?
399 done; 405 done
400 done 406 fi
401} 407}

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

  ViewVC Help
Powered by ViewVC 1.1.20