added check package versions #11

This commit is contained in:
wt
2025-05-19 17:17:35 +07:00
parent d1164975e3
commit fa04c30d6c
2 changed files with 144 additions and 138 deletions
+1 -3
View File
@@ -4,7 +4,6 @@ DESTDIR=/
install:
@if [ ! -d ${DESTDIR}/etc/pm ]; then \
mkdir -p ${DESTDIR}/etc/pm; \
# touch ${DESTDIR}/etc/pm/repo; \
fi
@if [ ! -d ${DESTDIR}/tmp ]; then \
mkdir -p ${DESTDIR}/tmp; \
@@ -14,7 +13,6 @@ install:
fi
@if [ ! -d ${DESTDIR}/usr/share/pm ]; then \
mkdir -p ${DESTDIR}/usr/share/pm/{packages,repos}; \
# touch ${DESTDIR}/usr/share/pm/query; \
fi
@if [ ! -d ${DESTDIR}/var/cache/pm/pkg ]; then \
mkdir -p ${DESTDIR}/var/cache/pm/pkg; \
@@ -51,4 +49,4 @@ clean:
fi
@if [ -f pm.pkg.gz ]; then \
rm pm.pkg.gz; \
fi
fi
+143 -135
View File
@@ -34,56 +34,40 @@ BGREEN="\033[1;32m"
#------------ check status function ------------
check() {
if [ $1 -ne 0 ]; then
printf "$BBLUE**$BWHITE $2 [$BRED ERROR $BWHITE]\n$RESET"
exit $1
fi
[ $1 -ne 0 ] \
&& printf "$BBLUE**$BWHITE $2 [$BRED ERROR $BWHITE]\n$RESET" \
&& exit $1
printf "$BBLUE**$BWHITE $2 [$BGREEN SUCCESS $BWHITE]\n$RESET"
}
#-----------------------------------------------
#---------------- check root priveleges function ------------------------
checkroot() {
if [[ $(id -u) != 0 ]]; then
printf "$BBLUE**$BWHITE You must be root [$BRED ERROR $BWHITE]\n$RESET"
exit 1
fi
[[ $(id -u) != 0 ]] \
&& printf "$BBLUE**$BWHITE You must be root [$BRED ERROR $BWHITE]\n$RESET" \
&& exit 1
}
#------------------------------------------------------------------------
# check non root priveleges function
checknonroot() {
[[ $(id -u) == 0 ]] \
&& printf "$BBLUE**$BWHITE You must be non root [$BRED ERROR $BWHITE]\n$RESET" \
&& exit 1
}
# ----------------------------------
#----------------- add(install) package function ------------------------
add() {
shift 1
if [[ "$1" == "" ]]; then
help
exit 0
fi
[[ "$1" == "" ]] \
&& help \
&& exit 0
for i in $@; do
case $i in
add|a)
continue
;;
esac
down $i
done
for i in $@; do
case $i in
add|a)
continue
;;
*)
down $i
;;
esac
done
for i in $@; do
case $i in
add|a)
continue
;;
*)
unpack $cachedpkgdir/$i.pkg.gz
;;
esac
unpack $cachedpkgdir/$i*.pkg.gz
done
}
a() {
@@ -93,10 +77,9 @@ a() {
#-------------------- download package without install function ---------
down() {
if [[ ! $1 ]]; then
help
exit 0
fi
[[ ! $1 ]] \
&& help \
&& exit 0
servers=$(/usr/bin/cat $repofile)
check $? "Read repositories"
for i in $servers; do
@@ -104,24 +87,25 @@ down() {
serverdir=$(echo $serverdir | sed -e "/ftp:\/\//s///")
repoquery=$(/usr/bin/cat $reposquerydir/$serverdir/query | sed -e '/.pkg.gz/s///')
for j in $repoquery; do
if [[ $j == $1 ]]; then
if [ ! -f $cachedpkgdir/$1.pkg.gz ]; then
# echo $(get_pkgname $j) $(get_pkgname $1)
if [[ "$(get_pkgname $j)" == "$1" ]]; then
if [ ! -f $cachedpkgdir/$j.pkg.gz ]; then
printf "$YELLOW**$RESET Downloading $YELLOW$serverdir: $Bwhite$j\n$RESET"
echo $i/$1.pkg.gz
wget $i/$1.pkg.gz -P $cachedpkgdir
echo $i/$j.pkg.gz
wget $i/$j.pkg.gz -P $cachedpkgdir
check $? "Download package"
# if [[ "$?" == "0" ]]; then
# printf "$YELLOW**$GREEN successful downloaded $1\n$RESET"
# return
# else
# else
# printf "$YELLOW**$RED error:$RESET can't download package\n"
# exit 1
# fi
else
printf "$YELLOW**$GREEN successful downloaded $1\n$RESET"
return
fi
return
fi
return
fi
done
done
@@ -135,28 +119,32 @@ download() {
#-------------------- unpack local package archive ----------------------
unpack() {
[[ "$1" == "" ]] \
&& printf "$BBLUE**$BWHITE Specify package name [$BRED ERROR $BWHITE]\n$RESET" \
&& exit 1
packagename=$(echo $1 | sed -e "/.gz/s///")
packagename=${packagename#*pkg/}
printf "$YELLOW**$GREEN Installing ${packagename%.pkg*}\n$RESET"
if [ ! -f $packagesqueryfile ]; then
printf "$BBLUE**$BWHITE Can't find $packagesqueryfile [$BRED ERROR $BWHITE]\n$RESET"
fi
[ ! -f $packagesqueryfile ] \
&& printf "$BBLUE**$BWHITE Can't find $packagesqueryfile [$BRED ERROR $BWHITE]\n$RESET" \
&& exit 1
installed=false
for i in $(/usr/bin/cat $packagesqueryfile); do
if [ "$i" = "${packagename%.pkg*}" ]; then
export installed=true
break
fi
[ "$i" = "${packagename%.pkg*}" ] \
&& export installed=true \
&& break
done
if [[ "$installed" == "true" ]]; then
printf "$YELLOW**$RESET Package already installed:$YELLOW reinstalling\n$RESET"
fi
[[ "$installed" == "true" ]] \
&& printf "$YELLOW**$RESET Package already installed:$YELLOW reinstalling\n$RESET"
tar -xvf $1 -C $rootdir/tmp &> /dev/null
cd $rootdir/tmp/$packagename
[ -f pre_install ] \
&& /usr/bin/sh -c ./pre_install \
&& rm -v pre_install &> /dev/null
if [ -f deps ]; then
printf "$YELLOW**$GREEN Checking deps\n$RESET"
deps=$(/usr/bin/cat deps)
for dep in $deps; do
for dep in $deps; do
have=false
for pack in $(/usr/bin/cat $packagesqueryfile); do
if [ $pack = $dep ]; then
@@ -176,17 +164,18 @@ unpack() {
fi
mkdir $installedpackagesdir/$packagename &> /dev/null
mv -v list $installedpackagesdir/$packagename &> /dev/null
[ -f pre_delete ] \
&& mv -v pre_delete $installedpackagesdir/$packagename &> /dev/null
[ -f post_delete ] \
&& mv -v post_delete $installedpackagesdir/$packagename &> /dev/null
cp -r ./* $rootdir/
if [ -f $rootdir/post_install ]; then
rm $rootdir/post_install
fi
if [ -f $rootdir/tmp/$packagename/post_install ]; then
printf "$YELLOW**$GREEN post_install script ${packagename%.pkg*}\n$RESET"
/usr/bin/sh -c $rootdir/tmp/$packagename/post_install &> /dev/null
fi
if [[ "$installed" == "false" ]]; then
echo ${packagename%.pkg*} >> $packagesqueryfile
fi
[ -f $rootdir/post_install ] \
&& rm $rootdir/post_install
[ -f $rootdir/tmp/$packagename/post_install ] \
&& printf "$YELLOW**$GREEN post_install script ${packagename%.pkg*}\n$RESET" \
&& /usr/bin/sh -c $rootdir/tmp/$packagename/post_install &> /dev/null
[[ "$installed" == "false" ]] \
&& echo ${packagename%.pkg*} >> $packagesqueryfile
rm -rv $rootdir/tmp/$packagename &> /dev/null
printf "$YELLOW**$GREEN Installed ${packagename%.pkg*} successful\n$RESET"
}
@@ -201,24 +190,25 @@ del() {
shift 1
for i in $@; do
printf "$YELLOW**$GREEN deleting $i\n$RESET"
if [ ! -f $installedpackagesdir/$i.pkg/list ]; then
printf "$BBLUE**$BWHITE package not installed ($i) [$BRED ERROR $BWHITE]\n$RESET"
exit 1
fi
for file in $(/usr/bin/cat $installedpackagesdir/$i.pkg/list); do
[ ! -f $installedpackagesdir/$i*.pkg/list ] \
&& printf "$BBLUE**$BWHITE package not installed ($i) [$BRED ERROR $BWHITE]\n$RESET" \
&& exit 1
[ -f $installedpackagesdir/$i*.pkg/pre_delete ] \
&& /usr/bin/sh -c $installedpackagesdir/$i*.pkg/pre_delete
for file in $(/usr/bin/cat $installedpackagesdir/$i*.pkg/list); do
rm -v $rootdir/$file
done
# rm -v $rootdir/$(/usr/bin/cat $installedpackagesdir/$i.pkg/list)
rm -rv $installedpackagesdir/$i.pkg &> /dev/null
rm -rv $installedpackagesdir/$i*.pkg &> /dev/null
packs=$(cat $packagesqueryfile)
rm $packagesqueryfile
for pack in $packs; do
if [ "$pack" = "$i" ]; then
if [ "$(get_pkgname $pack)" = "$i" ]; then
continue
fi
echo $pack >> $packagesqueryfile
done
# sed -i "s/$i/d" $packagesqueryfile
[ -f $installedpackagesdir/$i*.pkg/post_delete ] \
&& /usr/bin/sh -c $installedpackagesdir/$i*.pkg/post_delete
printf "$YELLOW**$GREEN delete $i successful\n$RESET"
done
}
@@ -231,18 +221,17 @@ d() {
list() {
if [[ "$1" == "" ]]; then
for i in $(/usr/bin/cat $packagesqueryfile); do
filelist=$(/usr/bin/cat $installedpackagesdir/$i.pkg/list)
filelist=$(/usr/bin/cat $installedpackagesdir/$i*.pkg/list)
for j in $filelist; do
printf "$YELLOW$i: $BWHITE$j$RESET\n"
done
done
exit 0
fi
if [[ "$(/usr/bin/cat $packagesqueryfile | grep $1)" == "" ]]; then
printf "$BBLUE**$BWHITE Package is not installed [$BRED ERROR $BWHITE]\n$RESET"
exit 1
fi
filelist=$(/usr/bin/cat $installedpackagesdir/$1.pkg/list)
[[ "$(/usr/bin/cat $packagesqueryfile | grep $1)" == "" ]] \
&& printf "$BBLUE**$BWHITE Package is not installed [$BRED ERROR $BWHITE]\n$RESET" \
&& exit 1
filelist=$(/usr/bin/cat $installedpackagesdir/$1*.pkg/list)
for i in $filelist; do
printf "$YELLOW$1: $BWHITE$i$RESET\n"
done
@@ -251,16 +240,30 @@ l() {
list $1
}
#------------------------------------------------------------------------
get_pkgrel() {
echo $(echo $1 | awk -F'-' '{ print $NF }')
}
get_pkgver() {
echo $(echo $1 | awk -F'-' '{ print $(NF-1) }')
}
get_pkgname() {
echo $( echo $(echo $1 | awk -F'-' '{ for (i=NF-2; i>=1; i--) print $i }') | awk -F' ' '{ if (NF==1) { print $NF } else { for (i=NF; i >= 1; i--) { if (i-1 >= 1) { printf $i "-" } else { printf $i } } } }')
}
printpkg() {
printf "$BWHITE$(get_pkgname $1) $BGREEN$(get_pkgver $1)-$(get_pkgrel $1)\n$RESET"
}
#------------------- show installed packages function -------------------
query() {
packagesquery=$(/usr/bin/cat $packagesqueryfile)
if [[ "$packagesquery" == "" ]]; then
printf "$YELLOW**$BWHITE No installed packages\n$RESET"
exit 0
fi
[[ "$packagesquery" == "" ]] \
&& printf "$YELLOW**$BWHITE No installed packages\n$RESET" \
&& exit 0
for i in $packagesquery; do
printf "$BWHITE$i\n$RESET"
printpkg $i
done
}
q() {
@@ -283,18 +286,7 @@ help() {
clear - clear all package archives from cached packages directory
sync, s - synchronize repository
deps [package name] - show package dependencies
$YELLOW**$GREEN examples$RESET :
$BWHITE pm add dnsmasq
pm unpack ./dnsmasq.pkg.gz
pm down dnsmasq
pm search dnsmasq
pm help
pm clear
pm sync
pm del dnsmasq
pm list dnsmasq
pm query
build - build package (./ must contain PMBUILD)
\n$RESET"
}
#------------------------------------------------------------------------
@@ -325,7 +317,17 @@ search() {
serverdir=$(echo $serverdir | sed -e "/ftp:\/\//s///")
repoquery=$(/usr/bin/cat $reposquerydir/$serverdir/query | sed -e '/.pkg.gz/s///')
for j in $repoquery; do
printf "$YELLOW$serverdir: $BWHITE$j\n$RESET"
installed=false
for inst in $(/usr/bin/cat $packagesqueryfile); do
# echo $inst
[ "$(get_pkgname $inst)" = "$(get_pkgname $j)" ] \
&& export installed=true \
&& break
done
printf "$YELLOW$serverdir: $BWHITE$(get_pkgname $j) $BGREEN$(get_pkgver $j)-$(get_pkgrel $j) $RESET"
[[ "$installed" == "true" ]] \
&& printf "$BBLUE[installed]\n$RESET" \
|| echo ""
done
done
exit 0
@@ -357,14 +359,12 @@ clear() {
#--- show package dependencies function ---
deps() {
if [ "$1" = "" ]; then
help
exit 1
fi
if [ ! -f $installedpackagesdir/$1.pkg/deps ]; then
printf "$YELLOW**$BWHITE No deps\n$RESET"
exit 0
fi
[ "$1" = "" ] \
&& help \
&& exit 1
[ ! -f $installedpackagesdir/$1.pkg/deps ] \
&& printf "$YELLOW**$BWHITE No deps\n$RESET" \
&& exit 0
cat $installedpackagesdir/$1.pkg/deps
}
#------------------------------------------
@@ -376,6 +376,7 @@ buildpkg() {
source $1
else
printf "$BBLUE**$BWHITE Can't find PMBUILD [$BRED ERROR $BWHITE]\n$RESET"
exit 1
fi
else
source ./PMBUILD
@@ -388,26 +389,25 @@ buildpkg() {
else
[ -d pkg ] && rm -r pkg
[ -d src ] && rm -r src
[ -f $pkgname-$pkgver-$pkgrel.pkg.gz ] && rm -r $pkgname-$pkgver-$pkgrel.pkg.gz
[ -f $pkgname-$pkgver-$pkgrel.pkg.gz ] \
&& rm -r $pkgname-$pkgver-$pkgrel.pkg.gz
fi
fi
[ ! -d src ] && mkdir -p src
[ ! -d pkg/$pkgname-$pkgver-$pkgrel.pkg ] && mkdir -p pkg/$pkgname-$pkgver-$pkgrel.pkg
[ ! -d pkg/$pkgname-$pkgver-$pkgrel.pkg ] \
&& mkdir -p pkg/$pkgname-$pkgver-$pkgrel.pkg
export pkgdir=$buildroot/pkg/$pkgname-$pkgver-$pkgrel.pkg
if [ "$srcdir" = "" ]; then
export srcdir=$buildroot/src/$pkgname
fi
if [ "$gitsource" != "" ]; then
printf "$BBLUE**$BWHITE Downloading sources\n$RESET"
git clone $gitsource $srcdir
check $? "Download sources"
fi
[ "$srcdir" = "" ] && export srcdir=$buildroot/src/$pkgname
[ "$gitsource" != "" ] \
&& printf "$BBLUE**$BWHITE Downloading sources\n$RESET" \
&& git clone $gitsource $srcdir \
&& check $? "Download sources"
if [ "$source" != "" ]; then
printf "$BBLUE**$BWHITE Downloading sources\n$RESET"
[ ! -d $srcdir ] && mkdir -p $srcdir
cd $srcdir
[ ! -f $pkgname-$pkgver.tar.* ] && wget $source
cd -
cd -
fi
if [[ $(type -t configure) = function ]]; then
printf "$BBLUE**$BWHITE Configuring package\n$RESET"
@@ -457,20 +457,28 @@ buildpkg() {
#-----------------------------------------------------
#--- main ---
case "$1" in
add|a|del|d)
checkroot
"$1" $@
;;
query|q|s|sync|clear|ss|search|list|l|unpack|u|down|download|deps)
checkroot
"$1" $2
;;
build)
buildpkg $2
;;
*)
help
;;
esac
main() {
case "$1" in
add|a|del|d)
checkroot
"$1" $@
;;
s|sync|clear|unpack|u|down|download)
checkroot
"$1" $2
;;
query|q|search|ss|list|l|deps)
"$1" $2
;;
build)
checknonroot
buildpkg $2
;;
*)
help
;;
esac
}
#------------
main $@