diff --git a/bin/compile b/bin/compile index af544c2..f63aa87 100755 --- a/bin/compile +++ b/bin/compile @@ -78,6 +78,8 @@ APT_OPTIONS="$APT_OPTIONS -o dir::etc::sourcelist=$APT_SOURCES -o dir::etc::sour topic "Updating apt caches" apt-get $APT_OPTIONS update | indent +PACKAGES="$CACHE_DIR/package-list" + for PACKAGE in $(cat $BUILD_DIR/Aptfile | grep -v -s -e '^#' | grep -v -s -e "^:repo:"); do if [[ $PACKAGE == *deb ]]; then PACKAGE_NAME=$(basename $PACKAGE .deb) @@ -85,12 +87,32 @@ for PACKAGE in $(cat $BUILD_DIR/Aptfile | grep -v -s -e '^#' | grep -v -s -e "^: topic "Fetching $PACKAGE" curl -s -L -z $PACKAGE_FILE -o $PACKAGE_FILE $PACKAGE 2>&1 | indent + echo $PACKAGE_NAME >> $PACKAGES else topic "Fetching .debs for $PACKAGE" apt-get $APT_OPTIONS -y $APT_FORCE_YES -d install --reinstall $PACKAGE | indent + + # Changes the archives location to ensure the package urls are printed. + # Nothing is downloaded/updated here so the archive location can be set to anywhere + # other than the actual archives location without issue. + apt-get $APT_OPTIONS -y $APT_FORCE_YES -o dir::cache::archives=$APT_CACHE_DIR \ + install --print-uris --reinstall $PACKAGE | \ + grep ^\' | awk '{print $2}' >> $PACKAGES fi done +# Removes duplicate file names +cat $PACKAGES | awk '!seen[$0]++' > "$PACKAGES-clean" +mv "$PACKAGES-clean" $PACKAGES + +# Remove outdated and leftover packages +topic "Cleaning up outdated and leftover packages" +find $APT_CACHE_DIR/archives -type f -name "*.deb" | \ + grep -vFf $PACKAGES | xargs --no-run-if-empty rm + +# Remove the packages list file so we have a clean file for subsequent runs +rm $PACKAGES + mkdir -p $BUILD_DIR/.apt for DEB in $(ls -1 $APT_CACHE_DIR/archives/*.deb); do diff --git a/test/compile_test.sh b/test/compile_test.sh index f5c4a40..9add8bd 100644 --- a/test/compile_test.sh +++ b/test/compile_test.sh @@ -21,7 +21,7 @@ testStackChange() { #Set the cached STACK value to a non-existent stack, so it is guaranteed to change. mkdir -p "$CACHE_DIR/.apt/" echo "cedar-10" > "$CACHE_DIR/.apt/STACK" - + #Load the Aptfile into the cache, to exclusively test the stack changes mkdir -p "$CACHE_DIR/apt/cache" cp $BUILD_DIR/Aptfile "$CACHE_DIR/apt/cache" @@ -56,4 +56,18 @@ testStackCached() { loadFixture() { cp -a $BUILDPACK_HOME/test/fixtures/$1/. ${BUILD_DIR} -} \ No newline at end of file +} + +testOutdatedPackageRemoval() { + loadFixture "Aptfile" + + fake_file="$CACHE_DIR/apt/cache/archives/wget_fake_old_version.deb" + # Create a fake old version of wget to test that it gets removed + mkdir -p "$CACHE_DIR/apt/cache/archives" + touch "${fake_file}" + + compile + + assertCaptured "Cleaning up outdated and leftover packages" + assertTrue 'Outdated packages removed' "[ ! -f ${fake_file} ]" +}