[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gentoo-dev
Subject:    Re: [gentoo-dev] [PATCH python-r1 4/8] Introduce functions to replicate Python scripts.
From:       Reinis Danne <rei4dan () gmail ! com>
Date:       2012-10-27 14:31:11
Message-ID: 20121027143111.GK27961 () RD-HC ! apollo ! lv
[Download RAW message or body]

On Sat, Oct 27, 2012 at 04:08:31PM +0200, Michał Górny wrote:
> On Sat, 27 Oct 2012 16:27:27 +0300
> Reinis Danne <rei4dan@gmail.com> wrote:
> 
> > On Sat, Oct 27, 2012 at 01:02:47PM +0200, Michał Górny wrote:
> > > This can be used to create copies of Python scripts for various
> > > implementation when build system doesn't do that.
> > > ---
> > >  gx86/eclass/python-r1.eclass | 126 +++++++++++++++++++++++++++++++++++++++++++
> > >  1 file changed, 126 insertions(+)
> > > 
> > > diff --git a/gx86/eclass/python-r1.eclass b/gx86/eclass/python-r1.eclass
> > > index d7cdfa8..6178969 100644
> > > --- a/gx86/eclass/python-r1.eclass
> > > +++ b/gx86/eclass/python-r1.eclass
> > > @@ -412,3 +412,129 @@ python_export_best() {
> > >  	debug-print "${FUNCNAME}: Best implementation is: ${impl}"
> > >  	python_export "${impl}" "${@}"
> > >  }
> > > +
> > > +# @FUNCTION: _python_rewrite_shebang
> > > +# @INTERNAL
> > > +# @USAGE: [<EPYTHON>] <path>...
> > > +# @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-c*)
> > > +			impl=${1}
> > > +			shift
> > > +			;;
> > > +		*)
> > > +			impl=${EPYTHON}
> > > +			[[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
> > > +			;;
> > > +	esac
> > > +	debug-print "${FUNCNAME}: implementation: ${impl}"
> > > +
> > > +	local f
> > > +	for f; do
> > > +		local shebang=$(head -n 1 "${f}")
> > > +		debug-print "${FUNCNAME}: path = ${f}"
> > > +		debug-print "${FUNCNAME}: shebang = ${shebang}"
> > > +
> > > +		if [[ ${shebang} != *python* ]]; then
> > > +			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
> > > +
> > > +		sed -i -e "s:python:${impl}:" "${f}" || die
> > > +	done
> > > +}
> > 
> > Better would be also to check if the shebang already has
> > specific python version and in such a case probably an error
> > message would be appropriate, so ebuild should explicitly change
> > it to bare "python" if replication for versions is desired.
> 
> To be honest, I think I lack a good, simple way of guessing that. But I
> guess a hack like [[ "${shebang} " != *'python '* ]] could work.
> 
> On the other hand, I'm not sure if it shouldn't work on things like
> 'python2' as well. But then, if someone ever needs that working, he can
> ping me/report a bug.

I think if there is anything else than "*\bpython[23]?\ *" it
becomes really error prone. I'm not sure how useful would be to
handle [23] though.


Reinis

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic