Gentoo Archives: gentoo-dev

From: Reinis Danne <rei4dan@×××××.com>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] [PATCH python-r1 4/8] Introduce functions to replicate Python scripts.
Date: Sat, 27 Oct 2012 13:28:26
Message-Id: 20121027132727.GJ27961@RD-HC.apollo.lv
In Reply to: [gentoo-dev] [PATCH python-r1 4/8] Introduce functions to replicate Python scripts. by "Michał Górny"
1 On Sat, Oct 27, 2012 at 01:02:47PM +0200, Michał Górny wrote:
2 > This can be used to create copies of Python scripts for various
3 > implementation when build system doesn't do that.
4 > ---
5 > gx86/eclass/python-r1.eclass | 126 +++++++++++++++++++++++++++++++++++++++++++
6 > 1 file changed, 126 insertions(+)
7 >
8 > diff --git a/gx86/eclass/python-r1.eclass b/gx86/eclass/python-r1.eclass
9 > index d7cdfa8..6178969 100644
10 > --- a/gx86/eclass/python-r1.eclass
11 > +++ b/gx86/eclass/python-r1.eclass
12 > @@ -412,3 +412,129 @@ python_export_best() {
13 > debug-print "${FUNCNAME}: Best implementation is: ${impl}"
14 > python_export "${impl}" "${@}"
15 > }
16 > +
17 > +# @FUNCTION: _python_rewrite_shebang
18 > +# @INTERNAL
19 > +# @USAGE: [<EPYTHON>] <path>...
20 > +# @DESCRIPTION:
21 > +# Replaces 'python' executable in the shebang with the executable name
22 > +# of the specified interpreter. If no EPYTHON value (implementation) is
23 > +# used, the current ${EPYTHON} will be used.
24 > +#
25 > +# All specified files must start with a 'python' shebang. A file not
26 > +# having a matching shebang will be refused. The exact shebang style
27 > +# will be preserved in order not to break anything.
28 > +#
29 > +# Example conversions:
30 > +# @CODE
31 > +# From: #!/usr/bin/python -R
32 > +# To: #!/usr/bin/python2.7 -R
33 > +#
34 > +# From: #!/usr/bin/env FOO=bar python
35 > +# To: #!/usr/bin/env FOO=bar python2.7
36 > +# @CODE
37 > +_python_rewrite_shebang() {
38 > + debug-print-function ${FUNCNAME} "${@}"
39 > +
40 > + local impl
41 > + case "${1}" in
42 > + python*|jython*|pypy-c*)
43 > + impl=${1}
44 > + shift
45 > + ;;
46 > + *)
47 > + impl=${EPYTHON}
48 > + [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
49 > + ;;
50 > + esac
51 > + debug-print "${FUNCNAME}: implementation: ${impl}"
52 > +
53 > + local f
54 > + for f; do
55 > + local shebang=$(head -n 1 "${f}")
56 > + debug-print "${FUNCNAME}: path = ${f}"
57 > + debug-print "${FUNCNAME}: shebang = ${shebang}"
58 > +
59 > + if [[ ${shebang} != *python* ]]; then
60 > + eerror "A file does not seem to have a supported shebang:"
61 > + eerror " file: ${f}"
62 > + eerror " shebang: ${shebang}"
63 > + die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
64 > + fi
65 > +
66 > + sed -i -e "s:python:${impl}:" "${f}" || die
67 > + done
68 > +}
69
70 Better would be also to check if the shebang already has
71 specific python version and in such a case probably an error
72 message would be appropriate, so ebuild should explicitly change
73 it to bare "python" if replication for versions is desired.
74
75
76 Reinis
77
78 > +
79 > +# @FUNCTION: _python_ln_rel
80 > +# @USAGE: <from> <to>
81 > +# @DESCRIPTION:
82 > +# Create a relative symlink.
83 > +_python_ln_rel() {
84 > + debug-print-function ${FUNCNAME} "${@}"
85 > +
86 > + local from=${1}
87 > + local to=${2}
88 > +
89 > + local frpath=${from%/*}/
90 > + local topath=${to%/*}/
91 > + local rel_path=
92 > +
93 > + # remove double slashes
94 > + frpath=${frpath/\/\///}
95 > + topath=${topath/\/\///}
96 > +
97 > + while [[ ${topath} ]]; do
98 > + local frseg=${frpath%%/*}
99 > + local toseg=${topath%%/*}
100 > +
101 > + if [[ ${frseg} != ${toseg} ]]; then
102 > + rel_path=../${rel_path}${frseg:+${frseg}/}
103 > + fi
104 > +
105 > + frpath=${frpath#${frseg}/}
106 > + topath=${topath#${toseg}/}
107 > + done
108 > + rel_path+=${frpath}${1##*/}
109 > +
110 > + debug-print "${FUNCNAME}: ${from} -> ${to}"
111 > + debug-print "${FUNCNAME}: rel_path = ${rel_path}"
112 > +
113 > + ln -fs "${rel_path}" "${to}"
114 > +}
115 > +
116 > +# @FUNCTION: python_replicate_scripts
117 > +# @USAGE: <path>...
118 > +# @DESCRIPTION:
119 > +# Copy the given script to variants for all enabled Python
120 > +# implementations, then replace it with a symlink to the wrapper.
121 > +#
122 > +# All specified files must start with a 'python' shebang. A file not
123 > +# having a matching shebang will be refused.
124 > +python_replicate_scripts() {
125 > + debug-print-function ${FUNCNAME} "${@}"
126 > +
127 > + local suffixes=()
128 > +
129 > + _add_suffix() {
130 > + suffixes+=( "${EPYTHON}" )
131 > + }
132 > + python_foreach_impl _add_suffix
133 > + debug-print "${FUNCNAME}: suffixes = ( ${suffixes[@]} )"
134 > +
135 > + local f suffix
136 > + for suffix in "${suffixes[@]}"; do
137 > + for f; do
138 > + local newf=${f}-${suffix}
139 > +
140 > + debug-print "${FUNCNAME}: ${f} -> ${newf}"
141 > + cp "${f}" "${newf}" || die
142 > + done
143 > +
144 > + _python_rewrite_shebang "${suffix}" "${@/%/-${suffix}}"
145 > + done
146 > +
147 > + for f; do
148 > + _python_ln_rel "${ED}"/usr/bin/python-exec "${f}" || die
149 > + done
150 > +}
151 > --
152 > 1.7.12.4
153 >
154 >

Replies