Files
scratchpkg/pkgdeplist

186 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
#
# scratchpkg
#
# Copyright (c) 2018 by Emmett1 (emmett1.2miligrams@gmail.com)
#
# 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 3 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 <https://www.gnu.org/licenses/>.
#
INDEX_DIR="/var/lib/scratchpkg/index"
REPO_FILE="/etc/scratchpkg.repo"
msgerr() {
echo -e "==> ERROR: $1"
}
msginst() {
echo -e "[i] $1"
}
msgmiss() {
echo -e "[m] $1"
}
msgforeign() {
echo -e "[f] $1"
}
msgnoinst() {
echo -e "[-] $1"
}
checkdep() {
local depends
PORT_PATH=$(getportpath "$1")
if [ "$PORT_PATH" ]; then
depends=$(grep "^# depends[[:blank:]]*:" $PORT_PATH/spkgbuild | sed 's/^# depends[[:blank:]]*:[[:blank:]]*//' | tr ' ' '\n' | awk '!a[$0]++')
for dep in ${depends[@]}; do
echo $dep
done
else
return 0
fi
}
getportpath() {
for repo in ${PORT_REPO[@]}; do
if [[ -f $repo/$1/spkgbuild ]]; then
echo "$(dirname $repo/$1/spkgbuild)"
return 0
fi
done
return 1
}
deplist() {
# check currently process package for loop
if [ ${#CHECK[@]} -gt 0 ]; then
if [[ "$(echo ${CHECK[@]} | tr " " "\n" | grep -x $1)" == "$1" ]]; then
if [ "$IGNORE_LOOP" = 1 ]; then
return 0
else
msgerr "loop dependency found: $1"
exit 1
fi
fi
fi
# add package to currently process
CHECK+=($1)
# check dependencies
for i in $(checkdep $1); do
[ -e $INDEX_DIR/$i/.pkginfo -a "$QUICK" ] && continue
if [[ $(echo ${DEP[@]} | tr " " "\n" | grep -x $i) = "" ]]; then
deplist $i
fi
done
# add dependency to list checked dep
if [[ $(echo ${DEP[@]} | tr " " "\n" | grep -x $1) = "" ]]; then
DEP+=($1)
fi
# delete process package array
for i in "${!CHECK[@]}"; do
if [[ ${CHECK[i]} = "$1" ]]; then
unset 'CHECK[i]'
fi
done
}
usage() {
cat << EOF
Usage:
$(basename $0) [ <options> <packages> ]
Options:
-q, --quick calculate only not-installed package/dependency
-i, --installed print only installed package/dependency
-n, --not-installed print only not-installed package/dependency
-m, --missing print only missing package/dependency
-f, --foreign print foreign package/dependency
-l, --loop ignore loop dependencies
-h, --help print this help message
EOF
}
while [ "$1" ]; do
case $1 in
-q|--quick) QUICK=1 ;;
-i|--installed) INSTALLED=1 ;;
-n|--not-installed) NOT_INSTALLED=1 ;;
-m|--missing) MISSING=1 ;;
-f|--foreign) FOREIGN=1 ;;
-l|--loop) IGNORE_LOOP=1 ;;
-h|--help) USAGE=1 ;;
-*) msgerr "Invalid option. ($1)"; exit 1 ;;
*) PKG+=($1) ;;
esac
shift
done
if [ "$USAGE" = 1 ]; then
usage
exit 0
fi
if [ ! -f "$REPO_FILE" ]; then
msgerr "repo file not exist. ($REPO_FILE)"
exit 1
fi
if [ "${#PKG[@]}" = 0 ]; then
msgerr "Please specify package name to list its dependencies order."
exit 1
fi
while read repodir repourl junk; do
case $repodir in
""|"#"*) continue ;;
esac
PORT_REPO+=($repodir)
done < "$REPO_FILE"
# set all to 1 (all) if not use any of it
if [ ! "$INSTALLED" ] && [ ! "$NOT_INSTALLED" ] && [ ! "$MISSING" ] && [ ! "$FOREIGN" ]; then
INSTALLED=1
NOT_INSTALLED=1
MISSING=1
FOREIGN=1
fi
# calculate dependencies
for pkg in ${PKG[@]}; do
deplist $pkg
done
for deps in ${DEP[@]}; do
if [ -e $INDEX_DIR/$deps/.pkginfo ]; then
if [ ! $(getportpath "$deps") ]; then
[ "$FOREIGN" ] && msgforeign "$deps" # print foreign pkg
else
[ "$INSTALLED" ] && msginst "$deps" # print installed
fi
elif [ ! $(getportpath "$deps") ]; then
[ "$MISSING" ] && msgmiss "$deps" # print missing port
else
[ "$NOT_INSTALLED" ] && msgnoinst "$deps" # print not installed
fi
done
exit 0