epm-autoremove 6.4 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2017, 2018  Etersoft
# Copyright (C) 2012, 2017, 2018  Vitaly Lipatov <lav@etersoft.ru>
5
#
6 7 8
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
9 10 11 12 13
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16 17
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18 19
#

20 21 22 23 24 25 26 27 28 29 30 31 32 33
load_helper epm-check_updated_repo

__epm_print_excluded()
{
	local pkgs="$1"
	local fullpkgs="$2"
	local excluded
	excluded="$(estrlist exclude "$pkgs" "$fullpkgs")"
	if [ -n "$excluded" ] ; then
		echo "Skipped manually installed:"
		estrlist union $excluded
	fi
}

34
__epm_autoremove_altrpm_pp()
35
{
36
	local pkgs fullpkgs
37 38 39 40 41

	info "Removing unused python/perl modules..."
	#[ -n "$force" ] || info "You can run with --force for more deep removing"
	local force=force

42 43
	local libexclude="$1"

44 45 46
	local flag=

	[ -n "$force" ] || libexclude=$libexclude'[^-]*$'
47

48
	showcmd "apt-cache list-nodeps | grep -E -- \"$libexclude\""
49 50
	fullpkgs=$(apt-cache list-nodeps | grep -E -- "$libexclude" )
	pkgs=$(skip_manually_installed $fullpkgs)
51 52 53 54

	if [ -n "$dryrun" ] ; then
		info "Packages for autoremoving:"
		echo "$pkgs"
55
		__epm_print_excluded "$pkgs" "$fullpkgs"
56 57
		return 0
	fi
58 59 60

	__epm_print_excluded "$pkgs" "$fullpkgs"

61 62 63 64 65
	[ -n "$pkgs" ] && sudocmd rpm -v -e $pkgs && flag=1

	if [ -n "$flag" ] ; then
		info ""
		info "call again for next cycle until all modules will be removed"
66
		__epm_autoremove_altrpm_pp "$libexclude"
67 68 69 70 71 72 73
	fi

	return 0
}

__epm_autoremove_altrpm_lib()
{
74
	local pkgs fullpkgs
75

76 77
	local nodevel="$1"

78
	info
79 80 81 82 83 84 85
	if [ "$nodevel" = "nodevel" ] ; then
		info "Removing all non -devel/-debuginfo libs packages not need by anything..."
		local develrule='-(devel|devel-static)$'
	else
		info "Removing all non -debuginfo libs packages (-devel too) not need by anything..."
		local develrule='-(NONONO)$'
	fi
86 87
	#[ -n "$force" ] || info "You can run with --force for more deep removing"
	local force=force
88 89

	local flag=
90 91
	local libgrep='^(lib|i586-lib|bzlib|zlib)'
	[ -n "$force" ] || libexclude=$libgrep'[^-]*$'
92

93
	# https://www.altlinux.org/APT_в_ALT_Linux/Советы_по_использованию#apt-cache_list-nodeps
94
	showcmd "apt-cache list-nodeps | grep -E -- \"$libgrep\""
95
	fullpkgs=$(apt-cache list-nodeps | grep -E -- "$libgrep" \
96
		| sed -e "s/[-\.]32bit$//g" \
97 98
		| grep -E -v -- "$develrule" \
		| grep -E -v -- "-(debuginfo)$" \
99
		| grep -E -v -- "-(util|utils|tool|tools|plugin|daemon|help)$" \
100
		| grep -E -v -- "^(libsystemd|libreoffice|libnss|libvirt-client|libvirt-daemon|libsasl2-plugin|eepm)" )
101
	pkgs=$(skip_manually_installed $fullpkgs)
102 103 104 105

	if [ -n "$dryrun" ] ; then
		info "Packages for autoremoving:"
		echo "$pkgs"
106
		__epm_print_excluded "$pkgs" "$fullpkgs"
107 108 109
		return 0
	fi

110 111
	__epm_print_excluded "$pkgs" "$fullpkgs"

112 113 114
	# commented, with hi probability user install i586- manually
	# workaround against missed i586- handling in apt-cache list-nodeps
	if epmqp i586-lib >/dev/null ; then
115
		info "You can try removing all i586- with follow command"
116 117 118
		showcmd rpm -v -e $(epmqp i586-lib)
	fi

119 120 121
	[ -n "$pkgs" ] && sudocmd rpm -v -e $pkgs && flag=1

	if [ -n "$flag" ] ; then
122 123
		info ""
		info "call again for next cycle until all libs will be removed"
124
		__epm_autoremove_altrpm_lib $nodevel
125
	fi
126 127 128 129

	return 0
}

