summaryrefslogtreecommitdiff
path: root/src/chroot-tools/chcleanup
blob: 9ad121e2d97b97f624ba67da89320c24921a8168 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
set -eE
# Copyright (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}

################################################################################
# Define these here to avoid having dependencies on outside files

if type gettext &>/dev/null; then
	_() { gettext "$@"; }
else
	_() { echo "$@"; }
fi

plain() {
	local mesg="$(_ "$1")"; shift
	printf "${BOLD}    ${mesg}${ALL_OFF}\n" "$@" >&2
}

msg() {
	local mesg="$(_ "$1")"; shift
	printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}

msg2() {
	local mesg="$(_ "$1")"; shift
	printf "${BLUE}  ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}

warning() {
	local mesg="$(_ "$1")"; shift
	printf "${YELLOW}==> $(gettext "WARNING:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}

error() {
	local mesg="$(_ "$1")"; shift
	printf "${RED}==> $(gettext "ERROR:")${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" >&2
}

################################################################################

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
	export CARCH="$(. /etc/makepkg.conf; printf '%s' "$CARCH")"
	source ./PKGBUILD
	CHROOTEXTRAPKG+=("${depends[@]}"
	                 "${makedepends[@]}"
	                 "${checkdepends[@]}")
fi

msg "Cleaning chroot..."

# Sync the local repo with pacman
cp /repo/repo.db /var/lib/pacman/sync/repo.db

# Setup the temporary directory
TEMPDIR="$(mktemp --tmpdir -d $(basename $0).XXXXX)"
trap "rm -rf '$TEMPDIR'" EXIT

cp -a /var/lib/pacman/sync "${TEMPDIR}/"
pkglist="${TEMPDIR}"/pkglist.txt

# Get the full list of packages needed by dependencies, including the base system
msg2 "Creating a full list of packages..."
pacman -b "${TEMPDIR}" \
       -Sp --print-format "%n" base-devel "${CHROOTEXTRAPKG[@]}" >"$pkglist" || {
	ret=$?
	error "Could not create a full list of packages, exiting."
	plain "This is likely caused by a dependency that could not be found."
	exit $ret
}

# Diff installed packages against a clean chroot then remove leftovers
packages=($(comm -23 <(pacman -Qq | sort -u) \
                     <(sort -u "${pkglist}")))

if [[ ${#packages[@]} = 0 ]]; then
	msg2 "No packages to remove"
else
	msg2 "Removing %d packages" ${#packages[@]}

	if ${DRYRUN}; then
		echo "${packages[*]}"
	else
		# Only remove leftovers, -Rcs removes too much
		pacman --noconfirm -Rn "${packages[@]}"
	fi
fi