Skip to content

Commit

Permalink
api: anything_installed_from_uri_suite_component multiple factor of…
Browse files Browse the repository at this point in the history
… magnitude speedup

instead of checking the apt-cache policy on each package individually, check all installed packages that exist in a repo at once. this results in a multiple factor of magnitude speedup on large repositories (eg: debian/ubuntu main repositories) from minutes to seconds
  • Loading branch information
theofficialgman committed Feb 24, 2024
1 parent 89a0e98 commit ced0073
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions api
Original file line number Diff line number Diff line change
Expand Up @@ -246,29 +246,21 @@ anything_installed_from_uri_suite_component() { #Given an apt repository uri, su
local repofile
for repofile in $repofiles ;do
#search the repo-file for installed packages

local packages_in_repo="$(grep '^Package' "$repofile" | awk '{print $2}' | sort)"
local installed_packages="$(apt list --installed 2>/dev/null | tail -n +2 | awk -F/ '{print $1}')"
local apt_cache_policy_output="$(echo "$packages_in_repo" | list_intersect "$installed_packages" | tr '\n' ' ' | xargs -r apt-cache policy)"

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
if [ -z "$3" ]; then
if echo "$apt_cache_policy_output" | grep -B1 "$(echo "$1" | sed 's+.*://++g' | sed "s,/$,,") $2" | awk '{print $1}' | grep -Fq '***' ;then
found=1
break
fi
else
if echo "$apt_cache_policy_output" | grep -B1 "$(echo "$1" | sed 's+.*://++g' | sed "s,/$,,") $2/$3" | awk '{print $1}' | grep -Fq '***' ;then
found=1
break
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

Expand Down

0 comments on commit ced0073

Please sign in to comment.