mgorny      14/05/26 16:13:35

  Modified:             ChangeLog python-r1.eclass python-single-r1.eclass
                        python-utils-r1.eclass
  Log:
  Move python_fix_shebang into python-utils-r1, therefore making it a part of 
public API for all eclasses.

Revision  Changes    Path
1.1274               eclass/ChangeLog

file : 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/ChangeLog?rev=1.1274&view=markup
plain: 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/ChangeLog?rev=1.1274&content-type=text/plain
diff : 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/ChangeLog?r1=1.1273&r2=1.1274

Index: ChangeLog
===================================================================
RCS file: /var/cvsroot/gentoo-x86/eclass/ChangeLog,v
retrieving revision 1.1273
retrieving revision 1.1274
diff -u -r1.1273 -r1.1274
--- ChangeLog   24 May 2014 08:48:40 -0000      1.1273
+++ ChangeLog   26 May 2014 16:13:35 -0000      1.1274
@@ -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 <mgo...@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 <u...@gentoo.org> elisp-common.eclass:
   elisp-site-regen: Look for site-init files only in site-gentoo.d



1.73                 eclass/python-r1.eclass

file : 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-r1.eclass?rev=1.73&view=markup
plain: 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-r1.eclass?rev=1.73&content-type=text/plain
diff : 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-r1.eclass?r1=1.72&r2=1.73

Index: python-r1.eclass
===================================================================
RCS file: /var/cvsroot/gentoo-x86/eclass/python-r1.eclass,v
retrieving revision 1.72
retrieving revision 1.73
diff -u -r1.72 -r1.73
--- python-r1.eclass    19 Apr 2014 10:29:06 -0000      1.72
+++ python-r1.eclass    26 May 2014 16:13:35 -0000      1.73
@@ -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 @@
        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 @@
                                doexe "${files[@]}"
                        )
 
-                       _python_rewrite_shebang "${EPYTHON}" \
+                       python_fix_shebang \
                                "${files[@]/*\//${D%/}/${PYTHON_SCRIPTDIR}/}"
                else
                        local f
@@ -800,7 +802,7 @@
                                cp -p "${f}" "${f}-${EPYTHON}" || die
                        done
 
-                       _python_rewrite_shebang "${EPYTHON}" \
+                       python_fix_shebang \
                                "${files[@]/%/-${EPYTHON}}"
                fi
        }



1.26                 eclass/python-single-r1.eclass

file : 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-single-r1.eclass?rev=1.26&view=markup
plain: 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-single-r1.eclass?rev=1.26&content-type=text/plain
diff : 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-single-r1.eclass?r1=1.25&r2=1.26

Index: python-single-r1.eclass
===================================================================
RCS file: /var/cvsroot/gentoo-x86/eclass/python-single-r1.eclass,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- python-single-r1.eclass     30 Nov 2013 17:57:11 -0000      1.25
+++ python-single-r1.eclass     26 May 2014 16:13:35 -0000      1.26
@@ -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_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



1.56                 eclass/python-utils-r1.eclass

file : 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-utils-r1.eclass?rev=1.56&view=markup
plain: 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-utils-r1.eclass?rev=1.56&content-type=text/plain
diff : 
http://sources.gentoo.org/viewvc.cgi/gentoo-x86/eclass/python-utils-r1.eclass?r1=1.55&r2=1.56

Index: python-utils-r1.eclass
===================================================================
RCS file: /var/cvsroot/gentoo-x86/eclass/python-utils-r1.eclass,v
retrieving revision 1.55
retrieving revision 1.56
diff -u -r1.55 -r1.56
--- python-utils-r1.eclass      16 May 2014 07:54:40 -0000      1.55
+++ python-utils-r1.eclass      26 May 2014 16:13:35 -0000      1.56
@@ -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 @@
        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 @@
 
        # 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 @@
        [[ ${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:




Reply via email to