Gentoo Archives: gentoo-portage-dev

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

Replies