forked from MiXXiM/miscellaneous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkgstats
363 lines (301 loc) · 7.49 KB
/
pkgstats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env bash
#cito M:755 O:0 G:0 T:/usr/local/bin/pkgstats
#------------------------------------------------------------------------------
# Project Name - ShellProjects/source/pkgstats
# Started On - Sat 17 Jun 09:08:12 BST 2023
# Last Change - Mon 19 Jun 23:34:57 BST 2023
# Author E-Mail - [email protected]
# Author GitHub - https://github.com/terminalforlife
#------------------------------------------------------------------------------
# Gather, store, and maintain list of available packages and their file sizes.
#
# Example:
#
# $ pkgstats -s packages_bytes_bionic.txt
# Packages: 82795
# TTL Size: 252.2G
# Avg. Size: 3.1M
# >= 500M: 6 (3.9G, 0.01%)
# <= 10M: 74287 (66G, 89.72%)
#
# Largest:
#
# - 'flightgear-data-base' (1004.6M)
# - 'redeclipse-data' (822.3M)
# - '0ad-data' (610.5M)
#
# Smallest:
#
# - 'libc6-armel-armhf-cross' (776)
# - 'libc6-armhf-armel-cross' (776)
# - 'libc6-dev-armel-armhf-cross' (780)
#
# NOTE: This can take a very long time, depending on the repositories you have
# available. Thanks to the ability for PKGStats to pick up where it left
# off, you needn't do it all in one session.
#
# If you run this, please be mindful of the servers. While sending a
# spider to grab the header is surely harmless, it's still constant
# connections, so please don't run this excessively!
#
# Features:
#
# N/A
#
# Bugs:
#
# N/A
#
# Dependencies:
#
# apt (>= 1.6.17)
# bash (>= 4.0)
# gawk (>= 4.1.4)
# wget (>= 1.19.4)
#------------------------------------------------------------------------------
CurVer='2023-06-19'
Progrm='pkgstats'
ProgrmFancy='PKGStats'
Usage() {
read -d '' <<-EOF
Usage: $Progrm [OPTS] FILE
-h, --help - Display this help information.
-v, --version - Output the version datestamp.
-s, --summary-only - Just display summary of existing FILE.
The use of '--' to ignore proceeding options is supported.
EOF
printf '%s' "$REPLY"
}
Err() {
printf 'Err: %s\n' "$2" 1>&2
(( $1 > 0 )) && exit $1
}
SummaryOnly='False'
Skip=
Args=()
while [[ -n $1 ]]; do
case $1 in
--)
Args+=(--)
Skip='True' ;;
-[^-]*)
if [[ $Skip == True ]]; then
Args+=("$1")
else
Str=${1#-}
Len=${#Str}
for (( Index = 0; Index < Len; Index++ )); {
Args+=(-"${Str:Index:1}")
}
fi ;;
*)
Args+=("$1") ;;
esac
shift
done
set -- "${Args[@]}"
while [[ -n $1 ]]; do
case $1 in
--)
break ;;
--help|-h)
Usage; exit 0 ;;
--version|-v)
printf '%s\n' "$CurVer"; exit 0 ;;
--summary-only|-s)
SummaryOnly='True' ;;
-*)
Err 1 'Incorrect option(s) specified.' ;;
*)
break ;;
esac
shift
done
Args=$#
if (( Args < 1 )); then
Err 0 'Argument required.'
read -d '' <<-EOF
$ProgrmFancy requires a file into which to save the package
information, consisting of a 'BYTES PACKAGE' format.
Nothing to do -- quitting.
EOF
printf '%s' "$REPLY"
exit 1
elif (( Args > 1 )); then
Err 1 'Invalid argument(s).'
fi
DepCount=0
for Dep in apt-get apt-cache wget awk; do
if ! type -P "$Dep" &> /dev/null; then
Err 0 "Dependency '$Dep' not met."
(( DepCount++ ))
fi
done
(( DepCount > 0 )) && exit 1
Interactive=
[[ -t 1 ]] && Interactive='True'
if [[ -d $1 ]]; then
Err 1 'Argument is a directory.'
elif [[ -f $1 ]]; then
FileExists='True'
if [[ $SummaryOnly == True ]]; then
[[ -s $1 ]] || Err 1 'Unable to summarize empty FILE.'
else
printf "NOTE: File '%s' already exists.\n" "$1"
while :; do
read -p ' Continue? [Y/N] '
case ${REPLY,,} in
y|yes)
break ;;
n|no)
exit 1 ;;
'')
Err 0 'Empty response -- try again.' ;;
*)
Err 0 'Unrecognised response -- try again.' ;;
esac
done
fi
else
if [[ $SummaryOnly == True ]]; then
Err 1 'Unable to summarize non-existent FILE.'
fi
fi
#-----------------------------------Gather Data from and Validate Existing File
if [[ $FileExists == True ]]; then
LineNr=0
LineErrs=0
declare -A FilePKGs=()
while read Bytes FilePKG; do
(( LineNr++ ))
[[ ${Bytes:0:1} == '#' ]] && continue
if [[ $Bytes != +([[:digit:]]) ]]; then
Err 0 "Invalid bytes on line $LineNr."
(( LineErrs++ ))
fi
if [[ $FilePKG == *[[:space:]]* ]]; then
Err 0 "Invalid package on line $LineNr."
(( LineErrs++ ))
fi
(( LineErrs > 0 )) || FilePKGs["$FilePKG"]=$Bytes
done < "$1"
(( LineErrs > 0 )) && exit 1
fi
#---------------------------------------Begin Gathering Package Names and Sizes
if [[ $SummaryOnly != True ]]; then
SigHandler() {
printf '\n'
exit 130
}
trap SigHandler INT
readarray -t Packages <<< "$(apt-cache pkgnames 2>&-)" || exit 1
[[ $FileExists == True ]] || > "$1"
Exists=
Count=0
Len=${#Packages[@]}
for Package in "${Packages[@]}"; {
if [[ $FileExists == True ]]; then
Exists=
if [[ -n ${FilePKGs["$Package"]} ]]; then
Bytes=${FilePKGs["$Package"]}
Exists='True'
fi
fi
if [[ $Exists != True ]]; then
while read Line; do
if [[ ${Line:0:1} == "'" ]]; then
Link=${Line%\'*}
Link=${Link#\'}
break
fi
done <<< "$(apt-get download --print-uris "$Package" 2>&-)"
while read F1 Bytes _; do
if [[ $F1 == Length: ]]; then
printf '%s %s\n' "$Bytes" "$Package" >> "$1"
break
fi
done <<< "$(wget --spider "$Link" 2>&1)"
fi
if [[ $Interactive == True ]]; then
if [[ $Exists == True ]]; then
printf '\r[???.????] \e[92m%s\e[0m is \e[93m%d\e[0m bytes\e[K'\
"$Package" $Bytes
continue
fi
awk -v C=$(( ++Count )) -v L=$Len -v PKG="$Package" -v B=$Bytes '
BEGIN {
Format = "\r[%08.4f%%] \033[92m%s\033[0m "
Format = Format "is \033[93m%d\033[0m bytes\033[K"
printf(Format, C / L * 100, PKG, B)
}
' 2>&-
fi
}
fi
#---------------------------Display Summary (AWK for Floating Point Arithmetic)
Interactive=True
if [[ $Interactive == True ]]; then
[[ $SummaryOnly == True ]] || printf '\rDone! Summary below.\e[K\n\n'
awk '
BEGIN {
GE500M = 0
LE10M = 0
}
# Underscores because AWK sucks at localizing variables.
function Human(_Size) {
_UnitStr = "b K M G T P E Z Y"
_Len = split(_UnitStr, _Units)
for (_Index = 1; _Index <= _Len; _Index++) {
if (_Size < 1024) {
_Result = sprintf("%.1f", _Size)
_Len = length(_Result)
if (substr(_Result, _Len - 1) == ".0") {
_Result = substr(_Result, 0, _Len - 2)
}
if (_Units[_Index] == "b") {
return(substr(_Result, 0, _Len - 1))
} else {
return(_Result _Units[_Index])
}
}
_Size = _Size / 1024
}
}
function Top10(__Sorted_Array, __Len) {
for (__Index = 1; __Index <= __Len; __Index++) {
split(__Sorted_Array[__Index], __Fields)
printf(" - '"'%s'"' (%s)\n", __Fields[2], Human(__Fields[1]))
if (__Index == 3) break
}
}
{
Lines[$0]
Total += $1
}
$1 >= 524288000 {
GE500M++
GE500M_Size += $1
}
$1 <= 10485760 {
LE10M++
LE10M_Size += $1
}
END {
printf(" Packages: %s\n", NR)
printf(" TTL Size: %s\n", Human(Total))
printf("Avg. Size: %s\n", Human(Total / NR))
Perc = GE500M / NR * 100
Size = Human(GE500M_Size)
printf(" >= 500M: %s (%s, %0.2f%%)\n", GE500M, Size, Perc)
Perc = LE10M / NR * 100
Size = Human(LE10M_Size)
printf(" <= 10M: %s (%s, %0.2f%%)\n\n", LE10M, Size, Perc)
print("Largest:\n")
Len = asorti(Lines, Sorted, "@ind_num_desc")
Top10(Sorted, Len)
print("\nSmallest:\n")
Len = asorti(Lines, Sorted, "@ind_num_asc")
Top10(Sorted, Len)
}
' "$1"
fi