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

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

  ViewVC Help
Powered by ViewVC 1.1.20