Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o, Fabian Groffen <grobian@g.o>
Subject: Re: [gentoo-portage-dev] [PATCH] __dyn_install: improve reporting of build and image sizes
Date: Sat, 26 Aug 2017 21:54:41
Message-Id: 07265992-8019-d8f5-3fce-1af7ababd3ef@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH] __dyn_install: improve reporting of build and image sizes by Fabian Groffen
1 On 08/24/2017 06:02 AM, Fabian Groffen wrote:
2 > Prior to this commit, the reported sizes would look like:
3 >
4 > * Final size of build directory: 34942 KiB
5 > * Final size of installed tree: 5627 KiB
6 >
7 > Because the sizes aren't aligned, it is hard to (visually) compare them.
8 > On top of this, because the numbers are sometimes bigger, print a human
9 > friendly size after the KiB size if applicable, like so:
10 >
11 > * Final size of build directory: 1906 KiB (1.8 MiB)
12 > * Final size of installed tree: 7 KiB
13 >
14 > It should be noted that in case both sizes have a human-readable
15 > variant, they are also aligned.
16 > ---
17 > bin/phase-functions.sh | 49 +++++++++++++++++++++++++++++++++++++++++++++----
18 > 1 file changed, 45 insertions(+), 4 deletions(-)
19 >
20 > diff --git a/bin/phase-functions.sh b/bin/phase-functions.sh
21 > index dfd8733c8..af45a0d49 100644
22 > --- a/bin/phase-functions.sh
23 > +++ b/bin/phase-functions.sh
24 > @@ -598,10 +598,51 @@ __dyn_install() {
25 >
26 > # record build & installed size in build log
27 > if type -P du &>/dev/null; then
28 > - local sz=( $(du -ks "${WORKDIR}") )
29 > - einfo "Final size of build directory: ${sz[0]} KiB"
30 > - sz=( $(du -ks "${D}") )
31 > - einfo "Final size of installed tree: ${sz[0]} KiB"
32 > + local nsz=( $(du -ks "${WORKDIR}") )
33 > + local isz=( $(du -ks "${D}") )
34 > +
35 > + # align $1 to the right to the width of the widest of $1 and $2
36 > + padl() {
37 > + local s1=$1
38 > + local s2=$2
39 > + local width=${#s1}
40 > + [[ ${#s2} -gt ${width} ]] && width=${#s2}
41 > + printf "%*s" ${width} "${s1}"
42 > + }
43 > +
44 > + # transform number in KiB into MiB, GiB or TiB based on size
45 > + human() {
46 > + local s1=$1
47 > + local units=( KiB MiB GiB TiB )
48 > +
49 > + s1=$((s1 * 10))
50 > + while [[ ${s1} -gt 10240 && ${#units[@]} -gt 1 ]] ; do
51 > + s1=$((s1 / 1024 ))
52 > + units=( ${units[@]:1} )
53 > + done
54 > +
55 > + local r=${s1: -1}
56 > + s1=$((s1 / 10))
57 > + printf "%s.%s %s" "${s1}" "${r}" "${units[0]}"
58 > + }
59 > +
60 > + size() {
61 > + local s1=$1
62 > + local s2=$2
63 > + local out="$(padl "${s1}" "${s2}") KiB"
64 > +
65 > + if [[ ${s1} -gt 1024 ]] ; then
66 > + s1=$(human ${s1})
67 > + if [[ ${s2} -gt 1024 ]] ; then
68 > + s2=$(human ${s2})
69 > + s1=$(padl ${s1} ${s2})
70 > + fi
71 > + out+=" (${s1})"
72 > + fi
73 > + echo "${out}"
74 > + }
75 > + einfo "Final size of build directory: $(size ${nsz[0]} ${isz[0]})"
76 > + einfo "Final size of installed tree: $(size ${isz[0]} ${nsz[0]})"
77 > __vecho
78 > fi
79 >
80 >
81
82 Since bash doesn't support local functions [1], please define and use
83 them in a subshell, so that they do not leak into the persistent
84 environment.
85
86 [1] https://stackoverflow.com/questions/34985408/achieve-local-function
87 --
88 Thanks,
89 Zac

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies