summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichał Górny <mgorny@gentoo.org>2012-12-20 23:36:15 +0000
committerMichał Górny <mgorny@gentoo.org>2012-12-20 23:36:15 +0000
commit02895f4cfb0ce2457bf56357c98314ac43b71653 (patch)
tree411cdb4c89723249f67b58b179d18d9b99f9e6dd /eclass/python-single-r1.eclass
parentCommonize the code for obtaining the Python interpreter dependency string. (diff)
downloadgentoo-2-02895f4cfb0ce2457bf56357c98314ac43b71653.tar.gz
gentoo-2-02895f4cfb0ce2457bf56357c98314ac43b71653.tar.bz2
gentoo-2-02895f4cfb0ce2457bf56357c98314ac43b71653.zip
Introduce python_fix_shebang(), to fix shebangs in installed Python scripts recursively.
Diffstat (limited to 'eclass/python-single-r1.eclass')
-rw-r--r--eclass/python-single-r1.eclass46
1 files changed, 45 insertions, 1 deletions
diff --git a/eclass/python-single-r1.eclass b/eclass/python-single-r1.eclass
index fa4a309dfe13..9d9e395f55d7 100644
--- a/eclass/python-single-r1.eclass
+++ b/eclass/python-single-r1.eclass
@@ -1,6 +1,6 @@
# Copyright 1999-2012 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.9 2012/12/20 23:35:17 mgorny Exp $
+# $Header: /var/cvsroot/gentoo-x86/eclass/python-single-r1.eclass,v 1.10 2012/12/20 23:36:15 mgorny Exp $
# @ECLASS: python-single-r1
# @MAINTAINER:
@@ -180,5 +180,49 @@ python-single-r1_pkg_setup() {
done
}
+# @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