/[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.7 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.7 2012/03/27 15:08:00 mr_bones_ 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)
61 S="${S:-${WORKDIR}/${P}}" 81 S="${S:-${WORKDIR}/${P}}"
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
86
87DEPEND="${DEPEND} !<sys-apps/portage-2.1.10.58"
66 88
67# @FUNCTION: _python-distutils-ng_get_binary_for_implementation 89# @FUNCTION: _python-distutils-ng_get_binary_for_implementation
68# @USAGE: implementation 90# @USAGE: implementation
69# @RETURN: Full path to Python binary for given implementation. 91# @RETURN: Full path to Python binary for given implementation.
70# @DESCRIPTION: 92# @DESCRIPTION:
95else 117else
96 REQUIRED_USE+=" ${required_use_str}" 118 REQUIRED_USE+=" ${required_use_str}"
97fi 119fi
98unset required_use_str 120unset required_use_str
99 121
122# avoid empty use deps
123_PYTHON_USE="${PYTHON_USE:+[${PYTHON_USE}]}"
124
125# set python DEPEND and RDEPEND
100for impl in ${PYTHON_COMPAT}; do 126for impl in ${PYTHON_COMPAT}; do
101 IUSE+=" python_targets_${impl}" 127 IUSE+=" python_targets_${impl}"
102 dep_str="${impl/_/.}" 128 dep_str="${impl/_/.}"
103 case "${dep_str}" in 129 case "${dep_str}" in
104 python?.?) 130 python?.?)
105 dep_str="dev-lang/python:${impl: -3}" ;; 131 dep_str="dev-lang/python:${dep_str: -3}${_PYTHON_USE}" ;;
106 jython?.?) 132 jython?.?)
107 dep_str="dev-java/jython:${impl: -3}" ;; 133 dep_str="dev-java/jython:${dep_str: -3}${_PYTHON_USE}" ;;
108 pypy?.?) 134 pypy?.?)
109 dep_str="dev-python/pypy:${impl: -3}" ;; 135 dep_str="dev-python/pypy:${dep_str: -3}${_PYTHON_USE}" ;;
110 *) 136 *)
111 die "Unsupported implementation: ${impl}" ;; 137 die "Unsupported implementation: ${impl}" ;;
112 esac 138 esac
113 dep_str="python_targets_${impl}? ( ${dep_str} )" 139 dep_str="python_targets_${impl}? ( ${dep_str} )"
114 140
132# 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.
133_python-distutils-ng_run_for_impl() { 159_python-distutils-ng_run_for_impl() {
134 local impl="${1}" 160 local impl="${1}"
135 local command="${2}" 161 local command="${2}"
136 162
137 S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}" 163 local S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}"
138 PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")" 164 PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
139 EPYTHON="${impl/_/.}" 165 EPYTHON="${impl/_/.}"
140 166
141 einfo "Running ${command} in ${S} for ${impl}" 167 einfo "Running ${command} in ${S} for ${impl}"
142 168
169 195
170# @FUNCTION: _python-distutils-ng_default_distutils_install 196# @FUNCTION: _python-distutils-ng_default_distutils_install
171# @DESCRIPTION: 197# @DESCRIPTION:
172# Default src_install for distutils-based packages. 198# Default src_install for distutils-based packages.
173_python-distutils-ng_default_distutils_install() { 199_python-distutils-ng_default_distutils_install() {
174 "${PYTHON}" setup.py install --no-compile --root="${D}/" || die 200 local compile_flags="--compile -O2"
175}
176 201
177# @FUNCTION: _python-distutils-ng_has_compileall
178# @USAGE: implementation
179# @RETURN: 0 if given implementation has compileall module
180# @DESCRIPTION:
181# This function is used to decide whenever to compile Python modules for given
182# implementation.
183_python-distutils-ng_has_compileall() {
184 case "${1}" in 202 case "${1}" in
185 python?_?|jython?_?) 203 jython*)
186 return 0 ;; 204 # Jython does not support optimizations
187 *) 205 compile_flags="--compile" ;;
188 return 1 ;;
189 esac 206 esac
190}
191 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
192# @FUNCTION: _python-distutils-ng_has_compileall_opt 227# @FUNCTION: python-distutils-ng_redoscript
193# @USAGE: implementation 228# @USAGE: script_file_path [destination_directory]
194# @RETURN: 0 if given implementation has compileall module and supports # optimizations
195# @DESCRIPTION: 229# @DESCRIPTION:
196# 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
197# 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.
198_python-distutils-ng_has_compileall_opt() { 235python-distutils-ng_redoscript() {
199 case "${1}" in 236 local sbn="$(basename "${1}")"
200 python?_?) 237 mkdir -p "${T}/_${sbn}/" || die "failed to create directory"
201 return 0 ;; 238 mv "${D}${1}" "${T}/_${sbn}/${sbn}" || die "failed to move file"
202 *) 239 python-distutils-ng_doscript "${T}/_${sbn}/${sbn}" "${2}"
203 return 1 ;;
204 esac
205} 240}
206 241
207# @FUNCTION: python-distutils-ng_doscript 242# @FUNCTION: python-distutils-ng_doscript
208# @USAGE: script_file_name [destination_directory] 243# @USAGE: script_file_name [destination_directory]
209# @DESCRIPTION: 244# @DESCRIPTION:
249 use "python_targets_${impl}" || continue 284 use "python_targets_${impl}" || continue
250 enabled_impls=$((enabled_impls + 1)) 285 enabled_impls=$((enabled_impls + 1))
251 done 286 done
252 287
253 if [[ -z "${default_impl}" ]]; then 288 if [[ -z "${default_impl}" ]]; then
254 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
255 use "python_targets_${impl}" || continue 290 use "python_targets_${impl}" || continue
256 default_impl="${impl}" 291 default_impl="${impl}"
257 break 292 break
258 done 293 done
259 else 294 else
260 use "python_targets_${impl}" || \ 295 use "python_targets_${default_impl}" || \
261 die "default implementation ${default_impl} not enabled" 296 die "default implementation ${default_impl} not enabled"
262 fi 297 fi
263 298
264 [[ -n "${default_impl}" ]] || die "Could not select default implementation" 299 [[ -n "${default_impl}" ]] || die "Could not select default implementation"
265 300
267 insinto "${destination_directory}" 302 insinto "${destination_directory}"
268 if [[ "${enabled_impls}" = "1" ]]; then 303 if [[ "${enabled_impls}" = "1" ]]; then
269 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}"
270 newins "${source_file}" "${destination_file}" 305 newins "${source_file}" "${destination_file}"
271 fperms 755 "${destination_directory}/${destination_file}" 306 fperms 755 "${destination_directory}/${destination_file}"
272 sed -i \ 307 python-distutils-ng_rewrite_hashbang "${D}${destination_directory}/${destination_file}" "${default_impl}"
273 -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
274 "${D}${destination_directory}/${destination_file}" || die
275 else 308 else
276 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}"
277 for impl in ${PYTHON_COMPAT}; do 310 for impl in ${PYTHON_COMPAT}; do
278 use "python_targets_${impl}" ${PYTHON_COMPAT} || continue 311 use "python_targets_${impl}" ${PYTHON_COMPAT} || continue
279 312
280 newins "${source_file}" "${destination_file}-${impl}" 313 newins "${source_file}" "${destination_file}-${impl}"
281 fperms 755 "${destination_directory}/${destination_file}-${impl}" 314 fperms 755 "${destination_directory}/${destination_file}-${impl}"
282 sed -i \ 315 python-distutils-ng_rewrite_hashbang "${D}${destination_directory}/${destination_file}-${impl}" "${impl}"
283 -e "1i#!$(_python-distutils-ng_get_binary_for_implementation "${impl}")" \
284 "${D}${destination_directory}/${destination_file}-${impl}" || die
285 done 316 done
286 317
287 dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}" 318 dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}"
288 fi 319 fi
289} 320}
359 else 390 else
360 _python-distutils-ng_run_for_each_impl \ 391 _python-distutils-ng_run_for_each_impl \
361 _python-distutils-ng_default_distutils_install 392 _python-distutils-ng_default_distutils_install
362 fi 393 fi
363 394
364 S="${WORKDIR}/${_PACKAGE_SPECIFIC_S}"
365 if type python_install_all &> /dev/null; then 395 if type python_install_all &> /dev/null; then
366 einfo "Running python_install_all in ${S} for all" 396 einfo "Running python_install_all in ${S} for all"
397 pushd "${S}" &> /dev/null
367 python_install_all 398 python_install_all
399 popd &> /dev/null
368 fi 400 fi
369 401
370 for impl in ${PYTHON_COMPAT}; do 402 if [[ -z "${PYTHON_DISABLE_SCRIPT_REDOS}" ]]; then
371 [[ "${PYTHON_DISABLE_COMPILATION}" = "yes" ]] && continue 403 for script_file in $(find "${D}"{,usr/}{,s}bin/ -type f -executable 2> /dev/null); do
372 use "python_targets_${impl}" ${PYTHON_COMPAT} || continue 404 python-distutils-ng_redoscript "/${script_file#${D}}"
373
374 PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")"
375 for accessible_path in $(${PYTHON} -c 'import sys; print " ".join(sys.path)'); do
376 [[ -d "${D}/${accessible_path}" ]] || continue
377
378 _python-distutils-ng_has_compileall "${impl}" || continue
379 ebegin "Compiling ${accessible_path} for ${impl}"
380 ${PYTHON} \
381 -m compileall -q -f "${D}/${accessible_path}" || die
382 eend $?
383
384 _python-distutils-ng_has_compileall_opt "${impl}" || continue
385 ebegin "Optimizing ${accessible_path} for ${impl}"
386 PYTHONOPTIMIZE=1 ${PYTHON} \
387 -m compileall -q -f "${D}/${accessible_path}" || die
388 eend $?
389 done; 405 done
390 done 406 fi
391} 407}

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

  ViewVC Help
Powered by ViewVC 1.1.20