epm-autoremove 6.45 KB
#!/bin/sh
#
# Copyright (C) 2012, 2017, 2018  Etersoft
# Copyright (C) 2012, 2017, 2018  Vitaly Lipatov <lav@etersoft.ru>
#
# 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
# (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
# GNU Affero General Public License for more details.
#
# 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/>.
#

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
}

__epm_autoremove_altrpm_pp()
{
	local pkgs fullpkgs

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

	local libexclude="$1"

	local flag=

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

	showcmd "apt-cache list-nodeps | grep -E -- \"$libexclude\""
	fullpkgs=$(apt-cache list-nodeps | grep -E -- "$libexclude" )
	pkgs=$(skip_manually_installed $fullpkgs)

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

	__epm_print_excluded "$pkgs" "$fullpkgs"

	[ -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"
		__epm_autoremove_altrpm_pp "$libexclude"
	fi

	return 0
}

__epm_autoremove_altrpm_lib()
{
	local pkgs fullpkgs

	local nodevel="$1"

	info
	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
	#[ -n "$force" ] || info "You can run with --force for more deep removing"
	local force=force

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

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

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

	__epm_print_excluded "$pkgs" "$fullpkgs"

	# 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 all i586- with follow command"
		showcmd rpm -v -e $(epmqp i586-lib)
	fi

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

	if [ -n "$flag" ] ; then
		info ""
		info "call again for next cycle until all libs will be removed"
		__epm_autoremove_altrpm_lib $nodevel
	fi

	return 0
}


__epm_autoremove_altrpm()
{
	local i
	load_helper epm-packages
	assure_exists /usr/share/apt/scripts/list-nodeps.lua apt-scripts

	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

	return 0
}

# TODO: keep our eepm package
epm_autoremove()
{

case $DISTRNAME in
	ALTLinux)
		__epm_autoremove_altrpm

		[ -n "$dryrun" ] && return

		docmd epm remove-old-kernels
		
		if which nvidia-clean-driver 2>/dev/null ; then
			sudocmd nvidia-clean-driver
		fi

		return
		;;
	*)
		;;
esac

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

case $PMTYPE in
	apt-dpkg|aptitude-dpkg)
		sudocmd apt-get autoremove $dryrun
		;;
	aura)
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
		sudocmd aura -Oj
		;;
	packagekit-*)
		docmd pkcon repair --autoremove
		;;
	yum-rpm)
		# cleanup orphanes?
		while true ; do
			# shellcheck disable=SC2046
			docmd package-cleanup --leaves $(subst_option non_interactive --assumeyes)
			# FIXME: package-cleanup have to use stderr for errors
			local PKGLIST=$(package-cleanup -q --leaves | grep -v "^eepm-")
			[ -n "$PKGLIST" ] || break
			showcmd epm remove $PKGLIST
		done
		;;
	dnf-rpm)
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
		sudocmd dnf autoremove
		;;
	# see autoorhans
	#urpm-rpm)
	#	sudocmd urpme --auto-orphans
	#	;;
	emerge)
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
		sudocmd emerge --depclean
		assure_exists revdep-rebuild
		sudocmd revdep-rebuild
		;;
	# see autoorhans
	#pacman)
	#	sudocmd pacman -Qdtq | sudocmd pacman -Rs -
	#	;;
	slackpkg)
		# clean-system removes non official packages
		#sudocmd slackpkg clean-system
		;;
	guix)
		sudocmd guix gc
		;;
	pkgng)
		sudocmd pkg autoremove
		;;
	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)
		showcmd epm remove --clean-deps $PKGLIST
		;;
	xbps)
		if [ -n "$dryrun" ] ; then
			fatal "--dry-run is not supported yet"
		fi
		sudocmd xbps-remove -O
		;;
	opkg)
		if [ -n "$dryrun" ] ; then
			sudocmd opkg --noaction --autoremove
		else
			sudocmd opkg --autoremove
		fi
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}