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

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

  ViewVC Help
Powered by ViewVC 1.1.20