2013-06-16 09:56:56 +02:00
|
|
|
#!/bin/sh
|
2013-12-24 13:24:58 +01:00
|
|
|
# pacsize -- display package sizes
|
|
|
|
#
|
2014-01-22 18:17:52 +01:00
|
|
|
# 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/>.
|
2013-06-16 09:56:56 +02:00
|
|
|
|
2014-01-22 18:48:53 +01:00
|
|
|
################################################################################
|
|
|
|
## Helper functions.
|
|
|
|
|
|
|
|
calc_total () {
|
|
|
|
awk '{
|
|
|
|
total += $1
|
|
|
|
print
|
|
|
|
}
|
|
|
|
END {
|
|
|
|
printf ("%7s KIB TOTAL\n", total)
|
|
|
|
}'
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_duplicates () {
|
|
|
|
awk '! table[$0]++'
|
|
|
|
}
|
2014-01-22 18:17:52 +01:00
|
|
|
|
2014-01-22 18:48:53 +01:00
|
|
|
usage() {
|
2013-06-16 09:56:56 +02:00
|
|
|
cat <<EOF
|
|
|
|
Usage: ${1##*/} [OPTIONS] PACKAGES
|
2013-12-24 11:29:49 +01:00
|
|
|
${1##*/} -a [OPTIONS]
|
2013-06-16 09:56:56 +02:00
|
|
|
|
2014-01-22 18:54:03 +01:00
|
|
|
Display package sizes. Duplicates are removed if any. Output can be sorted by
|
|
|
|
name or size. A grand total can be printed at the end. Both the sync and the
|
|
|
|
local databes can be used for lookup.
|
2013-06-16 09:56:56 +02:00
|
|
|
|
2013-12-24 13:24:58 +01:00
|
|
|
Options:
|
|
|
|
|
2014-01-22 17:50:32 +01:00
|
|
|
-a: All installed packages. Implies -Q.
|
2013-06-25 22:56:10 +02:00
|
|
|
-h: Show this help.
|
2013-12-23 22:52:59 +01:00
|
|
|
-n: Output is sorted by name.
|
2014-01-22 17:50:32 +01:00
|
|
|
-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.
|
2013-12-23 22:52:59 +01:00
|
|
|
-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
|
2014-01-22 18:54:03 +01:00
|
|
|
Convenient way to keep track of big packages.
|
2013-12-24 13:24:58 +01:00
|
|
|
|
2014-01-22 17:50:32 +01:00
|
|
|
${1##*/} -Q \$(pactree -ld1 linux)
|
2014-01-22 18:54:03 +01:00
|
|
|
Print the size of linux and all its direct dependencies.
|
2013-12-24 13:24:58 +01:00
|
|
|
|
2014-01-22 18:54:03 +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.
|
2013-06-16 09:56:56 +02:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2014-01-22 18:48:53 +01:00
|
|
|
db="Q"
|
2013-12-24 13:24:58 +01:00
|
|
|
opt_sort=false
|
|
|
|
opt_all=false
|
2014-01-22 18:48:53 +01:00
|
|
|
opt_total=false
|
2013-06-16 09:56:56 +02:00
|
|
|
|
2014-01-22 17:50:32 +01:00
|
|
|
while getopts ":ahnQSst" opt; do
|
2013-06-16 09:56:56 +02:00
|
|
|
case $opt in
|
2013-12-24 11:29:49 +01:00
|
|
|
a)
|
2013-12-24 13:24:58 +01:00
|
|
|
opt_all=true
|
2014-01-22 18:48:53 +01:00
|
|
|
db="Q" ;;
|
2013-06-16 09:56:56 +02:00
|
|
|
h)
|
2014-01-22 18:48:53 +01:00
|
|
|
usage "$0"
|
2013-12-24 13:24:58 +01:00
|
|
|
exit ;;
|
2013-06-16 09:56:56 +02:00
|
|
|
n)
|
2013-12-24 13:24:58 +01:00
|
|
|
opt_sort="sort -uk3" ;;
|
2014-01-22 17:50:32 +01:00
|
|
|
Q)
|
2014-01-22 18:48:53 +01:00
|
|
|
db="Q" ;;
|
2014-01-22 17:50:32 +01:00
|
|
|
S)
|
2014-01-22 18:48:53 +01:00
|
|
|
db="S" ;;
|
2013-12-23 22:52:59 +01:00
|
|
|
s)
|
2014-01-22 18:48:53 +01:00
|
|
|
opt_sort="sort -un" ;;
|
2013-12-24 12:12:25 +01:00
|
|
|
t)
|
2014-01-22 18:48:53 +01:00
|
|
|
opt_total="calc_total" ;;
|
2013-06-16 09:56:56 +02:00
|
|
|
?)
|
2014-01-22 18:48:53 +01:00
|
|
|
usage "$0"
|
2013-10-23 11:24:32 +02:00
|
|
|
exit 1 ;;
|
2013-06-16 09:56:56 +02:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
|
2014-01-22 18:17:52 +01:00
|
|
|
################################################################################
|
|
|
|
## 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
|
2014-01-22 17:50:32 +01:00
|
|
|
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
|
|
|
|
2014-01-22 18:48:53 +01:00
|
|
|
awk '/^%NAME%/ {
|
|
|
|
getline
|
|
|
|
pkg=$0
|
|
|
|
}
|
2014-01-22 18:17:52 +01:00
|
|
|
/^%SIZE%/ {
|
|
|
|
getline
|
|
|
|
size = $0 / 1024
|
|
|
|
printf ("%6s KiB %s\n", size, pkg)
|
2014-01-22 18:48:53 +01:00
|
|
|
}' "$DBPath"/local/*/desc | ($opt_sort || cat) | ($opt_total || cat)
|
2013-12-24 11:29:49 +01:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2014-01-22 18:17:52 +01:00
|
|
|
################################################################################
|
|
|
|
## Per-package mode.
|
2013-06-16 09:56:56 +02:00
|
|
|
if [ $# -eq 0 ]; then
|
2013-12-24 12:21:44 +01:00
|
|
|
echo "Missing argument." >&2
|
2014-01-22 18:48:53 +01:00
|
|
|
usage "$0"
|
2013-06-16 09:56:56 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-01-03 19:13:07 +01:00
|
|
|
if ! command -v pacman >/dev/null 2>&1; then
|
2014-01-22 18:48:53 +01:00
|
|
|
echo "'pacman' not found." >&2
|
2013-12-24 12:21:44 +01:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-01-22 18:17:52 +01:00
|
|
|
## 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
|
2014-01-22 18:48:53 +01:00
|
|
|
## 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
|
|
|
|
}
|
2014-01-22 18:17:52 +01:00
|
|
|
$0 ~ pkgsize {
|
|
|
|
gsub (/[\.,].*/, "")
|
2014-01-22 18:48:53 +01:00
|
|
|
printf ("%7s KiB %s\n", $2, pkg)
|
|
|
|
}' | ($opt_sort || remove_duplicates) | ($opt_total || cat)
|