nvidia-driver.eclass: Use next gen version of readme.gentoo eclass
[gentoo.git] / eclass / distutils.eclass
1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: distutils.eclass
6 # @MAINTAINER:
7 # Gentoo Python Project <python@gentoo.org>
8 # @BLURB: Eclass for packages with build systems using Distutils
9 # @DESCRIPTION:
10 # The distutils eclass defines phase functions for packages with build systems using Distutils.
11 #
12 # This eclass is DEPRECATED. Please use distutils-r1 instead.
13
14 if [[ -z "${_PYTHON_ECLASS_INHERITED}" ]]; then
15         inherit python
16 fi
17
18 inherit multilib
19
20 case "${EAPI:-0}" in
21         6)
22                 die "${ECLASS}.eclass is banned in EAPI ${EAPI}"
23                 ;;
24         0|1)
25                 EXPORT_FUNCTIONS src_unpack src_compile src_install pkg_postinst pkg_postrm
26                 ;;
27         *)
28                 EXPORT_FUNCTIONS src_prepare src_compile src_install pkg_postinst pkg_postrm
29                 ;;
30 esac
31
32 if [[ -z "$(declare -p PYTHON_DEPEND 2> /dev/null)" ]]; then
33         DEPEND="dev-lang/python"
34         RDEPEND="${DEPEND}"
35 fi
36
37         if has "${EAPI:-0}" 0 1 && [[ -n "${SUPPORT_PYTHON_ABIS}" ]]; then
38                 ewarn
39                 ewarn "\"${EBUILD}\":"
40                 ewarn "Deprecation Warning: Usage of distutils.eclass in packages supporting installation"
41                 ewarn "for multiple Python ABIs in EAPI <=1 is deprecated."
42                 ewarn "The ebuild should to be fixed. Please report a bug, if it has not been already reported."
43                 ewarn
44         elif has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
45                 ewarn
46                 ewarn "\"${EBUILD}\":"
47                 ewarn "Deprecation Warning: Usage of distutils.eclass in packages not supporting installation"
48                 ewarn "for multiple Python ABIs in EAPI <=2 is deprecated."
49                 ewarn "The ebuild should to be fixed. Please report a bug, if it has not been already reported."
50                 ewarn
51         fi
52
53 # 'python' variable is deprecated. Use PYTHON() instead.
54 if has "${EAPI:-0}" 0 1 2 && [[ -z "${SUPPORT_PYTHON_ABIS}" ]]; then
55         python="python"
56 else
57         python="die"
58 fi
59
60 # @ECLASS-VARIABLE: DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES
61 # @DESCRIPTION:
62 # Set this to use separate source directories for each enabled version of Python.
63
64 # @ECLASS-VARIABLE: DISTUTILS_SETUP_FILES
65 # @DESCRIPTION:
66 # Array of paths to setup files.
67 # Syntax:
68 #   [current_working_directory|]path_to_setup_file
69
70 # @ECLASS-VARIABLE: DISTUTILS_GLOBAL_OPTIONS
71 # @DESCRIPTION:
72 # Array of global options passed to setup files.
73 # Syntax in EAPI <4:
74 #   global_option
75 # Syntax in EAPI >=4:
76 #   Python_ABI_pattern global_option
77
78 # @ECLASS-VARIABLE: DISTUTILS_SRC_TEST
79 # @DESCRIPTION:
80 # Type of test command used by distutils_src_test().
81 # IUSE and DEPEND are automatically adjusted, unless DISTUTILS_DISABLE_TEST_DEPENDENCY is set.
82 # Valid values:
83 #   setup.py
84 #   nosetests
85 #   py.test
86 #   trial [arguments]
87
88 # @ECLASS-VARIABLE: DISTUTILS_DISABLE_TEST_DEPENDENCY
89 # @DESCRIPTION:
90 # Disable modification of IUSE and DEPEND caused by setting of DISTUTILS_SRC_TEST.
91
92 if [[ -n "${DISTUTILS_SRC_TEST}" && ! "${DISTUTILS_SRC_TEST}" =~ ^(setup\.py|nosetests|py\.test|trial(\ .*)?)$ ]]; then
93         die "'DISTUTILS_SRC_TEST' variable has unsupported value '${DISTUTILS_SRC_TEST}'"
94 fi
95
96 if [[ -z "${DISTUTILS_DISABLE_TEST_DEPENDENCY}" ]]; then
97         if [[ "${DISTUTILS_SRC_TEST}" == "nosetests" ]]; then
98                 IUSE="test"
99                 DEPEND+="${DEPEND:+ }test? ( dev-python/nose )"
100         elif [[ "${DISTUTILS_SRC_TEST}" == "py.test" ]]; then
101                 IUSE="test"
102                 DEPEND+="${DEPEND:+ }test? ( dev-python/pytest )"
103         # trial requires an argument, which is usually equal to "${PN}".
104         elif [[ "${DISTUTILS_SRC_TEST}" =~ ^trial(\ .*)?$ ]]; then
105                 IUSE="test"
106                 DEPEND+="${DEPEND:+ }test? ( dev-python/twisted-core )"
107         fi
108 fi
109
110 if [[ -n "${DISTUTILS_SRC_TEST}" ]]; then
111         EXPORT_FUNCTIONS src_test
112 fi
113
114 # Scheduled for deletion on 2011-06-01.
115 if [[ -n "${DISTUTILS_DISABLE_VERSIONING_OF_PYTHON_SCRIPTS}" ]]; then
116         eerror "Use PYTHON_NONVERSIONED_EXECUTABLES=(\".*\") instead of DISTUTILS_DISABLE_VERSIONING_OF_PYTHON_SCRIPTS variable."
117         die "DISTUTILS_DISABLE_VERSIONING_OF_PYTHON_SCRIPTS variable is banned"
118 fi
119
120 # @ECLASS-VARIABLE: DOCS
121 # @DESCRIPTION:
122 # Additional documentation files installed by distutils_src_install().
123
124 _distutils_get_build_dir() {
125         if _python_package_supporting_installation_for_multiple_python_abis && [[ -z "${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES}" ]]; then
126                 echo "build-${PYTHON_ABI}"
127         else
128                 echo "build"
129         fi
130 }
131
132 _distutils_get_PYTHONPATH() {
133         if _python_package_supporting_installation_for_multiple_python_abis && [[ -z "${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES}" ]]; then
134                 ls -d build-${PYTHON_ABI}/lib* 2> /dev/null
135         else
136                 ls -d build/lib* 2> /dev/null
137         fi
138 }
139
140 _distutils_hook() {
141         if [[ "$#" -ne 1 ]]; then
142                 die "${FUNCNAME}() requires 1 argument"
143         fi
144         if [[ "$(type -t "distutils_src_${EBUILD_PHASE}_$1_hook")" == "function" ]]; then
145                 "distutils_src_${EBUILD_PHASE}_$1_hook"
146         fi
147 }
148
149 _distutils_prepare_global_options() {
150         local element option pattern
151
152         if [[ -n "$(declare -p DISTUTILS_GLOBAL_OPTIONS 2> /dev/null)" && "$(declare -p DISTUTILS_GLOBAL_OPTIONS)" != "declare -a DISTUTILS_GLOBAL_OPTIONS="* ]]; then
153                 die "DISTUTILS_GLOBAL_OPTIONS should be indexed array"
154         fi
155
156         if has "${EAPI:-0}" 0 1 2 3; then
157                 _DISTUTILS_GLOBAL_OPTIONS=("${DISTUTILS_GLOBAL_OPTIONS[@]}")
158         else
159                 _DISTUTILS_GLOBAL_OPTIONS=()
160
161                 for element in "${DISTUTILS_GLOBAL_OPTIONS[@]}"; do
162                         if [[ ! "${element}" =~ ^[^[:space:]]+\ . ]]; then
163                                 die "Element '${element}' of DISTUTILS_GLOBAL_OPTIONS array has invalid syntax"
164                         fi
165                         pattern="${element%% *}"
166                         option="${element#* }"
167                         if _python_check_python_abi_matching "${PYTHON_ABI}" "${pattern}"; then
168                                 _DISTUTILS_GLOBAL_OPTIONS+=("${option}")
169                         fi
170                 done
171         fi
172 }
173
174 _distutils_prepare_current_working_directory() {
175         if [[ "$1" == *"|"*"|"* ]]; then
176                 die "Element '$1' of DISTUTILS_SETUP_FILES array has invalid syntax"
177         fi
178
179         if [[ "$1" == *"|"* ]]; then
180                 echo "${_BOLD}[${1%|*}]${_NORMAL}"
181                 pushd "${1%|*}" > /dev/null || die "Entering directory '${1%|*}' failed"
182         fi
183 }
184
185 _distutils_restore_current_working_directory() {
186         if [[ "$1" == *"|"* ]]; then
187                 popd > /dev/null || die "Leaving directory '${1%|*}' failed"
188         fi
189 }
190
191 # @FUNCTION: distutils_src_unpack
192 # @DESCRIPTION:
193 # The distutils src_unpack function. This function is exported.
194 distutils_src_unpack() {
195         if ! has "${EAPI:-0}" 0 1; then
196                 die "${FUNCNAME}() cannot be used in this EAPI"
197         fi
198
199         if [[ "${EBUILD_PHASE}" != "unpack" ]]; then
200                 die "${FUNCNAME}() can be used only in src_unpack() phase"
201         fi
202
203         unpack ${A}
204         cd "${S}"
205
206         distutils_src_prepare
207 }
208
209 # @FUNCTION: distutils_src_prepare
210 # @DESCRIPTION:
211 # The distutils src_prepare function. This function is exported.
212 distutils_src_prepare() {
213         if ! has "${EAPI:-0}" 0 1 && [[ "${EBUILD_PHASE}" != "prepare" ]]; then
214                 die "${FUNCNAME}() can be used only in src_prepare() phase"
215         fi
216
217         _python_check_python_pkg_setup_execution
218
219         local distribute_setup_existence="0" ez_setup_existence="0"
220
221         if [[ "$#" -ne 0 ]]; then
222                 die "${FUNCNAME}() does not accept arguments"
223         fi
224
225         # Delete ez_setup files to prevent packages from installing Setuptools on their own.
226         [[ -d ez_setup || -f ez_setup.py ]] && ez_setup_existence="1"
227         rm -fr ez_setup*
228         if [[ "${ez_setup_existence}" == "1" ]]; then
229                 echo "def use_setuptools(*args, **kwargs): pass" > ez_setup.py
230         fi
231
232         # Delete distribute_setup files to prevent packages from installing Distribute on their own.
233         [[ -d distribute_setup || -f distribute_setup.py ]] && distribute_setup_existence="1"
234         rm -fr distribute_setup*
235         if [[ "${distribute_setup_existence}" == "1" ]]; then
236                 echo "def use_setuptools(*args, **kwargs): pass" > distribute_setup.py
237         fi
238
239         if [[ -n "${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES}" ]]; then
240                 python_copy_sources
241         fi
242 }
243
244 # @FUNCTION: distutils_src_compile
245 # @DESCRIPTION:
246 # The distutils src_compile function. This function is exported.
247 # In ebuilds of packages supporting installation for multiple versions of Python, this function
248 # calls distutils_src_compile_pre_hook() and distutils_src_compile_post_hook(), if they are defined.
249 distutils_src_compile() {
250         if [[ "${EBUILD_PHASE}" != "compile" ]]; then
251                 die "${FUNCNAME}() can be used only in src_compile() phase"
252         fi
253
254         _python_check_python_pkg_setup_execution
255         _python_set_color_variables
256
257         local setup_file
258
259         if _python_package_supporting_installation_for_multiple_python_abis; then
260                 distutils_building() {
261                         _distutils_hook pre
262
263                         _distutils_prepare_global_options
264
265                         for setup_file in "${DISTUTILS_SETUP_FILES[@]-setup.py}"; do
266                                 _distutils_prepare_current_working_directory "${setup_file}"
267
268                                 echo ${_BOLD}"$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" build -b "$(_distutils_get_build_dir)" "$@"${_NORMAL}
269                                 "$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" build -b "$(_distutils_get_build_dir)" "$@" || return "$?"
270
271                                 _distutils_restore_current_working_directory "${setup_file}"
272                         done
273
274                         _distutils_hook post
275                 }
276                 python_execute_function ${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES:+-s} distutils_building "$@"
277                 unset -f distutils_building
278         else
279                 _distutils_prepare_global_options
280
281                 for setup_file in "${DISTUTILS_SETUP_FILES[@]-setup.py}"; do
282                         _distutils_prepare_current_working_directory "${setup_file}"
283
284                         echo ${_BOLD}"$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" build "$@"${_NORMAL}
285                         "$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" build "$@" || die "Building failed"
286
287                         _distutils_restore_current_working_directory "${setup_file}"
288                 done
289         fi
290 }
291
292 _distutils_src_test_hook() {
293         if [[ "$#" -ne 1 ]]; then
294                 die "${FUNCNAME}() requires 1 arguments"
295         fi
296
297         if ! _python_package_supporting_installation_for_multiple_python_abis; then
298                 return
299         fi
300
301         if [[ "$(type -t "distutils_src_test_pre_hook")" == "function" ]]; then
302                 eval "python_execute_$1_pre_hook() {
303                         distutils_src_test_pre_hook
304                 }"
305         fi
306
307         if [[ "$(type -t "distutils_src_test_post_hook")" == "function" ]]; then
308                 eval "python_execute_$1_post_hook() {
309                         distutils_src_test_post_hook
310                 }"
311         fi
312 }
313
314 # @FUNCTION: distutils_src_test
315 # @DESCRIPTION:
316 # The distutils src_test function. This function is exported, when DISTUTILS_SRC_TEST variable is set.
317 # In ebuilds of packages supporting installation for multiple versions of Python, this function
318 # calls distutils_src_test_pre_hook() and distutils_src_test_post_hook(), if they are defined.
319 distutils_src_test() {
320         if [[ "${EBUILD_PHASE}" != "test" ]]; then
321                 die "${FUNCNAME}() can be used only in src_test() phase"
322         fi
323
324         _python_check_python_pkg_setup_execution
325         _python_set_color_variables
326
327         local arguments setup_file
328
329         if [[ "${DISTUTILS_SRC_TEST}" == "setup.py" ]]; then
330                 if _python_package_supporting_installation_for_multiple_python_abis; then
331                         distutils_testing() {
332                                 _distutils_hook pre
333
334                                 _distutils_prepare_global_options
335
336                                 for setup_file in "${DISTUTILS_SETUP_FILES[@]-setup.py}"; do
337                                         _distutils_prepare_current_working_directory "${setup_file}"
338
339                                         echo ${_BOLD}PYTHONPATH="$(_distutils_get_PYTHONPATH)" "$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" $([[ -z "${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES}" ]] && echo build -b "$(_distutils_get_build_dir)") test "$@"${_NORMAL}
340                                         PYTHONPATH="$(_distutils_get_PYTHONPATH)" "$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" $([[ -z "${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES}" ]] && echo build -b "$(_distutils_get_build_dir)") test "$@" || return "$?"
341
342                                         _distutils_restore_current_working_directory "${setup_file}"
343                                 done
344
345                                 _distutils_hook post
346                         }
347                         python_execute_function ${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES:+-s} distutils_testing "$@"
348                         unset -f distutils_testing
349                 else
350                         _distutils_prepare_global_options
351
352                         for setup_file in "${DISTUTILS_SETUP_FILES[@]-setup.py}"; do
353                                 _distutils_prepare_current_working_directory "${setup_file}"
354
355                                 echo ${_BOLD}PYTHONPATH="$(_distutils_get_PYTHONPATH)" "$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" test "$@"${_NORMAL}
356                                 PYTHONPATH="$(_distutils_get_PYTHONPATH)" "$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" test "$@" || die "Testing failed"
357
358                                 _distutils_restore_current_working_directory "${setup_file}"
359                         done
360                 fi
361         elif [[ "${DISTUTILS_SRC_TEST}" == "nosetests" ]]; then
362                 _distutils_src_test_hook nosetests
363
364                 python_execute_nosetests -P '$(_distutils_get_PYTHONPATH)' ${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES:+-s} -- "$@"
365         elif [[ "${DISTUTILS_SRC_TEST}" == "py.test" ]]; then
366                 _distutils_src_test_hook py.test
367
368                 python_execute_py.test -P '$(_distutils_get_PYTHONPATH)' ${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES:+-s} -- "$@"
369         # trial requires an argument, which is usually equal to "${PN}".
370         elif [[ "${DISTUTILS_SRC_TEST}" =~ ^trial(\ .*)?$ ]]; then
371                 if [[ "${DISTUTILS_SRC_TEST}" == "trial "* ]]; then
372                         arguments="${DISTUTILS_SRC_TEST#trial }"
373                 else
374                         arguments="${PN}"
375                 fi
376
377                 _distutils_src_test_hook trial
378
379                 python_execute_trial -P '$(_distutils_get_PYTHONPATH)' ${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES:+-s} -- ${arguments} "$@"
380         else
381                 die "'DISTUTILS_SRC_TEST' variable has unsupported value '${DISTUTILS_SRC_TEST}'"
382         fi
383 }
384
385 # @FUNCTION: distutils_src_install
386 # @DESCRIPTION:
387 # The distutils src_install function. This function is exported.
388 # In ebuilds of packages supporting installation for multiple versions of Python, this function
389 # calls distutils_src_install_pre_hook() and distutils_src_install_post_hook(), if they are defined.
390 # It also installs some standard documentation files (AUTHORS, Change*, CHANGELOG, CONTRIBUTORS,
391 # KNOWN_BUGS, MAINTAINERS, NEWS, README*, TODO).
392 distutils_src_install() {
393         if [[ "${EBUILD_PHASE}" != "install" ]]; then
394                 die "${FUNCNAME}() can be used only in src_install() phase"
395         fi
396
397         _python_check_python_pkg_setup_execution
398         _python_initialize_prefix_variables
399         _python_set_color_variables
400
401         local default_docs doc line nspkg_pth_file nspkg_pth_files=() setup_file
402
403         if _python_package_supporting_installation_for_multiple_python_abis; then
404                 distutils_installation() {
405                         _distutils_hook pre
406
407                         _distutils_prepare_global_options
408
409                         for setup_file in "${DISTUTILS_SETUP_FILES[@]-setup.py}"; do
410                                 _distutils_prepare_current_working_directory "${setup_file}"
411
412                                 echo ${_BOLD}"$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" $([[ -z "${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES}" ]] && echo build -b "$(_distutils_get_build_dir)") install --no-compile --root="${T}/images/${PYTHON_ABI}" "$@"${_NORMAL}
413                                 "$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" $([[ -z "${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES}" ]] && echo build -b "$(_distutils_get_build_dir)") install --no-compile --root="${T}/images/${PYTHON_ABI}" "$@" || return "$?"
414
415                                 _distutils_restore_current_working_directory "${setup_file}"
416                         done
417
418                         _distutils_hook post
419                 }
420                 python_execute_function ${DISTUTILS_USE_SEPARATE_SOURCE_DIRECTORIES:+-s} distutils_installation "$@"
421                 unset -f distutils_installation
422
423                 python_merge_intermediate_installation_images "${T}/images"
424         else
425                 # Mark the package to be rebuilt after a Python upgrade.
426                 python_need_rebuild
427
428                 _distutils_prepare_global_options
429
430                 for setup_file in "${DISTUTILS_SETUP_FILES[@]-setup.py}"; do
431                         _distutils_prepare_current_working_directory "${setup_file}"
432
433                         echo ${_BOLD}"$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" install --root="${D}" --no-compile "$@"${_NORMAL}
434                         "$(PYTHON)" "${setup_file#*|}" "${_DISTUTILS_GLOBAL_OPTIONS[@]}" install --root="${D}" --no-compile "$@" || die "Installation failed"
435
436                         _distutils_restore_current_working_directory "${setup_file}"
437                 done
438         fi
439
440         while read -d $'\0' -r nspkg_pth_file; do
441                 nspkg_pth_files+=("${nspkg_pth_file}")
442         done < <(find "${ED}" -name "*-nspkg.pth" -type f -print0)
443
444         if [[ "${#nspkg_pth_files[@]}" -gt 0 ]]; then
445                 einfo
446                 einfo "Python namespaces:"
447                 for nspkg_pth_file in "${nspkg_pth_files[@]}"; do
448                         einfo "    '${nspkg_pth_file#${ED%/}}':"
449                         while read -r line; do
450                                 einfo "        $(echo "${line}" | sed -e "s/.*types\.ModuleType('\([^']\+\)').*/\1/")"
451                         done < "${nspkg_pth_file}"
452                         #if ! has "${EAPI:-0}" 0 1 2 3; then
453                         #       rm -f "${nspkg_pth_file}" || die "Deletion of '${nspkg_pth_file}' failed"
454                         #fi
455                 done
456                 einfo
457         fi
458
459         if [[ -e "${ED}usr/local" ]]; then
460                 die "Illegal installation into /usr/local"
461         fi
462
463         default_docs="AUTHORS Change* CHANGELOG CONTRIBUTORS KNOWN_BUGS MAINTAINERS NEWS README* TODO"
464
465         for doc in ${default_docs}; do
466                 [[ -s "${doc}" ]] && dodoc "${doc}"
467         done
468
469         if has "${EAPI:-0}" 0 1 2 3; then
470                 if [[ -n "${DOCS}" ]]; then
471                         dodoc ${DOCS} || die "dodoc failed"
472                 fi
473         else
474                 if [[ -n "${DOCS}" ]]; then
475                         dodoc -r ${DOCS} || die "dodoc failed"
476                 fi
477         fi
478
479         DISTUTILS_SRC_INSTALL_EXECUTED="1"
480 }
481
482 # @FUNCTION: distutils_pkg_postinst
483 # @DESCRIPTION:
484 # The distutils pkg_postinst function. This function is exported.
485 # When PYTHON_MODNAME variable is set, then this function calls python_mod_optimize() with modules
486 # specified in PYTHON_MODNAME variable. Otherwise it calls python_mod_optimize() with module, whose
487 # name is equal to name of current package, if this module exists.
488 distutils_pkg_postinst() {
489         if [[ "${EBUILD_PHASE}" != "postinst" ]]; then
490                 die "${FUNCNAME}() can be used only in pkg_postinst() phase"
491         fi
492
493         _python_check_python_pkg_setup_execution
494         _python_initialize_prefix_variables
495
496         if [[ -z "${DISTUTILS_SRC_INSTALL_EXECUTED}" ]]; then
497                 die "${FUNCNAME}() called illegally"
498         fi
499
500         local pylibdir pymod
501
502         if [[ "$#" -ne 0 ]]; then
503                 die "${FUNCNAME}() does not accept arguments"
504         fi
505
506         if [[ -z "$(declare -p PYTHON_MODNAME 2> /dev/null)" ]]; then
507                 for pylibdir in "${EROOT}"usr/$(get_libdir)/python* "${EROOT}"usr/share/jython-*/Lib; do
508                         if [[ -d "${pylibdir}/site-packages/${PN}" ]]; then
509                                 PYTHON_MODNAME="${PN}"
510                         fi
511                 done
512         fi
513
514         if [[ -n "${PYTHON_MODNAME}" ]]; then
515                 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
516                         python_mod_optimize ${PYTHON_MODNAME}
517                 else
518                         for pymod in ${PYTHON_MODNAME}; do
519                                 python_mod_optimize "$(python_get_sitedir)/${pymod}"
520                         done
521                 fi
522         fi
523 }
524
525 # @FUNCTION: distutils_pkg_postrm
526 # @DESCRIPTION:
527 # The distutils pkg_postrm function. This function is exported.
528 # When PYTHON_MODNAME variable is set, then this function calls python_mod_cleanup() with modules
529 # specified in PYTHON_MODNAME variable. Otherwise it calls python_mod_cleanup() with module, whose
530 # name is equal to name of current package, if this module exists.
531 distutils_pkg_postrm() {
532         if [[ "${EBUILD_PHASE}" != "postrm" ]]; then
533                 die "${FUNCNAME}() can be used only in pkg_postrm() phase"
534         fi
535
536         _python_check_python_pkg_setup_execution
537         _python_initialize_prefix_variables
538
539         if [[ -z "${DISTUTILS_SRC_INSTALL_EXECUTED}" ]]; then
540                 die "${FUNCNAME}() called illegally"
541         fi
542
543         local pylibdir pymod
544
545         if [[ "$#" -ne 0 ]]; then
546                 die "${FUNCNAME}() does not accept arguments"
547         fi
548
549         if [[ -z "$(declare -p PYTHON_MODNAME 2> /dev/null)" ]]; then
550                 for pylibdir in "${EROOT}"usr/$(get_libdir)/python* "${EROOT}"usr/share/jython-*/Lib; do
551                         if [[ -d "${pylibdir}/site-packages/${PN}" ]]; then
552                                 PYTHON_MODNAME="${PN}"
553                         fi
554                 done
555         fi
556
557         if [[ -n "${PYTHON_MODNAME}" ]]; then
558                 if ! has "${EAPI:-0}" 0 1 2 || _python_package_supporting_installation_for_multiple_python_abis; then
559                         python_mod_cleanup ${PYTHON_MODNAME}
560                 else
561                         for pymod in ${PYTHON_MODNAME}; do
562                                 for pylibdir in "${EROOT}"usr/$(get_libdir)/python*; do
563                                         if [[ -d "${pylibdir}/site-packages/${pymod}" ]]; then
564                                                 python_mod_cleanup "${pylibdir#${EROOT%/}}/site-packages/${pymod}"
565                                         fi
566                                 done
567                         done
568                 fi
569         fi
570 }
571
572 # @FUNCTION: distutils_get_intermediate_installation_image
573 # @DESCRIPTION:
574 # Print path to intermediate installation image.
575 #
576 # This function can be used only in distutils_src_install_pre_hook() and distutils_src_install_post_hook().
577 distutils_get_intermediate_installation_image() {
578         if [[ "${EBUILD_PHASE}" != "install" ]]; then
579                 die "${FUNCNAME}() can be used only in src_install() phase"
580         fi
581
582         if ! _python_package_supporting_installation_for_multiple_python_abis; then
583                 die "${FUNCNAME}() cannot be used in ebuilds of packages not supporting installation for multiple Python ABIs"
584         fi
585
586         _python_check_python_pkg_setup_execution
587
588         if [[ ! "${FUNCNAME[1]}" =~ ^distutils_src_install_(pre|post)_hook$ ]]; then
589                 die "${FUNCNAME}() can be used only in distutils_src_install_pre_hook() and distutils_src_install_post_hook()"
590         fi
591
592         if [[ "$#" -ne 0 ]]; then
593                 die "${FUNCNAME}() does not accept arguments"
594         fi
595
596         echo "${T}/images/${PYTHON_ABI}"
597 }