#!/bin/bash # Copyright 2012 Nicolás Reynolds Licensed under GPLv3 # # Smart cleanup of the chroot, restores chroot to its original state but also # leaves the dependencies already installed that are needed by the current # package. In other words, it removes everything that's left. # # Useful when you're building a lot of packages one after another and they # share some dependencies. # # Logic: tap into `makepkg -sr`, collect required packages and remove the # leftovers # # Use it as the PACMAN var for makepkg: `PACMAN=$0 makepkg` set -e set -x cleanup_log=/tmp/libretools-cleanup.log cmd="$1"; shift # make -Rn respect PACMAN_OPTS flags=($(echo "$@" | grep -o "\-\-no\(confirm\|progressbar\)" || true)) args="$@" case $cmd in # Collect the packages that are going to be installed, but use a clean database # to get the full needed list. # See update-cleansystem -T) # Use sudo because $0 is run as normal user # TODO -Sy only once sudo pacman -b "${BD:-/var/lib/libretools/clean}" -Sy >/dev/null 2>&1 sudo pacman -b "${BD:-/var/lib/libretools/clean}" \ -Sp \ --print-format "%n" \ ${args[@]} >>${cleanup_log} 2>/dev/null ;; # Diff against previously installed packages and remove the unneeded ones # # We don't collect during -S because we never get here if depencies are met # during -T -S) cleanup=($(comm -23 \ <(pacman -Qq | sort) \ <(cat /etc/libretools.d/cleansystem ${cleanup_log} | sort -u) )) if [ ${#cleanup[@]} -gt 0 ]; then # At this point $0 is run as root pacman -Rn ${flags[@]} ${cleanup[@]} fi # Remove the cleanup log at the end rm ${cleanup_log} ;; # DON'T LET MAKEPKG DO REMOVALS OF ITS OWN -R) exit 0;; esac # Make makepkg dreams come true pacman $cmd ${args[@]}