Fix bug in issue #37 by combining install and dep listing reads. Ensures only installed deps are cached.

This commit is contained in:
awalsh128 2022-07-25 22:30:31 -07:00
parent a239526b16
commit 28694f7296
2 changed files with 30 additions and 43 deletions

View file

@ -24,10 +24,14 @@ log "Clean installing and caching ${package_count} package(s)."
log_empty_line log_empty_line
manifest_main=""
log "Package list:" log "Package list:"
for package in ${normalized_packages}; do for package in ${normalized_packages}; do
log "- ${package}" read package_name package_ver < <(get_package_name_ver "${package}")
manifest_main="${manifest_main}${package_name}:${package_ver},"
log "- ${package_name}:${package_ver}"
done done
write_manifest "main" "${manifest_main:0:-1}" "${cache_dir}/manifest_main.log"
log_empty_line log_empty_line
@ -42,52 +46,35 @@ manifest_main=""
# Contains all packages including dependencies. # Contains all packages including dependencies.
manifest_all="" manifest_all=""
log "Gathering install information for ${package_count} packages..." install_log_filepath="${cache_dir}/install.log"
log_empty_line
cached_packages=""
for package in ${normalized_packages}; do
read package_name package_ver < <(get_package_name_ver "${package}")
# Comma delimited name:ver pairs in the main requested packages manifest.
manifest_main="${manifest_main}${package_name}:${package_ver},"
cached_packages="${cached_packages} ${package_name}:${package_ver}"
read dep_packages < <(get_dep_packages "${package_name}") || exit 2
cached_packages="${cached_packages} ${dep_packages}"
if test -z "${dep_packages}"; then
dep_packages_text="none";
else
dep_packages_text="${dep_packages}"
fi
log "- ${package_name}"
log " * Version: ${package_ver}"
log " * Dependencies: ${dep_packages_text}"
log_empty_line
done
log "done"
log_empty_line
log "Clean installing ${package_count} packages..." log "Clean installing ${package_count} packages..."
# Zero interaction while installing or upgrading the system via apt. # Zero interaction while installing or upgrading the system via apt.
#sudo DEBIAN_FRONTEND=noninteractive apt-fast --yes install ${normalized_packages} > /dev/null sudo DEBIAN_FRONTEND=noninteractive apt-fast --yes install ${normalized_packages} > "${install_log_filepath}"
sudo apt-fast --yes install ${normalized_packages}
log "done" log "done"
log "Installation log written to ${install_log_filepath}"
log_empty_line log_empty_line
cached_package_count=$(wc -w <<< "${cached_packages}") installed_packages=$(get_installed_packages "${install_log_filepath}")
log "Caching ${cached_package_count} installed packages..." log "Installed package list:"
for cached_package in ${cached_packages}; do for installed_package in ${installed_packages}; do
cache_filepath="${cache_dir}/${cached_package}.tar.gz" log "- ${installed_package}"
done
log_empty_line
installed_package_count=$(wc -w <<< "${installed_packages}")
log "Caching ${installed_package_count} installed packages..."
for installed_package in ${installed_packages}; do
cache_filepath="${cache_dir}/${installed_package}.tar.gz"
# Sanity test in case APT enumerates duplicates.
if test ! -f "${cache_filepath}"; then if test ! -f "${cache_filepath}"; then
read cached_package_name cached_package_ver < <(get_package_name_ver "${cached_package}") read installed_package_name installed_package_ver < <(get_package_name_ver "${installed_package}")
log " * Caching ${cached_package_name} to ${cache_filepath}..." log " * Caching ${installed_package_name} to ${cache_filepath}..."
# Pipe all package files (no folders) to Tar. # Pipe all package files (no folders) to Tar.
dpkg -L "${cached_package_name}" | dpkg -L "${installed_package_name}" |
while IFS= read -r f; do while IFS= read -r f; do
if test -f $f || test -L $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows if test -f $f || test -L $f; then echo "${f:1}"; fi; #${f:1} removes the leading slash that Tar disallows
done | done |
@ -96,11 +83,10 @@ for cached_package in ${cached_packages}; do
fi fi
# Comma delimited name:ver pairs in the all packages manifest. # Comma delimited name:ver pairs in the all packages manifest.
manifest_all="${manifest_all}${cached_package_name}:${cached_package_ver}," manifest_all="${manifest_all}${installed_package_name}:${installed_package_ver},"
done done
log "done (total cache size $(du -h ${cache_dir} | tail -1 | awk '{print $1}'))" log "done (total cache size $(du -h ${cache_dir} | tail -1 | awk '{print $1}'))"
log_empty_line log_empty_line
write_manifest "all" "${manifest_all}" "${cache_dir}/manifest_all.log" write_manifest "all" "${manifest_all}" "${cache_dir}/manifest_all.log"
write_manifest "main" "${manifest_main}" "${cache_dir}/manifest_main.log"

9
lib.sh
View file

@ -9,10 +9,11 @@ function normalize_package_list {
echo "${sorted}" echo "${sorted}"
} }
# Gets a package list of dependencies as space delimited pairs with each pair colon delimited. # Gets a list of installed packages as space delimited pairs with each pair colon delimited.
# <name>:<version> <name:version>... # <name>:<version> <name:version>...
function get_dep_packages { function get_installed_packages {
local regex="^Inst ([^ ]+) (\[[^ ]+\]\s)?\(([^ ]+)" install_log_filepath="${1}"
local regex="^Unpacking ([^ ]+) (\[[^ ]+\]\s)?\(([^ )]+)"
dep_packages="" dep_packages=""
while read -r line; do while read -r line; do
if [[ "${line}" =~ ${regex} ]]; then if [[ "${line}" =~ ${regex} ]]; then
@ -21,7 +22,7 @@ function get_dep_packages {
log_err "Unable to parse package name and version from \"$line\"" log_err "Unable to parse package name and version from \"$line\""
exit 2 exit 2
fi fi
done < <(apt-fast install --dry-run --yes "${1}" | grep "^Inst" | grep -v "^Inst ${1} " | sort) done < <(grep "^Unpacking " ${install_log_filepath})
if test -n "${dep_packages}"; then if test -n "${dep_packages}"; then
echo "${dep_packages:0:-1}" # Removing trailing space. echo "${dep_packages:0:-1}" # Removing trailing space.
else else