/[gentoo-x86]/eclass/check-reqs.eclass
Gentoo

Contents of /eclass/check-reqs.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide annotations) (download)
Fri Mar 28 15:08:16 2014 UTC (4 months, 3 weeks ago) by ulm
Branch: MAIN
Changes since 1.13: +11 -11 lines
Output binary prefixes for units according to IEC 80000-13, as calculations are 1024 based. Fix documentation of check-reqs_get_unit function, and other minor fixes.

1 ulm 1.14 # Copyright 1999-2014 Gentoo Foundation
2 ciaranm 1.1 # Distributed under the terms of the GNU General Public License v2
3 ulm 1.14 # $Header: /var/cvsroot/gentoo-x86/eclass/check-reqs.eclass,v 1.13 2012/10/19 03:35:15 patrick Exp $
4 zlin 1.6
5     # @ECLASS: check-reqs.eclass
6     # @MAINTAINER:
7 scarabeus 1.9 # QA Team <qa@gentoo.org>
8     # @AUTHOR:
9 zlin 1.6 # Bo ├śrsted Andresen <zlin@gentoo.org>
10 ciaranm 1.1 # Original Author: Ciaran McCreesh <ciaranm@gentoo.org>
11 zlin 1.6 # @BLURB: Provides a uniform way of handling ebuild which have very high build requirements
12     # @DESCRIPTION:
13 ciaranm 1.1 # This eclass provides a uniform way of handling ebuilds which have very high
14 zlin 1.6 # build requirements in terms of memory or disk space. It provides a function
15 ciaranm 1.1 # which should usually be called during pkg_setup().
16     #
17     # The chosen action only happens when the system's resources are detected
18     # correctly and only if they are below the threshold specified by the package.
19     #
20 zlin 1.6 # @CODE
21 scarabeus 1.9 # # need this much memory (does *not* check swap)
22     # CHECKREQS_MEMORY="256M"
23 ciaranm 1.1 #
24 scarabeus 1.9 # # need this much temporary build space
25     # CHECKREQS_DISK_BUILD="2G"
26 ciaranm 1.1 #
27 scarabeus 1.9 # # install will need this much space in /usr
28     # CHECKREQS_DISK_USR="1G"
29 ciaranm 1.1 #
30 scarabeus 1.9 # # install will need this much space in /var
31     # CHECKREQS_DISK_VAR="1024M"
32 ciaranm 1.1 #
33 zlin 1.6 # @CODE
34 ciaranm 1.1 #
35 ciaranm 1.2 # If you don't specify a value for, say, CHECKREQS_MEMORY, then the test is not
36 ciaranm 1.1 # carried out.
37     #
38     # These checks should probably mostly work on non-Linux, and they should
39     # probably degrade gracefully if they don't. Probably.
40    
41     inherit eutils
42    
43 zlin 1.6 # @ECLASS-VARIABLE: CHECKREQS_MEMORY
44 scarabeus 1.9 # @DEFAULT_UNSET
45 zlin 1.6 # @DESCRIPTION:
46 scarabeus 1.9 # How much RAM is needed? Eg.: CHECKREQS_MEMORY=15M
47 zlin 1.6
48     # @ECLASS-VARIABLE: CHECKREQS_DISK_BUILD
49 scarabeus 1.9 # @DEFAULT_UNSET
50 zlin 1.6 # @DESCRIPTION:
51 scarabeus 1.9 # How much diskspace is needed to build the package? Eg.: CHECKREQS_DISK_BUILD=2T
52 zlin 1.6
53     # @ECLASS-VARIABLE: CHECKREQS_DISK_USR
54 scarabeus 1.9 # @DEFAULT_UNSET
55 zlin 1.6 # @DESCRIPTION:
56 scarabeus 1.9 # How much space in /usr is needed to install the package? Eg.: CHECKREQS_DISK_USR=15G
57 zlin 1.6
58     # @ECLASS-VARIABLE: CHECKREQS_DISK_VAR
59 scarabeus 1.9 # @DEFAULT_UNSET
60 zlin 1.6 # @DESCRIPTION:
61 scarabeus 1.9 # How much space is needed in /var? Eg.: CHECKREQS_DISK_VAR=3000M
62    
63     EXPORT_FUNCTIONS pkg_setup
64     case "${EAPI:-0}" in
65     0|1|2|3) ;;
66 axs 1.11 4|5) EXPORT_FUNCTIONS pkg_pretend ;;
67 scarabeus 1.9 *) die "EAPI=${EAPI} is not supported" ;;
68     esac
69 zlin 1.6
70     # @FUNCTION: check_reqs
71     # @DESCRIPTION:
72 scarabeus 1.9 # Obsolete function executing all the checks and priting out results
73 ciaranm 1.1 check_reqs() {
74 scarabeus 1.9 debug-print-function ${FUNCNAME} "$@"
75    
76     echo
77     ewarn "QA: Package calling old ${FUNCNAME} function."
78     ewarn "QA: Please file a bug against the package."
79     ewarn "QA: It should call check-reqs_pkg_pretend and check-reqs_pkg_setup"
80     ewarn "QA: and possibly use EAPI=4 or later."
81     echo
82    
83     check-reqs_pkg_setup "$@"
84     }
85    
86     # @FUNCTION: check-reqs_pkg_setup
87     # @DESCRIPTION:
88     # Exported function running the resources checks in pkg_setup phase.
89     # It should be run in both phases to ensure condition changes between
90     # pkg_pretend and pkg_setup won't affect the build.
91     check-reqs_pkg_setup() {
92     debug-print-function ${FUNCNAME} "$@"
93    
94 patrick 1.12 [[ ${MERGE_TYPE} == binary ]] && return
95    
96 scarabeus 1.9 check-reqs_prepare
97     check-reqs_run
98     check-reqs_output
99     }
100 ciaranm 1.1
101 scarabeus 1.9 # @FUNCTION: check-reqs_pkg_pretend
102     # @DESCRIPTION:
103     # Exported function running the resources checks in pkg_pretend phase.
104     check-reqs_pkg_pretend() {
105     debug-print-function ${FUNCNAME} "$@"
106    
107     check-reqs_pkg_setup "$@"
108     }
109    
110     # @FUNCTION: check-reqs_prepare
111     # @DESCRIPTION:
112     # Internal function that checks the variables that should be defined.
113     check-reqs_prepare() {
114     debug-print-function ${FUNCNAME} "$@"
115    
116     if [[ -z ${CHECKREQS_MEMORY} &&
117     -z ${CHECKREQS_DISK_BUILD} &&
118     -z ${CHECKREQS_DISK_USR} &&
119     -z ${CHECKREQS_DISK_VAR} ]]; then
120     eerror "Set some check-reqs eclass variables if you want to use it."
121     eerror "If you are user and see this message file a bug against the package."
122     die "${FUNCNAME}: check-reqs eclass called but not actualy used!"
123 ciaranm 1.1 fi
124 scarabeus 1.9 }
125 ciaranm 1.1
126 scarabeus 1.9 # @FUNCTION: check-reqs_run
127     # @DESCRIPTION:
128     # Internal function that runs the check based on variable settings.
129     check-reqs_run() {
130     debug-print-function ${FUNCNAME} "$@"
131    
132     # some people are *censored*
133     unset CHECKREQS_FAILED
134    
135     [[ -n ${CHECKREQS_MEMORY} ]] && \
136     check-reqs_memory \
137     ${CHECKREQS_MEMORY}
138    
139     [[ -n ${CHECKREQS_DISK_BUILD} ]] && \
140     check-reqs_disk \
141     "${T}" \
142     "${CHECKREQS_DISK_BUILD}"
143    
144     [[ -n ${CHECKREQS_DISK_USR} ]] && \
145     check-reqs_disk \
146     "${EROOT}/usr" \
147     "${CHECKREQS_DISK_USR}"
148    
149     [[ -n ${CHECKREQS_DISK_VAR} ]] && \
150     check-reqs_disk \
151     "${EROOT}/var" \
152     "${CHECKREQS_DISK_VAR}"
153     }
154    
155     # @FUNCTION: check-reqs_get_mebibytes
156     # @DESCRIPTION:
157     # Internal function that returns number in mebibytes.
158 ulm 1.14 # Returns 1024 for 1G or 1048576 for 1T.
159 scarabeus 1.9 check-reqs_get_mebibytes() {
160     debug-print-function ${FUNCNAME} "$@"
161    
162     [[ -z ${1} ]] && die "Usage: ${FUNCNAME} [size]"
163    
164     local unit=${1:(-1)}
165     local size=${1%[GMT]}
166    
167     case ${unit} in
168     G) echo $((1024 * size)) ;;
169     [M0-9]) echo ${size} ;;
170     T) echo $((1024 * 1024 * size)) ;;
171     *)
172     die "${FUNCNAME}: Unknown unit: ${unit}"
173     ;;
174     esac
175     }
176    
177     # @FUNCTION: check-reqs_get_number
178     # @DESCRIPTION:
179 ulm 1.14 # Internal function that returns the numerical value without the unit.
180     # Returns "1" for "1G" or "150" for "150T".
181 scarabeus 1.9 check-reqs_get_number() {
182     debug-print-function ${FUNCNAME} "$@"
183    
184     [[ -z ${1} ]] && die "Usage: ${FUNCNAME} [size]"
185    
186     local unit=${1:(-1)}
187     local size=${1%[GMT]}
188    
189     # Check for unset units and warn about them.
190     # Backcompat.
191     if [[ ${size} == ${1} ]]; then
192     ewarn "QA: Package does not specify unit for the size check"
193 ulm 1.14 ewarn "QA: Assuming mebibytes."
194 scarabeus 1.9 ewarn "QA: File bug against the package. It should specify the unit."
195 ciaranm 1.1 fi
196    
197 scarabeus 1.9 echo ${size}
198     }
199    
200     # @FUNCTION: check-reqs_get_unit
201     # @DESCRIPTION:
202 ulm 1.14 # Internal function that return the unit without the numerical value.
203     # Returns "GiB" for "1G" or "TiB" for "150T".
204 scarabeus 1.9 check-reqs_get_unit() {
205     debug-print-function ${FUNCNAME} "$@"
206    
207     [[ -z ${1} ]] && die "Usage: ${FUNCNAME} [size]"
208    
209     local unit=${1:(-1)}
210    
211     case ${unit} in
212 ulm 1.14 G) echo "GiB" ;;
213     [M0-9]) echo "MiB" ;;
214     T) echo "TiB" ;;
215 scarabeus 1.9 *)
216     die "${FUNCNAME}: Unknown unit: ${unit}"
217     ;;
218     esac
219 ciaranm 1.1 }
220    
221 scarabeus 1.9 # @FUNCTION: check-reqs_output
222 zlin 1.6 # @DESCRIPTION:
223 scarabeus 1.9 # Internal function that prints the warning and dies if required based on
224     # the test results.
225     check-reqs_output() {
226     debug-print-function ${FUNCNAME} "$@"
227    
228     local msg="ewarn"
229    
230     [[ ${EBUILD_PHASE} == "pretend" && -z ${I_KNOW_WHAT_I_AM_DOING} ]] && msg="eerror"
231     if [[ -n ${CHECKREQS_FAILED} ]]; then
232     ${msg}
233     ${msg} "Space constrains set in the ebuild were not met!"
234     ${msg} "The build will most probably fail, you should enhance the space"
235     ${msg} "as per failed tests."
236     ${msg}
237 ciaranm 1.5
238 scarabeus 1.9 [[ ${EBUILD_PHASE} == "pretend" && -z ${I_KNOW_WHAT_I_AM_DOING} ]] && \
239     die "Build requirements not met!"
240 ciaranm 1.5 fi
241 scarabeus 1.9 }
242 ciaranm 1.5
243 scarabeus 1.9 # @FUNCTION: check-reqs_memory
244     # @DESCRIPTION:
245     # Internal function that checks size of RAM.
246     check-reqs_memory() {
247     debug-print-function ${FUNCNAME} "$@"
248    
249     [[ -z ${1} ]] && die "Usage: ${FUNCNAME} [size]"
250    
251     local size=${1}
252     local actual_memory
253    
254     check-reqs_start_phase \
255     ${size} \
256     "RAM"
257 ciaranm 1.5
258 zlin 1.6 if [[ -r /proc/meminfo ]] ; then
259 scarabeus 1.9 actual_memory=$(awk '/MemTotal/ { print $2 }' /proc/meminfo)
260 ciaranm 1.1 else
261     actual_memory=$(sysctl hw.physmem 2>/dev/null )
262 zlin 1.6 [[ "$?" == "0" ]] &&
263 ciaranm 1.1 actual_memory=$(echo $actual_memory | sed -e 's/^[^:=]*[:=]//' )
264     fi
265 scarabeus 1.9 if [[ -n ${actual_memory} ]] ; then
266     if [[ ${actual_memory} -lt $((1024 * $(check-reqs_get_mebibytes ${size}))) ]] ; then
267 ciaranm 1.1 eend 1
268 scarabeus 1.9 check-reqs_unsatisfied \
269     ${size} \
270     "RAM"
271 ciaranm 1.1 else
272     eend 0
273     fi
274     else
275     eend 1
276 scarabeus 1.9 ewarn "Couldn't determine amount of memory, skipping..."
277 ciaranm 1.1 fi
278     }
279    
280 scarabeus 1.9 # @FUNCTION: check-reqs_disk
281     # @DESCRIPTION:
282     # Internal function that checks space on the harddrive.
283     check-reqs_disk() {
284     debug-print-function ${FUNCNAME} "$@"
285    
286     [[ -z ${2} ]] && die "Usage: ${FUNCNAME} [path] [size]"
287    
288     local path=${1}
289     local size=${2}
290     local space_megs
291    
292     check-reqs_start_phase \
293     ${size} \
294     "disk space at \"${path}\""
295    
296     space_megs=$(df -Pm "${1}" 2>/dev/null | awk 'FNR == 2 {print $4}')
297    
298     if [[ $? == 0 && -n ${space_megs} ]] ; then
299     if [[ ${space_megs} -lt $(check-reqs_get_mebibytes ${size}) ]] ; then
300 ciaranm 1.1 eend 1
301 scarabeus 1.9 check-reqs_unsatisfied \
302     ${size} \
303     "disk space at \"${path}\""
304 ciaranm 1.1 else
305     eend 0
306     fi
307     else
308     eend 1
309 scarabeus 1.9 ewarn "Couldn't determine disk space, skipping..."
310 ciaranm 1.1 fi
311     }
312    
313 scarabeus 1.9 # @FUNCTION: check-reqs_start_phase
314     # @DESCRIPTION:
315     # Internal function that inform about started check
316     check-reqs_start_phase() {
317     debug-print-function ${FUNCNAME} "$@"
318    
319     [[ -z ${2} ]] && die "Usage: ${FUNCNAME} [size] [location]"
320    
321     local size=${1}
322     local location=${2}
323     local sizeunit="$(check-reqs_get_number ${size}) $(check-reqs_get_unit ${size})"
324 ciaranm 1.1
325 scarabeus 1.9 ebegin "Checking for at least ${sizeunit} ${location}"
326 ciaranm 1.1 }
327    
328 scarabeus 1.9 # @FUNCTION: check-reqs_unsatisfied
329     # @DESCRIPTION:
330     # Internal function that inform about check result.
331     # It has different output between pretend and setup phase,
332     # where in pretend phase it is fatal.
333     check-reqs_unsatisfied() {
334     debug-print-function ${FUNCNAME} "$@"
335    
336     [[ -z ${2} ]] && die "Usage: ${FUNCNAME} [size] [location]"
337    
338     local msg="ewarn"
339     local size=${1}
340     local location=${2}
341     local sizeunit="$(check-reqs_get_number ${size}) $(check-reqs_get_unit ${size})"
342    
343     [[ ${EBUILD_PHASE} == "pretend" && -z ${I_KNOW_WHAT_I_AM_DOING} ]] && msg="eerror"
344 scarabeus 1.10 ${msg} "There is NOT at least ${sizeunit} ${location}"
345 scarabeus 1.9
346     # @ECLASS-VARIABLE: CHECKREQS_FAILED
347     # @DESCRIPTION:
348     # @INTERNAL
349     # If set the checks failed and eclass should abort the build.
350     # Internal, do not set yourself.
351     CHECKREQS_FAILED="true"
352 ciaranm 1.1 }

  ViewVC Help
Powered by ViewVC 1.1.20