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