From 28694f72962cdee5028991cd7ebe057fe84ec646 Mon Sep 17 00:00:00 2001 From: awalsh128 Date: Mon, 25 Jul 2022 22:30:31 -0700 Subject: [PATCH] Fix bug in issue #37 by combining install and dep listing reads. Ensures only installed deps are cached. --- install_and_cache_pkgs.sh | 64 +++++++++++++++------------------------ lib.sh | 9 +++--- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/install_and_cache_pkgs.sh b/install_and_cache_pkgs.sh index 1dd6122..c3ca77b 100755 --- a/install_and_cache_pkgs.sh +++ b/install_and_cache_pkgs.sh @@ -24,10 +24,14 @@ log "Clean installing and caching ${package_count} package(s)." log_empty_line +manifest_main="" log "Package list:" 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 +write_manifest "main" "${manifest_main:0:-1}" "${cache_dir}/manifest_main.log" log_empty_line @@ -42,52 +46,35 @@ manifest_main="" # Contains all packages including dependencies. manifest_all="" -log "Gathering install information for ${package_count} packages..." -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 +install_log_filepath="${cache_dir}/install.log" log "Clean installing ${package_count} packages..." # Zero interaction while installing or upgrading the system via apt. -#sudo DEBIAN_FRONTEND=noninteractive apt-fast --yes install ${normalized_packages} > /dev/null -sudo apt-fast --yes install ${normalized_packages} +sudo DEBIAN_FRONTEND=noninteractive apt-fast --yes install ${normalized_packages} > "${install_log_filepath}" log "done" +log "Installation log written to ${install_log_filepath}" log_empty_line -cached_package_count=$(wc -w <<< "${cached_packages}") -log "Caching ${cached_package_count} installed packages..." -for cached_package in ${cached_packages}; do - cache_filepath="${cache_dir}/${cached_package}.tar.gz" +installed_packages=$(get_installed_packages "${install_log_filepath}") +log "Installed package list:" +for installed_package in ${installed_packages}; do + 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 - read cached_package_name cached_package_ver < <(get_package_name_ver "${cached_package}") - log " * Caching ${cached_package_name} to ${cache_filepath}..." + read installed_package_name installed_package_ver < <(get_package_name_ver "${installed_package}") + log " * Caching ${installed_package_name} to ${cache_filepath}..." # Pipe all package files (no folders) to Tar. - dpkg -L "${cached_package_name}" | + dpkg -L "${installed_package_name}" | 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 done | @@ -96,11 +83,10 @@ for cached_package in ${cached_packages}; do fi # 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 log "done (total cache size $(du -h ${cache_dir} | tail -1 | awk '{print $1}'))" log_empty_line write_manifest "all" "${manifest_all}" "${cache_dir}/manifest_all.log" -write_manifest "main" "${manifest_main}" "${cache_dir}/manifest_main.log" diff --git a/lib.sh b/lib.sh index fbc1c80..103acfb 100755 --- a/lib.sh +++ b/lib.sh @@ -9,10 +9,11 @@ function normalize_package_list { 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. # : ... -function get_dep_packages { - local regex="^Inst ([^ ]+) (\[[^ ]+\]\s)?\(([^ ]+)" +function get_installed_packages { + install_log_filepath="${1}" + local regex="^Unpacking ([^ ]+) (\[[^ ]+\]\s)?\(([^ )]+)" dep_packages="" while read -r line; do if [[ "${line}" =~ ${regex} ]]; then @@ -21,7 +22,7 @@ function get_dep_packages { log_err "Unable to parse package name and version from \"$line\"" exit 2 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 echo "${dep_packages:0:-1}" # Removing trailing space. else