summaryrefslogtreecommitdiff
path: root/eclass
diff options
context:
space:
mode:
authorMichał Górny <mgorny@gentoo.org>2014-05-26 16:13:35 +0000
committerMichał Górny <mgorny@gentoo.org>2014-05-26 16:13:35 +0000
commit9683e399faa2b512f9b399e9df1a9bfaf8a6a363 (patch)
treee1e01a08b4ef66c6baeee575a24a8a0b759529a2 /eclass
parentInitial import from the qt overlay, thanks to Harvey Mittens and Davide Pesav... (diff)
downloadhistorical-9683e399faa2b512f9b399e9df1a9bfaf8a6a363.tar.gz
historical-9683e399faa2b512f9b399e9df1a9bfaf8a6a363.tar.bz2
historical-9683e399faa2b512f9b399e9df1a9bfaf8a6a363.zip
Move python_fix_shebang into python-utils-r1, therefore making it a part of public API for all eclasses.
Diffstat (limited to 'eclass')
-rw-r--r--eclass/ChangeLog7
-rw-r--r--eclass/python-r1.eclass8
-rw-r--r--eclass/python-single-r1.eclass48
-rw-r--r--eclass/python-utils-r1.eclass172
4 files changed, 110 insertions, 125 deletions
diff --git a/eclass/ChangeLog b/eclass/ChangeLog
index e6dbd2257e9d..438543e44685 100644
--- a/eclass/ChangeLog
+++ b/eclass/ChangeLog
@@ -1,6 +1,11 @@
# ChangeLog for eclass directory
# Copyright 1999-2014 Gentoo Foundation; Distributed under the GPL v2
-# $Header: /var/cvsroot/gentoo-x86/eclass/ChangeLog,v 1.1273 2014/05/24 08:48:40 ulm Exp $
+# $Header: /var/cvsroot/gentoo-x86/eclass/ChangeLog,v 1.1274 2014/05/26 16:13:35 mgorny Exp $
+
+ 26 May 2014; Michał Górny <mgorny@gentoo.org> python-r1.eclass,
+ python-single-r1.eclass, python-utils-r1.eclass:
+ Move python_fix_shebang into python-utils-r1, therefore making it a part of
+ public API for all eclasses.
24 May 2014; Ulrich Müller <ulm@gentoo.org> elisp-common.eclass:
elisp-site-regen: Look for site-init files only in site-gentoo.d
diff --git a/eclass/python-r1.eclass b/eclass/python-r1.eclass
index 0fb188d0d6bb..8670ab6d4073 100644
--- a/eclass/python-r1.eclass
+++ b/eclass/python-r1.eclass
@@ -1,6 +1,6 @@
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
-# $Header: /var/cvsroot/gentoo-x86/eclass/python-r1.eclass,v 1.72 2014/04/19 10:29:06 mgorny Exp $
+# $Header: /var/cvsroot/gentoo-x86/eclass/python-r1.eclass,v 1.73 2014/05/26 16:13:35 mgorny Exp $
# @ECLASS: python-r1
# @MAINTAINER:
@@ -783,6 +783,8 @@ python_replicate_script() {
debug-print-function ${FUNCNAME} "${@}"
_python_replicate_script() {
+ local _PYTHON_FIX_SHEBANG_QUIET=1
+
if _python_want_python_exec2; then
local PYTHON_SCRIPTDIR
python_export PYTHON_SCRIPTDIR
@@ -792,7 +794,7 @@ python_replicate_script() {
doexe "${files[@]}"
)
- _python_rewrite_shebang "${EPYTHON}" \
+ python_fix_shebang \
"${files[@]/*\//${D%/}/${PYTHON_SCRIPTDIR}/}"
else
local f
@@ -800,7 +802,7 @@ python_replicate_script() {
cp -p "${f}" "${f}-${EPYTHON}" || die
done
- _python_rewrite_shebang "${EPYTHON}" \
+ python_fix_shebang \
"${files[@]/%/-${EPYTHON}}"
fi
}
diff --git a/eclass/python-single-r1.eclass b/eclass/python-single-r1.eclass
index 7ce57c6c6b30..c5a8702ee89e 100644
--- a/eclass/python-single-r1.eclass
+++ b/eclass/python-single-r1.eclass
@@ -1,6 +1,6 @@
-# Copyright 1999-2013 Gentoo Foundation
+# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
-# $Header: /var/cvsroot/gentoo-x86/eclass/python-single-r1.eclass,v 1.25 2013/11/30 17:57:11 floppym Exp $
+# $Header: /var/cvsroot/gentoo-x86/eclass/python-single-r1.eclass,v 1.26 2014/05/26 16:13:35 mgorny Exp $
# @ECLASS: python-single-r1
# @MAINTAINER:
@@ -261,49 +261,5 @@ python-single-r1_pkg_setup() {
python_setup
}
-# @FUNCTION: python_fix_shebang
-# @USAGE: <path>...
-# @DESCRIPTION:
-# Replace the shebang in Python scripts with the current Python
-# implementation (EPYTHON). If a directory is passed, works recursively
-# on all Python scripts.
-#
-# Only files having a 'python' shebang will be modified; other files
-# will be skipped. If a script has a complete shebang matching
-# the chosen interpreter version, it is left unmodified. If a script has
-# a complete shebang matching other version, the command dies.
-python_fix_shebang() {
- debug-print-function ${FUNCNAME} "${@}"
-
- [[ ${1} ]] || die "${FUNCNAME}: no paths given"
- [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
-
- local path f
- for path; do
- while IFS= read -r -d '' f; do
- local shebang=$(head -n 1 "${f}")
-
- case "${shebang}" in
- '#!'*${EPYTHON}*)
- debug-print "${FUNCNAME}: in file ${f#${D}}"
- debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
- ;;
- '#!'*python[23].[0123456789]*|'#!'*pypy-c*|'#!'*jython*)
- debug-print "${FUNCNAME}: in file ${f#${D}}"
- debug-print "${FUNCNAME}: incorrect specific shebang: ${shebang}"
-
- die "${f#${D}} has a specific Python shebang not matching EPYTHON"
- ;;
- '#!'*python*)
- debug-print "${FUNCNAME}: in file ${f#${D}}"
- debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
-
- einfo "Fixing shebang in ${f#${D}}"
- _python_rewrite_shebang "${f}"
- esac
- done < <(find "${path}" -type f -print0)
- done
-}
-
_PYTHON_SINGLE_R1=1
fi
diff --git a/eclass/python-utils-r1.eclass b/eclass/python-utils-r1.eclass
index 81d6691d86ed..6f7f8aea677b 100644
--- a/eclass/python-utils-r1.eclass
+++ b/eclass/python-utils-r1.eclass
@@ -1,6 +1,6 @@
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
-# $Header: /var/cvsroot/gentoo-x86/eclass/python-utils-r1.eclass,v 1.55 2014/05/16 07:54:40 mgorny Exp $
+# $Header: /var/cvsroot/gentoo-x86/eclass/python-utils-r1.eclass,v 1.56 2014/05/26 16:13:35 mgorny Exp $
# @ECLASS: python-utils-r1
# @MAINTAINER:
@@ -478,79 +478,6 @@ python_get_scriptdir() {
echo "${PYTHON_SCRIPTDIR}"
}
-# @FUNCTION: _python_rewrite_shebang
-# @USAGE: [<EPYTHON>] <path>...
-# @INTERNAL
-# @DESCRIPTION:
-# Replaces 'python' executable in the shebang with the executable name
-# of the specified interpreter. If no EPYTHON value (implementation) is
-# used, the current ${EPYTHON} will be used.
-#
-# All specified files must start with a 'python' shebang. A file not
-# having a matching shebang will be refused. The exact shebang style
-# will be preserved in order not to break anything.
-#
-# Example conversions:
-# @CODE
-# From: #!/usr/bin/python -R
-# To: #!/usr/bin/python2.7 -R
-#
-# From: #!/usr/bin/env FOO=bar python
-# To: #!/usr/bin/env FOO=bar python2.7
-# @CODE
-_python_rewrite_shebang() {
- debug-print-function ${FUNCNAME} "${@}"
-
- local impl
- case "${1}" in
- python*|jython*|pypy*)
- impl=${1}
- shift
- ;;
- *)
- impl=${EPYTHON}
- [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
- ;;
- esac
- debug-print "${FUNCNAME}: implementation: ${impl}"
-
- local f
- for f; do
- local from shebang
- read -r shebang < "${f}"
- shebang=${shebang%$'\r'}
- debug-print "${FUNCNAME}: path = ${f}"
- debug-print "${FUNCNAME}: shebang = ${shebang}"
-
- if [[ "${shebang} " == *"${impl} "* ]]; then
- # skip files with correct impl
- continue
- elif [[ "${shebang} " == *'python '* ]]; then
- from=python
- elif [[ "${shebang} " == *'python2 '* ]]; then
- from=python2
- elif [[ "${shebang} " == *'python3 '* ]]; then
- from=python3
- else
- eerror "A file does not seem to have a supported shebang:"
- eerror " file: ${f}"
- eerror " shebang: ${shebang}"
- die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
- fi
-
- if { [[ ${from} == python2 ]] && python_is_python3 "${impl}"; } \
- || { [[ ${from} == python3 ]] && ! python_is_python3 "${impl}"; } then
- eerror "A file does have shebang not supporting requested impl:"
- eerror " file: ${f}"
- eerror " shebang: ${shebang}"
- eerror " impl: ${impl}"
- die "${FUNCNAME}: ${f} does have shebang not supporting ${EPYTHON}"
- fi
-
- sed -i -e "1s:${from}:${impl}:" "${f}" || die
- done
-}
-
# @FUNCTION: _python_ln_rel
# @USAGE: <from> <to>
# @INTERNAL
@@ -743,7 +670,8 @@ python_newexe() {
# don't use this at home, just call python_doscript() instead
if [[ ${_PYTHON_REWRITE_SHEBANG} ]]; then
- _python_rewrite_shebang "${ED%/}/${d}/${newfn}"
+ local _PYTHON_FIX_SHEBANG_QUIET=1
+ python_fix_shebang "${ED%/}/${d}/${newfn}"
fi
}
@@ -1006,6 +934,100 @@ python_is_python3() {
[[ ${impl} == python3* ]]
}
+# @FUNCTION: python_fix_shebang
+# @USAGE: <path>...
+# @DESCRIPTION:
+# Replace the shebang in Python scripts with the current Python
+# implementation (EPYTHON). If a directory is passed, works recursively
+# on all Python scripts.
+#
+# Only files having a 'python*' shebang will be modified. Files with
+# other shebang will either be skipped when working recursively
+# on a directory or treated as error when specified explicitly.
+#
+# Shebangs matching explicitly current Python version will be left
+# unmodified. Shebangs requesting another Python version will be treated
+# as fatal error.
+python_fix_shebang() {
+ debug-print-function ${FUNCNAME} "${@}"
+
+ [[ ${1} ]] || die "${FUNCNAME}: no paths given"
+ [[ ${EPYTHON} ]] || die "${FUNCNAME}: EPYTHON unset (pkg_setup not called?)"
+
+ local path f
+ for path; do
+ local any_correct any_fixed is_recursive
+
+ [[ -d ${path} ]] && is_recursive=1
+
+ while IFS= read -r -d '' f; do
+ local shebang=$(head -n 1 "${f}")
+ local error
+
+ case "${shebang} " in
+ '#!'*"${EPYTHON} "*)
+ debug-print "${FUNCNAME}: in file ${f#${D}}"
+ debug-print "${FUNCNAME}: shebang matches EPYTHON: ${shebang}"
+
+ # Nothing to do, move along.
+ any_correct=1
+ ;;
+ '#!'*python" "*|'#!'*python[23]" "*)
+ debug-print "${FUNCNAME}: in file ${f#${D}}"
+ debug-print "${FUNCNAME}: rewriting shebang: ${shebang}"
+
+ # Note: for internal use.
+ if [[ ! ${_PYTHON_FIX_SHEBANG_QUIET} ]]; then
+ einfo "Fixing shebang in ${f#${D}}."
+ fi
+
+ local from
+ if [[ "${shebang} " == *'python2 '* ]]; then
+ from=python2
+ python_is_python3 "${EPYTHON}" && error=1
+ elif [[ "${shebang} " == *'python3 '* ]]; then
+ from=python3
+ python_is_python3 "${EPYTHON}" || error=1
+ else
+ from=python
+ fi
+
+ if [[ ! ${error} ]]; then
+ sed -i -e "1s:${from}:${EPYTHON}:" "${f}" || die
+ any_fixed=1
+ fi
+ ;;
+ '#!'*python[23].[0123456789]" "*|'#!'*pypy" "*|'#!'*jython[23].[0123456789]" "*)
+ # Explicit mismatch.
+ error=1
+ ;;
+ *)
+ # Non-Python shebang. Allowed in recursive mode,
+ # disallowed when specifying file explicitly.
+ [[ ${is_recursive} ]] || error=1
+ ;;
+ esac
+
+ if [[ ${error} ]]; then
+ eerror "The file has incompatible shebang:"
+ eerror " file: ${f#${D}}"
+ eerror " current shebang: ${shebang}"
+ eerror " requested impl: ${EPYTHON}"
+ die "${FUNCNAME}: conversion of incompatible shebang requested"
+ fi
+ done < <(find "${path}" -type f -print0)
+
+ if [[ ! ${any_fixed} ]]; then
+ eqawarn "QA warning: ${FUNCNAME}, ${path#${D}} did not match any fixable files."
+ if [[ ${any_correct} ]]; then
+ eqawarn "All files have ${EPYTHON} shebang already."
+ else
+ eqawarn "There are no Python files in specified directory."
+ fi
+ fi
+ done
+}
+
# @FUNCTION: _python_want_python_exec2
# @INTERNAL
# @DESCRIPTION: