epm-autoremove 5.73 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2017  Etersoft
# Copyright (C) 2012, 2017  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
__epm_autoremove_altrpm_pp()
21
{
22 23 24 25 26 27
	local pkgs

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

28 29
	local libexclude="$1"

30 31 32
	local flag=

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

34 35
	showcmd "apt-cache list-nodeps | grep -E -- \"$libexclude\""
	pkgs=$(apt-cache list-nodeps | grep -E -- "$libexclude" )
36 37 38 39 40 41 42

	if [ -n "$dryrun" ] ; then
		info "Packages for autoremoving:"
		echo "$pkgs"
		return 0
	fi
	
43 44 45 46 47
	[ -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"
48
		__epm_autoremove_altrpm_pp "$libexclude"
49 50 51 52 53 54 55 56 57
	fi

	return 0
}

__epm_autoremove_altrpm_lib()
{
	local pkgs

58 59
	local nodevel="$1"

60
	info
61 62 63 64 65 66 67
	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
68 69
	#[ -n "$force" ] || info "You can run with --force for more deep removing"
	local force=force
70 71

	local flag=
72 73
	local libgrep='^(lib|i586-lib|bzlib|zlib)'
	[ -n "$force" ] || libexclude=$libgrep'[^-]*$'
74

75
	# https://www.altlinux.org/APT_в_ALT_Linux/Советы_по_использованию#apt-cache_list-nodeps
76 77
	showcmd "apt-cache list-nodeps | grep -E -- \"$libgrep\""
	pkgs=$(apt-cache list-nodeps | grep -E -- "$libgrep" \
78
		| sed -e "s/[-\.]32bit$//g" \
79 80
		| grep -E -v -- "$develrule" \
		| grep -E -v -- "-(debuginfo)$" \
81
		| grep -E -v -- "-(util|utils|tool|tools|plugin|daemon|help)$" \
82
		| grep -E -v -- "^(libsystemd|libreoffice|libnss|libvirt-client|libvirt-daemon|libsasl2-plugin|eepm)" )
83 84 85 86 87 88 89

	if [ -n "$dryrun" ] ; then
		info "Packages for autoremoving:"
		echo "$pkgs"
		return 0
	fi

90 91 92 93 94 95 96
	# 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
		info "You can try removing i586- with follow command"
		showcmd rpm -v -e $(epmqp i586-lib)
	fi

97 98 99
	[ -n "$pkgs" ] && sudocmd rpm -v -e $pkgs && flag=1

	if [ -n "$flag" ] ; then
100 101
		info ""
		info "call again for next cycle until all libs will be removed"
102
		__epm_autoremove_altrpm_lib $nodevel
103
	fi
104 105 106 107

	return 0
}

108 109 110

__epm_autoremove_altrpm()
{
111
	local i
112 113 114
	load_helper epm-packages
	assure_exists /etc/buildreqs/files/ignore.d/apt-scripts apt-scripts

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	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
140 141 142 143

	return 0
}

144
# TODO: keep our eepm package
145
epm_autoremove()
146
{
147

148 149
case $DISTRNAME in
	ALTLinux)
150
		__epm_autoremove_altrpm
151 152 153

		[ -n "$dryrun" ] && return

154
		docmd epm remove-old-kernels
155 156 157 158
		
		if which nvidia-clean-driver 2>/dev/null ; then
			sudocmd nvidia-clean-driver
		fi
159

160 161 162
		return
		;;
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
163
		;;
164 165
esac

166 167
[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"

168
case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
169
	apt-dpkg|aptitude-dpkg)
170
		sudocmd apt-get autoremove $dryrun
171
		;;
172
	aura)
173 174 175
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
176 177
		sudocmd aura -Oj
		;;
178 179 180
	yum-rpm)
		# cleanup orphanes?
		while true ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
181
			# shellcheck disable=SC2046
182
			docmd package-cleanup --leaves $(subst_option non_interactive --assumeyes)
183
			# FIXME: package-cleanup have to use stderr for errors
184
			local PKGLIST=$(package-cleanup -q --leaves | grep -v "^eepm-")
185
			[ -n "$PKGLIST" ] || break
186
			showcmd epm remove $PKGLIST
187 188
		done
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
189
	dnf-rpm)
190 191 192
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
193 194
		sudocmd dnf autoremove
		;;
195 196 197 198
	# see autoorhans
	#urpm-rpm)
	#	sudocmd urpme --auto-orphans
	#	;;
199
	emerge)
200 201 202
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
203
		sudocmd emerge --depclean
204
		assure_exists revdep-rebuild
Vitaly Lipatov's avatar
Vitaly Lipatov committed
205
		sudocmd revdep-rebuild
206
		;;
207 208 209 210
	# see autoorhans
	#pacman)
	#	sudocmd pacman -Qdtq | sudocmd pacman -Rs -
	#	;;
211 212 213 214
	slackpkg)
		# clean-system removes non official packages
		#sudocmd slackpkg clean-system
		;;
215 216 217
	guix)
		sudocmd guix gc
		;;
218 219 220
	pkgng)
		sudocmd pkg autoremove
		;;
221 222 223 224 225 226
	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)
227
		showcmd epm remove --clean-deps $PKGLIST
228
		;;
229
	xbps)
230 231 232 233
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
		sudocmd xbps-remove -O
234
		;;
235
	*)
236
		fatal "Have no suitable command for $PMTYPE"
237 238 239 240
		;;
esac

}