epm-autoremove 8.71 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2017, 2018, 2021  Etersoft
# Copyright (C) 2012, 2017, 2018, 2021  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

	info "Removing unused python/perl modules..."

40 41
	local libexclude="$1"

42 43 44
	local flag=

	showcmd "apt-cache list-nodeps | grep -E -- \"$libexclude\""
45 46
	fullpkgs=$(apt-cache list-nodeps | grep -E -- "$libexclude" )
	pkgs=$(skip_manually_installed $fullpkgs)
47 48 49 50

	if [ -n "$dryrun" ] ; then
		info "Packages for autoremoving:"
		echo "$pkgs"
51
		__epm_print_excluded "$pkgs" "$fullpkgs"
52 53
		return 0
	fi
54

55 56 57 58 59 60 61 62 63
	if [ -n "$pkgs" ] ; then
		info "The command we will run:"
		showcmd rpm -v -e $pkgs
		__epm_print_excluded "$pkgs" "$fullpkgs"

		confirm_info "We will remove unused (without dependencies) packages above."

		sudocmd rpm -v -e $pkgs && flag=1
	fi
64

65 66 67 68

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

	return 0
}

75 76 77 78 79 80 81 82
__epm_autoremove_altrpm_package_group()
{
	if epmqp "$*" ; then
		confirm_info "We will remove unused (without dependencies) packages above."
		docmd epm remove $(epmqp --short "$*")
	fi
}

83 84
__epm_autoremove_altrpm_lib()
{
85
	local pkgs fullpkgs
86

87 88 89
	local flag=''
	local opt="$1"
	local libgrep=''
90
	info
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	case "$opt" in
		libs)
			info "Removing all non -devel/-debuginfo libs packages not need by anything..."
			local develrule='-(devel|devel-static)$'
			libgrep='^(lib|bzlib|zlib)'
			;;
		i586-libs)
			info "Removing all non -devel/-debuginfo i586-libs packages not need by anything..."
			local develrule='-(devel|devel-static)$'
			libgrep='^(i586-lib|i586-bzlib|i586-zlib)'
			;;
		devel)
			info "Removing all non -debuginfo libs packages (-devel too) not need by anything..."
			local develrule='-(NONONO)$'
			libgrep='^(lib|bzlib|zlib)'
			;;
		*)
			fatal "Internal error: unsupported opt $opt"
	esac
110

111
	# https://www.altlinux.org/APT_в_ALT_Linux/Советы_по_использованию#apt-cache_list-nodeps
112
	showcmd "apt-cache list-nodeps | grep -E -- \"$libgrep\""
113
	fullpkgs=$(apt-cache list-nodeps | grep -E -- "$libgrep" \
114
		| sed -e "s/[-\.]32bit$//g" \
115 116
		| grep -E -v -- "$develrule" \
		| grep -E -v -- "-(debuginfo)$" \
117
		| grep -E -v -- "-(util|utils|tool|tools|plugin|daemon|help)$" \
118
		| grep -E -v -- "^(libsystemd|libreoffice|libnss|libvirt-client|libvirt-daemon|libsasl2-plugin|eepm|distro_info)" )
119
	pkgs=$(skip_manually_installed $fullpkgs)
120 121 122 123

	if [ -n "$dryrun" ] ; then
		info "Packages for autoremoving:"
		echo "$pkgs"
124
		__epm_print_excluded "$pkgs" "$fullpkgs"
125 126 127
		return 0
	fi

128 129 130 131 132
	if [ -n "$pkgs" ] ; then
		info "The command we will run:"
		showcmd rpm -v -e $pkgs
		__epm_print_excluded "$pkgs" "$fullpkgs"
		confirm_info "We will remove unused (without dependencies) packages above."
133

134
		sudocmd rpm -v -e $pkgs && flag=1
135 136
	fi

137
	if [ -n "$flag" ] ; then
138 139
		info ""
		info "call again for next cycle until all libs will be removed"
140
		__epm_autoremove_altrpm_lib $opt
141
	fi
142 143 144 145

	return 0
}

146

147
epm_autoremove_default_groups="python2 python3 perl gem ruby libs"
148

149 150
__epm_autoremove_altrpm()
{
151
	local i
152
	load_helper epm-packages
153
	assure_exists /usr/share/apt/scripts/list-nodeps.lua apt-scripts
154

155
	if [ -z "$pkg_names" ] ; then
156
		pkg_names="$epm_autoremove_default_groups"
157 158
	elif [ "$pkg_names" = "python" ] ; then
		pkg_names="python2 python3"
159 160 161 162 163
	fi

	for i in $pkg_names ; do
		case $i in
		libs)
164 165 166 167 168 169 170
			__epm_autoremove_altrpm_lib libs
			;;
		i586-libs)
			__epm_autoremove_altrpm_lib i586-libs
			;;
		debuginfo)
			__epm_autoremove_altrpm_package_group '-debuginfo-'
171
			;;
172 173
		devel)
			__epm_autoremove_altrpm_package_group '^(rpm-build-|gcc-|glibc-devel-)'
174
			;;
175 176 177 178
		python2)
			__epm_autoremove_altrpm_pp '^(python-module-|python-modules-)'
			;;
		python3)