130 131 132

__epm_autoremove_altrpm()
{
133
	local i
134 135 136
	load_helper epm-packages
	assure_exists /etc/buildreqs/files/ignore.d/apt-scripts apt-scripts

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
	if [ -z "$pkg_names" ] ; then
		__epm_autoremove_altrpm_pp '^(python-module-|python3-module-|python-modules-|python3-modules|perl-)'
		__epm_autoremove_altrpm_lib nodevel
		return 0
	fi

	for i in $pkg_names ; do
		case $i in
		libs)
			__epm_autoremove_altrpm_lib nodevel
			;;
		python)
			__epm_autoremove_altrpm_pp '^(python-module-|python3-module-|python-modules-|python3-modules)'
			;;
		perl)
			__epm_autoremove_altrpm_pp '^(perl-)'
			;;
		libs-devel)
			__epm_autoremove_altrpm_lib
			;;
		*)
			fatal "autoremove: unsupported '$i'. Use libs, python, perl, libs-devel."
			;;
		esac
	done
162 163 164 165

	return 0
}

166
# TODO: keep our eepm package
167
epm_autoremove()
168
{
169

170 171
case $DISTRNAME in
	ALTLinux)
172
		__epm_autoremove_altrpm
173 174 175

		[ -n "$dryrun" ] && return

176
		docmd epm remove-old-kernels
177 178 179 180
		
		if which nvidia-clean-driver 2>/dev/null ; then
			sudocmd nvidia-clean-driver
		fi
181

182 183 184
		return
		;;
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
185
		;;
186 187
esac

188 189
[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"

190
case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
191
	apt-dpkg|aptitude-dpkg)
192
		sudocmd apt-get autoremove $dryrun
193
		;;
194
	aura)
195 196 197
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
198 199
		sudocmd aura -Oj
		;;
200 201 202
	yum-rpm)
		# cleanup orphanes?
		while true ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
203
			# shellcheck disable=SC2046
204
			docmd package-cleanup --leaves $(subst_option non_interactive --assumeyes)
205
			# FIXME: package-cleanup have to use stderr for errors
206
			local PKGLIST=$(package-cleanup -q --leaves | grep -v "^eepm-")
207
			[ -n "$PKGLIST" ] || break
208
			showcmd epm remove $PKGLIST
209 210
		done
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
211
	dnf-rpm)
212 213 214
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
215 216
		sudocmd dnf autoremove
		;;
217 218 219 220
	# see autoorhans
	#urpm-rpm)
	#	sudocmd urpme --auto-orphans
	#	;;
221
	emerge)
222 223 224
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
225
		sudocmd emerge --depclean
226
		assure_exists revdep-rebuild
Vitaly Lipatov's avatar
Vitaly Lipatov committed
227
		sudocmd revdep-rebuild
228
		;;
229 230 231 232
	# see autoorhans
	#pacman)
	#	sudocmd pacman -Qdtq | sudocmd pacman -Rs -
	#	;;
233 234 235 236
	slackpkg)
		# clean-system removes non official packages
		#sudocmd slackpkg clean-system
		;;
237 238 239
	guix)
		sudocmd guix gc
		;;
240 241 242
	pkgng)
		sudocmd pkg autoremove
		;;
243 244 245 246 247 248
	zypper-rpm)
		# https://www.linux.org.ru/forum/desktop/11931830
		assure_exists zypper zypper 1.9.3
		sudocmd zypper packages --unneeded
		# FIXME: x86_64/i586 are duplicated
		local PKGLIST=$(zypper packages --unneeded | tail -n +5 | cut -d \| -f 3 | sort -u)
249
		showcmd epm remove --clean-deps $PKGLIST
250
		;;
251
	xbps)
252 253 254 255
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
		sudocmd xbps-remove -O
256
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
257 258 259 260 261 262 263
	opkg)
		if [ -n "$dryrun" ] ; then
			sudocmd opkg --noaction --autoremove
		else
			sudocmd opkg --autoremove
		fi
		;;
264
	*)
265
		fatal "Have no suitable command for $PMTYPE"
266 267 268 269
		;;
esac

}