From 291761f074b1a388dd4ad0880857d184ac6d8292 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Mon, 29 Sep 2025 19:08:54 +0700 Subject: [PATCH 1/3] added parse_opts parse_command and updated all funcs --- src/pm | 360 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 225 insertions(+), 135 deletions(-) diff --git a/src/pm b/src/pm index 2645cd9..c07fa7a 100755 --- a/src/pm +++ b/src/pm @@ -19,6 +19,9 @@ packagesqueryfile=$rootdir/usr/share/pm/query reposquerydir=$rootdir/usr/share/pm/repos cachedpkgdir=$rootdir/var/cache/pm/pkg installedpackagesdir=$rootdir/usr/share/pm/packages +command="" +packages="" +archives="" #----------------------------------------------------- #----- colors ------- @@ -59,136 +62,137 @@ checknonroot() { #----------------- add(install) package function ------------------------ add() { - shift 1 - [[ "$1" == "" ]] \ + [[ "$packages" == "" ]] \ && help \ && exit 0 - for i in $@; do - down $i - done - for i in $@; do - unpack $cachedpkgdir/$i*.pkg.gz + down + for i in $packages; do + archives="$archives $(echo $cachedpkgdir/$i*.pkg.gz)" done + unpack } a() { - add $@ + add } #------------------------------------------------------------------------ #-------------------- download package without install function --------- down() { - [[ ! $1 ]] \ + [[ "$packages" == "" ]] \ && help \ && exit 0 - servers=$(/usr/bin/cat $repofile) - check $? "Read repositories" - for i in $servers; do - serverdir=$(echo $i | sed -e "/http:\/\//s///") - serverdir=$(echo $serverdir | sed -e "/ftp:\/\//s///") - repoquery=$(/usr/bin/cat $reposquerydir/$serverdir/query | sed -e '/.pkg.gz/s///') - for j in $repoquery; do - # 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/$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 - # printf "$YELLOW**$RED error:$RESET can't download package\n" - # exit 1 - # fi - else - printf "$YELLOW**$GREEN successful downloaded $1\n$RESET" - return + for k in $packages; do + downloaded=n + servers=$(/usr/bin/cat $repofile) + check $? "Read repositories" + for i in $servers; do + serverdir=$(echo $i | sed -e "/http:\/\//s///") + 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 [[ "$(get_pkgname $j)" == "$k" ]]; then + if [ ! -f $cachedpkgdir/$j.pkg.gz ]; then + printf "$YELLOW**$RESET Downloading $YELLOW$serverdir: $Bwhite$j\n$RESET" + echo $i/$j.pkg.gz + wget $i/$j.pkg.gz -P $cachedpkgdir + check $? "Download package" + else + printf "$YELLOW**$GREEN successful downloaded $k\n$RESET" + downloaded=y + fi + downloaded=y fi - return - fi + done done + if [ "$downloaded" == "n" ]; then + printf "$BBLUE**$BWHITE Can't find package $k [$BRED ERROR $BWHITE]\n$RESET" + fi + continue done - printf "$BBLUE**$BWHITE Can't find package $1 [$BRED ERROR $BWHITE]\n$RESET" - exit 1 } download() { - down $1 + down } #------------------------------------------------------------------------ #-------------------- unpack local package archive ---------------------- unpack() { - [[ "$1" == "" ]] \ + [[ "$archives" == "" ]] \ && 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" - [ ! -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 - [ "$i" = "${packagename%.pkg*}" ] \ - && export installed=true \ - && break - done - [[ "$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 - have=false - for pack in $(/usr/bin/cat $packagesqueryfile); do - if [ $pack = $dep ]; then - have=yes - break + for i in $archives; do + if [ ! -f $i ]; then + printf "$BBLUE**$BWHITE File $i not found [$BRED ERROR $BWHITE]\n$RESET" \ + && exit 1 + fi + packagename=$(echo $i | sed -e "/.gz/s///") + packagename=${packagename#*pkg/} + printf "$YELLOW**$GREEN Installing ${packagename%.pkg*}\n$RESET" + [ ! -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 + [ "$i" = "${packagename%.pkg*}" ] \ + && export installed=true \ + && break + done + [[ "$installed" == "true" ]] \ + && printf "$YELLOW**$RESET Package already installed:$YELLOW reinstalling\n$RESET" + tar -xf ${i}.pkg.gz -C $rootdir/tmp + 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 + have=false + for pack in $(/usr/bin/cat $packagesqueryfile); do + if [ $pack = $dep ]; then + have=yes + break + fi + done + if [ "$have" = "false" ]; then + pkgnow=$packagename + add "" $dep + unpack $cachedpkgdir/$pkgnow.gz + return fi done - if [ "$have" = "false" ]; then - pkgnow=$packagename - add "" $dep - unpack $cachedpkgdir/$pkgnow.gz - return - fi - done + mkdir $installedpackagesdir/$packagename &> /dev/null + mv -v deps $installedpackagesdir/$packagename &> /dev/null + fi mkdir $installedpackagesdir/$packagename &> /dev/null - mv -v deps $installedpackagesdir/$packagename &> /dev/null - 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/ - [ -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" + 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/ + [ -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" + cd - &> /dev/null + done } u() { - unpack $1 + unpack } #------------------------------------------------------------------------ #------------------ delete package function ----------------------------- del() { printf "$YELLOW**$GREEN deleting\n$RESET" - shift 1 - for i in $@; do + for i in $packages; do printf "$YELLOW**$GREEN deleting $i\n$RESET" [ ! -f $installedpackagesdir/$i*.pkg/list ] \ && printf "$BBLUE**$BWHITE package not installed ($i) [$BRED ERROR $BWHITE]\n$RESET" \ @@ -213,31 +217,46 @@ del() { done } d() { - del $@ + del } #------------------------------------------------------------------------ #------------------- list files in package function --------------------- list() { - if [[ "$1" == "" ]]; then + if [[ "$packages" == "" ]]; then for i in $(/usr/bin/cat $packagesqueryfile); do - filelist=$(/usr/bin/cat $installedpackagesdir/$i*.pkg/list) + cd $installedpackagesdir + pkgname=$(echo $(find . -name "$i*") | sed -e "/.\//s///" | sed -e "/.pkg/s///") + cd - &>/dev/null + if [[ "$(/usr/bin/cat $packagesqueryfile | grep $i)" == "" ]]; then + printf "$BBLUE**$BWHITE Package "$i" is not installed [$BRED ERROR $BWHITE]\n$RESET" + continue + fi + filelist=$(/usr/bin/cat $installedpackagesdir/$pkgname*.pkg/list) for j in $filelist; do - printf "$YELLOW$i: $BWHITE$j$RESET\n" + printf "$YELLOW$(get_pkgname $pkgname) $BGREEN$(get_pkgver $pkgname)-$(get_pkgrel $pkgname): $BWHITE$j$RESET\n" done + unset pkgname done exit 0 fi - [[ "$(/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" + for i in $packages; do + cd $installedpackagesdir + pkgname=$(echo $(find . -name "$i*") | sed -e "/.\//s///" | sed -e "/.pkg/s///") + cd - &>/dev/null + if [[ "$(/usr/bin/cat $packagesqueryfile | grep $i)" == "" ]]; then + printf "$BBLUE**$BWHITE Package "$i" is not installed [$BRED ERROR $BWHITE]\n$RESET" + continue + fi + filelist=$(/usr/bin/cat $installedpackagesdir/"${pkgname}".pkg/list) + for j in $filelist; do + printf "$YELLOW$(get_pkgname $pkgname) $BGREEN$(get_pkgver $pkgname)-$(get_pkgrel $pkgname): $BWHITE$j$RESET\n" + done + unset pkgname done } l() { - list $1 + list } #------------------------------------------------------------------------ get_pkgrel() { @@ -319,7 +338,6 @@ search() { for j in $repoquery; do installed=false for inst in $(/usr/bin/cat $packagesqueryfile); do - # echo $inst [ "$(get_pkgname $inst)" = "$(get_pkgname $j)" ] \ && export installed=true \ && break @@ -359,14 +377,40 @@ clear() { #--- show package dependencies function --- deps() { - [ "$1" = "" ] \ - && help \ - && exit 1 - [ ! -f $installedpackagesdir/$1*.pkg/deps ] \ - && printf "$YELLOW**$BWHITE No deps\n$RESET" \ - && exit 0 - for i in $(cat $installedpackagesdir/$1*.pkg/deps); do - printpkg $i + if [[ "$packages" == "" ]]; then + for i in $(/usr/bin/cat $packagesqueryfile); do + cd $installedpackagesdir + pkgname=$(echo $(find . -name "$i*") | sed -e "/.\//s///" | sed -e "/.pkg/s///") + cd - &>/dev/null + printf "$YELLOW$(get_pkgname $pkgname) $BGREEN$(get_pkgver $pkgname)-$(get_pkgrel $pkgname)$BWHITE deps: \n$RESET" + [ ! -f $installedpackagesdir/${pkgname}.pkg/deps ] \ + && printf "\t$YELLOW**$BWHITE No deps\n$RESET" \ + && continue + for j in $(cat $installedpackagesdir/${pkgname}.pkg/deps); do + printf "\t" + printpkg $j + done + unset pkgname + done + exit 0 + fi + for i in $packages; do + cd $installedpackagesdir + pkgname=$(echo $(find . -name "$i*") | sed -e "/.\//s///" | sed -e "/.pkg/s///") + cd - &>/dev/null + if [ "$pkgname" = "" ]; then + printf "$BBLUE**$BWHITE Package $i not found [$BRED ERROR $BWHITE]\n$RESET" + continue + fi + printf "$YELLOW$(get_pkgname $pkgname) $BGREEN$(get_pkgver $pkgname)-$(get_pkgrel $pkgname)$BWHITE deps: \n$RESET" + [ ! -f $installedpackagesdir/${pkgname}.pkg/deps ] \ + && printf "\t$YELLOW**$BWHITE No deps\n$RESET" \ + && continue + for j in $(cat $installedpackagesdir/${pkgname}.pkg/deps); do + printf "\t" + printpkg $j + done + unset pkgname done } #------------------------------------------ @@ -461,29 +505,75 @@ buildpkg() { unset pkgdir srcdir buildroot } #----------------------------------------------------- +parse_command() { + while [ $1 ]; do + case "$1" in + add|a|del|d) + command=$1 + shift + packages=$@ + break; + ;; + s|sync|clear|down|download) + command=$1 + shift + packages=$@ + break; + ;; + unpack|u) + command=$1 + shift + archives=$@ + break; + ;; + query|q|search|ss|list|l|deps) + command=$1 + shift + packages=$@ + break + ;; + build) + checknonroot + buildpkg $@ + ;; + *) + shift + ;; + esac + done +} +parse_opts() { + while [ $1 ]; do + case "$1" in + -h|--help) + help + exit 0 + ;; + -r|--rootdir) + rootdir=$2 + shift 2 + ;; + *) + if [ "${1#--}" != "$1" ]; then + echo "Invalid option: $1" >&2 + help + exit 1 + fi + shift + ;; + esac + done +} #--- main --- 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 + parse_command "$@" + parse_opts "$@" + if [ -z "$command" ]; then + help + exit 1 + fi + $command } #------------ From 0c7652fe09d2fed67838decbdd00dc234621e78f Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Mon, 29 Sep 2025 19:15:02 +0700 Subject: [PATCH 2/3] fix unpack error --- src/pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pm b/src/pm index c07fa7a..f687968 100755 --- a/src/pm +++ b/src/pm @@ -139,7 +139,7 @@ unpack() { done [[ "$installed" == "true" ]] \ && printf "$YELLOW**$RESET Package already installed:$YELLOW reinstalling\n$RESET" - tar -xf ${i}.pkg.gz -C $rootdir/tmp + tar -xf $cachedpkgdir/$packagename.gz -C $rootdir/tmp cd $rootdir/tmp/$packagename [ -f pre_install ] \ && /usr/bin/sh -c ./pre_install \ From 950f6bc0b3e4c29c0536b5ec606125b749cf8499 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Mon, 29 Sep 2025 19:51:38 +0700 Subject: [PATCH 3/3] added upgrade up and c clean clear --- src/pm | 110 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 28 deletions(-) diff --git a/src/pm b/src/pm index f687968..d10c818 100755 --- a/src/pm +++ b/src/pm @@ -293,20 +293,22 @@ q() { #--------------------- show help function ------------------------------- help() { printf " - $YELLOW**$GREEN pm usage$RESET : - $BWHITE add, a [package name] - download and install from repository - unpack, u [/path/to/package/archive] - unpack(install) from local package archive - down, download [package name] - download package from repository without install - del, d [package name] - delete package - list, l [package name] - list files in package - search, ss [package name] - search package in repository - query, q - show installed packages - help - this help message - clear - clear all package archives from cached packages directory - sync, s - synchronize repository - deps [package name] - show package dependencies - build - build package (./ must contain PMBUILD) - \n$RESET" +$YELLOW**$GREEN pm usage$RESET: + ${BWHITE}add, a [package name] - download and install from repository + unpack, u [/path/to/package/archive] - unpack(install) from local package archive + down, download [package name] - download package from repository without install + up, upgrade - upgrade packages + del, d [package name] - delete package + list, l [package name] - list files in package + search, ss [package name] - search package in repository + query, q - show installed packages + help - this help message + clear, c - clear all package archives from cached packages directory + sync, s - synchronize repository + deps [package name] - show package dependencies + build - build package (./ must contain PMBUILD) + index, i - scan directory with packages and print packages in terminal (to create repository query use pm index > query) +$RESET" } #------------------------------------------------------------------------ @@ -330,7 +332,7 @@ s() { #----------------- search package in repo function ---------------------- search() { servers=$(/usr/bin/cat $repofile) - if [[ ! $1 ]]; then + if [[ "$packages" == "" ]]; then for i in $servers; do serverdir=$(echo $i | sed -e "/http:\/\//s///") serverdir=$(echo $serverdir | sed -e "/ftp:\/\//s///") @@ -339,6 +341,8 @@ search() { installed=false for inst in $(/usr/bin/cat $packagesqueryfile); do [ "$(get_pkgname $inst)" = "$(get_pkgname $j)" ] \ + && [ "$(get_pkgver $inst)" = "$(get_pkgver $j)" ] \ + && [ "$(get_pkgrel $inst)" = "$(get_pkgrel $j)" ] \ && export installed=true \ && break done @@ -350,22 +354,24 @@ search() { done exit 0 fi - found=false - for i in $servers; do - serverdir=$(echo $i | sed -e "/http:\/\//s///") - 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 - printf "$YELLOW$serverdir: $BWHITE$j\n$RESET" - found=true - fi + for k in $packages; do + found=false + for i in $servers; do + serverdir=$(echo $i | sed -e "/http:\/\//s///") + 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 =~ $k ]]; then + printf "$YELLOW$serverdir: $BWHITE$j\n$RESET" + found=true + fi + done done + [ "$found" = "false" ] && printf "$YELLOW**$RED error:$RESET can't find package ($k)\n" done - [ "$found" = "false" ] && printf "$YELLOW**$RED error:$RESET can't find package ($1)\n" } ss() { - search $1 + search } #------------------------------------------------------------------------ @@ -373,6 +379,12 @@ ss() { clear() { rm -v $cachedpkgdir/* } +clean() { + clear +} +c() { + clear +} #------------------------------------------------------------------------ #--- show package dependencies function --- @@ -505,6 +517,48 @@ buildpkg() { unset pkgdir srcdir buildroot } #----------------------------------------------------- + +# upgrade system packages function +upgrade() { + total_updates=0 + for i in $(cat $packagesqueryfile); do + package=$(search $(get_pkgname $i) | awk -F' ' '{ for (i=2; i<=NF; i++) { print $i } }') + repo=$(search $(get_pkgname $i) | awk -F' ' '{ print $1 }') + for j in $package; do + if [ $(get_pkgrel $j) -gt $(get_pkgrel $i) ]; then + printf "${BRED}old: $(printpkg $i)" + printf "${BGREEN}new: $(printpkg $j)" + printf "${BGREEN}from: $(printf $repo | sed 's/://g')\n$RESET" + total_updates=$((total_updates+1)) + continue + fi + instver=$(echo $(get_pkgver $i) | awk -F'.' '{ for (i=1; i<=NF; i++) printf $i }') + repover=$(echo $(get_pkgver $j) | awk -F'.' '{ for (i=1; i<=NF; i++) printf $i }') + if [ $repover -gt $instver ]; then + printf "${BRED}old: $(printpkg $i)" + printf "${BGREEN}new: $(printpkg $j)" + printf "${BGREEN}from: $(printf $repo | sed 's/://g')\n$RESET" + total_updates=$((total_updates+1)) + continue + fi + done + done + printf "${YELLOW}Total updates: ${BWHITE}$total_updates\n$RESET" + return +} +up() { + upgrade +} + +# index packages in directory and create query file for create pm repository +index() { + for i in $(ls | grep .pkg.gz);do echo $i | sed 's/.pkg.gz//g';done +} +i() { + index +} +#--------------------------------------------------------------------------- + parse_command() { while [ $1 ]; do case "$1" in @@ -520,7 +574,7 @@ parse_command() { packages=$@ break; ;; - unpack|u) + unpack|u|up|upgrade|i|index) command=$1 shift archives=$@