ambevar-dotfiles/.scripts/pacsize

155 lines
3.9 KiB
Plaintext
Raw Normal View History

#!/bin/sh
2013-12-24 13:24:58 +01:00
# pacsize -- display package sizes
#
# Copyright (C) 2014 Pierre Neidhardt <ambrevar@gmail.com>
2013-12-24 13:24:58 +01:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################################
## Helper functions.
calc_total () {
awk '{
total += $1
print
}
END {
printf ("%7s KIB TOTAL\n", total)
}'
}
remove_duplicates () {
awk '! table[$0]++'
}
usage() {
cat <<EOF
Usage: ${1##*/} [OPTIONS] PACKAGES
2013-12-24 11:29:49 +01:00
${1##*/} -a [OPTIONS]
2013-12-24 13:24:58 +01:00
Display package sizes. Output can be sorted by name or size, while removing
duplicates if any. A grand total can be printed at the end. By default, search
the sync database. For faster processing and local package support, you can use
the local databases. If 'expac' is found in the path, it will be used for faster
processing.
2013-12-24 13:24:58 +01:00
Options:
-a: All installed packages. Implies -Q.
2013-06-25 22:56:10 +02:00
-h: Show this help.
-n: Output is sorted by name.
-Q: Use local database. It speeds up queries and allows displaying
size of local packages not available in repos.
-S: Use sync database. It is slower but allows to display size of
non-installed packages.
-s: Output is sorted by size.
2013-12-24 12:12:25 +01:00
-t: Print total.
2013-06-25 22:56:10 +02:00
2013-12-24 13:24:58 +01:00
Examples:
${1##*/} -ast
Convenient way to monitor big packages.
${1##*/} -Q \$(pactree -ld1 linux)
2013-12-24 13:24:58 +01:00
Print the size of linux and its direct dependencies.
2013-12-29 10:17:17 +01:00
${1##*/} -qst \$(pacman -Qdtq)
2013-12-24 13:24:58 +01:00
Print a grand total of orphan packages, and sort by size.
EOF
}
db="Q"
2013-12-24 13:24:58 +01:00
opt_sort=false
opt_all=false
opt_total=false
while getopts ":ahnQSst" opt; do
case $opt in
2013-12-24 11:29:49 +01:00
a)
2013-12-24 13:24:58 +01:00
opt_all=true
db="Q" ;;
h)
usage "$0"
2013-12-24 13:24:58 +01:00
exit ;;
n)
2013-12-24 13:24:58 +01:00
opt_sort="sort -uk3" ;;
Q)
db="Q" ;;
S)
db="S" ;;
s)
opt_sort="sort -un" ;;
2013-12-24 12:12:25 +01:00
t)
opt_total="calc_total" ;;
?)
usage "$0"
exit 1 ;;
:)
echo "Missing argument."
usage "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
################################################################################
## All-packages mode. We use a dedicated algorithm which is much faster than
## per-package mode.
2013-12-24 13:24:58 +01:00
if $opt_all; then
DBPath="$(awk -F = '/^ *DBPath/{print $2}' /etc/pacman.conf)"
2013-12-24 11:29:49 +01:00
[ ! -d "$DBPath" ] && DBPath="/var/lib/pacman/"
2013-12-24 12:21:44 +01:00
[ ! -d "$DBPath/local/" ] && exit
2013-12-24 11:29:49 +01:00
awk '/^%NAME%/ {
getline
pkg=$0
}
/^%SIZE%/ {
getline
size = $0 / 1024
printf ("%6s KiB %s\n", size, pkg)
}' "$DBPath"/local/*/desc | ($opt_sort || cat) | ($opt_total || cat)
2013-12-24 11:29:49 +01:00
exit
fi
################################################################################
## Per-package mode.
if [ $# -eq 0 ]; then
2013-12-24 12:21:44 +01:00
echo "Missing argument." >&2
usage "$0"
exit 1
fi
if ! command -v pacman >/dev/null 2>&1; then
echo "'pacman' not found." >&2
2013-12-24 12:21:44 +01:00
exit 1
fi
## Locale.
LC_NAME="$(gettext pacman "Name")"
LC_SIZE="$(gettext pacman "Installed Size :" | sed 's/ *: *$//')"
2013-10-29 21:49:48 +01:00
2013-12-24 12:12:25 +01:00
## We use external variable for awk to fit current locales. In the following
## command, we strip the decimals. This makes output lighter.
pacman -${db}i "$@" 2>/dev/null | awk -F ": " \
-v pkgsize="$LC_SIZE" -v pkgname="$LC_NAME" \
'$0 ~ pkgname {
pkg = $2
}
$0 ~ pkgsize {
gsub (/[\.,].*/, "")
printf ("%7s KiB %s\n", $2, pkg)
}' | ($opt_sort || remove_duplicates) | ($opt_total || cat)