summaryrefslogtreecommitdiff
path: root/src/chroot-tools/chcleanup
diff options
context:
space:
mode:
Diffstat (limited to 'src/chroot-tools/chcleanup')
-rwxr-xr-xsrc/chroot-tools/chcleanup57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/chroot-tools/chcleanup b/src/chroot-tools/chcleanup
new file mode 100755
index 0000000..821c572
--- /dev/null
+++ b/src/chroot-tools/chcleanup
@@ -0,0 +1,57 @@
+#!/bin/bash -eE
+# (c) Nicolás Reynolds <fauno@parabola.nu>
+# Released under GPLv3
+#
+# Performs chroot cleanup smartly, it only removes the unneeded packages or
+# leaves you with a cleansystem
+#
+# See: HOOKPREBUILD
+
+DRYRUN=${DRYRUN:-false}
+source "$(which libremessages)"
+
+if [[ ! -f /.arch-chroot ]] && ! ${DRYRUN}; then
+ error "(chcleanup): Must be run inside of a chroot"
+ exit 1
+fi
+
+source /etc/libretools.d/chroot.conf
+# If we're running makepkg
+if [ -f PKGBUILD ]; then
+ source PKGBUILD
+ CHROOTEXTRAPKG+=("${depends[@]}"
+ "${makedepends[@]}"
+ "${checkdepends[@]}")
+fi
+
+msg "Cleaning chroot..."
+
+TEMPDIR="$(mktemp --tmpdir -d $(basename $0).XXXXX)"
+cp -a /var/lib/pacman/sync "${TEMPDIR}/"
+cleanup_log="${TEMPDIR}"/libretools-cleanup.log
+
+# Get the full list of packages needed by dependencies, including the base system
+pacman -b "${TEMPDIR}" \
+ -Sp --print-format "%n" \
+ base base-devel sudo "${CHROOTEXTRAPKG[@]}" \
+ >"${cleanup_log}"
+
+# Diff installed packages against a clean chroot then remove leftovers
+packages=($(comm -23 <(pacman -Qq | sort -u) \
+ <(sort -u "${cleanup_log}")))
+
+RET=0
+if [[ ${#packages[@]} != 0 ]]; then
+ msg2 "Removing %d packages" ${#packages[@]}
+
+ if ${DRYRUN}; then
+ echo "${packages[@]}"
+ else
+ # Only remove leftovers, -Rcs removes too much
+ pacman --noconfirm -Rn "${packages[@]}" || RET=$?
+ fi
+fi
+# Cleanup
+rm -rf "${TEMPDIR}"
+
+exit $RET