179 180 181 182 183 184 185 186 187 188
			__epm_autoremove_altrpm_pp '^(python3-module-|python3-modules-)'
			;;
		php)
			__epm_autoremove_altrpm_pp '^(php7-|php5-|php8-)'
			;;
		gem)
			__epm_autoremove_altrpm_pp '^(gem-)'
			;;
		ruby)
			__epm_autoremove_altrpm_pp '^(ruby-)'
189
			;;
190 191 192 193
		perl)
			__epm_autoremove_altrpm_pp '^(perl-)'
			;;
		libs-devel)
194
			__epm_autoremove_altrpm_lib devel
195 196
			;;
		*)
197
			fatal "autoremove: unsupported '$i'. Use epm autoremove --help to list supported ones"
198 199 200
			;;
		esac
	done
201 202 203 204

	return 0
}

205 206 207 208 209 210 211 212 213 214
epm_autoremove_print_help()
{
	echo "epm autoremove removes unneeded packages from the system"
	echo "run 'epm autoremove' to use apt-get autoremove"
	echo "or run 'epm autoremove --direct [group1] [group2] ...' to use epm implementation"
	echo "Default groups: $epm_autoremove_default_groups"
	cat <<EOF
Supported package groups:
    libs       - unused libraries
    libs-devel - unused -devel packages
215 216 217
    i586-libs  - unused i586-libs libraries
    debuginfo  - all debuginfo packages
    devel      - all packages used for build/developing
218 219 220
    python     - all python modules
    python2    - python2 modules
    python3    - python3 modules
221 222 223 224 225 226
    perl       - perl modules
    gem        - gem modules
    ruby       - ruby modules

Use
--auto|--assumeyes|--non-interactive  for non interactive mode
227 228 229 230
EOF
}


231
# TODO: keep our eepm package
232
epm_autoremove()
233
{
234

235
case $DISTRNAME in
236
	ALTLinux|ALTServer)
237 238 239 240
		if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "help" ] ; then
			epm_autoremove_print_help
			return 0
		fi
241

242
		if [ -z "$direct" ] ; then
243
			[ -n "$1" ] && fatal "Run autoremove without args or with --direct. Check epm autoremove --help to available commands."
244
			sudocmd apt-get $(subst_option non_interactive -y) autoremove $dryrun
245
			local RET=$?
246 247 248 249 250
			if [ "$RET" != 0 ] ; then
				echo
				info "Also you can run 'epm autoremove --direct' to use epm implementation of autoremove (see --help)"
				return
			fi
251
		else
252
			__epm_autoremove_altrpm "$@"
253
		fi
254 255 256

		[ -n "$dryrun" ] && return

257
		# remove old kernels only by a default way
258
		[ -n "$1" ] && return
259

260 261
		docmd epm remove-old-kernels $(subst_option non_interactive --auto)

262 263 264 265 266
		if [ -z "$direct" ] ; then
			echo
			info "Also you can run 'epm autoremove --direct' to use epm implementation of autoremove (see --help)"
		fi

267 268 269
		return
		;;
	*)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
270
		;;
271 272
esac

273 274
[ -z "$pkg_filenames" ] || fatal "No arguments are allowed here"

275
case $PMTYPE in
Vitaly Lipatov's avatar
Vitaly Lipatov committed
276
	apt-dpkg|aptitude-dpkg)
277
		sudocmd apt-get autoremove local $(subst_option non_interactive -y) $dryrun
278
		;;
279
	aura)
280 281 282
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
283 284
		sudocmd aura -Oj
		;;
285
	packagekit)
286 287
		docmd pkcon repair --autoremove
		;;
288 289 290
	yum-rpm)
		# cleanup orphanes?
		while true ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
291
			# shellcheck disable=SC2046
292
			docmd package-cleanup --leaves $(subst_option non_interactive --assumeyes)
293
			# FIXME: package-cleanup have to use stderr for errors
294
			local PKGLIST=$(package-cleanup -q --leaves | grep -v "^eepm-")
295
			[ -n "$PKGLIST" ] || break
296
			docmd epm remove $PKGLIST
297 298
		done
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
299
	dnf-rpm)
300 301 302
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
Vitaly Lipatov's avatar
Vitaly Lipatov committed
303 304
		sudocmd dnf autoremove
		;;
305 306 307 308
	# see autoorhans
	#urpm-rpm)
	#	sudocmd urpme --auto-orphans
	#	;;
309
	emerge)
310 311 312
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
313
		sudocmd emerge --depclean
314
		assure_exists revdep-rebuild
Vitaly Lipatov's avatar
Vitaly Lipatov committed
315
		sudocmd revdep-rebuild
316
		;;
317 318 319 320
	# see autoorhans
	#pacman)
	#	sudocmd pacman -Qdtq | sudocmd pacman -Rs -
	#	;;
321 322 323 324
	slackpkg)
		# clean-system removes non official packages
		#sudocmd slackpkg clean-system
		;;
325 326 327
	guix)
		sudocmd guix gc
		;;
328 329 330
	pkgng)
		sudocmd pkg autoremove
		;;
331 332 333 334 335 336
	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)
337
		showcmd epm remove --clean-deps $PKGLIST
338
		;;
339
	xbps)
340 341 342 343
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
		sudocmd xbps-remove -O
344
		;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
345 346 347 348 349 350 351
	opkg)
		if [ -n "$dryrun" ] ; then
			sudocmd opkg --noaction --autoremove
		else
			sudocmd opkg --autoremove
		fi
		;;
352
	*)
353
		fatal "Have no suitable command for $PMTYPE"
354 355 356 357
		;;
esac

}