epm-autoremove 6.68 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
	load_helper epm-packages
135
	assure_exists /usr/share/apt/scripts/list-nodeps.lua apt-scripts
136

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 173 174 175 176 177 178 179
		if [ -z "$direct" ] ; then
			sudocmd apt-get autoremove $dryrun
			local RET=$?
			info "Also you can run 'epm autoremove --direct' to use low level autoremove (epm internal implementation)"
			[ "$RET" = 0 ] || return
		else
			__epm_autoremove_altrpm
		fi
180 181 182

		[ -n "$dryrun" ] && return

183
		docmd epm remove-old-kernels
184 185 186 187
		
		if which nvidia-clean-driver 2>/dev/null ; then
			sudocmd nvidia-clean-driver
		fi
188

189 190 191
		return
		;;
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
192
		;;
193 194
esac

195 196
[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"

197
case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
198
	apt-dpkg|aptitude-dpkg)
199
		sudocmd apt-get autoremove $dryrun
200
		;;
201
	aura)
202 203 204
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
205 206
		sudocmd aura -Oj
		;;
207
	packagekit)
208 209
		docmd pkcon repair --autoremove
		;;
210 211 212
	yum-rpm)
		# cleanup orphanes?
		while true ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
213
			# shellcheck disable=SC2046
214
			docmd package-cleanup --leaves $(subst_option non_interactive --assumeyes)
215
			# FIXME: package-cleanup have to use stderr for errors
216
			local PKGLIST=$(package-cleanup -q --leaves | grep -v "^eepm-")
217
			[ -n "$PKGLIST" ] || break
218
			showcmd epm remove $PKGLIST
219 220
		done
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
221
	dnf-rpm)
222 223 224
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
225 226
		sudocmd dnf autoremove
		;;
227 228 229 230
	# see autoorhans
	#urpm-rpm)
	#	sudocmd urpme --auto-orphans
	#	;;
231
	emerge)
232 233 234
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
235
		sudocmd emerge --depclean
236
		assure_exists revdep-rebuild
Vitaly Lipatov's avatar
Vitaly Lipatov committed
237
		sudocmd revdep-rebuild
238
		;;
239 240 241 242
	# see autoorhans
	#pacman)
	#	sudocmd pacman -Qdtq | sudocmd pacman -Rs -
	#	;;
243 244 245 246
	slackpkg)
		# clean-system removes non official packages
		#sudocmd slackpkg clean-system
		;;
247 248 249
	guix)
		sudocmd guix gc
		;;
250 251 252
	pkgng)
		sudocmd pkg autoremove
		;;
253 254 255 256 257 258
	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)
259
		showcmd epm remove --clean-deps $PKGLIST
260
		;;
261
	xbps)
262 263 264 265
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
		sudocmd xbps-remove -O
266
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
267 268 269 270 271 272 273
	opkg)
		if [ -n "$dryrun" ] ; then
			sudocmd opkg --noaction --autoremove
		else
			sudocmd opkg --autoremove
		fi
		;;
274
	*)
275
		fatal "Have no suitable command for $PMTYPE"
276 277 278 279
		;;
esac

}