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

Diff of /eclass/python.eclass

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

Revision 1.72 Revision 1.83
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.72 2009/09/11 19:55:05 arfrever Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.83 2010/01/10 17:03:08 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.
13 11
14inherit multilib 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
24DEPEND="${DEPEND} >=app-shells/bash-3.2"
25if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
26 DEPEND="${DEPEND} >=app-admin/eselect-python-20090804" 26DEPEND+=" >=app-admin/eselect-python-20090804"
27fi
28 27
29__python_eclass_test() { 28__python_eclass_test() {
30 __python_version_extract 2.3 29 __python_version_extract 2.3
31 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR" 30 echo -n "2.3 -> PYVER: $PYVER PYVER_MAJOR: $PYVER_MAJOR"
32 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO" 31 echo " PYVER_MINOR: $PYVER_MINOR PYVER_MICRO: $PYVER_MICRO"
60 59
61python_version() { 60python_version() {
62 [[ -n "${PYVER}" ]] && return 0 61 [[ -n "${PYVER}" ]] && return 0
63 local tmpstr 62 local tmpstr
64 python=${python:-/usr/bin/python} 63 python=${python:-/usr/bin/python}
65 tmpstr="$(${python} -V 2>&1 )" 64 tmpstr="$(EPYTHON= ${python} -V 2>&1 )"
66 export PYVER_ALL="${tmpstr#Python }" 65 export PYVER_ALL="${tmpstr#Python }"
67 __python_version_extract $PYVER_ALL 66 __python_version_extract $PYVER_ALL
68} 67}
69 68
70# @FUNCTION: PYTHON 69# @FUNCTION: PYTHON
71# @USAGE: [-a|--absolute-path] [--] <Python_ABI="${PYTHON_ABI}"> 70# @USAGE: [-2] [-3] [--ABI] [-A|--active] [-a|--absolute-path] [-f|--final-ABI] [--] <Python_ABI="${PYTHON_ABI}">
72# @DESCRIPTION: 71# @DESCRIPTION:
73# Get Python interpreter filename for specified Python ABI. If Python_ABI argument 72# Get Python interpreter filename for specified Python ABI. If Python_ABI argument
74# is ommitted, then PYTHON_ABI environment variable must be set and is used. 73# is ommitted, then PYTHON_ABI environment variable must be set and is used.
74# If -2 option is specified, then active version of Python 2 is used.
75# If -3 option is specified, then active version of Python 3 is used.
76# If --active option is specified, then active version of Python is used.
77# Active version of Python can be set by python_set_active_version().
78# If --final-ABI option is specified, then final ABI from the list of enabled ABIs is used.
79# -2, -3, --active and --final-ABI options and Python_ABI argument cannot be specified simultaneously.
80# If --ABI option is specified, then only specified Python ABI is printed instead of
81# Python interpreter filename.
82# --ABI and --absolute-path options cannot be specified simultaneously.
75PYTHON() { 83PYTHON() {
76 local absolute_path="0" slot= 84 local ABI_output="0" absolute_path_output="0" active="0" final_ABI="0" python2="0" python3="0" slot=
77 85
78 while (($#)); do 86 while (($#)); do
79 case "$1" in 87 case "$1" in
88 -2)
89 python2="1"
90 ;;
91 -3)
92 python3="1"
93 ;;
94 --ABI)
95 ABI_output="1"
96 ;;
97 -A|--active)
98 active="1"
99 ;;
80 -a|--absolute-path) 100 -a|--absolute-path)
81 absolute_path="1" 101 absolute_path_output="1"
102 ;;
103 -f|--final-ABI)
104 final_ABI="1"
82 ;; 105 ;;
83 --) 106 --)
84 break 107 break
85 ;; 108 ;;
86 -*) 109 -*)
87 die "${FUNCNAME}(): Unrecognized option $1" 110 die "${FUNCNAME}(): Unrecognized option '$1'"
88 ;; 111 ;;
89 *) 112 *)
90 break 113 break
91 ;; 114 ;;
92 esac 115 esac
93 shift 116 shift
94 done 117 done
95 118
119 if [[ "${ABI_output}" == "1" && "${absolute_path_output}" == "1" ]]; then
120 die "${FUNCNAME}(): '--ABI and '--absolute-path' options cannot be specified simultaneously"
121 fi
122
123 if [[ "$((${python2} + ${python3} + ${active} + ${final_ABI}))" -gt 1 ]]; then
124 die "${FUNCNAME}(): '-2', '-3', '--active' or '--final-ABI' options cannot be specified simultaneously"
125 fi
126
96 if [[ "$#" -eq "0" ]]; then 127 if [[ "$#" -eq 0 ]]; then
128 if [[ "${active}" == "1" ]]; then
129 if [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
130 die "${FUNCNAME}(): '--active' option cannot be used in ebuilds of packages supporting installation for multiple versions of Python"
131 fi
132 slot="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
133 elif [[ "${final_ABI}" == "1" ]]; then
134 validate_PYTHON_ABIS
135 slot="${PYTHON_ABIS##* }"
136 elif [[ "${python2}" == "1" ]]; then
137 slot="$(eselect python show --python2)"
138 if [[ -z "${slot}" ]]; then
139 die "${FUNCNAME}(): Active Python 2 interpreter not set"
140 elif [[ "${slot}" != "python2."* ]]; then
141 die "${FUNCNAME}(): Internal error in \`eselect python show --python2\`"
142 fi
143 slot="${slot#python}"
144 elif [[ "${python3}" == "1" ]]; then
145 slot="$(eselect python show --python3)"
146 if [[ -z "${slot}" ]]; then
147 die "${FUNCNAME}(): Active Python 3 interpreter not set"
148 elif [[ "${slot}" != "python3."* ]]; then
149 die "${FUNCNAME}(): Internal error in \`eselect python show --python3\`"
150 fi
151 slot="${slot#python}"
97 if [[ -n "${PYTHON_ABI}" ]]; then 152 elif [[ -n "${PYTHON_ABI}" ]]; then
98 slot="${PYTHON_ABI}" 153 slot="${PYTHON_ABI}"
99 else 154 else
100 die "${FUNCNAME}(): Invalid usage" 155 die "${FUNCNAME}(): Invalid usage"
101 fi 156 fi
102 elif [[ "$#" -eq "1" ]]; then 157 elif [[ "$#" -eq 1 ]]; then
158 if [[ "${active}" == "1" ]]; then
159 die "${FUNCNAME}(): '--active' option and Python ABI cannot be specified simultaneously"
160 fi
161 if [[ "${final_ABI}" == "1" ]]; then
162 die "${FUNCNAME}(): '--final-ABI' option and Python ABI cannot be specified simultaneously"
163 fi
164 if [[ "${python2}" == "1" ]]; then
165 die "${FUNCNAME}(): '-2' option and Python ABI cannot be specified simultaneously"
166 fi
167 if [[ "${python3}" == "1" ]]; then
168 die "${FUNCNAME}(): '-3' option and Python ABI cannot be specified simultaneously"
169 fi
103 slot="$1" 170 slot="$1"
104 else 171 else
105 die "${FUNCNAME}(): Invalid usage" 172 die "${FUNCNAME}(): Invalid usage"
106 fi 173 fi
107 174
175 if [[ "${ABI_output}" == "1" ]]; then
176 echo -n "${slot}"
177 return
108 if [[ "${absolute_path}" == "1" ]]; then 178 elif [[ "${absolute_path_output}" == "1" ]]; then
109 echo -n "/usr/bin/python${slot}" 179 echo -n "/usr/bin/python${slot}"
110 else 180 else
111 echo -n "python${slot}" 181 echo -n "python${slot}"
112 fi 182 fi
183
184 if [[ -n "${ABI}" && "${ABI}" != "${DEFAULT_ABI}" && "${DEFAULT_ABI}" != "default" ]]; then
185 echo -n "-${ABI}"
186 fi
113} 187}
188
189# @FUNCTION: python_set_active_version
190# @USAGE: <Python_ABI|2|3>
191# @DESCRIPTION:
192# Set active version of Python.
193python_set_active_version() {
194 if [[ "$#" -ne "1" ]]; then
195 die "${FUNCNAME}() requires 1 argument"
196 fi
197
198 if [[ "$1" =~ ^[[:digit:]]+\.[[:digit:]]+$ ]]; then
199 if ! has_version "dev-lang/python:$1"; then
200 die "${FUNCNAME}(): 'dev-lang/python:$1' isn't installed"
201 fi
202 export EPYTHON="$(PYTHON "$1")"
203 elif [[ "$1" == "2" ]]; then
204 if ! has_version "=dev-lang/python-2*"; then
205 die "${FUNCNAME}(): '=dev-lang/python-2*' isn't installed"
206 fi
207 export EPYTHON="$(PYTHON -2)"
208 elif [[ "$1" == "3" ]]; then
209 if ! has_version "=dev-lang/python-3*"; then
210 die "${FUNCNAME}(): '=dev-lang/python-3*' isn't installed"
211 fi
212 export EPYTHON="$(PYTHON -3)"
213 else
214 die "${FUNCNAME}(): Unrecognized argument '$1'"
215 fi
216
217 # PYTHON_ABI variable is intended to be used only in ebuilds/eclasses,
218 # so it doesn't need to be exported to subprocesses.
219 PYTHON_ABI="${EPYTHON#python}"
220 PYTHON_ABI="${PYTHON_ABI%%-*}"
221}
222
223unset PYTHON_ABIS
224unset PYTHON_ABIS_SANITY_CHECKS
114 225
115# @FUNCTION: validate_PYTHON_ABIS 226# @FUNCTION: validate_PYTHON_ABIS
116# @DESCRIPTION: 227# @DESCRIPTION:
117# Make sure PYTHON_ABIS variable has valid value. 228# Ensure that PYTHON_ABIS variable has valid value.
118validate_PYTHON_ABIS() { 229validate_PYTHON_ABIS() {
119 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable. 230 # Ensure that some functions cannot be accidentally successfully used in EAPI <= 2 without setting SUPPORT_PYTHON_ABIS variable.
120 if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then 231 if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
121 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable" 232 die "${FUNCNAME}() cannot be used in this EAPI without setting SUPPORT_PYTHON_ABIS variable"
122 fi 233 fi
123 234
124 # Ensure that /usr/bin/python and /usr/bin/python-config are valid. 235 # Ensure that /usr/bin/python and /usr/bin/python-config are valid.
125 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then 236 if [[ "$(readlink /usr/bin/python)" != "python-wrapper" ]]; then
237 eerror "'/usr/bin/python' is not valid symlink."
238 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
126 die "'/usr/bin/python' isn't valid symlink" 239 die "'/usr/bin/python' is not valid symlink"
127 fi 240 fi
128 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then 241 if [[ "$(</usr/bin/python-config)" != *"Gentoo python-config wrapper script"* ]]; then
242 eerror "'/usr/bin/python-config' is not valid script"
243 eerror "Use \`eselect python set \${python_interpreter}\` to fix this problem."
129 die "'/usr/bin/python-config' isn't valid script" 244 die "'/usr/bin/python-config' is not valid script"
130 fi 245 fi
131 246
132 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. 247 # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 5.
133 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then 248 if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3 4; then
134 local ABI support_ABI supported_PYTHON_ABIS= restricted_ABI 249 local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS=
135 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" 250 PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2"
251 python2_supported_versions="2.4 2.5 2.6 2.7"
252 python3_supported_versions="3.0 3.1 3.2"
136 253
137 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then 254 if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then
255 local python2_enabled="0" python3_enabled="0"
256
138 if [[ -z "${USE_PYTHON}" ]]; then 257 if [[ -z "${USE_PYTHON}" ]]; then
139 die "USE_PYTHON variable is empty" 258 die "USE_PYTHON variable is empty"
140 fi 259 fi
141 260
142 for ABI in ${USE_PYTHON}; do 261 for PYTHON_ABI in ${USE_PYTHON}; do
143 if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then 262 if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then
144 die "USE_PYTHON variable contains invalid value '${ABI}'" 263 die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'"
145 fi 264 fi
265
266 if has "${PYTHON_ABI}" ${python2_supported_versions}; then
267 python2_enabled="1"
268 fi
269 if has "${PYTHON_ABI}" ${python3_supported_versions}; then
270 python3_enabled="1"
271 fi
272
146 support_ABI="1" 273 support_ABI="1"
147 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 274 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
148 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${ABI}', '${restricted_ABI}'))"; then 275 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
149 support_ABI="0" 276 support_ABI="0"
150 break 277 break
151 fi 278 fi
152 done 279 done
153 [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" 280 [[ "${support_ABI}" == "1" ]] && export PYTHON_ABIS+="${PYTHON_ABIS:+ }${PYTHON_ABI}"
154 done 281 done
155 export PYTHON_ABIS="${supported_PYTHON_ABIS# }"
156 282
157 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then 283 if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then
158 die "USE_PYTHON variable doesn't enable any Python version supported by ${CATEGORY}/${PF}" 284 die "USE_PYTHON variable doesn't enable any version of Python supported by ${CATEGORY}/${PF}"
285 fi
286
287 if [[ "${python2_enabled}" == "0" ]]; then
288 ewarn "USE_PYTHON variable doesn't enable any version of Python 2. This configuration is unsupported."
289 fi
290 if [[ "${python3_enabled}" == "0" ]]; then
291 ewarn "USE_PYTHON variable doesn't enable any version of Python 3. This configuration is unsupported."
159 fi 292 fi
160 else 293 else
161 local restricted_ABI 294 local python_version python2_version= python3_version= support_python_major_version
162 python_version
163 295
296 python_version="$(/usr/bin/python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
297
298 if has_version "=dev-lang/python-2*"; then
299 if [[ "$(readlink /usr/bin/python2)" != "python2."* ]]; then
300 die "'/usr/bin/python2' isn't valid symlink"
301 fi
302
303 python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
304
305 for PYTHON_ABI in ${python2_supported_versions}; do
306 support_python_major_version="1"
164 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do 307 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
165 if python -c "from fnmatch import fnmatch; exit(not fnmatch('${PYVER}', '${restricted_ABI}'))"; then 308 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
166 die "Active Python version isn't supported by ${CATEGORY}/${PF}" 309 support_python_major_version="0"
167 fi 310 fi
311 done
312 [[ "${support_python_major_version}" == "1" ]] && break
168 done 313 done
169 export PYTHON_ABIS="${PYVER}" 314 if [[ "${support_python_major_version}" == "1" ]]; then
315 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
316 if [[ "${python2_version}" == ${restricted_ABI} ]]; then
317 die "Active version of Python 2 isn't supported by ${CATEGORY}/${PF}"
318 fi
319 done
320 else
321 python2_version=""
322 fi
170 fi 323 fi
324
325 if has_version "=dev-lang/python-3*"; then
326 if [[ "$(readlink /usr/bin/python3)" != "python3."* ]]; then
327 die "'/usr/bin/python3' isn't valid symlink"
328 fi
329
330 python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')"
331
332 for PYTHON_ABI in ${python3_supported_versions}; do
333 support_python_major_version="1"
334 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
335 if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then
336 support_python_major_version="0"
337 fi
338 done
339 [[ "${support_python_major_version}" == "1" ]] && break
340 done
341 if [[ "${support_python_major_version}" == "1" ]]; then
342 for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do
343 if [[ "${python3_version}" == ${restricted_ABI} ]]; then
344 die "Active version of Python 3 isn't supported by ${CATEGORY}/${PF}"
345 fi
346 done
347 else
348 python3_version=""
349 fi
350 fi
351
352 if [[ -n "${python2_version}" && "${python_version}" == "2."* && "${python_version}" != "${python2_version}" ]]; then
353 eerror "Python wrapper is configured incorrectly or /usr/bin/python2 symlink"
354 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
355 die "Incorrect configuration of Python"
356 fi
357 if [[ -n "${python3_version}" && "${python_version}" == "3."* && "${python_version}" != "${python3_version}" ]]; then
358 eerror "Python wrapper is configured incorrectly or /usr/bin/python3 symlink"
359 eerror "is set incorrectly. Use \`eselect python\` to fix configuration."
360 die "Incorrect configuration of Python"
361 fi
362
363 PYTHON_ABIS="${python2_version} ${python3_version}"
364 PYTHON_ABIS="${PYTHON_ABIS# }"
365 export PYTHON_ABIS="${PYTHON_ABIS% }"
171 fi 366 fi
367 fi
172 368
369 if [[ "$(declare -p PYTHON_ABIS_SANITY_CHECKS 2> /dev/null)" != "declare -- PYTHON_ABIS_SANITY_CHECKS="* ]]; then
173 local PYTHON_ABI 370 local PYTHON_ABI
174 for PYTHON_ABI in ${PYTHON_ABIS}; do 371 for PYTHON_ABI in ${PYTHON_ABIS}; do
175 # Ensure that appropriate Python version is installed. 372 # Ensure that appropriate version of Python is installed.
176 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then 373 if ! has_version "dev-lang/python:${PYTHON_ABI}"; then
177 die "dev-lang/python:${PYTHON_ABI} isn't installed" 374 die "dev-lang/python:${PYTHON_ABI} isn't installed"
178 fi 375 fi
179 376
180 # Ensure that EPYTHON variable is respected. 377 # Ensure that EPYTHON variable is respected.
181 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then 378 if [[ "$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" != "${PYTHON_ABI}" ]]; then
379 eerror "python: '$(type -p python)'"
380 eerror "ABI: '${ABI}'"
381 eerror "DEFAULT_ABI: '${DEFAULT_ABI}'"
382 eerror "EPYTHON: '$(PYTHON)'"
383 eerror "PYTHON_ABI: '${PYTHON_ABI}'"
384 eerror "Version of enabled Python: '$(EPYTHON="$(PYTHON)" python -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')'"
182 die "'python' doesn't respect EPYTHON variable" 385 die "'python' doesn't respect EPYTHON variable"
183 fi 386 fi
184 done 387 done
388 PYTHON_ABIS_SANITY_CHECKS="1"
389 fi
185} 390}
186 391
187# @FUNCTION: python_copy_sources 392# @FUNCTION: python_copy_sources
188# @USAGE: [--no-link] [--] [directory] 393# @USAGE: [--no-link] [--] [directory]
189# @DESCRIPTION: 394# @DESCRIPTION:
207 ;; 412 ;;
208 esac 413 esac
209 shift 414 shift
210 done 415 done
211 416
212 if [[ "$#" -eq "0" ]]; then 417 if [[ "$#" -eq 0 ]]; then
213 if [[ "${WORKDIR}" == "${S}" ]]; then 418 if [[ "${WORKDIR}" == "${S}" ]]; then
214 die "${FUNCNAME}() cannot be used" 419 die "${FUNCNAME}() cannot be used"
215 fi 420 fi
216 dirs="${S}" 421 dirs="${S}"
217 else 422 else
244 rm -f "${dir}" || die "Deletion of '${dir}' failed" 449 rm -f "${dir}" || die "Deletion of '${dir}' failed"
245 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed" 450 ln -s "${dir}-${PYTHON_ABI}" "${dir}" || die "Creation of '${dir}' directory symlink failed"
246} 451}
247 452
248# @FUNCTION: python_execute_function 453# @FUNCTION: python_execute_function
249# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--] <function> [arguments] 454# @USAGE: [--action-message message] [-d|--default-function] [--failure-message message] [--nonfatal] [-q|--quiet] [-s|--separate-build-dirs] [--source-dir source_directory] [--] <function> [arguments]
250# @DESCRIPTION: 455# @DESCRIPTION:
251# Execute specified function for each value of PYTHON_ABIS, optionally passing additional 456# Execute specified function for each value of PYTHON_ABIS, optionally passing additional
252# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables. 457# arguments. The specified function can use PYTHON_ABI and BUILDDIR variables.
253python_execute_function() { 458python_execute_function() {
254 local action action_message action_message_template= default_function="0" failure_message failure_message_template= function nonfatal="0" previous_directory_stack_length PYTHON_ABI quiet="0" separate_build_dirs="0" 459 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=
255 460
256 while (($#)); do 461 while (($#)); do
257 case "$1" in 462 case "$1" in
258 --action-message) 463 --action-message)
259 action_message_template="$2" 464 action_message_template="$2"
273 quiet="1" 478 quiet="1"
274 ;; 479 ;;
275 -s|--separate-build-dirs) 480 -s|--separate-build-dirs)
276 separate_build_dirs="1" 481 separate_build_dirs="1"
277 ;; 482 ;;
483 --source-dir)
484 source_dir="$2"
485 shift
486 ;;
278 --) 487 --)
279 break 488 break
280 ;; 489 ;;
281 -*) 490 -*)
282 die "${FUNCNAME}(): Unrecognized option '$1'" 491 die "${FUNCNAME}(): Unrecognized option '$1'"
286 ;; 495 ;;
287 esac 496 esac
288 shift 497 shift
289 done 498 done
290 499
500 if [[ -n "${source_dir}" && "${separate_build_dirs}" == 0 ]]; then
501 die "${FUNCNAME}(): '--source-dir' option can be specified only with '--separate-build-dirs' option"
502 fi
503
291 if [[ "${default_function}" == "0" ]]; then 504 if [[ "${default_function}" == "0" ]]; then
292 if [[ "$#" -eq "0" ]]; then 505 if [[ "$#" -eq 0 ]]; then
293 die "${FUNCNAME}(): Missing function name" 506 die "${FUNCNAME}(): Missing function name"
294 fi 507 fi
295 function="$1" 508 function="$1"
296 shift 509 shift
297 510
305 if has "${EAPI:-0}" 0 1; then 518 if has "${EAPI:-0}" 0 1; then
306 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI" 519 die "${FUNCNAME}(): '--default-function' option cannot be used in this EAPI"
307 fi 520 fi
308 521
309 if [[ "${EBUILD_PHASE}" == "configure" ]]; then 522 if [[ "${EBUILD_PHASE}" == "configure" ]]; then
310 if has "${EAPI}" 2; then 523 if has "${EAPI}" 2 3; then
311 python_default_function() { 524 python_default_function() {
312 econf 525 econf
313 } 526 }
314 else 527 else
315 python_default_function() { 528 python_default_function() {
331 elif [[ "${EBUILD_PHASE}" == "install" ]]; then 544 elif [[ "${EBUILD_PHASE}" == "install" ]]; then
332 python_default_function() { 545 python_default_function() {
333 emake DESTDIR="${D}" install 546 emake DESTDIR="${D}" install
334 } 547 }
335 else 548 else
336 die "${FUNCNAME}(): --default-function option cannot be used in this ebuild phase" 549 die "${FUNCNAME}(): '--default-function' option cannot be used in this ebuild phase"
337 fi 550 fi
338 function="python_default_function" 551 function="python_default_function"
339 fi 552 fi
340 553
341 if [[ "${quiet}" == "0" ]]; then 554 if [[ "${quiet}" == "0" ]]; then
375 fi 588 fi
376 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}" 589 echo " ${GREEN}*${NORMAL} ${BLUE}${action_message}${NORMAL}"
377 fi 590 fi
378 591
379 if [[ "${separate_build_dirs}" == "1" ]]; then 592 if [[ "${separate_build_dirs}" == "1" ]]; then
593 if [[ -n "${source_dir}" ]]; then
594 export BUILDDIR="${S}/${source_dir}-${PYTHON_ABI}"
595 else
380 export BUILDDIR="${S}-${PYTHON_ABI}" 596 export BUILDDIR="${S}-${PYTHON_ABI}"
597 fi
381 pushd "${BUILDDIR}" > /dev/null || die "pushd failed" 598 pushd "${BUILDDIR}" > /dev/null || die "pushd failed"
382 else 599 else
383 export BUILDDIR="${S}" 600 export BUILDDIR="${S}"
384 fi 601 fi
385 602
386 previous_directory_stack_length="${#DIRSTACK[@]}" 603 previous_directory="$(pwd)"
604 previous_directory_stack="$(dirs -p)"
605 previous_directory_stack_length="$(dirs -p | wc -l)"
387 606
388 if ! has "${EAPI}" 0 1 2 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 607 if ! has "${EAPI}" 0 1 2 3 && has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
389 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@" 608 EPYTHON="$(PYTHON)" nonfatal "${function}" "$@"
390 else 609 else
391 EPYTHON="$(PYTHON)" "${function}" "$@" 610 EPYTHON="$(PYTHON)" "${function}" "$@"
392 fi 611 fi
393 612
402 if [[ "${quiet}" == "0" ]]; then 621 if [[ "${quiet}" == "0" ]]; then
403 ewarn "${RED}${failure_message}${NORMAL}" 622 ewarn "${RED}${failure_message}${NORMAL}"
404 fi 623 fi
405 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then 624 elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then
406 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then 625 if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then
407 local ABI enabled_PYTHON_ABIS= 626 local enabled_PYTHON_ABIS= other_PYTHON_ABI
408 for ABI in ${PYTHON_ABIS}; do 627 for other_PYTHON_ABI in ${PYTHON_ABIS}; do
409 [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}" 628 [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+="${enabled_PYTHON_ABIS:+ }${other_PYTHON_ABI}"
410 done 629 done
411 export PYTHON_ABIS="${enabled_PYTHON_ABIS# }" 630 export PYTHON_ABIS="${enabled_PYTHON_ABIS}"
412 fi 631 fi
413 if [[ "${quiet}" == "0" ]]; then 632 if [[ "${quiet}" == "0" ]]; then
414 ewarn "${RED}${failure_message}${NORMAL}" 633 ewarn "${RED}${failure_message}${NORMAL}"
415 fi 634 fi
416 if [[ -z "${PYTHON_ABIS}" ]]; then 635 if [[ -z "${PYTHON_ABIS}" ]]; then
419 else 638 else
420 die "${failure_message}" 639 die "${failure_message}"
421 fi 640 fi
422 fi 641 fi
423 642
643 # Ensure that directory stack hasn't been decreased.
424 if [[ "${#DIRSTACK[@]}" -lt "${previous_directory_stack_length}" ]]; then 644 if [[ "$(dirs -p | wc -l)" -lt "${previous_directory_stack_length}" ]]; then
425 die "Directory stack decreased illegally" 645 die "Directory stack decreased illegally"
426 fi 646 fi
427 647
648 # Avoid side effects of earlier returning from the specified function.
428 while [[ "${#DIRSTACK[@]}" -gt "${previous_directory_stack_length}" ]]; do 649 while [[ "$(dirs -p | wc -l)" -gt "${previous_directory_stack_length}" ]]; do
429 popd > /dev/null || die "popd failed" 650 popd > /dev/null || die "popd failed"
430 done 651 done
431 652
653 # Ensure that the bottom part of directory stack hasn't been changed. Restore
654 # previous directory (from before running of the specified function) before
655 # comparison of directory stacks to avoid mismatch of directory stacks after
656 # potential using of 'cd' to change current directory. Restoration of previous
657 # directory allows to safely use 'cd' to change current directory in the
658 # specified function without changing it back to original directory.
659 cd "${previous_directory}"
660 if [[ "$(dirs -p)" != "${previous_directory_stack}" ]]; then
661 die "Directory stack changed illegally"
662 fi
663
432 if [[ "${separate_build_dirs}" == "1" ]]; then 664 if [[ "${separate_build_dirs}" == "1" ]]; then
433 popd > /dev/null || die "popd failed" 665 popd > /dev/null || die "popd failed"
434 fi 666 fi
435 unset BUILDDIR 667 unset BUILDDIR
436 done 668 done
438 if [[ "${default_function}" == "1" ]]; then 670 if [[ "${default_function}" == "1" ]]; then
439 unset -f python_default_function 671 unset -f python_default_function
440 fi 672 fi
441} 673}
442 674
675# @FUNCTION: python_convert_shebangs
676# @USAGE: [-q|--quiet] [-r|--recursive] [-x|--only-executables] [--] <Python_version> <file|directory> [files|directories]
677# @DESCRIPTION:
678# Convert shebangs in specified files. Directories can be specified only with --recursive option.
679python_convert_shebangs() {
680 local argument file files=() only_executables="0" python_version quiet="0" recursive="0"
681
682 while (($#)); do
683 case "$1" in
684 -r|--recursive)
685 recursive="1"
686 ;;
687 -q|--quiet)
688 quiet="1"
689 ;;
690 -x|--only-executables)
691 only_executables="1"
692 ;;
693 --)
694 break
695 ;;
696 -*)
697 die "${FUNCNAME}(): Unrecognized option '$1'"
698 ;;
699 *)
700 break
701 ;;
702 esac
703 shift
704 done
705
706 if [[ "$#" -eq 0 ]]; then
707 die "${FUNCNAME}(): Missing Python version and files or directories"
708 elif [[ "$#" -eq 1 ]]; then
709 die "${FUNCNAME}(): Missing files or directories"
710 fi
711
712 python_version="$1"
713 shift
714
715 for argument in "$@"; do
716 if [[ ! -e "${argument}" ]]; then
717 die "${FUNCNAME}(): '${argument}' doesn't exist"
718 elif [[ -f "${argument}" ]]; then
719 files+=("${argument}")
720 elif [[ -d "${argument}" ]]; then
721 if [[ "${recursive}" == "1" ]]; then
722 if [[ "${only_executables}" == "1" ]]; then
723 files+=($(find "${argument}" -perm /111 -type f))
724 else
725 files+=($(find "${argument}" -type f))
726 fi
727 else
728 die "${FUNCNAME}(): '${argument}' isn't a regular file"
729 fi
730 else
731 die "${FUNCNAME}(): '${argument}' isn't a regular file or a directory"
732 fi
733 done
734
735 for file in "${files[@]}"; do
736 file="${file#./}"
737 [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue
738
739 if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then
740 if [[ "${quiet}" == "0" ]]; then
741 einfo "Converting shebang in '${file}'"
742 fi
743 sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed"
744
745 # Delete potential whitespace after "#!".
746 sed -e '1s/\(^#!\)[[:space:]]*/\1/' -i "${file}" || die "sed '${file}' failed"
747 fi
748 done
749}
750
751# @FUNCTION: python_generate_wrapper_scripts
752# @USAGE: [-E|--respect-EPYTHON] [-f|--force] [-q|--quiet] [--] <file> [files]
753# @DESCRIPTION:
754# Generate wrapper scripts. Existing files are overwritten only with --force option.
755# If --respect-EPYTHON option is specified, then generated wrapper scripts will
756# respect EPYTHON variable at run time.
757python_generate_wrapper_scripts() {
758 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"
759 python2_supported_versions="2.4 2.5 2.6 2.7"
760 python3_supported_versions="3.0 3.1 3.2"
761
762 while (($#)); do
763 case "$1" in
764 -E|--respect-EPYTHON)
765 respect_EPYTHON="1"
766 ;;
767 -f|--force)
768 force="1"
769 ;;
770 -q|--quiet)
771 quiet="1"
772 ;;
773 --)
774 break
775 ;;
776 -*)
777 die "${FUNCNAME}(): Unrecognized option '$1'"
778 ;;
779 *)
780 break
781 ;;
782 esac
783 shift
784 done
785
786 if [[ "$#" -eq 0 ]]; then
787 die "${FUNCNAME}(): Missing arguments"
788 fi
789
790 validate_PYTHON_ABIS
791 for PYTHON_ABI in ${python2_supported_versions}; do
792 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
793 python2_enabled="1"
794 fi
795 done
796 for PYTHON_ABI in ${python3_supported_versions}; do
797 if has "${PYTHON_ABI}" ${PYTHON_ABIS}; then
798 python3_enabled="1"
799 fi
800 done
801
802 if [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "1" ]]; then
803 eselect_python_option=
804 elif [[ "${python2_enabled}" == "1" && "${python3_enabled}" == "0" ]]; then
805 eselect_python_option="--python2"
806 elif [[ "${python2_enabled}" == "0" && "${python3_enabled}" == "1" ]]; then
807 eselect_python_option="--python3"
808 else
809 die "${FUNCNAME}(): Unsupported environment"
810 fi
811
812 for file in "$@"; do
813 if [[ -f "${file}" && "${force}" == "0" ]]; then
814 die "${FUNCNAME}(): '$1' already exists"
815 fi
816
817 if [[ "${quiet}" == "0" ]]; then
818 einfo "Generating '${file#${D%/}}' wrapper script"
819 fi
820
821 cat << EOF > "${file}"
822#!/usr/bin/env python
823# Gentoo '${file##*/}' wrapper script
824
825import os
826import re
827import subprocess
828import sys
829
830EPYTHON_re = re.compile(r"^python(\d+\.\d+)$")
831
832EOF
833 if [[ "$?" != "0" ]]; then
834 die "${FUNCNAME}(): Generation of '$1' failed"
835 fi
836 if [[ "${respect_EPYTHON}" == "1" ]]; then
837 cat << EOF >> "${file}"
838EPYTHON = os.environ.get("EPYTHON")
839if EPYTHON:
840 EPYTHON_matched = EPYTHON_re.match(EPYTHON)
841 if EPYTHON_matched:
842 PYTHON_ABI = EPYTHON_matched.group(1)
843 else:
844 sys.stderr.write("EPYTHON variable has unrecognized value '%s'\n" % EPYTHON)
845 sys.exit(1)
846else:
847 try:
848 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
849 if eselect_process.wait() != 0:
850 raise ValueError
851 except (OSError, ValueError):
852 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
853 sys.exit(1)
854
855 eselect_output = eselect_process.stdout.read()
856 if not isinstance(eselect_output, str):
857 # Python 3
858 eselect_output = eselect_output.decode()
859
860 EPYTHON_matched = EPYTHON_re.match(eselect_output)
861 if EPYTHON_matched:
862 PYTHON_ABI = EPYTHON_matched.group(1)
863 else:
864 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
865 sys.exit(1)
866EOF
867 if [[ "$?" != "0" ]]; then
868 die "${FUNCNAME}(): Generation of '$1' failed"
869 fi
870 else
871 cat << EOF >> "${file}"
872try:
873 eselect_process = subprocess.Popen(["/usr/bin/eselect", "python", "show"${eselect_python_option:+, $(echo "\"")}${eselect_python_option}${eselect_python_option:+$(echo "\"")}], stdout=subprocess.PIPE)
874 if eselect_process.wait() != 0:
875 raise ValueError
876except (OSError, ValueError):
877 sys.stderr.write("Execution of 'eselect python show${eselect_python_option:+ }${eselect_python_option}' failed\n")
878 sys.exit(1)
879
880eselect_output = eselect_process.stdout.read()
881if not isinstance(eselect_output, str):
882 # Python 3
883 eselect_output = eselect_output.decode()
884
885EPYTHON_matched = EPYTHON_re.match(eselect_output)
886if EPYTHON_matched:
887 PYTHON_ABI = EPYTHON_matched.group(1)
888else:
889 sys.stderr.write("'eselect python show${eselect_python_option:+ }${eselect_python_option}' printed unrecognized value '%s" % eselect_output)
890 sys.exit(1)
891EOF
892 if [[ "$?" != "0" ]]; then
893 die "${FUNCNAME}(): Generation of '$1' failed"
894 fi
895 fi
896 cat << EOF >> "${file}"
897
898os.environ["PYTHON_PROCESS_NAME"] = sys.argv[0]
899target_executable = "%s-%s" % (os.path.realpath(sys.argv[0]), PYTHON_ABI)
900if not os.path.exists(target_executable):
901 sys.stderr.write("'%s' does not exist\n" % target_executable)
902 sys.exit(1)
903
904os.execv(target_executable, sys.argv)
905EOF
906 if [[ "$?" != "0" ]]; then
907 die "${FUNCNAME}(): Generation of '$1' failed"
908 fi
909 fperms +x "${file#${D%/}}" || die "fperms '${file}' failed"
910 done
911}
443 912
444# @ECLASS-VARIABLE: PYTHON_USE_WITH 913# @ECLASS-VARIABLE: PYTHON_USE_WITH
445# @DESCRIPTION: 914# @DESCRIPTION:
446# Set this to a space separated list of use flags 915# Set this to a space separated list of use flags
447# the python slot in use must be built with. 916# the python slot in use must be built with.
460# @FUNCTION: python_pkg_setup 929# @FUNCTION: python_pkg_setup
461# @DESCRIPTION: 930# @DESCRIPTION:
462# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags 931# Makes sure PYTHON_USE_WITH or PYTHON_USE_WITH_OR listed use flags
463# are respected. Only exported if one of those variables is set. 932# are respected. Only exported if one of those variables is set.
464if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then 933if ! has "${EAPI:-0}" 0 1 && [[ -n ${PYTHON_USE_WITH} || -n ${PYTHON_USE_WITH_OR} ]]; then
934 python_pkg_setup() {
465 python_pkg_setup_fail() { 935 python_pkg_setup_fail() {
466 eerror "${1}" 936 eerror "${1}"
467 die "${1}" 937 die "${1}"
938 }
939
940 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
941
942 python_pkg_setup_check_USE_flags() {
943 local pyatom use
944 if [[ -n "${PYTHON_ABI}" ]]; then
945 pyatom="dev-lang/python:${PYTHON_ABI}"
946 else
947 pyatom="dev-lang/python:$(PYTHON -A --ABI)"
948 fi
949
950 for use in ${PYTHON_USE_WITH}; do
951 if ! has_version "${pyatom}[${use}]"; then
952 python_pkg_setup_fail "Please rebuild ${pyatom} with the following USE flags enabled: ${PYTHON_USE_WITH}"
953 fi
954 done
955
956 for use in ${PYTHON_USE_WITH_OR}; do
957 if has_version "${pyatom}[${use}]"; then
958 return
959 fi
960 done
961
962 if [[ ${PYTHON_USE_WITH_OR} ]]; then
963 python_pkg_setup_fail "Please rebuild ${pyatom} with at least one of the following USE flags enabled: ${PYTHON_USE_WITH_OR}"
964 fi
965 }
966
967 if ! has "${EAPI:-0}" 0 1 2 || [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
968 python_execute_function -q python_pkg_setup_check_USE_flags
969 else
970 python_pkg_setup_check_USE_flags
971 fi
468 } 972 }
469 973
470 python_pkg_setup() {
471 [[ ${PYTHON_USE_WITH_OPT} ]] && use !${PYTHON_USE_WITH_OPT} && return
472
473 python_version
474 local failed
475 local pyatom="dev-lang/python:${PYVER}"
476
477 for use in ${PYTHON_USE_WITH}; do
478 if ! has_version "${pyatom}[${use}]"; then
479 python_pkg_setup_fail \
480 "Please rebuild ${pyatom} with use flags: ${PYTHON_USE_WITH}"
481 fi
482 done
483
484 for use in ${PYTHON_USE_WITH_OR}; do
485 if has_version "${pyatom}[${use}]"; then
486 return
487 fi
488 done
489
490 if [[ ${PYTHON_USE_WITH_OR} ]]; then
491 python_pkg_setup_fail \
492 "Please rebuild ${pyatom} with one of: ${PYTHON_USE_WITH_OR}"
493 fi
494 }
495
496 EXPORT_FUNCTIONS pkg_setup 974 EXPORT_FUNCTIONS pkg_setup
497 975
498 if [[ ${PYTHON_USE_WITH} ]]; then 976 if [[ -n "${PYTHON_USE_WITH}" ]]; then
499 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]" 977 PYTHON_USE_WITH_ATOM="${PYTHON_ATOM}[${PYTHON_USE_WITH/ /,}]"
500 elif [[ ${PYTHON_USE_WITH_OR} ]]; then 978 elif [[ -n "${PYTHON_USE_WITH_OR}" ]]; then
501 PYTHON_USE_WITH_ATOM="|| ( " 979 PYTHON_USE_WITH_ATOM="|| ( "
502 for use in ${PYTHON_USE_WITH_OR}; do 980 for use in ${PYTHON_USE_WITH_OR}; do
503 PYTHON_USE_WITH_ATOM=" 981 PYTHON_USE_WITH_ATOM+=" ${PYTHON_ATOM}[${use}]"
504 ${PYTHON_USE_WITH_ATOM}
505 ${PYTHON_ATOM}[${use}]"
506 done 982 done
507 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_ATOM} )" 983 unset use
984 PYTHON_USE_WITH_ATOM+=" )"
508 fi 985 fi
509 if [[ ${PYTHON_USE_WITH_OPT} ]]; then 986 if [[ -n "${PYTHON_USE_WITH_OPT}" ]]; then
510 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )" 987 PYTHON_USE_WITH_ATOM="${PYTHON_USE_WITH_OPT}? ( ${PYTHON_USE_WITH_ATOM} )"
511 fi 988 fi
512 DEPEND="${PYTHON_USE_WITH_ATOM}" 989 DEPEND+=" ${PYTHON_USE_WITH_ATOM}"
513 RDEPEND="${PYTHON_USE_WITH_ATOM}" 990 RDEPEND+=" ${PYTHON_USE_WITH_ATOM}"
514fi 991fi
515 992
516# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS 993# @ECLASS-VARIABLE: PYTHON_DEFINE_DEFAULT_FUNCTIONS
517# @DESCRIPTION: 994# @DESCRIPTION:
518# Set this to define default functions for the following ebuild phases: 995# Set this to define default functions for the following ebuild phases:
545# timestamps/version stamps have changed. 1022# timestamps/version stamps have changed.
546python_enable_pyc() { 1023python_enable_pyc() {
547 unset PYTHONDONTWRITEBYTECODE 1024 unset PYTHONDONTWRITEBYTECODE
548} 1025}
549 1026
550python_disable_pyc
551
552# @FUNCTION: python_need_rebuild 1027# @FUNCTION: python_need_rebuild
553# @DESCRIPTION: Run without arguments, specifies that the package should be 1028# @DESCRIPTION: Run without arguments, specifies that the package should be
554# rebuilt after a python upgrade. 1029# rebuilt after a python upgrade.
555python_need_rebuild() { 1030python_need_rebuild() {
556 python_version 1031 export PYTHON_NEED_REBUILD="$(PYTHON -A --ABI)"
557 export PYTHON_NEED_REBUILD=${PYVER}
558} 1032}
559 1033
560# @FUNCTION: python_get_includedir 1034# @FUNCTION: python_get_includedir
561# @DESCRIPTION: 1035# @DESCRIPTION:
562# Run without arguments, returns the Python include directory. 1036# Run without arguments, returns the Python include directory.
563python_get_includedir() { 1037python_get_includedir() {
564 if [[ -n "${PYTHON_ABI}" ]]; then 1038 if [[ -n "${PYTHON_ABI}" ]]; then
565 echo "/usr/include/python${PYTHON_ABI}" 1039 echo "/usr/include/python${PYTHON_ABI}"
566 else 1040 else
567 python_version
568 echo "/usr/include/python${PYVER}" 1041 echo "/usr/include/python$(PYTHON -A --ABI)"
569 fi 1042 fi
570} 1043}
571 1044
572# @FUNCTION: python_get_libdir 1045# @FUNCTION: python_get_libdir
573# @DESCRIPTION: 1046# @DESCRIPTION:
574# Run without arguments, returns the Python library directory. 1047# Run without arguments, returns the Python library directory.
575python_get_libdir() { 1048python_get_libdir() {
576 if [[ -n "${PYTHON_ABI}" ]]; then 1049 if [[ -n "${PYTHON_ABI}" ]]; then
577 echo "/usr/$(get_libdir)/python${PYTHON_ABI}" 1050 echo "/usr/$(get_libdir)/python${PYTHON_ABI}"
578 else 1051 else
579 python_version
580 echo "/usr/$(get_libdir)/python${PYVER}" 1052 echo "/usr/$(get_libdir)/python$(PYTHON -A --ABI)"
581 fi 1053 fi
582} 1054}
583 1055
584# @FUNCTION: python_get_sitedir 1056# @FUNCTION: python_get_sitedir
585# @DESCRIPTION: 1057# @DESCRIPTION:
637 1109
638 # Check if phase is pkg_postinst() 1110 # Check if phase is pkg_postinst()
639 [[ ${EBUILD_PHASE} != postinst ]] &&\ 1111 [[ ${EBUILD_PHASE} != postinst ]] &&\
640 die "${FUNCNAME} should only be run in pkg_postinst()" 1112 die "${FUNCNAME} should only be run in pkg_postinst()"
641 1113
642 # allow compiling for older python versions
643 if [[ "${PYTHON_OVERRIDE_PYVER}" ]]; then
644 PYVER=${PYTHON_OVERRIDE_PYVER}
645 else
646 python_version
647 fi
648
649 # strip trailing slash 1114 # strip trailing slash
650 myroot="${ROOT%/}" 1115 myroot="${ROOT%/}"
651 1116
652 # respect ROOT 1117 # respect ROOT
653 for f in "$@"; do 1118 for f in "$@"; do
654 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}") 1119 [[ -f "${myroot}/${f}" ]] && myfiles+=("${myroot}/${f}")
655 done 1120 done
656 1121
657 if ((${#myfiles[@]})); then 1122 if ((${#myfiles[@]})); then
658 python${PYVER} ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" 1123 "$(PYTHON -A)" "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}"
659 python${PYVER} -O ${myroot}/usr/$(get_libdir)/python${PYVER}/py_compile.py "${myfiles[@]}" &> /dev/null 1124 "$(PYTHON -A)" -O "${myroot}$(python_get_libdir)/py_compile.py" "${myfiles[@]}" &> /dev/null
660 else 1125 else
661 ewarn "No files to compile!" 1126 ewarn "No files to compile!"
662 fi 1127 fi
663} 1128}
664 1129
711 ewarn "'${root}/$1' is not a file or a directory!" 1176 ewarn "'${root}/$1' is not a file or a directory!"
712 else 1177 else
713 ewarn "'${root}/$1' doesn't exist!" 1178 ewarn "'${root}/$1' doesn't exist!"
714 fi 1179 fi
715 else 1180 else
716 for PYTHON_ABI in ${PYTHON_ABIS}; do 1181 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
717 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then 1182 if [[ -d "${root}$(python_get_sitedir)/$1" ]]; then
718 site_packages_dirs+=("$1") 1183 site_packages_dirs+=("$1")
719 break 1184 break
720 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then 1185 elif [[ -f "${root}$(python_get_sitedir)/$1" ]]; then
721 site_packages_files+=("$1") 1186 site_packages_files+=("$1")
733 done 1198 done
734 1199
735 # Set additional options. 1200 # Set additional options.
736 options+=("-q") 1201 options+=("-q")
737 1202
738 for PYTHON_ABI in ${PYTHON_ABIS}; do 1203 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
739 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then 1204 if ((${#site_packages_dirs[@]})) || ((${#site_packages_files[@]})); then
740 return_code="0" 1205 return_code="0"
741 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}" 1206 ebegin "Compilation and optimization of Python modules for Python ${PYTHON_ABI}"
742 if ((${#site_packages_dirs[@]})); then 1207 if ((${#site_packages_dirs[@]})); then
743 for dir in "${site_packages_dirs[@]}"; do 1208 for dir in "${site_packages_dirs[@]}"; do
761 # Don't use PYTHON_ABI in next calls to python_get_libdir(). 1226 # Don't use PYTHON_ABI in next calls to python_get_libdir().
762 unset PYTHON_ABI 1227 unset PYTHON_ABI
763 1228
764 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then 1229 if ((${#other_dirs[@]})) || ((${#other_files[@]})); then
765 return_code="0" 1230 return_code="0"
766 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." 1231 ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python $(PYTHON -A --ABI)"
767 if ((${#other_dirs[@]})); then 1232 if ((${#other_dirs[@]})); then
768 python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" 1233 "$(PYTHON -A)" "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1"
769 python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" 1234 "$(PYTHON -A)" -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1"
770 fi 1235 fi
771 if ((${#other_files[@]})); then 1236 if ((${#other_files[@]})); then
772 python${PYVER} "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1" 1237 "$(PYTHON -A)" "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" || return_code="1"
773 python${PYVER} -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1" 1238 "$(PYTHON -A)" -O "${root}$(python_get_libdir)/py_compile.py" "${other_files[@]}" &> /dev/null || return_code="1"
774 fi 1239 fi
775 eend "${return_code}" 1240 eend "${return_code}"
776 fi 1241 fi
777 else 1242 else
778 local myroot mydirs=() myfiles=() myopts=() return_code="0" 1243 local myroot mydirs=() myfiles=() myopts=() return_code="0"
807 ;; 1272 ;;
808 esac 1273 esac
809 shift 1274 shift
810 done 1275 done
811 1276
812 # allow compiling for older python versions
813 if [ -n "${PYTHON_OVERRIDE_PYVER}" ]; then
814 PYVER=${PYTHON_OVERRIDE_PYVER}
815 else
816 python_version
817 fi
818
819 # set additional opts 1277 # set additional opts
820 myopts+=(-q) 1278 myopts+=(-q)
821 1279
822 ebegin "Byte compiling python modules for python-${PYVER} .." 1280 ebegin "Compilation and optimization of Python modules for Python $(PYTHON -A --ABI)"
823 if ((${#mydirs[@]})); then 1281 if ((${#mydirs[@]})); then
824 python${PYVER} \ 1282 "$(PYTHON -A)" "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" || return_code="1"
825 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ 1283 "$(PYTHON -A)" -O "${myroot}$(python_get_libdir)/compileall.py" "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
826 "${myopts[@]}" "${mydirs[@]}" || return_code="1"
827 python${PYVER} -O \
828 "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \
829 "${myopts[@]}" "${mydirs[@]}" &> /dev/null || return_code="1"
830 fi 1284 fi
831 1285
832 if ((${#myfiles[@]})); then 1286 if ((${#myfiles[@]})); then
833 python_mod_compile "${myfiles[@]}" 1287 python_mod_compile "${myfiles[@]}"
834 fi 1288 fi
836 eend "${return_code}" 1290 eend "${return_code}"
837 fi 1291 fi
838} 1292}
839 1293
840# @FUNCTION: python_mod_cleanup 1294# @FUNCTION: python_mod_cleanup
841# @USAGE: [directory] 1295# @USAGE: [directory|file]
842# @DESCRIPTION: 1296# @DESCRIPTION:
843# Run with optional arguments, where arguments are directories of 1297# Run with optional arguments, where arguments are Python modules. If none given,
844# python modules. If none given, it will look in /usr/lib/python[0-9].[0-9]. 1298# it will look in /usr/lib/python[0-9].[0-9].
845# 1299#
846# It will recursively scan all compiled Python modules in the directories and 1300# It will recursively scan all compiled Python modules in the directories and
847# determine if they are orphaned (i.e. their corresponding .py files are missing.) 1301# determine if they are orphaned (i.e. their corresponding .py files are missing.)
848# If they are, then it will remove their corresponding .pyc and .pyo files. 1302# If they are, then it will remove their corresponding .pyc and .pyo files.
849# 1303#
850# This function should only be run in pkg_postrm(). 1304# This function should only be run in pkg_postrm().
851python_mod_cleanup() { 1305python_mod_cleanup() {
852 local PYTHON_ABI SEARCH_PATH=() root src_py 1306 local path py_file PYTHON_ABI SEARCH_PATH=() root
853 1307
854 # Check if phase is pkg_postrm(). 1308 # Check if phase is pkg_postrm().
855 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" 1309 [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()"
856 1310
857 # Strip trailing slash from ROOT. 1311 # Strip trailing slash from ROOT.
863 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then 1317 if [[ "$1" =~ ^/usr/lib(32|64)?/python[[:digit:]]+\.[[:digit:]]+ ]]; then
864 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories" 1318 die "${FUNCNAME} doesn't support absolute paths of directories/files in site-packages directories"
865 elif [[ "$1" =~ ^/ ]]; then 1319 elif [[ "$1" =~ ^/ ]]; then
866 SEARCH_PATH+=("${root}/${1#/}") 1320 SEARCH_PATH+=("${root}/${1#/}")
867 else 1321 else
868 for PYTHON_ABI in ${PYTHON_ABIS}; do 1322 for PYTHON_ABI in ${PYTHON_ABIS-${PYTHON_ABI-$(PYTHON -A --ABI)}}; do
869 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1") 1323 SEARCH_PATH+=("${root}$(python_get_sitedir)/$1")
870 done 1324 done
871 fi 1325 fi
872 shift 1326 shift
873 done 1327 done
874 else 1328 else
875 SEARCH_PATH=("${@#/}") 1329 SEARCH_PATH=("${@#/}")
876 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") 1330 SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}")
877 fi 1331 fi
878 else 1332 else
879 SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) 1333 local dir sitedir
1334 for dir in "${root}"/usr/lib*; do
1335 if [[ -d "${dir}" && ! -L "${dir}" ]]; then
1336 for sitedir in "${dir}"/python*/site-packages; do
1337 if [[ -d "${sitedir}" ]]; then
1338 SEARCH_PATH+=("${sitedir}")
1339 fi
1340 done
1341 fi
1342 done
1343 fi
1344
1345 local BLUE CYAN NORMAL
1346 if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then
1347 BLUE=$'\e[1;34m'
1348 CYAN=$'\e[1;36m'
1349 NORMAL=$'\e[0m'
1350 else
1351 BLUE=
1352 CYAN=
1353 NORMAL=
880 fi 1354 fi
881 1355
882 for path in "${SEARCH_PATH[@]}"; do 1356 for path in "${SEARCH_PATH[@]}"; do
883 [[ ! -d "${path}" ]] && continue 1357 if [[ -d "${path}" ]]; then
884 einfo "Cleaning orphaned Python bytecode from ${path} .."
885 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do 1358 find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do
886 src_py="${REPLY%[co]}" 1359 py_file="${REPLY%[co]}"
887 [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue 1360 [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue
888 einfo "Purging ${src_py}[co]" 1361 einfo "${BLUE}<<< ${py_file}[co]${NORMAL}"
889 rm -f "${src_py}"[co] 1362 rm -f "${py_file}"[co]
890 done 1363 done
891 1364
892 # Attempt to remove directories that may be empty. 1365 # Attempt to delete directories, which may be empty.
893 find "${path}" -type d | sort -r | while read -r dir; do 1366 find "${path}" -type d | sort -r | while read -r dir; do
894 rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}" 1367 rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}"
895 done 1368 done
1369 elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then
1370 einfo "${BLUE}<<< ${path}[co]${NORMAL}"
1371 rm -f "${path}"[co]
1372 fi
896 done 1373 done
897} 1374}

Legend:
Removed from v.1.72  
changed lines
  Added in v.1.83

  ViewVC Help
Powered by ViewVC 1.1.20