| 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.77 2009/10/11 13:34:23 arfrever Exp $ |
3 | # $Header: /var/cvsroot/gentoo-x86/eclass/python.eclass,v 1.78 2009/11/15 14:25:55 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 | # @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. |
| … | |
… | |
| 104 | if [[ "${absolute_path}" == "1" ]]; then |
104 | if [[ "${absolute_path}" == "1" ]]; then |
| 105 | echo -n "/usr/bin/python${slot}" |
105 | echo -n "/usr/bin/python${slot}" |
| 106 | else |
106 | else |
| 107 | echo -n "python${slot}" |
107 | echo -n "python${slot}" |
| 108 | fi |
108 | fi |
|
|
109 | |
|
|
110 | if [[ "${ABI}" != "${DEFAULT_ABI}" ]]; then |
|
|
111 | echo -n "-${ABI}" |
|
|
112 | fi |
| 109 | } |
113 | } |
| 110 | |
114 | |
| 111 | unset PYTHON_ABIS |
115 | unset PYTHON_ABIS |
| 112 | unset PYTHON_ABIS_SANITY_CHECKS |
116 | unset PYTHON_ABIS_SANITY_CHECKS |
| 113 | |
117 | |
| … | |
… | |
| 128 | die "'/usr/bin/python-config' isn't valid script" |
132 | die "'/usr/bin/python-config' isn't valid script" |
| 129 | fi |
133 | fi |
| 130 | |
134 | |
| 131 | # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. |
135 | # USE_${ABI_TYPE^^} and RESTRICT_${ABI_TYPE^^}_ABIS variables hopefully will be included in EAPI >= 4. |
| 132 | if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then |
136 | if [[ "$(declare -p PYTHON_ABIS 2> /dev/null)" != "declare -x PYTHON_ABIS="* ]] && has "${EAPI:-0}" 0 1 2 3; then |
| 133 | local ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS= |
137 | local PYTHON_ABI python2_supported_versions python3_supported_versions restricted_ABI support_ABI supported_PYTHON_ABIS= |
| 134 | PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" |
138 | PYTHON_ABI_SUPPORTED_VALUES="2.4 2.5 2.6 2.7 3.0 3.1 3.2" |
| 135 | python2_supported_versions="2.4 2.5 2.6 2.7" |
139 | python2_supported_versions="2.4 2.5 2.6 2.7" |
| 136 | python3_supported_versions="3.0 3.1 3.2" |
140 | python3_supported_versions="3.0 3.1 3.2" |
| 137 | |
141 | |
| 138 | if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then |
142 | if [[ "$(declare -p USE_PYTHON 2> /dev/null)" == "declare -x USE_PYTHON="* ]]; then |
| … | |
… | |
| 140 | |
144 | |
| 141 | if [[ -z "${USE_PYTHON}" ]]; then |
145 | if [[ -z "${USE_PYTHON}" ]]; then |
| 142 | die "USE_PYTHON variable is empty" |
146 | die "USE_PYTHON variable is empty" |
| 143 | fi |
147 | fi |
| 144 | |
148 | |
| 145 | for ABI in ${USE_PYTHON}; do |
149 | for PYTHON_ABI in ${USE_PYTHON}; do |
| 146 | if ! has "${ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then |
150 | if ! has "${PYTHON_ABI}" ${PYTHON_ABI_SUPPORTED_VALUES}; then |
| 147 | die "USE_PYTHON variable contains invalid value '${ABI}'" |
151 | die "USE_PYTHON variable contains invalid value '${PYTHON_ABI}'" |
| 148 | fi |
152 | fi |
| 149 | |
153 | |
| 150 | if has "${ABI}" ${python2_supported_versions}; then |
154 | if has "${PYTHON_ABI}" ${python2_supported_versions}; then |
| 151 | python2_enabled="1" |
155 | python2_enabled="1" |
| 152 | fi |
156 | fi |
| 153 | if has "${ABI}" ${python3_supported_versions}; then |
157 | if has "${PYTHON_ABI}" ${python3_supported_versions}; then |
| 154 | python3_enabled="1" |
158 | python3_enabled="1" |
| 155 | fi |
159 | fi |
| 156 | |
160 | |
| 157 | support_ABI="1" |
161 | support_ABI="1" |
| 158 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
162 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
| 159 | if [[ "${ABI}" == ${restricted_ABI} ]]; then |
163 | if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then |
| 160 | support_ABI="0" |
164 | support_ABI="0" |
| 161 | break |
165 | break |
| 162 | fi |
166 | fi |
| 163 | done |
167 | done |
| 164 | [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${ABI}" |
168 | [[ "${support_ABI}" == "1" ]] && supported_PYTHON_ABIS+=" ${PYTHON_ABI}" |
| 165 | done |
169 | done |
| 166 | export PYTHON_ABIS="${supported_PYTHON_ABIS# }" |
170 | export PYTHON_ABIS="${supported_PYTHON_ABIS# }" |
| 167 | |
171 | |
| 168 | if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then |
172 | if [[ -z "${PYTHON_ABIS//[${IFS}]/}" ]]; then |
| 169 | die "USE_PYTHON variable doesn't enable any version of Python supported by ${CATEGORY}/${PF}" |
173 | die "USE_PYTHON variable doesn't enable any version of Python supported by ${CATEGORY}/${PF}" |
| … | |
… | |
| 185 | die "'/usr/bin/python2' isn't valid symlink" |
189 | die "'/usr/bin/python2' isn't valid symlink" |
| 186 | fi |
190 | fi |
| 187 | |
191 | |
| 188 | python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
192 | python2_version="$(/usr/bin/python2 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
| 189 | |
193 | |
| 190 | for ABI in ${python2_supported_versions}; do |
194 | for PYTHON_ABI in ${python2_supported_versions}; do |
| 191 | support_python_major_version="1" |
195 | support_python_major_version="1" |
| 192 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
196 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
| 193 | if [[ "${ABI}" == ${restricted_ABI} ]]; then |
197 | if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then |
| 194 | support_python_major_version="0" |
198 | support_python_major_version="0" |
| 195 | fi |
199 | fi |
| 196 | done |
200 | done |
| 197 | [[ "${support_python_major_version}" == "1" ]] && break |
201 | [[ "${support_python_major_version}" == "1" ]] && break |
| 198 | done |
202 | done |
| … | |
… | |
| 212 | die "'/usr/bin/python3' isn't valid symlink" |
216 | die "'/usr/bin/python3' isn't valid symlink" |
| 213 | fi |
217 | fi |
| 214 | |
218 | |
| 215 | python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
219 | python3_version="$(/usr/bin/python3 -c 'from sys import version_info; print(".".join([str(x) for x in version_info[:2]]))')" |
| 216 | |
220 | |
| 217 | for ABI in ${python3_supported_versions}; do |
221 | for PYTHON_ABI in ${python3_supported_versions}; do |
| 218 | support_python_major_version="1" |
222 | support_python_major_version="1" |
| 219 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
223 | for restricted_ABI in ${RESTRICT_PYTHON_ABIS}; do |
| 220 | if [[ "${ABI}" == ${restricted_ABI} ]]; then |
224 | if [[ "${PYTHON_ABI}" == ${restricted_ABI} ]]; then |
| 221 | support_python_major_version="0" |
225 | support_python_major_version="0" |
| 222 | fi |
226 | fi |
| 223 | done |
227 | done |
| 224 | [[ "${support_python_major_version}" == "1" ]] && break |
228 | [[ "${support_python_major_version}" == "1" ]] && break |
| 225 | done |
229 | done |
| … | |
… | |
| 483 | if [[ "${quiet}" == "0" ]]; then |
487 | if [[ "${quiet}" == "0" ]]; then |
| 484 | ewarn "${RED}${failure_message}${NORMAL}" |
488 | ewarn "${RED}${failure_message}${NORMAL}" |
| 485 | fi |
489 | fi |
| 486 | elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then |
490 | elif has "${PYTHON_ABI}" ${FAILURE_TOLERANT_PYTHON_ABIS}; then |
| 487 | if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then |
491 | if [[ "${EBUILD_PHASE}" != "test" ]] || ! has test-fail-continue ${FEATURES}; then |
| 488 | local ABI enabled_PYTHON_ABIS= |
492 | local enabled_PYTHON_ABIS= other_PYTHON_ABI |
| 489 | for ABI in ${PYTHON_ABIS}; do |
493 | for other_PYTHON_ABI in ${PYTHON_ABIS}; do |
| 490 | [[ "${ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${ABI}" |
494 | [[ "${other_PYTHON_ABI}" != "${PYTHON_ABI}" ]] && enabled_PYTHON_ABIS+=" ${other_PYTHON_ABI}" |
| 491 | done |
495 | done |
| 492 | export PYTHON_ABIS="${enabled_PYTHON_ABIS# }" |
496 | export PYTHON_ABIS="${enabled_PYTHON_ABIS# }" |
| 493 | fi |
497 | fi |
| 494 | if [[ "${quiet}" == "0" ]]; then |
498 | if [[ "${quiet}" == "0" ]]; then |
| 495 | ewarn "${RED}${failure_message}${NORMAL}" |
499 | ewarn "${RED}${failure_message}${NORMAL}" |
| … | |
… | |
| 593 | die "${FUNCNAME}(): '${argument}' isn't a regular file or a directory" |
597 | die "${FUNCNAME}(): '${argument}' isn't a regular file or a directory" |
| 594 | fi |
598 | fi |
| 595 | done |
599 | done |
| 596 | |
600 | |
| 597 | for file in "${files[@]}"; do |
601 | for file in "${files[@]}"; do |
|
|
602 | file="${file#./}" |
| 598 | [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue |
603 | [[ "${only_executables}" == "1" && ! -x "${file}" ]] && continue |
| 599 | |
604 | |
| 600 | if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then |
605 | if [[ "$(head -n1 "${file}")" =~ ^'#!'.*python ]]; then |
| 601 | [[ "${quiet}" == "0" ]] && einfo "Converting shebang in '${file}'" |
606 | [[ "${quiet}" == "0" ]] && einfo "Converting shebang in '${file}'" |
| 602 | sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed" |
607 | sed -e "1s/python\([[:digit:]]\+\(\.[[:digit:]]\+\)\?\)\?/python${python_version}/" -i "${file}" || die "Conversion of shebang in '${file}' failed" |
| … | |
… | |
| 719 | # Tell Python to automatically recompile modules to .pyc/.pyo if the |
724 | # Tell Python to automatically recompile modules to .pyc/.pyo if the |
| 720 | # timestamps/version stamps have changed. |
725 | # timestamps/version stamps have changed. |
| 721 | python_enable_pyc() { |
726 | python_enable_pyc() { |
| 722 | unset PYTHONDONTWRITEBYTECODE |
727 | unset PYTHONDONTWRITEBYTECODE |
| 723 | } |
728 | } |
| 724 | |
|
|
| 725 | python_disable_pyc |
|
|
| 726 | |
729 | |
| 727 | # @FUNCTION: python_need_rebuild |
730 | # @FUNCTION: python_need_rebuild |
| 728 | # @DESCRIPTION: Run without arguments, specifies that the package should be |
731 | # @DESCRIPTION: Run without arguments, specifies that the package should be |
| 729 | # rebuilt after a python upgrade. |
732 | # rebuilt after a python upgrade. |
| 730 | python_need_rebuild() { |
733 | python_need_rebuild() { |
| … | |
… | |
| 936 | # Don't use PYTHON_ABI in next calls to python_get_libdir(). |
939 | # Don't use PYTHON_ABI in next calls to python_get_libdir(). |
| 937 | unset PYTHON_ABI |
940 | unset PYTHON_ABI |
| 938 | |
941 | |
| 939 | if ((${#other_dirs[@]})) || ((${#other_files[@]})); then |
942 | if ((${#other_dirs[@]})) || ((${#other_files[@]})); then |
| 940 | return_code="0" |
943 | return_code="0" |
| 941 | ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}..." |
944 | ebegin "Compilation and optimization of Python modules placed outside of site-packages directories for Python ${PYVER}" |
| 942 | if ((${#other_dirs[@]})); then |
945 | if ((${#other_dirs[@]})); then |
| 943 | python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" |
946 | python${PYVER} "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" || return_code="1" |
| 944 | python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" |
947 | python${PYVER} -O "${root}$(python_get_libdir)/compileall.py" "${options[@]}" "${other_dirs[@]}" &> /dev/null || return_code="1" |
| 945 | fi |
948 | fi |
| 946 | if ((${#other_files[@]})); then |
949 | if ((${#other_files[@]})); then |
| … | |
… | |
| 992 | fi |
995 | fi |
| 993 | |
996 | |
| 994 | # set additional opts |
997 | # set additional opts |
| 995 | myopts+=(-q) |
998 | myopts+=(-q) |
| 996 | |
999 | |
| 997 | ebegin "Byte compiling python modules for python-${PYVER} .." |
1000 | ebegin "Compilation and optimization of Python modules for Python ${PYVER}" |
| 998 | if ((${#mydirs[@]})); then |
1001 | if ((${#mydirs[@]})); then |
| 999 | python${PYVER} \ |
1002 | python${PYVER} \ |
| 1000 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
1003 | "${myroot}"/usr/$(get_libdir)/python${PYVER}/compileall.py \ |
| 1001 | "${myopts[@]}" "${mydirs[@]}" || return_code="1" |
1004 | "${myopts[@]}" "${mydirs[@]}" || return_code="1" |
| 1002 | python${PYVER} -O \ |
1005 | python${PYVER} -O \ |
| … | |
… | |
| 1022 | # determine if they are orphaned (i.e. their corresponding .py files are missing.) |
1025 | # determine if they are orphaned (i.e. their corresponding .py files are missing.) |
| 1023 | # If they are, then it will remove their corresponding .pyc and .pyo files. |
1026 | # If they are, then it will remove their corresponding .pyc and .pyo files. |
| 1024 | # |
1027 | # |
| 1025 | # This function should only be run in pkg_postrm(). |
1028 | # This function should only be run in pkg_postrm(). |
| 1026 | python_mod_cleanup() { |
1029 | python_mod_cleanup() { |
| 1027 | local PYTHON_ABI SEARCH_PATH=() root src_py |
1030 | local path py_file PYTHON_ABI SEARCH_PATH=() root |
| 1028 | |
1031 | |
| 1029 | # Check if phase is pkg_postrm(). |
1032 | # Check if phase is pkg_postrm(). |
| 1030 | [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" |
1033 | [[ ${EBUILD_PHASE} != "postrm" ]] && die "${FUNCNAME} should only be run in pkg_postrm()" |
| 1031 | |
1034 | |
| 1032 | # Strip trailing slash from ROOT. |
1035 | # Strip trailing slash from ROOT. |
| … | |
… | |
| 1049 | else |
1052 | else |
| 1050 | SEARCH_PATH=("${@#/}") |
1053 | SEARCH_PATH=("${@#/}") |
| 1051 | SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") |
1054 | SEARCH_PATH=("${SEARCH_PATH[@]/#/${root}/}") |
| 1052 | fi |
1055 | fi |
| 1053 | else |
1056 | else |
| 1054 | SEARCH_PATH=("${root}"/usr/lib*/python*/site-packages) |
1057 | local dir sitedir |
|
|
1058 | for dir in "${root}"/usr/lib*; do |
|
|
1059 | if [[ -d "${dir}" && ! -L "${dir}" ]]; then |
|
|
1060 | for sitedir in "${dir}"/python*/site-packages; do |
|
|
1061 | if [[ -d "${sitedir}" ]]; then |
|
|
1062 | SEARCH_PATH+=("${sitedir}") |
|
|
1063 | fi |
|
|
1064 | done |
|
|
1065 | fi |
|
|
1066 | done |
|
|
1067 | fi |
|
|
1068 | |
|
|
1069 | local BLUE CYAN NORMAL |
|
|
1070 | if [[ "${NOCOLOR:-false}" =~ ^(false|no)$ ]]; then |
|
|
1071 | BLUE=$'\e[34m' |
|
|
1072 | CYAN=$'\e[36m' |
|
|
1073 | NORMAL=$'\e[0m' |
|
|
1074 | else |
|
|
1075 | BLUE= |
|
|
1076 | CYAN= |
|
|
1077 | NORMAL= |
| 1055 | fi |
1078 | fi |
| 1056 | |
1079 | |
| 1057 | for path in "${SEARCH_PATH[@]}"; do |
1080 | for path in "${SEARCH_PATH[@]}"; do |
| 1058 | [[ ! -d "${path}" ]] && continue |
1081 | if [[ -d "${path}" ]]; then |
| 1059 | einfo "Cleaning orphaned Python bytecode from ${path} .." |
|
|
| 1060 | find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do |
1082 | find "${path}" -name '*.py[co]' -print0 | while read -rd ''; do |
| 1061 | src_py="${REPLY%[co]}" |
1083 | py_file="${REPLY%[co]}" |
| 1062 | [[ -f "${src_py}" || (! -f "${src_py}c" && ! -f "${src_py}o") ]] && continue |
1084 | [[ -f "${py_file}" || (! -f "${py_file}c" && ! -f "${py_file}o") ]] && continue |
| 1063 | einfo "Purging ${src_py}[co]" |
1085 | einfo "${BLUE}<<< ${py_file}[co]${NORMAL}" |
| 1064 | rm -f "${src_py}"[co] |
1086 | rm -f "${py_file}"[co] |
| 1065 | done |
1087 | done |
| 1066 | |
1088 | |
| 1067 | # Attempt to remove directories that may be empty. |
1089 | # Attempt to delete directories, which may be empty. |
| 1068 | find "${path}" -type d | sort -r | while read -r dir; do |
1090 | find "${path}" -type d | sort -r | while read -r dir; do |
| 1069 | rmdir "${dir}" 2>/dev/null && einfo "Removing empty directory ${dir}" |
1091 | rmdir "${dir}" 2>/dev/null && einfo "${CYAN}<<< ${dir}${NORMAL}" |
| 1070 | done |
1092 | done |
|
|
1093 | elif [[ "${path}" == *.py && ! -f "${path}" && (-f "${path}c" || -f "${path}o") ]]; then |
|
|
1094 | einfo "${BLUE}<<< ${path}[co]${NORMAL}" |
|
|
1095 | rm -f "${path}"[co] |
|
|
1096 | fi |
| 1071 | done |
1097 | done |
| 1072 | } |
1098 | } |