Skip to content

Commit

Permalink
api: add anything_installed_from_uri_suite_component to fully defin…
Browse files Browse the repository at this point in the history
…ed apt repository for installed packages
  • Loading branch information
theofficialgman committed Feb 24, 2024
1 parent 0292b19 commit aede2fa
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions api
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,62 @@ anything_installed_from_repo() { #Given an apt repository URL, determine if any
fi
}

anything_installed_from_uri_suite_component() { #Given an apt repository uri, suite, and component, determine if any packages from it are currently installed
[ -z "$1" ] && error "anything_installed_from_uri_suite_component: A repository uri must be specified."
[ -z "$2" ] && error "anything_installed_from_uri_suite_component: A repository suite must be specified."

#component is an optional specification
if [ -z "$3" ]; then
local filepath="/var/lib/apt/lists/$(echo "$1" | sed 's+.*://++g' | sed "s,/$,," | tr '/' '_')_$(echo "$2" | sed "s,/$,," | tr '/' '_')_"
else
local filepath="/var/lib/apt/lists/$(echo "$1" | sed 's+.*://++g' | sed "s,/$,," | tr '/' '_')_dists_$(echo "$2" | sed "s,/$,," | tr '/' '_')_"
fi
debug $filepath

#find all relevant package-lists
local repofiles="$(ls $filepath*Packages)"
debug "$repofiles"

#for every repo-file, check if any of them have an installed file
local found=0
local IFS=$'\n'
local repofile
for repofile in $repofiles ;do
#search the repo-file for installed packages

grep '^Package' "$repofile" | awk '{print $2}' | while read -r package ;do
if package_installed "$package" ;then
#this package is installed; check if the version available on this repo is the current version (prevents false positives package being installed from another uri/suite/component)
#component is an optional specification
if [ -z "$3" ]; then
if apt-cache policy "$package" | grep -B1 "$(echo "$1" | sed 's+.*://++g' | sed "s,/$,,") $2" | head -n1 | awk '{print $1}' | grep -Fq '***' ;then
echo "Package installed: $package"
exit 1
fi
else
if apt-cache policy "$package" | grep -B1 "$(echo "$1" | sed 's+.*://++g' | sed "s,/$,,") $2/$3" | head -n1 | awk '{print $1}' | grep -Fq '***' ;then
echo "Package installed: $package"
exit 1
fi
fi
fi
done #if exit code is 1, search was successful. If exit code is 0, no packages from the repo were installed.

found=$?

if [ $found == 1 ];then
break
fi
done

#return an exit code
if [ $found == 1 ];then
return 0
else
return 1
fi
}

remove_repofile_if_unused() { #Given a sources.list.d file, delete it if nothing from that repository is currently installed. Deletion skipped if $2 is 'test'
local file="$1"
local testmode="$2"
Expand Down

0 comments on commit aede2fa

Please sign in to comment.