ambevar-dotfiles/.scripts/pacman-deps

122 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env zsh
if [ -f "${0%/*}/.pacman-init" ];then
. "${0%/*}/.pacman-init"
else
echo "Missing init file" && exit 1
fi
# Note: we really need to use an array for this one, so Bourne shells will not
# work.
_printhelp()
{
cat <<EOF
Usage: ${1##*/} [-a|-h|-q] [-r] PACKAGES
Display package dependencies. Output contains no double and is alphabetically
sorted. It will only work for repos packages by default.
-a: Uses Yaourt queries instead of repos database. It slows down query but allow displaying size of any packages not available in repos.
-h: Show this help.
-r: Call function recursively.
-q: Uses installed packages database instead of repos database. It speeds up queries and allows displaying size of local packages not available in repos.
EOF
}
CMD="pacman -Si"
OPT_REC=0
SEP=": "
while getopts ":ahrq" opt; do
case $opt in
a)
CMD="yaourt -Si"
;;
h)
_printhelp "$0"
exit 1
;;
r)
OPT_REC=1
;;
q)
CMD="pacman -Qi"
;;
?)
_printhelp "$0"
exit 1
;;
:)
echo "Missing argument."
_printhelp "$0"
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
echo "Missing argument."
_printhelp "$0"
exit 1
fi
if [ $OPT_REC -eq 0 ]; then
eval "${CMD} $@ 2>/dev/null" | awk -F "$SEP" -v filter="$pacman_deps" \
'$0 ~ filter {gsub(/[>=<][^ ]*/,"",$2) ; gsub(/ +/,"\n",$2) ; print $2}' \
| sort -u
else
## Recursive dependencies.
## Store the complete DB into a variable to speed up access.
FULLDEPLIST=$(eval "${CMD}" | grep "${pacman_name}\|${pacman_deps}" | sed -n "/${pacman_name}/{s/.*: \(.*\)/\1:/;h;n;s/${pacman_deps}.*: //;s/[>=<][^ ]*//g;H;x;s/\n//;p}")
## Note: using an array is important for Ksh/Zsh compatibility.
DEPLIST=()
## Check if argument $1 is in DEPLIST.
_not_in_array()
{
if [ ${#DEPLIST} -eq 0 ]; then
return 0
else
for j in ${DEPLIST[*]}; do
if [ "$j" = "$1" ]; then
return 1
fi
done
return 0
fi
}
## This function calls recursively over the dependencies. It stops whenever
## a package has already be handled, or when it has no dependency. Note:
## 'grep | cut' is a little faster than 'sed' for simple stuff. Since it is
## a loop, it is quite a performance boost.
_pacman_deps_rec()
{
SUBDEPLIST=()
for i ; do
SUBDEPLIST=(${SUBDEPLIST[*]} $(echo "$FULLDEPLIST" | grep "^$i\s*:" | cut -d':' -f2))
done
for i in ${SUBDEPLIST[*]} ; do
if [ ! "$i" = "$pacman_deps_none" ] && _not_in_array "$i" ; then
DEPLIST=(${DEPLIST[*]} "$i")
_pacman_deps_rec "$i"
fi
done
}
for i ; do
_pacman_deps_rec "$@"
done
## There might be duplicates, so we remove them with the '-u' parameter of
## sort.
echo "${DEPLIST[*]}" | sed 's/ /\n/g' | sort -u
fi