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

Contents of /eclass/python.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.84 - (hide annotations) (download)
Mon Jan 11 16:07:23 2010 UTC (4 years, 9 months ago) by arfrever
Branch: MAIN
Changes since 1.83: +44 -34 lines
Disable has_version() checks for Python implementations (bug #300579).
Improve some messages.

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

  ViewVC Help
Powered by ViewVC 1.1.20