/[gentoo-x86]/eclass/python.eclass
Gentoo

Diff of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.60 Revision 1.86
1# Copyright 1999-2009 Gentoo Foundation 1# Copyright 1999-2010 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/python.eclass,v 1.60 2009/08/05 18:31:30 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.86 2010/01/15 14:46:20 arfrever Exp $
4 4
5# @ECLASS: python.eclass 5# @ECLASS: python.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# python@gentoo.org 7# python@gentoo.org
8#
9# original author: Alastair Tse <liquidx@gentoo.org>
10# @BLURB: A Utility Eclass that should be inherited by anything that deals with Python or Python modules. 8# @BLURB: A utility eclass that should be inherited by anything that deals with Python or Python modules.
11# @DESCRIPTION: 9# @DESCRIPTION:
12# Some useful functions for dealing with python. 10# Some useful functions for dealing with Python.
13inherit alternatives multilib
14 11
12inherit multilib
15 13
14if ! has "${EAPI:-0}" 0 1 2; then
15 die "API of python.eclass in EAPI=\"${EAPI}\" not established"
16fi
17
16if [[ -n "${NEED_PYTHON}" ]] ; then 18if [[ -n "${NEED_PYTHON}" ]]; then
17 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}" 19 PYTHON_ATOM=">=dev-lang/python-${NEED_PYTHON}"
18 DEPEND="${PYTHON_ATOM}" 20 DEPEND="${PYTHON_ATOM}"
19 RDEPEND="${DEPEND}" 21 RDEPEND="${DEPEND}"
20else 22else
21 PYTHON_ATOM="dev-lang/python" 23 PYTHON_ATOM="dev-lang/python"
22fi 24fi
23 25
26DEPEND+=" >=app-admin/eselect-python-20090804"
27
28# @ECLASS-VARIABLE: PYTHON_USE_WITH
29# @DESCRIPTION:
30# Set this to a space separated list of use flags
31# the python slot in use must be built with.
32
33# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
34# @DESCRIPTION:
35# Set this to a space separated list of use flags
36# of which one must be turned on for the slot of
37# in use.
38
39# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
40# @DESCRIPTION:
41# Set this if you need to make either PYTHON_USE_WITH or
42# PYTHON_USE_WITH_OR atoms conditional under a use flag.
43
44# @FUNCTION: python_pkg_setup
45# @DESCRIPTION:
46# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
47# are respected. Only exported if one of those variables is set.
48if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
49 python_pkg_setup() {
50 python_pkg_setup_fail() {
51 eerror "${1}"
52 die "${1}"
53 }
54
55 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
56
57 python_pkg_setup_check_USE_flags() {
58 local pyatom use
59 if [[ -n "${PYTHON_ABI}" ]]; then
60 pyatom="dev-lang/python:${PYTHON_ABI}"
61 else
62 pyatom="dev-lang/python:$(PYTHON -A --ABI)"
63 fi
64
65 for use in ${PYTHON_USE_WITH}; do
66 if ! has_version "${pyatom}[${use}]"; then
67 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
68 fi
69 done
70
71 for use in ${PYTHON_USE_WITH_OR}; do
72 if has_version "${pyatom}[${use}]"; then
73 return
74 fi
75 done
76
77 if [[ ${PYTHON_USE_WITH_OR} ]]; then
78 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
79 fi
80 }
81
24if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 82 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
25 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804" 83 python_execute_function -q python_pkg_setup_check_USE_flags
84 else
85 python_pkg_setup_check_USE_flags
86 fi
87 }
88
89 EXPORT_FUNCTIONS pkg_setup
90
91 if [[ -n "${PYTHON_USE_WITH}" ]]; then
92 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
93 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
94 PYTHON_USE_WITH_ATOM="|| ( "
95 for use in ${PYTHON_USE_WITH_OR}; do
96 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
97 done
98 unset use
99 PYTHON_USE_WITH_ATOM+=" )"
100 fi
101 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
102 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
103 fi
104 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
105 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
26fi 106fi
27 107
28__python_eclass_test() { 108# ================================================================================================
29 __python_version_extract 2.3 109# ======== FUNCTIONS FOR PACKAGES SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ========
30 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 110# ================================================================================================
31 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
32 __python_version_extract 2.3.4
33 echo -n "2.3.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
34 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
35 __python_version_extract 2.3.5
36 echo -n "2.3.5 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
37 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
38 __python_version_extract 2.4
39 echo -n "2.4 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
40 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
41 __python_version_extract 2.5b3
42 echo -n "2.5b3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
43 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
44}
45 111
46# @FUNCTION: python_version 112# @ECLASS-VARIABLE: SUPPORT_PYTHON_ABIS
47# @DESCRIPTION: 113# @DESCRIPTION:
48# Run without arguments and it will export the version of python 114# Set this in EAPI <= 4 to indicate that given package supports installation for
49# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR 115# multiple versions of Python.
50__python_version_extract() { 116
51 local verstr=$1 117# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
52 export PYVER_MAJOR=${verstr:0:1} 118# @DESCRIPTION:
53 export PYVER_MINOR=${verstr:2:1} 119# Set this to define default functions for the following ebuild phases:
54 if [[ ${verstr:3:1} == . ]]; then 120# src_prepare, src_configure, src_compile, src_test, src_install.
55 export PYVER_MICRO=${verstr:4} 121if ! has "${EAPI:-0}" 0 1 && [[ -n "${PYTHON_DEFINE_DEFAULT_FUNCTIONS}" ]]; then
122 python_src_prepare() {
123 python_copy_sources
124 }
125
126 for python_default_function in src_configure src_compile src_test src_install; do
127 eval "python_${python_default_function}() { python_execute_function -d -s; }"
128 done
129 unset python_default_function
130
131 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
56 fi 132fi
57 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
58}
59 133
60python_version() { 134unset PYTHON_ABIS
61 [[ -n "${PYVER}" ]] && return 0 135unset PYTHON_ABIS_SANITY_CHECKS
62 local tmpstr
63 python=${python:-/usr/bin/python}
64 tmpstr="$(${python} -V 2>&1 )"
65 export PYVER_ALL="${tmpstr#Python }"
66 __python_version_extract $PYVER_ALL
67}
68 136
69# @FUNCTION: PYTHON 137# @FUNCTION: validate_PYTHON_ABIS
70# @USAGE: [-a|--absolute-path] <Python_ABI="${PYTHON_ABI}">
71# @DESCRIPTION: 138# @DESCRIPTION:
72# Get Python interpreter filename for specified Python ABI. If Python_ABI argument 139# Ensure that PYTHON_ABIS variable has valid value.
73# is ommitted, then PYTHON_ABI environment variable must be set and is used. 140# This function usually should not be directly called in ebuilds.
74PYTHON() { 141validate_PYTHON_ABIS() {
75 local absolute_path="0" slot= 142 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 4 without setting SUPPORT_PYTHON_ABIS variable.
143 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
144 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
145 fi
76 146
77 while (($#)); do 147 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
78 case "$1" in 148 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then
79 -a|--absolute-path) 149 eerror "'/usr/bin/python' is not valid symlink."
80 absolute_path="1" 150 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
81 ;; 151 die "'/usr/bin/python' is not valid symlink"
82 -*) 152 fi
83 die "${FUNCNAME}(): Unrecognized option $1" 153 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
84 ;; 154 eerror "'/usr/bin/python-config' is not valid script"
85 *) 155 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
156 die "'/usr/bin/python-config' is not valid script"
157 fi
158
159 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
160 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
161 local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS=
162 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
163 python2_supported_versions="2.4 2.5 2.6 2.7"
164 python3_supported_versions="3.0 3.1 3.2"
165
166 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
167 local python2_enabled="0" python3_enabled="0"
168
169 if [[ -z "${USE_PYTHON}" ]]; then
170 die "USE_PYTHON variable is empty"
171 fi
172
173 for PYTHON_ABI in ${USE_PYTHON}; do
174 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
175 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
176 fi
177
178 if has "${PYTHON_ABI}" ${python2_supported_versions}; then
179 python2_enabled="1"
180 fi
181 if has "${PYTHON_ABI}" ${python3_supported_versions}; then
182 python3_enabled="1"
183 fi
184
185 support_ABI="1"
186 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
187 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
188 support_ABI="0"
86 break 189 break
87 ;; 190 fi
88 esac 191 done
89 shift 192 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
90 done 193 done
91 194
92 if [[ "$#" -eq "0" ]]; then
93 if [[ -n "${PYTHON_ABI}" ]]; then 195 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
94 slot="${PYTHON_ABI}" 196 die "USE_PYTHON variable does not enable any version of Python supported by ${CATEGORY}/${PF}"
197 fi
198
199 if [[ "${python2_enabled}" == "0" ]]; then
200 ewarn "USE_PYTHON variable does not enable any version of Python 2. This configuration is unsupported."
201 fi
202 if [[ "${python3_enabled}" == "0" ]]; then
203 ewarn "USE_PYTHON variable does not enable any version of Python 3. This configuration is unsupported."
204 fi
95 else 205 else
96 die "${FUNCNAME}(): Invalid usage" 206 local python_version python2_version= python3_version= support_python_major_version
97 fi
98 elif [[ "$#" -eq "1" ]]; then
99 slot="$1"
100 else
101 die "${FUNCNAME}(): Invalid usage"
102 fi
103 207
104 if [[ "${absolute_path}" == "1" ]]; then 208 python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
105 echo -n "/usr/bin/python${slot}"
106 else
107 echo -n "python${slot}"
108 fi
109}
110 209
111# @FUNCTION: validate_PYTHON_ABIS 210 if has_version "=dev-lang/python-2*"; then
112# @DESCRIPTION: 211 if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then
113# Make sure PYTHON_ABIS variable has valid value.
114validate_PYTHON_ABIS() {
115 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
116 if [[ "$(</usr/bin/python)" != *"Gentoo Python wrapper program"* ]]; then
117 die "/usr/bin/python isn't valid program" 212 die "'/usr/bin/python2' is not valid symlink"
118 fi
119 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
120 die "/usr/bin/python-config isn't valid script"
121 fi
122
123 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4.
124 if [[ -z "${PYTHON_ABIS}" ]] && has "${EAPI:-0}" 0 1 2 3; then
125 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI
126 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
127 for ABI in ${USE_PYTHON}; do
128 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
129 ewarn "Ignoring unsupported Python ABI '${ABI}'"
130 continue
131 fi 213 fi
132 support_ABI="1" 214
215 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
216
217 for PYTHON_ABI in ${python2_supported_versions}; do
218 support_python_major_version="1"
133 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 219 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
134 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 220 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
135 support_ABI="0" 221 support_python_major_version="0"
136 break
137 fi 222 fi
223 done
224 [[ "${support_python_major_version}" == "1" ]] && break
138 done 225 done
139 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 226 if [[ "${support_python_major_version}" == "1" ]]; then
227 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
228 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
229 die "Active version of Python 2 is not supported by ${CATEGORY}/${PF}"
230 fi
231 done
232 else
233 python2_version=""
234 fi
235 fi
236
237 if has_version "=dev-lang/python-3*"; then
238 if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then
239 die "'/usr/bin/python3' is not valid symlink"
240 fi
241
242 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
243
244 for PYTHON_ABI in ${python3_supported_versions}; do
245 support_python_major_version="1"
246 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
247 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
248 support_python_major_version="0"
249 fi
250 done
251 [[ "${support_python_major_version}" == "1" ]] && break
252 done
253 if [[ "${support_python_major_version}" == "1" ]]; then
254 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
255 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
256 die "Active version of Python 3 is not supported by ${CATEGORY}/${PF}"
257 fi
258 done
259 else
260 python3_version=""
261 fi
262 fi
263
264 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
265 eerror "Python wrapper is configured incorrectly or /usr/bin/python2 symlink"
266 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
267 die "Incorrect configuration of Python"
268 fi
269 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
270 eerror "Python wrapper is configured incorrectly or /usr/bin/python3 symlink"
271 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
272 die "Incorrect configuration of Python"
273 fi
274
275 PYTHON_ABIS="${python2_version} ${python3_version}"
276 PYTHON_ABIS="${PYTHON_ABIS# }"
277 export PYTHON_ABIS="${PYTHON_ABIS% }"
278 fi
279 fi
280
281 if ! _python_implementation && [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
282 local PYTHON_ABI
283 for PYTHON_ABI in ${PYTHON_ABIS}; do
284 # Ensure that appropriate version of Python is installed.
285 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
286 die "dev-lang/python:${PYTHON_ABI} is not installed"
287 fi
288
289 # Ensure that EPYTHON variable is respected.
290 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
291 eerror "python: '$(type -p python)'"
292 eerror "ABI: '${ABI}'"
293 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
294 eerror "EPYTHON: '$(PYTHON)'"
295 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
296 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')'"
297 die "'python' does not respect EPYTHON variable"
298 fi
140 done 299 done
141 export PYTHON_ABIS="${supported_PYTHON_ABIS# }" 300 PYTHON_ABIS_SANITY_CHECKS="1"
142 fi
143
144 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
145 python_version
146 export PYTHON_ABIS="${PYVER}"
147 fi
148}
149
150# @FUNCTION: python_copy_sources
151# @USAGE: [directory]
152# @DESCRIPTION:
153# Copy unpacked sources of given package for each Python ABI.
154python_copy_sources() {
155 local dir dirs=() PYTHON_ABI
156
157 if [[ "$#" -eq "0" ]]; then
158 if [[ "${WORKDIR}" == "${S}" ]]; then
159 die "${FUNCNAME}() cannot be used"
160 fi 301 fi
161 dirs="${S}"
162 else
163 dirs="$@"
164 fi
165
166 validate_PYTHON_ABIS
167 for PYTHON_ABI in ${PYTHON_ABIS}; do
168 for dir in "${dirs[@]}"; do
169 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
170 done
171 done
172}
173
174# @FUNCTION: python_set_build_dir_symlink
175# @USAGE: [directory="build"]
176# @DESCRIPTION:
177# Create build directory symlink.
178python_set_build_dir_symlink() {
179 local dir="$1"
180
181 [[ -z "${PYTHON_ABIS}" ]] && die "PYTHON_ABIS variable not set"
182 [[ -z "${dir}" ]] && dir="build"
183
184 # Don't delete preexistent directories.
185 rm -f "${dir}" || die "Deletion of '${dir}' failed"
186 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
187} 302}
188 303
189# @FUNCTION: python_execute_function 304# @FUNCTION: python_execute_function
190# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] <function> [arguments] 305# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
191# @DESCRIPTION: 306# @DESCRIPTION:
192# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 307# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
193# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 308# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
194python_execute_function() { 309python_execute_function() {
195 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" PYTHON_ABI quiet="0" separate_build_dirs="0" 310 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory previous_directory_stack previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" source_dir=
196 311
197 while (($#)); do 312 while (($#)); do
198 case "$1" in 313 case "$1" in
199 --action-message) 314 --action-message)
200 action_message_template="$2" 315 action_message_template="$2"
214 quiet="1" 329 quiet="1"
215 ;; 330 ;;
216 -s|--separate-build-dirs) 331 -s|--separate-build-dirs)
217 separate_build_dirs="1" 332 separate_build_dirs="1"
218 ;; 333 ;;
334 --source-dir)
335 source_dir="$2"
336 shift
337 ;;
338 --)
339 break
340 ;;
219 -*) 341 -*)
220 die "${FUNCNAME}(): Unrecognized option $1" 342 die "${FUNCNAME}(): Unrecognized option '$1'"
221 ;; 343 ;;
222 *) 344 *)
223 break 345 break
224 ;; 346 ;;
225 esac 347 esac
226 shift 348 shift
227 done 349 done
228 350
351 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
352 die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
353 fi
354
229 if [[ "${default_function}" == "0" ]]; then 355 if [[ "${default_function}" == "0" ]]; then
230 if [[ "$#" -eq "0" ]]; then 356 if [[ "$#" -eq 0 ]]; then
231 die "${FUNCNAME}(): Missing function name" 357 die "${FUNCNAME}(): Missing function name"
232 fi 358 fi
233 function="$1" 359 function="$1"
234 shift 360 shift
361
362 if [[ -z "$(type -t "${function}")" ]]; then
363 die "${FUNCNAME}(): '${function}' function is not defined"
364 fi
235 else 365 else
236 if [[ "$#" -ne "0" ]]; then 366 if [[ "$#" -ne "0" ]]; then
237 die "${FUNCNAME}(): --default-function option and function name cannot be specified simultaneously" 367 die "${FUNCNAME}(): '--default-function' option and function name cannot be specified simultaneously"
238 fi 368 fi
239 if has "${EAPI:-0}" 0 1; then 369 if has "${EAPI:-0}" 0 1; then
240 die "${FUNCNAME}(): --default-function option cannot be used in this EAPI" 370 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
241 fi 371 fi
242 372
243 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 373 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
374 if has "${EAPI}" 2 3; then
244 python_default_function() { 375 python_default_function() {
245 econf 376 econf
246 } 377 }
378 else
379 python_default_function() {
380 nonfatal econf
381 }
382 fi
247 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then 383 elif [[ "${EBUILD_PHASE}" == "compile" ]]; then
248 python_default_function() { 384 python_default_function() {
249 emake 385 emake
250 } 386 }
251 elif [[ "${EBUILD_PHASE}" == "test" ]]; then 387 elif [[ "${EBUILD_PHASE}" == "test" ]]; then
259 elif [[ "${EBUILD_PHASE}" == "install" ]]; then 395 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
260 python_default_function() { 396 python_default_function() {
261 emake DESTDIR="${D}" install 397 emake DESTDIR="${D}" install
262 } 398 }
263 else 399 else
264 die "${FUNCNAME}(): --default-function option cannot be used in this ebuild phase" 400 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
265 fi 401 fi
266 function="python_default_function" 402 function="python_default_function"
267 fi 403 fi
268 404
269 if [[ "${quiet}" == "0" ]]; then 405 if [[ "${quiet}" == "0" ]]; then
301 else 437 else
302 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..." 438 action_message="${action} of ${CATEGORY}/${PF} with Python ${PYTHON_ABI}..."
303 fi 439 fi
304 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 440 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
305 fi 441 fi
442
306 if [[ "${separate_build_dirs}" == "1" ]]; then 443 if [[ "${separate_build_dirs}" == "1" ]]; then
444 if [[ -n "${source_dir}" ]]; then
445 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
446 else
307 export BUILDDIR="${S}-${PYTHON_ABI}" 447 export BUILDDIR="${S}-${PYTHON_ABI}"
448 fi
308 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 449 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
309 else 450 else
310 export BUILDDIR="${S}" 451 export BUILDDIR="${S}"
311 fi 452 fi
453
454 previous_directory="$(pwd)"
455 previous_directory_stack="$(dirs -p)"
456 previous_directory_stack_length="$(dirs -p | wc -l)"
457
458 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
459 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
460 else
312 if ! EPYTHON="$(PYTHON)" "${function}" "$@"; then 461 EPYTHON="$(PYTHON)" "${function}" "$@"
462 fi
463
464 if [[ "$?" != "0" ]]; then
313 if [[ -n "${failure_message_template}" ]]; then 465 if [[ -n "${failure_message_template}" ]]; then
314 failure_message="$(eval echo -n "${failure_message_template}")" 466 failure_message="$(eval echo -n "${failure_message_template}")"
315 else 467 else
316 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function" 468 failure_message="${action} failed with Python ${PYTHON_ABI} in ${function}() function"
317 fi 469 fi
318 if [[ "${nonfatal}" == "1" ]] || has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 470
319 local ABI enabled_PYTHON_ABIS 471 if [[ "${nonfatal}" == "1" ]]; then
320 for ABI in ${PYTHON_ABIS}; do
321 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}"
322 done
323 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }"
324 if [[ "${quiet}" == "0" ]]; then 472 if [[ "${quiet}" == "0" ]]; then
325 ewarn "${RED}${failure_message}${NORMAL}" 473 ewarn "${RED}${failure_message}${NORMAL}"
326 fi 474 fi
475 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
476 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
477 local enabled_PYTHON_ABIS= other_PYTHON_ABI
478 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
479 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
480 done
481 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
482 fi
483 if [[ "${quiet}" == "0" ]]; then
484 ewarn "${RED}${failure_message}${NORMAL}"
485 fi
486 if [[ -z "${PYTHON_ABIS}" ]]; then
487 die "${function}() function failed with all enabled versions of Python"
488 fi
327 else 489 else
328 die "${failure_message}" 490 die "${failure_message}"
329 fi 491 fi
330 fi 492 fi
493
494 # Ensure that directory stack has not been decreased.
495 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
496 die "Directory stack decreased illegally"
497 fi
498
499 # Avoid side effects of earlier returning from the specified function.
500 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
501 popd > /dev/null || die "popd failed"
502 done
503
504 # Ensure that the bottom part of directory stack has not been changed. Restore
505 # previous directory (from before running of the specified function) before
506 # comparison of directory stacks to avoid mismatch of directory stacks after
507 # potential using of 'cd' to change current directory. Restoration of previous
508 # directory allows to safely use 'cd' to change current directory in the
509 # specified function without changing it back to original directory.
510 cd "${previous_directory}"
511 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
512 die "Directory stack changed illegally"
513 fi
514
331 if [[ "${separate_build_dirs}" == "1" ]]; then 515 if [[ "${separate_build_dirs}" == "1" ]]; then
332 popd > /dev/null || die "popd failed" 516 popd > /dev/null || die "popd failed"
333 fi 517 fi
334 unset BUILDDIR 518 unset BUILDDIR
335 done 519 done
337 if [[ "${default_function}" == "1" ]]; then 521 if [[ "${default_function}" == "1" ]]; then
338 unset -f python_default_function 522 unset -f python_default_function
339 fi 523 fi
340} 524}
341 525
342
343# @ECLASS-VARIABLE: PYTHON_USE_WITH
344# @DESCRIPTION:
345# Set this to a space separated list of use flags
346# the python slot in use must be built with.
347
348# @ECLASS-VARIABLE: PYTHON_USE_WITH_OR
349# @DESCRIPTION:
350# Set this to a space separated list of use flags
351# of which one must be turned on for the slot of
352# in use.
353
354# @ECLASS-VARIABLE: PYTHON_USE_WITH_OPT
355# @DESCRIPTION:
356# Set this if you need to make either PYTHON_USE_WITH or
357# PYTHON_USE_WITH_OR atoms conditional under a use flag.
358
359# @FUNCTION: python_pkg_setup 526# @FUNCTION: python_copy_sources
527# @USAGE: [--no-link] [--] [directory]
360# @DESCRIPTION: 528# @DESCRIPTION:
361# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 529# Copy unpacked sources of given package for each Python ABI.
362# are respected. Only exported if one of those variables is set. 530python_copy_sources() {
363if ! has ${EAPI} 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 531 local dir dirs=() no_link="0" PYTHON_ABI
364 python_pkg_setup_fail() {
365 eerror "${1}"
366 die "${1}"
367 }
368 532
369 python_pkg_setup() { 533 while (($#)); do
370 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return 534 case "$1" in
535 --no-link)
536 no_link="1"
537 ;;
538 --)
539 break
540 ;;
541 -*)
542 die "${FUNCNAME}(): Unrecognized option '$1'"
543 ;;
544 *)
545 break
546 ;;
547 esac
548 shift
549 done
371 550
372 python_version 551 if [[ "$#" -eq 0 ]]; then
373 local failed 552 if [[ "${WORKDIR}" == "${S}" ]]; then
374 local pyatom="dev-lang/python:${PYVER}" 553 die "${FUNCNAME}() cannot be used"
554 fi
555 dirs="${S}"
556 else
557 dirs="$@"
558 fi
375 559
376 for use in ${PYTHON_USE_WITH}; do 560 validate_PYTHON_ABIS
377 if ! has_version "${pyatom}[${use}]"; then 561 for PYTHON_ABI in ${PYTHON_ABIS}; do
378 python_pkg_setup_fail \ 562 for dir in "${dirs[@]}"; do
379 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}" 563 if [[ "${no_link}" == "1" ]]; then
564 cp -pr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
565 else
566 cp -lpr "${dir}" "${dir}-${PYTHON_ABI}" > /dev/null || die "Copying of sources failed"
380 fi 567 fi
381 done 568 done
382
383 for use in ${PYTHON_USE_WITH_OR}; do
384 if has_version "${pyatom}[${use}]"; then
385 return
386 fi
387 done 569 done
570}
388 571
389 if [[ ${PYTHON_USE_WITH_OR} ]]; then 572# @FUNCTION: python_set_build_dir_symlink
390 python_pkg_setup_fail \ 573# @USAGE: [directory="build"]
391 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}" 574# @DESCRIPTION:
392 fi 575# Create build directory symlink.
393 } 576python_set_build_dir_symlink() {
577 local dir="$1"
394 578
395 EXPORT_FUNCTIONS pkg_setup 579 [[ -z "${PYTHON_ABI}" ]] && die "PYTHON_ABI variable not set"
580 [[ -z "${dir}" ]] && dir="build"
396 581
397 if [[ ${PYTHON_USE_WITH} ]]; then 582 # Do not delete preexistent directories.
398 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 583 rm -f "${dir}" || die "Deletion of '${dir}' failed"
399 elif [[ ${PYTHON_USE_WITH_OR} ]]; then 584 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
400 PYTHON_USE_WITH_ATOM="|| ( " 585}
401 for use in ${PYTHON_USE_WITH_OR}; do 586
402 PYTHON_USE_WITH_ATOM=" 587# @FUNCTION: python_generate_wrapper_scripts
403 ${PYTHON_USE_WITH_ATOM} 588# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
404 ${PYTHON_ATOM}[${use}]" 589# @DESCRIPTION:
590# Generate wrapper scripts. Existing files are overwritten only with --force option.
591# If --respect-EPYTHON option is specified, then generated wrapper scripts will
592# respect EPYTHON variable at run time.
593python_generate_wrapper_scripts() {
594 local eselect_python_option file force="0" quiet="0" PYTHON_ABI python2_enabled="0" python2_supported_versions python3_enabled="0" python3_supported_versions respect_EPYTHON="0"
595 python2_supported_versions="2.4 2.5 2.6 2.7"
596 python3_supported_versions="3.0 3.1 3.2"
597
598 while (($#)); do
599 case "$1" in
600 -E|--respect-EPYTHON)
601 respect_EPYTHON="1"
602 ;;
603 -f|--force)
604 force="1"
605 ;;
606 -q|--quiet)
607 quiet="1"
608 ;;
609 --)
610 break
611 ;;
612 -*)
613 die "${FUNCNAME}(): Unrecognized option '$1'"
614 ;;
615 *)
616 break
617 ;;
618 esac
619 shift
405 done 620 done
406 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )" 621
622 if [[ "$#" -eq 0 ]]; then
623 die "${FUNCNAME}(): Missing arguments"
624 fi
625
626 validate_PYTHON_ABIS
627 for PYTHON_ABI in ${python2_supported_versions}; do
628 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
629 python2_enabled="1"
407 fi 630 fi
408 if [[ ${PYTHON_USE_WITH_OPT} ]]; then 631 done
409 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 632 for PYTHON_ABI in ${python3_supported_versions}; do
633 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
634 python3_enabled="1"
410 fi 635 fi
411 DEPEND="${PYTHON_USE_WITH_ATOM}" 636 done
412 RDEPEND="${PYTHON_USE_WITH_ATOM}" 637
638 if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
639 eselect_python_option=
640 elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
641 eselect_python_option="--python2"
642 elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
643 eselect_python_option="--python3"
644 else
645 die "${FUNCNAME}(): Unsupported environment"
413fi 646 fi
414 647
415# @FUNCTION: python_disable_pyc 648 for file in "$@"; do
416# @DESCRIPTION: 649 if [[ -f "${file}" && "${force}" == "0" ]]; then
417# Tells python not to automatically recompile modules to .pyc/.pyo 650 die "${FUNCNAME}(): '$1' already exists"
418# even if the timestamps/version stamps don't match. This is done 651 fi
419# to protect sandbox.
420#
421# note: supported by >=dev-lang/python-2.2.3-r3 only.
422#
423python_disable_pyc() {
424 export PYTHONDONTWRITEBYTECODE=1 # For 2.6 and above
425 export PYTHON_DONTCOMPILE=1 # For 2.5 and below
426}
427 652
428# @FUNCTION: python_enable_pyc 653 if [[ "${quiet}" == "0" ]]; then
429# @DESCRIPTION: 654 einfo "Generating '${file#${D%/}}' wrapper script"
430# Tells python to automatically recompile modules to .pyc/.pyo if the 655 fi
431# timestamps/version stamps change
432python_enable_pyc() {
433 unset PYTHONDONTWRITEBYTECODE
434 unset PYTHON_DONTCOMPILE
435}
436 656
437python_disable_pyc 657 cat << EOF > "${file}"
658#!/usr/bin/env python
659# Gentoo '${file##*/}' wrapper script
660
661import os
662import re
663import subprocess
664import sys
665
666EPYTHON_re = re.compile(r"^python(\d+\.\d+)$")
667
668EOF
669 if [[ "$?" != "0" ]]; then
670 die "${FUNCNAME}(): Generation of '$1' failed"
671 fi
672 if [[ "${respect_EPYTHON}" == "1" ]]; then
673 cat << EOF >> "${file}"
674EPYTHON = os.environ.get("EPYTHON")
675if EPYTHON:
676 EPYTHON_matched = EPYTHON_re.match(EPYTHON)
677 if EPYTHON_matched:
678 PYTHON_ABI = EPYTHON_matched.group(1)
679 else:
680 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
681 sys.exit(1)
682else:
683 try:
684 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
685 if eselect_process.wait() != 0:
686 raise ValueError
687 except (OSError, ValueError):
688 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
689 sys.exit(1)
690
691 eselect_output = eselect_process.stdout.read()
692 if not isinstance(eselect_output, str):
693 # Python 3
694 eselect_output = eselect_output.decode()
695
696 EPYTHON_matched = EPYTHON_re.match(eselect_output)
697 if EPYTHON_matched:
698 PYTHON_ABI = EPYTHON_matched.group(1)
699 else:
700 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
701 sys.exit(1)
702EOF
703 if [[ "$?" != "0" ]]; then
704 die "${FUNCNAME}(): Generation of '$1' failed"
705 fi
706 else
707 cat << EOF >> "${file}"
708try:
709 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
710 if eselect_process.wait() != 0:
711 raise ValueError
712except (OSError, ValueError):
713 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
714 sys.exit(1)
715
716eselect_output = eselect_process.stdout.read()
717if not isinstance(eselect_output, str):
718 # Python 3
719 eselect_output = eselect_output.decode()
720
721EPYTHON_matched = EPYTHON_re.match(eselect_output)
722if EPYTHON_matched:
723 PYTHON_ABI = EPYTHON_matched.group(1)
724else:
725 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
726 sys.exit(1)
727EOF
728 if [[ "$?" != "0" ]]; then
729 die "${FUNCNAME}(): Generation of '$1' failed"
730 fi
731 fi
732 cat << EOF >> "${file}"
733
734os.environ["PYTHON_PROCESS_NAME"] = sys.argv[0]
735target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI)
736if not os.path.exists(target_executable):
737 sys.stderr.write("'%s' does not exist\n" % target_executable)
738 sys.exit(1)
739
740os.execv(target_executable, sys.argv)
741EOF
742 if [[ "$?" != "0" ]]; then
743 die "${FUNCNAME}(): Generation of '$1' failed"
744 fi
745 fperms +x "${file#${D%/}}" || die "fperms '${file}' failed"
746 done
747}
748
749# ================================================================================================
750# ====== FUNCTIONS FOR PACKAGES NOT SUPPORTING INSTALLATION FOR MULTIPLE VERSIONS OF PYTHON ======
751# ================================================================================================
752
753# @FUNCTION: python_set_active_version
754# @USAGE: <Python_ABI|2|3>
755# @DESCRIPTION:
756# Set active version of Python.
757python_set_active_version() {
758 if [[ "$#" -ne "1" ]]; then
759 die "${FUNCNAME}() requires 1 argument"
760 fi
761
762 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
763 if ! _python_implementation && ! has_version "dev-lang/python:$1"; then
764 die "${FUNCNAME}(): 'dev-lang/python:$1' is not installed"
765 fi
766 export EPYTHON="$(PYTHON "$1")"
767 elif [[ "$1" == "2" ]]; then
768 if ! _python_implementation && ! has_version "=dev-lang/python-2*"; then
769 die "${FUNCNAME}(): '=dev-lang/python-2*' is not installed"
770 fi
771 export EPYTHON="$(PYTHON -2)"
772 elif [[ "$1" == "3" ]]; then
773 if ! _python_implementation && ! has_version "=dev-lang/python-3*"; then
774 die "${FUNCNAME}(): '=dev-lang/python-3*' is not installed"
775 fi
776 export EPYTHON="$(PYTHON -3)"
777 else
778 die "${FUNCNAME}(): Unrecognized argument '$1'"
779 fi
780
781 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
782 # so it does not need to be exported to subprocesses.
783 PYTHON_ABI="${EPYTHON#python}"
784 PYTHON_ABI="${PYTHON_ABI%%-*}"
785}
438 786
439# @FUNCTION: python_need_rebuild 787# @FUNCTION: python_need_rebuild
440# @DESCRIPTION: Run without arguments, specifies that the package should be 788# @DESCRIPTION: Run without arguments, specifies that the package should be
441# rebuilt after a python upgrade. 789# rebuilt after a python upgrade.
790# Do not use this function in ebuilds of packages supporting installation
791# for multiple versions of Python.
442python_need_rebuild() { 792python_need_rebuild() {
443 python_version 793 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
444 export PYTHON_NEED_REBUILD=${PYVER} 794}
795
796# ================================================================================================
797# ======================================= GETTER FUNCTIONS =======================================
798# ================================================================================================
799
800# @FUNCTION: PYTHON
801# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
802# @DESCRIPTION:
803# Get Python interpreter filename for specified Python ABI. If Python_ABI argument
804# is ommitted, then PYTHON_ABI environment variable must be set and is used.
805# If -2 option is specified, then active version of Python 2 is used.
806# If -3 option is specified, then active version of Python 3 is used.
807# If --active option is specified, then active version of Python is used.
808# Active version of Python can be set by python_set_active_version().
809# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
810# -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
811# If --ABI option is specified, then only specified Python ABI is printed instead of
812# Python interpreter filename.
813# --ABI and --absolute-path options cannot be specified simultaneously.
814PYTHON() {
815 local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" python2="0" python3="0" slot=
816
817 while (($#)); do
818 case "$1" in
819 -2)
820 python2="1"
821 ;;
822 -3)
823 python3="1"
824 ;;
825 --ABI)
826 ABI_output="1"
827 ;;
828 -A|--active)
829 active="1"
830 ;;
831 -a|--absolute-path)
832 absolute_path_output="1"
833 ;;
834 -f|--final-ABI)
835 final_ABI="1"
836 ;;
837 --)
838 break
839 ;;
840 -*)
841 die "${FUNCNAME}(): Unrecognized option '$1'"
842 ;;
843 *)
844 break
845 ;;
846 esac
847 shift
848 done
849
850 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
851 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
852 fi
853
854 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then
855 die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously"
856 fi
857
858 if [[ "$#" -eq 0 ]]; then
859 if [[ "${active}" == "1" ]]; then
860 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
861 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
862 fi
863 slot="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
864 elif [[ "${final_ABI}" == "1" ]]; then
865 if has "${EAPI:-0}" 0 1 2 3 4 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
866 die "${FUNCNAME}(): '--final-ABI' option cannot be used in ebuilds of packages not supporting installation for multiple versions of Python"
867 fi
868 validate_PYTHON_ABIS
869 slot="${PYTHON_ABIS##* }"
870 elif [[ "${python2}" == "1" ]]; then
871 slot="$(eselect python show --python2)"
872 if [[ -z "${slot}" ]]; then
873 die "${FUNCNAME}(): Active Python 2 interpreter not set"
874 elif [[ "${slot}" != "python2."* ]]; then
875 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
876 fi
877 slot="${slot#python}"
878 elif [[ "${python3}" == "1" ]]; then
879 slot="$(eselect python show --python3)"
880 if [[ -z "${slot}" ]]; then
881 die "${FUNCNAME}(): Active Python 3 interpreter not set"
882 elif [[ "${slot}" != "python3."* ]]; then
883 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
884 fi
885 slot="${slot#python}"
886 elif [[ -n "${PYTHON_ABI}" ]]; then
887 slot="${PYTHON_ABI}"
888 else
889 die "${FUNCNAME}(): Invalid usage"
890 fi
891 elif [[ "$#" -eq 1 ]]; then
892 if [[ "${active}" == "1" ]]; then
893 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
894 fi
895 if [[ "${final_ABI}" == "1" ]]; then
896 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
897 fi
898 if [[ "${python2}" == "1" ]]; then
899 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
900 fi
901 if [[ "${python3}" == "1" ]]; then
902 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
903 fi
904 slot="$1"
905 else
906 die "${FUNCNAME}(): Invalid usage"
907 fi
908
909 if [[ "${ABI_output}" == "1" ]]; then
910 echo -n "${slot}"
911 return
912 elif [[ "${absolute_path_output}" == "1" ]]; then
913 echo -n "/usr/bin/python${slot}"
914 else
915 echo -n "python${slot}"
916 fi
917
918 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
919 echo -n "-${ABI}"
920 fi
445} 921}
446 922
447# @FUNCTION: python_get_includedir 923# @FUNCTION: python_get_includedir
924# @USAGE: [-f|--final-ABI]
448# @DESCRIPTION: 925# @DESCRIPTION:
449# Run without arguments, returns the Python include directory. 926# Print Python include directory.
927# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
450python_get_includedir() { 928python_get_includedir() {
929 local final_ABI="0"
930
931 while (($#)); do
932 case "$1" in
933 -f|--final-ABI)
934 final_ABI="1"
935 ;;
936 -*)
937 die "${FUNCNAME}(): Unrecognized option '$1'"
938 ;;
939 *)
940 die "${FUNCNAME}(): Invalid usage"
941 ;;
942 esac
943 shift
944 done
945
946 if [[ "${final_ABI}" == "1" ]]; then
947 echo "/usr/include/python$(PYTHON -f --ABI)"
451 if [[ -n "${PYTHON_ABI}" ]]; then 948 elif [[ -n "${PYTHON_ABI}" ]]; then
452 echo "/usr/include/python${PYTHON_ABI}" 949 echo "/usr/include/python${PYTHON_ABI}"
453 else 950 else
454 python_version
455 echo "/usr/include/python${PYVER}" 951 echo "/usr/include/python$(PYTHON -A --ABI)"
456 fi 952 fi
457} 953}
458 954
459# @FUNCTION: python_get_libdir 955# @FUNCTION: python_get_libdir
956# @USAGE: [-f|--final-ABI]
460# @DESCRIPTION: 957# @DESCRIPTION:
461# Run without arguments, returns the Python library directory. 958# Print Python library directory.
959# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
462python_get_libdir() { 960python_get_libdir() {
961 local final_ABI="0"
962
963 while (($#)); do
964 case "$1" in
965 -f|--final-ABI)
966 final_ABI="1"
967 ;;
968 -*)
969 die "${FUNCNAME}(): Unrecognized option '$1'"
970 ;;
971 *)
972 die "${FUNCNAME}(): Invalid usage"
973 ;;
974 esac
975 shift
976 done
977
978 if [[ "${final_ABI}" == "1" ]]; then
979 echo "/usr/$(get_libdir)/python$(PYTHON -f --ABI)"
463 if [[ -n "${PYTHON_ABI}" ]]; then 980 elif [[ -n "${PYTHON_ABI}" ]]; then
464 echo "/usr/$(get_libdir)/python${PYTHON_ABI}" 981 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
465 else 982 else
466 python_version
467 echo "/usr/$(get_libdir)/python${PYVER}" 983 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)"
468 fi 984 fi
469} 985}
470 986
471# @FUNCTION: python_get_sitedir 987# @FUNCTION: python_get_sitedir
988# @USAGE: [-f|--final-ABI]
472# @DESCRIPTION: 989# @DESCRIPTION:
473# Run without arguments, returns the Python site-packages directory. 990# Print Python site-packages directory.
991# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
474python_get_sitedir() { 992python_get_sitedir() {
993 local options=()
994
995 while (($#)); do
996 case "$1" in
997 -f|--final-ABI)
998 options+=("$1")
999 ;;
1000 -*)
1001 die "${FUNCNAME}(): Unrecognized option '$1'"
1002 ;;
1003 *)
1004 die "${FUNCNAME}(): Invalid usage"
1005 ;;
1006 esac
1007 shift
1008 done
1009
475 echo "$(python_get_libdir)/site-packages" 1010 echo "$(python_get_libdir "${options[@]}")/site-packages"
476} 1011}
477 1012
478# @FUNCTION: python_makesym 1013# ================================================================================================
479# @DESCRIPTION: 1014# =================================== MISCELLANEOUS FUNCTIONS ====================================
480# Run without arguments, it will create the /usr/bin/python symlinks 1015# ================================================================================================
481# to the latest installed version
482python_makesym() {
483 alternatives_auto_makesym "/usr/bin/python" "python[0-9].[0-9]"
484 alternatives_auto_makesym "/usr/bin/python2" "python2.[0-9]"
485}
486 1016
487# @FUNCTION: python_tkinter_exists 1017_python_implementation() {
1018 if [[ "${CATEGORY}/${PN}" == "dev-lang/python" ]]; then
1019 return 0
1020 else
1021 return 1
1022 fi
1023}
1024
1025# @FUNCTION: python_convert_shebangs
1026# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
488# @DESCRIPTION: 1027# @DESCRIPTION:
489# Run without arguments, checks if python was compiled with Tkinter 1028# Convert shebangs in specified files. Directories can be specified only with --recursive option.
490# support. If not, prints an error message and dies. 1029python_convert_shebangs() {
491python_tkinter_exists() { 1030 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
492 if ! python -c "import Tkinter" >/dev/null 2>&1; then 1031
493 eerror "You need to recompile python with Tkinter support." 1032 while (($#)); do
494 eerror "Try adding: 'dev-lang/python tk'" 1033 case "$1" in
495 eerror "in to /etc/portage/package.use" 1034 -r|--recursive)
496 echo 1035 recursive="1"
497 die "missing tkinter support with installed python" 1036 ;;
1037 -q|--quiet)
1038 quiet="1"
1039 ;;
1040 -x|--only-executables)
1041 only_executables="1"
1042 ;;
1043 --)
1044 break
1045 ;;
1046 -*)
1047 die "${FUNCNAME}(): Unrecognized option '$1'"
1048 ;;
1049 *)
1050 break
1051 ;;
1052 esac
1053 shift
1054 done
1055
1056 if [[ "$#" -eq 0 ]]; then
1057 die "${FUNCNAME}(): Missing Python version and files or directories"
1058 elif [[ "$#" -eq 1 ]]; then
1059 die "${FUNCNAME}(): Missing files or directories"
1060 fi
1061
1062 python_version="$1"
1063 shift
1064
1065 for argument in "$@"; do
1066 if [[ ! -e "${argument}" ]]; then
1067 die "${FUNCNAME}(): '${argument}' does not exist"
1068 elif [[ -f "${argument}" ]]; then
1069 files+=("${argument}")
1070 elif [[ -d "${argument}" ]]; then
1071 if [[ "${recursive}" == "1" ]]; then
1072 if [[ "${only_executables}" == "1" ]]; then
1073 files+=($(find "${argument}" -perm /111 -type f))
1074 else
1075 files+=($(find "${argument}" -type f))
1076 fi
1077 else
1078 die "${FUNCNAME}(): '${argument}' is not a regular file"
1079 fi
1080 else
1081 die "${FUNCNAME}(): '${argument}' is not a regular file or a directory"
498 fi 1082 fi
1083 done
1084
1085 for file in "${files[@]}"; do
1086 file="${file#./}"
1087 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
1088
1089 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
1090 if [[ "${quiet}" == "0" ]]; then
1091 einfo "Converting shebang in '${file}'"
1092 fi
1093 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
1094
1095 # Delete potential whitespace after "#!".
1096 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
1097 fi
1098 done
499} 1099}
500 1100
501# @FUNCTION: python_mod_exists 1101# @FUNCTION: python_mod_exists
502# @USAGE: <module> 1102# @USAGE: <module>
503# @DESCRIPTION: 1103# @DESCRIPTION:
513python_mod_exists() { 1113python_mod_exists() {
514 [[ "$1" ]] || die "${FUNCNAME} requires an argument!" 1114 [[ "$1" ]] || die "${FUNCNAME} requires an argument!"
515 python -c "import $1" &>/dev/null 1115 python -c "import $1" &>/dev/null
516} 1116}
517 1117
1118# @FUNCTION: python_tkinter_exists
1119# @DESCRIPTION:
1120# Run without arguments, checks if python was compiled with Tkinter
1121# support. If not, prints an error message and dies.
1122python_tkinter_exists() {
1123 if ! python -c "import Tkinter" >/dev/null 2>&1; then
1124 eerror "You need to recompile python with Tkinter support."
1125 eerror "Try adding: 'dev-lang/python tk'"
1126 eerror "in to /etc/portage/package.use"
1127 echo
1128 die "missing tkinter support with installed python"
1129 fi
1130}
1131
1132# ================================================================================================
1133# ======================= FUNCTIONS FOR HANDLING OF BYTE-COMPILED MODULES ========================
1134# ================================================================================================
1135
1136# @FUNCTION: python_enable_pyc
1137# @DESCRIPTION:
1138# Tell Python to automatically recompile modules to .pyc/.pyo if the
1139# timestamps/version stamps have changed.
1140python_enable_pyc() {
1141 unset PYTHONDONTWRITEBYTECODE
1142}
1143
518# @FUNCTION: python_mod_compile 1144# @FUNCTION: python_disable_pyc
519# @USAGE: <file> [more files ...]
520# @DESCRIPTION: 1145# @DESCRIPTION:
521# Given filenames, it will pre-compile the module's .pyc and .pyo. 1146# Tell Python not to automatically recompile modules to .pyc/.pyo
522# This function should only be run in pkg_postinst() 1147# even if the timestamps/version stamps do not match. This is done
523# 1148# to protect sandbox.
524# Example: 1149python_disable_pyc() {
525# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py 1150 export PYTHONDONTWRITEBYTECODE="1"
526#
527python_mod_compile() {
528 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
529 die "${FUNCNAME}() cannot be used in this EAPI"
530 fi
531
532 local f myroot myfiles=()
533
534 # Check if phase is pkg_postinst()
535 [[ ${EBUILD_PHASE} != postinst ]] &&\
536 die "${FUNCNAME} should only be run in pkg_postinst()"
537
538 # allow compiling for older python versions
539 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
540 PYVER=${PYTHON_OVERRIDE_PYVER}
541 else
542 python_version
543 fi
544
545 # strip trailing slash
546 myroot="${ROOT%/}"
547
548 # respect ROOT
549 for f in "$@"; do
550 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
551 done
552
553 if ((${#myfiles[@]})); then
554 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
555 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}"
556 else
557 ewarn "No files to compile!"
558 fi
559} 1151}
560 1152
561# @FUNCTION: python_mod_optimize 1153# @FUNCTION: python_mod_optimize
562# @USAGE: [options] [directory|file] 1154# @USAGE: [options] [directory|file]
563# @DESCRIPTION: 1155# @DESCRIPTION:
575python_mod_optimize() { 1167python_mod_optimize() {
576 # Check if phase is pkg_postinst(). 1168 # Check if phase is pkg_postinst().
577 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()" 1169 [[ ${EBUILD_PHASE} != "postinst" ]] && die "${FUNCNAME} should only be run in pkg_postinst()"
578 1170
579 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1171 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
580 local dir file options=() other_dirs=() other_files=() PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=() 1172 local dir file options=() other_dirs=() other_files=() previous_PYTHON_ABI="${PYTHON_ABI}" PYTHON_ABI return_code root site_packages_absolute_dirs=() site_packages_dirs=() site_packages_absolute_files=() site_packages_files=()
581 1173
582 # Strip trailing slash from ROOT. 1174 # Strip trailing slash from ROOT.
583 root="${ROOT%/}" 1175 root="${ROOT%/}"
584 1176
585 # Respect ROOT and options passed to compileall.py. 1177 # Respect ROOT and options passed to compileall.py.
591 -d|-x) 1183 -d|-x)
592 options+=("$1" "$2") 1184 options+=("$1" "$2")
593 shift 1185 shift
594 ;; 1186 ;;
595 -*) 1187 -*)
596 ewarn "${FUNCNAME}: Ignoring compile option $1" 1188 ewarn "${FUNCNAME}: Ignoring option '$1'"
597 ;; 1189 ;;
598 *) 1190 *)
599 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1191 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
600 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 1192 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories"
601 elif [[ "$1" =~ ^/ ]]; then 1193 elif [[ "$1" =~ ^/ ]]; then
602 if [[ -d "${root}/$1" ]]; then 1194 if [[ -d "${root}/$1" ]]; then
603 other_dirs+=("${root}/$1") 1195 other_dirs+=("${root}/$1")
604 elif [[ -f "${root}/$1" ]]; then 1196 elif [[ -f "${root}/$1" ]]; then
605 other_files+=("${root}/$1") 1197 other_files+=("${root}/$1")
606 elif [[ -e "${root}/$1" ]]; then 1198 elif [[ -e "${root}/$1" ]]; then
607 ewarn "'${root}/$1' is not a file or a directory!" 1199 ewarn "'${root}/$1' is not a file or a directory!"
608 else 1200 else
609 ewarn "'${root}/$1' doesn't exist!" 1201 ewarn "'${root}/$1' does not exist!"
610 fi 1202 fi
611 else 1203 else
612 for PYTHON_ABI in ${PYTHON_ABIS}; do 1204 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
613 if [[ -d "${root}/$(python_get_sitedir)/$1" ]]; then 1205 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
614 site_packages_dirs+=("$1") 1206 site_packages_dirs+=("$1")
615 break 1207 break
616 elif [[ -f "${root}/$(python_get_sitedir)/$1" ]]; then 1208 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
617 site_packages_files+=("$1") 1209 site_packages_files+=("$1")
618 break 1210 break
619 elif [[ -e "${root}/$(python_get_sitedir)/$1" ]]; then 1211 elif [[ -e "${root}$(python_get_sitedir)/$1" ]]; then
620 ewarn "'$1' is not a file or a directory!" 1212 ewarn "'$1' is not a file or a directory!"
621 else 1213 else
622 ewarn "'$1' doesn't exist!" 1214 ewarn "'$1' does not exist!"
623 fi 1215 fi
624 done 1216 done
625 fi 1217 fi
626 ;; 1218 ;;
627 esac 1219 esac
629 done 1221 done
630 1222
631 # Set additional options. 1223 # Set additional options.
632 options+=("-q") 1224 options+=("-q")
633 1225
634 for PYTHON_ABI in ${PYTHON_ABIS}; do 1226 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
635 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 1227 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
636 return_code="0" 1228 return_code="0"
637 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" 1229 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
638 if ((${#site_packages_dirs[@]})); then 1230 if ((${#site_packages_dirs[@]})); then
639 for dir in "${site_packages_dirs[@]}"; do 1231 for dir in "${site_packages_dirs[@]}"; do
640 site_packages_absolute_dirs+=("${root}/$(python_get_sitedir)/${dir}") 1232 site_packages_absolute_dirs+=("${root}$(python_get_sitedir)/${dir}")
641 done 1233 done
642 "$(PYTHON)" "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" 1234 "$(PYTHON)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1"
643 "$(PYTHON)" -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" || return_code="1" 1235 "$(PYTHON)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${site_packages_absolute_dirs[@]}" &> /dev/null || return_code="1"
644 fi 1236 fi
645 if ((${#site_packages_files[@]})); then 1237 if ((${#site_packages_files[@]})); then
646 for file in "${site_packages_files[@]}"; do 1238 for file in "${site_packages_files[@]}"; do
647 site_packages_absolute_files+=("${root}/$(python_get_sitedir)/${file}") 1239 site_packages_absolute_files+=("${root}$(python_get_sitedir)/${file}")
648 done 1240 done
649 "$(PYTHON)" "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" 1241 "$(PYTHON)" "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1"
650 "$(PYTHON)" -O "${root}/$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" || return_code="1" 1242 "$(PYTHON)" -O "${root}$(python_get_libdir)/py_compile.py" "${site_packages_absolute_files[@]}" &> /dev/null || return_code="1"
651 fi 1243 fi
652 eend "${return_code}" 1244 eend "${return_code}"
653 fi 1245 fi
654 unset site_packages_absolute_dirs site_packages_absolute_files 1246 unset site_packages_absolute_dirs site_packages_absolute_files
655 done 1247 done
656 1248
657 # Don't use PYTHON_ABI in next calls to python_get_libdir(). 1249 # Restore previous value of PYTHON_ABI.
1250 if [[ -n "${previous_PYTHON_ABI}" ]]; then
1251 PYTHON_ABI="${previous_PYTHON_ABI}"
1252 else
658 unset PYTHON_ABI 1253 unset PYTHON_ABI
1254 fi
659 1255
660 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1256 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
661 return_code="0" 1257 return_code="0"
662 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." 1258 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)"
663 if ((${#other_dirs[@]})); then 1259 if ((${#other_dirs[@]})); then
664 python${PYVER} "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 1260 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
665 python${PYVER} -O "${root}/$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 1261 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
666 fi 1262 fi
667 if ((${#other_files[@]})); then 1263 if ((${#other_files[@]})); then
668 python${PYVER} "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 1264 "$(PYTHON "${PYTHON_ABI--A}")" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
669 python${PYVER} -O "${root}/$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 1265 "$(PYTHON "${PYTHON_ABI--A}")" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
670 fi 1266 fi
671 eend "${return_code}" 1267 eend "${return_code}"
672 fi 1268 fi
673 else 1269 else
674 local myroot mydirs=() myfiles=() myopts=() 1270 local myroot mydirs=() myfiles=() myopts=() return_code="0"
675 1271
676 # strip trailing slash 1272 # strip trailing slash
677 myroot="${ROOT%/}" 1273 myroot="${ROOT%/}"
678 1274
679 # respect ROOT and options passed to compileall.py 1275 # respect ROOT and options passed to compileall.py
685 -d|-x) 1281 -d|-x)
686 myopts+=("$1" "$2") 1282 myopts+=("$1" "$2")
687 shift 1283 shift
688 ;; 1284 ;;
689 -*) 1285 -*)
690 ewarn "${FUNCNAME}: Ignoring compile option $1" 1286 ewarn "${FUNCNAME}: Ignoring option '$1'"
691 ;; 1287 ;;
692 *) 1288 *)
693 if [[ -d "${myroot}"/$1 ]]; then 1289 if [[ -d "${myroot}"/$1 ]]; then
694 mydirs+=("${myroot}/$1") 1290 mydirs+=("${myroot}/$1")
695 elif [[ -f "${myroot}"/$1 ]]; then 1291 elif [[ -f "${myroot}"/$1 ]]; then
696 # Files are passed to python_mod_compile which is ROOT-aware 1292 # Files are passed to python_mod_compile which is ROOT-aware
697 myfiles+=("$1") 1293 myfiles+=("$1")
698 elif [[ -e "${myroot}/$1" ]]; then 1294 elif [[ -e "${myroot}/$1" ]]; then
699 ewarn "${myroot}/$1 is not a file or directory!" 1295 ewarn "${myroot}/$1 is not a file or directory!"
700 else 1296 else
701 ewarn "${myroot}/$1 doesn't exist!" 1297 ewarn "${myroot}/$1 does not exist!"
702 fi 1298 fi
703 ;; 1299 ;;
704 esac 1300 esac
705 shift 1301 shift
706 done 1302 done
707 1303
708 # allow compiling for older python versions
709 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
710 PYVER=${PYTHON_OVERRIDE_PYVER}
711 else
712 python_version
713 fi
714
715 # set additional opts 1304 # set additional opts
716 myopts+=(-q) 1305 myopts+=(-q)
717 1306
718 ebegin "Byte compiling python modules for python-${PYVER} .." 1307 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)"
719 if ((${#mydirs[@]})); then 1308 if ((${#mydirs[@]})); then
720 python${PYVER} \ 1309 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
721 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 1310 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
722 "${myopts[@]}" "${mydirs[@]}"
723 python${PYVER} -O \
724 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
725 "${myopts[@]}" "${mydirs[@]}"
726 fi 1311 fi
727 1312
728 if ((${#myfiles[@]})); then 1313 if ((${#myfiles[@]})); then
729 python_mod_compile "${myfiles[@]}" 1314 python_mod_compile "${myfiles[@]}"
730 fi 1315 fi
731 1316
732 eend $? 1317 eend "${return_code}"
733 fi 1318 fi
734} 1319}
735 1320
736# @FUNCTION: python_mod_cleanup 1321# @FUNCTION: python_mod_cleanup
737# @USAGE: [directory] 1322# @USAGE: [directory|file]
738# @DESCRIPTION: 1323# @DESCRIPTION:
739# Run with optional arguments, where arguments are directories of 1324# Run with optional arguments, where arguments are Python modules. If none given,
740# python modules. If none given, it will look in /usr/lib/python[0-9].[0-9]. 1325# it will look in /usr/lib/python[0-9].[0-9].
741# 1326#
742# It will recursively scan all compiled Python modules in the directories and 1327# It will recursively scan all compiled Python modules in the directories and
743# determine if they are orphaned (i.e. their corresponding .py files are missing.) 1328# determine if they are orphaned (i.e. their corresponding .py files are missing.)
744# If they are, then it will remove their corresponding .pyc and .pyo files. 1329# If they are, then it will remove their corresponding .pyc and .pyo files.
745# 1330#
746# This function should only be run in pkg_postrm(). 1331# This function should only be run in pkg_postrm().
747python_mod_cleanup() { 1332python_mod_cleanup() {
748 local PYTHON_ABI SEARCH_PATH=() root src_py 1333 local path py_file PYTHON_ABI SEARCH_PATH=() root
749 1334
750 # Check if phase is pkg_postrm(). 1335 # Check if phase is pkg_postrm().
751 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" 1336 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
752 1337
753 # Strip trailing slash from ROOT. 1338 # Strip trailing slash from ROOT.
754 root="${ROOT%/}" 1339 root="${ROOT%/}"
755 1340
756 if (($#)); then 1341 if (($#)); then
757 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then 1342 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
758 while (($#)); do 1343 while (($#)); do
759 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1344 if ! _python_implementation && [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
760 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 1345 die "${FUNCNAME} does not support absolute paths of directories/files in site-packages directories"
761 elif [[ "$1" =~ ^/ ]]; then 1346 elif [[ "$1" =~ ^/ ]]; then
762 SEARCH_PATH+=("${root}/${1#/}") 1347 SEARCH_PATH+=("${root}/${1#/}")
763 else 1348 else
764 for PYTHON_ABI in ${PYTHON_ABIS}; do 1349 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
765 SEARCH_PATH+=("${root}/$(python_get_sitedir)/$1") 1350 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
766 done 1351 done
767 fi 1352 fi
768 shift 1353 shift
769 done 1354 done
770 else 1355 else
771 SEARCH_PATH=("${@#/}") 1356 SEARCH_PATH=("${@#/}")
772 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 1357 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
773 fi 1358 fi
774 else 1359 else
775 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) 1360 local dir sitedir
1361 for dir in "${root}"/usr/lib*; do
1362 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
1363 for sitedir in "${dir}"/python*/site-packages; do
1364 if [[ -d "${sitedir}" ]]; then
1365 SEARCH_PATH+=("${sitedir}")
1366 fi
1367 done
1368 fi
1369 done
1370 fi
1371
1372 local BLUE CYAN NORMAL
1373 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1374 BLUE=$'\e[1;34m'
1375 CYAN=$'\e[1;36m'
1376 NORMAL=$'\e[0m'
1377 else
1378 BLUE=
1379 CYAN=
1380 NORMAL=
776 fi 1381 fi
777 1382
778 for path in "${SEARCH_PATH[@]}"; do 1383 for path in "${SEARCH_PATH[@]}"; do
779 einfo "Cleaning orphaned Python bytecode from ${path} .." 1384 if [[ -d "${path}" ]]; then
780 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 1385 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
781 src_py="${REPLY%[co]}" 1386 py_file="${REPLY%[co]}"
782 [[ -f "${src_py}" ]] && continue 1387 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
783 einfo "Purging ${src_py}[co]" 1388 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
784 rm -f "${src_py}"[co] 1389 rm -f "${py_file}"[co]
785 done 1390 done
786 1391
787 # Attempt to remove directories that may be empty. 1392 # Attempt to delete directories, which may be empty.
788 find "${path}" -type d | sort -r | while read -r dir; do 1393 find "${path}" -type d | sort -r | while read -r dir; do
789 rmdir "${dir}" 2>/dev/null 1394 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
790 done 1395 done
1396 elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then
1397 einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1398 rm -f "${path}"[co]
1399 fi
791 done 1400 done
792} 1401}
1402
1403# ================================================================================================
1404# ===================================== DEPRECATED FUNCTIONS =====================================
1405# ================================================================================================
1406
1407# @FUNCTION: python_version
1408# @DESCRIPTION:
1409# Run without arguments and it will export the version of python
1410# currently in use as $PYVER; sets PYVER/PYVER_MAJOR/PYVER_MINOR
1411python_version() {
1412 [[ -n "${PYVER}" ]] && return 0
1413 local tmpstr
1414 python="${python:-/usr/bin/python}"
1415 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
1416 export PYVER_ALL="${tmpstr#Python }"
1417 export PYVER_MAJOR="${PYVER_ALL:0:1}"
1418 export PYVER_MINOR="${PYVER_ALL:2:1}"
1419 if [[ "${PYVER_ALL:3:1}" == "." ]]; then
1420 export PYVER_MICRO="${PYVER_ALL:4}"
1421 fi
1422 export PYVER="${PYVER_MAJOR}.${PYVER_MINOR}"
1423}
1424
1425# @FUNCTION: python_mod_compile
1426# @USAGE: <file> [more files ...]
1427# @DESCRIPTION:
1428# Given filenames, it will pre-compile the module's .pyc and .pyo.
1429# This function should only be run in pkg_postinst()
1430#
1431# Example:
1432# python_mod_compile /usr/lib/python2.3/site-packages/pygoogle.py
1433#
1434python_mod_compile() {
1435 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
1436 die "${FUNCNAME}() cannot be used in this EAPI"
1437 fi
1438
1439 local f myroot myfiles=()
1440
1441 # Check if phase is pkg_postinst()
1442 [[ ${EBUILD_PHASE} != postinst ]] &&\
1443 die "${FUNCNAME} should only be run in pkg_postinst()"
1444
1445 # strip trailing slash
1446 myroot="${ROOT%/}"
1447
1448 # respect ROOT
1449 for f in "$@"; do
1450 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
1451 done
1452
1453 if ((${#myfiles[@]})); then
1454 "$(PYTHON "${PYTHON_ABI--A}")" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
1455 "$(PYTHON "${PYTHON_ABI--A}")" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
1456 else
1457 ewarn "No files to compile!"
1458 fi
1459}

Legend:
Removed from v.1.60  
changed lines
  Added in v.1.86

  ViewVC Help
Powered by ViewVC 1.1.20