| 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.28 2012/09/27 16:35:41 axs 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: Install Python packages using distutils. |
| 11 |
# @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 |
# directory (so it will not contain any implementation-specific files) |
| 28 |
|
| 29 |
# @ECLASS-VARIABLE: PYTHON_COMPAT |
| 30 |
# @DEFAULT_UNSET |
| 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_8 pypy1_9" |
| 42 |
fi |
| 43 |
|
| 44 |
# @ECLASS-VARIABLE: PYTHON_OPTIONAL |
| 45 |
# @DEFAULT_UNSET |
| 46 |
# @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 |
# @DEFAULT_UNSET |
| 52 |
# @DESCRIPTION: |
| 53 |
# Set the value to "yes" to skip compilation and/or optimization of Python |
| 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 |
| 73 |
|
| 74 |
EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install |
| 75 |
|
| 76 |
case "${EAPI}" in |
| 77 |
0|1|2|3) |
| 78 |
die "Unsupported EAPI=${EAPI} (too old) for python-distutils-ng.eclass" ;; |
| 79 |
4|5) |
| 80 |
# EAPI=4 needed for REQUIRED_USE |
| 81 |
S="${S:-${WORKDIR}/${P}}" |
| 82 |
;; |
| 83 |
*) |
| 84 |
die "Unsupported EAPI=${EAPI} (unknown) for python-distutils-ng.eclass" ;; |
| 85 |
esac |
| 86 |
|
| 87 |
DEPEND="${DEPEND} !<sys-apps/portage-2.1.10.58" |
| 88 |
|
| 89 |
# @FUNCTION: _python-distutils-ng_get_binary_for_implementation |
| 90 |
# @USAGE: implementation |
| 91 |
# @RETURN: Full path to Python binary for given implementation. |
| 92 |
# @DESCRIPTION: |
| 93 |
# This function returns full path for Python binary for given implementation. |
| 94 |
# |
| 95 |
# Binary returned by this function should be used instead of simply calling |
| 96 |
# `python'. |
| 97 |
_python-distutils-ng_get_binary_for_implementation() { |
| 98 |
local impl="${1/_/.}" |
| 99 |
case "${impl}" in |
| 100 |
python?.?|jython?.?) |
| 101 |
echo "${EPREFIX}/usr/bin/${impl}" ;; |
| 102 |
pypy?.?) |
| 103 |
echo "${EPREFIX}/usr/bin/pypy-c${impl: -3}" ;; |
| 104 |
*) |
| 105 |
die "Unsupported implementation: ${1}" ;; |
| 106 |
esac |
| 107 |
} |
| 108 |
|
| 109 |
required_use_str="" |
| 110 |
for impl in ${PYTHON_COMPAT}; do |
| 111 |
required_use_str+=" python_targets_${impl}" |
| 112 |
done |
| 113 |
required_use_str=" || ( ${required_use_str} )" |
| 114 |
if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then |
| 115 |
IUSE+=" python" |
| 116 |
REQUIRED_USE+=" python? ( ${required_use_str} )" |
| 117 |
else |
| 118 |
REQUIRED_USE+=" ${required_use_str}" |
| 119 |
fi |
| 120 |
unset required_use_str |
| 121 |
|
| 122 |
# avoid empty use deps |
| 123 |
_PYTHON_USE="${PYTHON_USE:+[${PYTHON_USE}]}" |
| 124 |
|
| 125 |
# set python DEPEND and RDEPEND |
| 126 |
for impl in ${PYTHON_COMPAT}; do |
| 127 |
IUSE+=" python_targets_${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} )" |
| 140 |
|
| 141 |
if [[ "${PYTHON_OPTIONAL}" = "yes" ]]; then |
| 142 |
RDEPEND="${RDEPEND} python? ( ${dep_str} )" |
| 143 |
DEPEND="${DEPEND} python? ( ${dep_str} )" |
| 144 |
else |
| 145 |
RDEPEND="${RDEPEND} ${dep_str}" |
| 146 |
DEPEND="${DEPEND} ${dep_str}" |
| 147 |
fi |
| 148 |
unset dep_str |
| 149 |
done |
| 150 |
|
| 151 |
_PACKAGE_SPECIFIC_S="${S#${WORKDIR}/}" |
| 152 |
|
| 153 |
# @FUNCTION: _python-distutils-ng_run_for_impl |
| 154 |
# @USAGE: implementation command_to_run |
| 155 |
# @DESCRIPTION: |
| 156 |
# Run command_to_run using specified Python implementation. |
| 157 |
# |
| 158 |
# This will run the command_to_run in implementation-specific working directory. |
| 159 |
_python-distutils-ng_run_for_impl() { |
| 160 |
local impl="${1}" |
| 161 |
local command="${2}" |
| 162 |
|
| 163 |
local S="${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}" |
| 164 |
PYTHON="$(_python-distutils-ng_get_binary_for_implementation "${impl}")" |
| 165 |
EPYTHON="${impl/_/.}" |
| 166 |
|
| 167 |
einfo "Running ${command} in ${S} for ${impl}" |
| 168 |
|
| 169 |
pushd "${S}" &> /dev/null |
| 170 |
"${command}" "${impl}" "${PYTHON}" |
| 171 |
popd &> /dev/null |
| 172 |
} |
| 173 |
|
| 174 |
# @FUNCTION: _python-distutils-ng_run_for_each_impl |
| 175 |
# @USAGE: command_to_run |
| 176 |
# @DESCRIPTION: |
| 177 |
# Run command_to_run for all enabled Python implementations. |
| 178 |
# |
| 179 |
# See also _python-distutils-ng_run_for_impl |
| 180 |
_python-distutils-ng_run_for_each_impl() { |
| 181 |
local command="${1}" |
| 182 |
|
| 183 |
for impl in ${PYTHON_COMPAT}; do |
| 184 |
use "python_targets_${impl}" ${PYTHON_COMPAT} || continue |
| 185 |
_python-distutils-ng_run_for_impl "${impl}" "${command}" |
| 186 |
done |
| 187 |
} |
| 188 |
|
| 189 |
# @FUNCTION: _python-distutils-ng_default_distutils_compile |
| 190 |
# @DESCRIPTION: |
| 191 |
# Default src_compile for distutils-based packages. |
| 192 |
_python-distutils-ng_default_distutils_compile() { |
| 193 |
"${PYTHON}" setup.py build || die |
| 194 |
} |
| 195 |
|
| 196 |
# @FUNCTION: _python-distutils-ng_default_distutils_install |
| 197 |
# @DESCRIPTION: |
| 198 |
# Default src_install for distutils-based packages. |
| 199 |
_python-distutils-ng_default_distutils_install() { |
| 200 |
local compile_flags="--compile -O2" |
| 201 |
|
| 202 |
case "${1}" in |
| 203 |
jython*) |
| 204 |
# Jython does not support optimizations |
| 205 |
compile_flags="--compile" ;; |
| 206 |
esac |
| 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. |
| 217 |
python-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 |
|
| 227 |
# @FUNCTION: python-distutils-ng_redoscript |
| 228 |
# @USAGE: script_file_path [destination_directory] |
| 229 |
# @DESCRIPTION: |
| 230 |
# Reinstall script installed already by setup.py. This works by first moving the |
| 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. |
| 235 |
python-distutils-ng_redoscript() { |
| 236 |
local sbn="$(basename "${1}")" |
| 237 |
mkdir -p "${T}/_${sbn}/" || die "failed to create directory" |
| 238 |
mv "${D}${1}" "${T}/_${sbn}/${sbn}" || die "failed to move file" |
| 239 |
python-distutils-ng_doscript "${T}/_${sbn}/${sbn}" "${2}" |
| 240 |
} |
| 241 |
|
| 242 |
# @FUNCTION: python-distutils-ng_doscript |
| 243 |
# @USAGE: script_file_name [destination_directory] |
| 244 |
# @DESCRIPTION: |
| 245 |
# Install given script file in destination directory (for default value check |
| 246 |
# python-distutils-ng_newscript) for all enabled implementations using original |
| 247 |
# script name as a base name. |
| 248 |
# |
| 249 |
# See also python-distutils-ng_newscript for more details. |
| 250 |
python-distutils-ng_doscript() { |
| 251 |
python-distutils-ng_newscript "${1}" "$(basename "${1}")" "${2}" |
| 252 |
} |
| 253 |
|
| 254 |
# @FUNCTION: python-distutils-ng_newscript |
| 255 |
# @USAGE: script_file_name new_file_name [destination_directory] |
| 256 |
# @DESCRIPTION: |
| 257 |
# Install given script file in destination directory for all enabled |
| 258 |
# implementations using new_file_name as a base name. |
| 259 |
# |
| 260 |
# Destination directory defaults to /usr/bin. |
| 261 |
# |
| 262 |
# If only one Python implementation is enabled the script will be installed |
| 263 |
# as-is. Otherwise each script copy will have the name mangled to |
| 264 |
# "new_file_name-IMPLEMENTATION". For every installed script new hash-bang line |
| 265 |
# will be inserted to reference specific Python interpreter. |
| 266 |
# |
| 267 |
# In case of multiple implementations there will be also a symlink with name |
| 268 |
# equal to new_file_name that will be a symlink to default implementation, which |
| 269 |
# defaults to value of PYTHON_DEFAULT_IMPLEMENTATION, if not specified the |
| 270 |
# function will pick default implementation: it will the be first enabled one |
| 271 |
# from the following list: |
| 272 |
# python2_7, python2_6, python2_5, python3_2, python3_1, pypy1_8, pypy1_7, jython2_5 |
| 273 |
python-distutils-ng_newscript() { |
| 274 |
[[ -n "${1}" ]] || die "Missing source file name" |
| 275 |
[[ -n "${2}" ]] || die "Missing destination file name" |
| 276 |
local source_file="${1}" |
| 277 |
local destination_file="${2}" |
| 278 |
local default_impl="${PYTHON_DEFAULT_IMPLEMENTATION}" |
| 279 |
local enabled_impls=0 |
| 280 |
local destination_directory="/usr/bin" |
| 281 |
[[ -n "${3}" ]] && destination_directory="${3}" |
| 282 |
|
| 283 |
for impl in ${PYTHON_COMPAT}; do |
| 284 |
use "python_targets_${impl}" || continue |
| 285 |
enabled_impls=$((enabled_impls + 1)) |
| 286 |
done |
| 287 |
|
| 288 |
if [[ -z "${default_impl}" ]]; then |
| 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 |
| 290 |
use "python_targets_${impl}" || continue |
| 291 |
default_impl="${impl}" |
| 292 |
break |
| 293 |
done |
| 294 |
else |
| 295 |
use "python_targets_${default_impl}" || \ |
| 296 |
die "default implementation ${default_impl} not enabled" |
| 297 |
fi |
| 298 |
|
| 299 |
[[ -n "${default_impl}" ]] || die "Could not select default implementation" |
| 300 |
|
| 301 |
dodir "${destination_directory}" |
| 302 |
insinto "${destination_directory}" |
| 303 |
if [[ "${enabled_impls}" = "1" ]]; then |
| 304 |
einfo "Installing ${source_file} for single implementation (${default_impl}) in ${destination_directory}" |
| 305 |
newins "${source_file}" "${destination_file}" |
| 306 |
fperms 755 "${destination_directory}/${destination_file}" |
| 307 |
python-distutils-ng_rewrite_hashbang "${D}${destination_directory}/${destination_file}" "${default_impl}" |
| 308 |
else |
| 309 |
einfo "Installing ${source_file} for multiple implementations (default: ${default_impl}) in ${destination_directory}" |
| 310 |
for impl in ${PYTHON_COMPAT}; do |
| 311 |
use "python_targets_${impl}" ${PYTHON_COMPAT} || continue |
| 312 |
|
| 313 |
newins "${source_file}" "${destination_file}-${impl}" |
| 314 |
fperms 755 "${destination_directory}/${destination_file}-${impl}" |
| 315 |
python-distutils-ng_rewrite_hashbang "${D}${destination_directory}/${destination_file}-${impl}" "${impl}" |
| 316 |
done |
| 317 |
|
| 318 |
dosym "${destination_file}-${default_impl}" "${destination_directory}/${destination_file}" |
| 319 |
fi |
| 320 |
} |
| 321 |
|
| 322 |
# Phase function: src_prepare |
| 323 |
python-distutils-ng_src_prepare() { |
| 324 |
[[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; } |
| 325 |
|
| 326 |
# Try to run binary for each implementation: |
| 327 |
for impl in ${PYTHON_COMPAT}; do |
| 328 |
use "python_targets_${impl}" ${PYTHON_COMPAT} || continue |
| 329 |
$(_python-distutils-ng_get_binary_for_implementation "${impl}") \ |
| 330 |
-c "import sys" || die |
| 331 |
done |
| 332 |
|
| 333 |
# Run prepare shared by all implementations: |
| 334 |
if type python_prepare_all &> /dev/null; then |
| 335 |
einfo "Running python_prepare_all in ${S} for all" |
| 336 |
python_prepare_all |
| 337 |
fi |
| 338 |
|
| 339 |
# Create a copy of S for each implementation: |
| 340 |
for impl in ${PYTHON_COMPAT}; do |
| 341 |
use "python_targets_${impl}" ${PYTHON_COMPAT} || continue |
| 342 |
|
| 343 |
einfo "Creating copy for ${impl} in ${WORKDIR}/impl_${impl}" |
| 344 |
mkdir -p "${WORKDIR}/impl_${impl}" || die |
| 345 |
cp -pr "${S}" "${WORKDIR}/impl_${impl}/${_PACKAGE_SPECIFIC_S}" || die |
| 346 |
done |
| 347 |
|
| 348 |
# Run python_prepare for each implementation: |
| 349 |
if type python_prepare &> /dev/null; then |
| 350 |
_python-distutils-ng_run_for_each_impl python_prepare |
| 351 |
fi |
| 352 |
} |
| 353 |
|
| 354 |
# Phase function: src_configure |
| 355 |
python-distutils-ng_src_configure() { |
| 356 |
[[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; } |
| 357 |
|
| 358 |
if type python_configure &> /dev/null; then |
| 359 |
_python-distutils-ng_run_for_each_impl python_configure |
| 360 |
fi |
| 361 |
} |
| 362 |
|
| 363 |
# Phase function: src_compile |
| 364 |
python-distutils-ng_src_compile() { |
| 365 |
[[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; } |
| 366 |
|
| 367 |
if type python_compile &> /dev/null; then |
| 368 |
_python-distutils-ng_run_for_each_impl python_compile |
| 369 |
else |
| 370 |
_python-distutils-ng_run_for_each_impl \ |
| 371 |
_python-distutils-ng_default_distutils_compile |
| 372 |
fi |
| 373 |
} |
| 374 |
|
| 375 |
# Phase function: src_test |
| 376 |
python-distutils-ng_src_test() { |
| 377 |
[[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; } |
| 378 |
|
| 379 |
if type python_test &> /dev/null; then |
| 380 |
_python-distutils-ng_run_for_each_impl python_test |
| 381 |
fi |
| 382 |
} |
| 383 |
|
| 384 |
# Phase function: src_install |
| 385 |
python-distutils-ng_src_install() { |
| 386 |
[[ "${PYTHON_OPTIONAL}" = "yes" ]] && { use python || return; } |
| 387 |
|
| 388 |
if type python_install &> /dev/null; then |
| 389 |
_python-distutils-ng_run_for_each_impl python_install |
| 390 |
else |
| 391 |
_python-distutils-ng_run_for_each_impl \ |
| 392 |
_python-distutils-ng_default_distutils_install |
| 393 |
fi |
| 394 |
|
| 395 |
if type python_install_all &> /dev/null; then |
| 396 |
einfo "Running python_install_all in ${S} for all" |
| 397 |
pushd "${S}" &> /dev/null |
| 398 |
python_install_all |
| 399 |
popd &> /dev/null |
| 400 |
fi |
| 401 |
|
| 402 |
if [[ -z "${PYTHON_DISABLE_SCRIPT_REDOS}" ]]; then |
| 403 |
for script_file in $(find "${ED}"{,usr/}{,s}bin/ -type f -executable 2> /dev/null); do |
| 404 |
python-distutils-ng_redoscript "/${script_file#${D}}" |
| 405 |
done |
| 406 |
fi |
| 407 |
} |