Gentoo Archives: gentoo-dev

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

Replies