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