Gentoo Archives: gentoo-dev

From: "Michał Górny" <mgorny@g.o>
To: gentoo-dev@l.g.o
Cc: python@g.o, "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH] Check for USE_PYTHON <-> PYTHON_TARGETS sync.
Date: Fri, 16 Nov 2012 18:43:57
Message-Id: 1353091455-14190-1-git-send-email-mgorny@gentoo.org
1 This is much more complex than the previous version.
2
3 If user doesn't have USE_PYTHON set, it tries to solve the issue using
4 eselect-python only, suggesting user to switch the active
5 implementation.
6
7 If eselect-python won't help or user has USE_PYTHON set, it nicely
8 suggests a new value, highlighting added and listing removed entries.
9
10 The basic issue here is that the package can only check PYTHON_TARGETS
11 values in IUSE, so only the implementations supported by the package. It
12 tries hard to help and not to do any damage, so user may need to modify
13 his USE_PYTHON more than once.
14
15 For example, consider the following:
16
17 - installed: 2.6, 2.7, 3.1, 3.2, pypy1.8
18 - active: 2.6, 3.2
19 - USE_PYTHON not set
20 - PYTHON_TARGETS: 2.7, 3.1, 3.2, pypy1.8
21
22 Package 1 supports Python 2 only. The eclass can't access Python 3
23 PYTHON_TARGETS, thus it can only suggest user to switch active Python
24 interpreter to Python 2.7. This solves the Python2 packages only.
25
26 Package 2 supports both Python 2 & 3. The eclass notices two Python 3
27 versions and suggests user to add USE_PYTHON='2.7 3.1 3.2'.
28
29 Package 3 supports PyPy additionally. The eclass notices that, and
30 suggests user to add '2.7-pypy-1.8' to USE_PYTHON.
31
32 Of course, that assumes building one-by-one. If more packages get pulled
33 into the dep tree, the results of checks may look even contradictory.
34 Hopefully, after a few changes and rebuilds user will finally get
35 the correct value.
36 ---
37 gx86/eclass/python-r1.eclass | 198 +++++++++++++++++++++++++++++++++++++++++++
38 1 file changed, 198 insertions(+)
39
40 diff --git a/gx86/eclass/python-r1.eclass b/gx86/eclass/python-r1.eclass
41 index 0d6ef4c..61edf94 100644
42 --- a/gx86/eclass/python-r1.eclass
43 +++ b/gx86/eclass/python-r1.eclass
44 @@ -363,6 +363,202 @@ python_copy_sources() {
45 done
46 }
47
48 +# @FUNCTION: _python_check_USE_PYTHON
49 +# @INTERNAL
50 +# @DESCRIPTION:
51 +# Check whether USE_PYTHON and PYTHON_TARGETS are in sync. Output
52 +# warnings if they are not.
53 +_python_check_USE_PYTHON() {
54 + debug-print-function ${FUNCNAME} "${@}"
55 +
56 + if [[ ! ${_PYTHON_USE_PYTHON_CHECKED} ]]; then
57 + _PYTHON_USE_PYTHON_CHECKED=1
58 +
59 + # python-exec has profile-forced flags.
60 + if [[ ${CATEGORY}/${PN} == dev-python/python-exec ]]; then
61 + return
62 + fi
63 +
64 + _try_eselect() {
65 + # The eselect solution will work only with one py2 & py3.
66 +
67 + local impl py2 py3 dis_py2 dis_py3
68 + for impl in "${PYTHON_COMPAT[@]}"; do
69 + if use "python_targets_${impl}"; then
70 + case "${impl}" in
71 + python2_*)
72 + if [[ ${py2+1} ]]; then
73 + debug-print "${FUNCNAME}: -> more than one py2: ${py2} ${impl}"
74 + return 1
75 + fi
76 + py2=${impl/_/.}
77 + ;;
78 + python3_*)
79 + if [[ ${py3+1} ]]; then
80 + debug-print "${FUNCNAME}: -> more than one py3: ${py3} ${impl}"
81 + return 1
82 + fi
83 + py3=${impl/_/.}
84 + ;;
85 + *)
86 + return 1
87 + ;;
88 + esac
89 + else
90 + case "${impl}" in
91 + python2_*)
92 + dis_py2=1
93 + ;;
94 + python3_*)
95 + dis_py3=1
96 + ;;
97 + esac
98 + fi
99 + done
100 +
101 + # The eselect solution won't work if the disabled Python version
102 + # is installed.
103 + if [[ ! ${py2+1} && ${dis_py2} ]]; then
104 + debug-print "${FUNCNAME}: -> all py2 versions disabled"
105 + if has_version '=dev-lang/python-2*'; then
106 + debug-print "${FUNCNAME}: ---> but =python-2* installed!"
107 + return 1
108 + fi
109 + fi
110 + if [[ ! ${py3+1} && ${dis_py3} ]]; then
111 + debug-print "${FUNCNAME}: -> all py3 versions disabled"
112 + if has_version '=dev-lang/python-3*'; then
113 + debug-print "${FUNCNAME}: ---> but =python-3* installed!"
114 + return 1
115 + fi
116 + fi
117 +
118 + local warned
119 +
120 + # Now check whether the correct implementations are active.
121 + if [[ ${py2+1} ]]; then
122 + local sel_py2=$(eselect python show --python2)
123 +
124 + debug-print "${FUNCNAME}: -> py2 built: ${py2}, active: ${sel_py2}"
125 + if [[ ${py2} != ${sel_py2} ]]; then
126 + ewarn "Building package for ${py2} only while ${sel_py2} is active."
127 + ewarn "Please consider switching the active Python 2 interpreter:"
128 + ewarn
129 + ewarn " eselect python set --python2 ${py2}"
130 + warned=1
131 + fi
132 + fi
133 +
134 + if [[ ${py3+1} ]]; then
135 + local sel_py3=$(eselect python show --python3)
136 +
137 + debug-print "${FUNCNAME}: -> py3 built: ${py3}, active: ${sel_py3}"
138 + if [[ ${py3} != ${sel_py3} ]]; then
139 + [[ ${warned} ]] && ewarn
140 + ewarn "Building package for ${py3} only while ${sel_py3} is active."
141 + ewarn "Please consider switching the active Python 3 interpreter:"
142 + ewarn
143 + ewarn " eselect python set --python3 ${py3}"
144 + warned=1
145 + fi
146 + fi
147 +
148 + if [[ ${warned} ]]; then
149 + ewarn
150 + ewarn "Please note that after switching the active Python interpreter,"
151 + ewarn "you may need to run 'python-updater' to ensure the system integrity."
152 + ewarn
153 + ewarn "For more information on python.eclass compatibility, please see"
154 + ewarn "the appropriate python-r1 User's Guide chapter [1]."
155 + ewarn
156 + ewarn "[1] http://www.gentoo.org/proj/en/Python/python-r1/user-guide.xml#doc_chap2"
157 + fi
158 + }
159 +
160 + # If user has no USE_PYTHON, try to avoid it.
161 + if [[ ! ${USE_PYTHON} ]]; then
162 + debug-print "${FUNCNAME}: trying eselect solution ..."
163 + _try_eselect && return
164 + fi
165 +
166 + debug-print "${FUNCNAME}: trying USE_PYTHON solution ..."
167 + debug-print "${FUNCNAME}: -> USE_PYTHON=${USE_PYTHON}"
168 +
169 + local impl old=${USE_PYTHON} new=() removed=()
170 +
171 + for impl in "${PYTHON_COMPAT[@]}"; do
172 + local abi
173 + case "${impl}" in
174 + python*)
175 + abi=${impl#python}
176 + ;;
177 + jython*)
178 + abi=${impl#jython}-jython
179 + ;;
180 + pypy*)
181 + abi=2.7-pypy-${impl#pypy}
182 + ;;
183 + *)
184 + die "Unexpected Python implementation: ${impl}"
185 + ;;
186 + esac
187 + abi=${abi/_/.}
188 +
189 + has "${abi}" ${USE_PYTHON}
190 + local has_abi=${?}
191 + use "python_targets_${impl}"
192 + local has_impl=${?}
193 +
194 + # 0 = has, 1 = does not have
195 + if [[ ${has_abi} == 0 && ${has_impl} == 1 ]]; then
196 + debug-print "${FUNCNAME}: ---> remove ${abi}"
197 + # remove from USE_PYTHON
198 + old=${old/${abi}/}
199 + removed+=( ${abi} )
200 + elif [[ ${has_abi} == 1 && ${has_impl} == 0 ]]; then
201 + debug-print "${FUNCNAME}: ---> add ${abi}"
202 + # add to USE_PYTHON
203 + new+=( ${abi} )
204 + fi
205 + done
206 +
207 + if [[ ${removed[@]} || ${new[@]} ]]; then
208 + old=( ${old} )
209 +
210 + debug-print "${FUNCNAME}: -> old: ${old[@]}"
211 + debug-print "${FUNCNAME}: -> new: ${new[@]}"
212 + debug-print "${FUNCNAME}: -> removed: ${removed[@]}"
213 +
214 + if [[ ${USE_PYTHON} ]]; then
215 + ewarn "It seems that your USE_PYTHON setting does list different Python"
216 + ewarn "implementations than your PYTHON_TARGETS variable. Please consider"
217 + ewarn "using the following value instead:"
218 + ewarn
219 + ewarn " USE_PYTHON='\033[35m${old[@]}${new[@]+ \033[1m${new[@]}}\033[0m'"
220 +
221 + if [[ ${removed[@]} ]]; then
222 + ewarn
223 + ewarn "(removed \033[31m${removed[@]}\033[0m)"
224 + fi
225 + else
226 + ewarn "It seems that you need to set USE_PYTHON to make sure that legacy"
227 + ewarn "packages will be built with respect to PYTHON_TARGETS correctly:"
228 + ewarn
229 + ewarn " USE_PYTHON='\033[35;1m${new[@]}\033[0m'"
230 + fi
231 +
232 + ewarn
233 + ewarn "Please note that after changing the USE_PYTHON variable, you may need"
234 + ewarn "to run 'python-updater' to ensure the system integrity."
235 + ewarn
236 + ewarn "For more information on python.eclass compatibility, please see"
237 + ewarn "the appropriate python-r1 User's Guide chapter [1]."
238 + ewarn
239 + ewarn "[1] http://www.gentoo.org/proj/en/Python/python-r1/user-guide.xml#doc_chap2"
240 + fi
241 + fi
242 +}
243 +
244 # @FUNCTION: python_foreach_impl
245 # @USAGE: <command> [<args>...]
246 # @DESCRIPTION:
247 @@ -376,6 +572,8 @@ python_copy_sources() {
248 python_foreach_impl() {
249 debug-print-function ${FUNCNAME} "${@}"
250
251 + _python_check_USE_PYTHON
252 +
253 local impl
254 local bdir=${BUILD_DIR:-${S}}
255
256 --
257 1.8.0

Replies