epm-remove 6.88 KB
Newer Older
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012-2014, 2016, 2017, 2019-2021  Etersoft
# Copyright (C) 2012-2014, 2016, 2017, 2019-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
load_helper epm-sh-altlinux
21
load_helper epm-query
22
load_helper epm-print
23
load_helper epm-sh-warmup
24
load_helper epm-sh-install
25

26 27
RPMISNOTINSTALLED=202

28 29
__check_rpm_e_result()
{
30
    grep -q "is not installed" $1 && return $RPMISNOTINSTALLED
31 32 33 34
    return $2
}


35
# Try remove with low level removing
36 37
epm_remove_low()
{
38
	[ -z "$1" ] && return
39 40 41

	warmup_lowbase

42
	case $PMTYPE in
43
		*-rpm)
44
			cd /tmp || fatal
45
			__epm_check_vendor $@
46
			store_output sudocmd rpm -ev $noscripts $nodeps $@
47
			# rpm returns number of packages if failed on removing
48 49 50 51 52
			__check_rpm_e_result $RC_STDOUT $?
			RES=$?
			clean_store_output
			cd - >/dev/null
			return $RES ;;
53
		*-dpkg|-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
54
			# shellcheck disable=SC2046
55
			sudocmd dpkg -P $(subst_option nodeps --force-all) $(print_name "$@")
56
			return ;;
57
		pkgsrc)
58
			sudocmd pkg_delete -r $@
59
			return ;;
60 61 62
		pkgng)
			sudocmd pkg delete -R $@
			return ;;
63
		emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
64
			sudocmd emerge --unmerge $@
65
			return ;;
66 67 68
		pacman)
			sudocmd pacman -R $@
			return ;;
69 70 71
		appget|winget)
			sudocmd $PMTYPE uninstall $@
			return ;;
72
		slackpkg)
73
			sudocmd /sbin/removepkg $@
74
			return ;;
75 76
	esac
	return 1
77 78
}

79 80 81
epm_remove_names()
{
	[ -z "$1" ] && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
82

83 84
	warmup_bases

85 86
	local APTOPTIONS="$(subst_option non_interactive -y)"

87
	case $PMTYPE in
88
		apt-dpkg)
89
			sudocmd apt-get remove --purge $APTOPTIONS $@
90
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
91 92 93
		aptitude-dpkg)
			sudocmd aptitude purge $@
			return ;;
94
		apt-rpm)
95
			sudocmd apt-get remove $APTOPTIONS $@
96
			return ;;
97
		packagekit)
98 99
			docmd pkcon remove $@
			return ;;
100 101 102
		deepsolver-rpm)
			sudocmd ds-remove $@
			return ;;
103
		urpm-rpm)
104
			sudocmd urpme $@
105
			return ;;
106
		pkgsrc) # without dependencies
107
			sudocmd pkg_delete $@
108
			return ;;
109 110 111
		pkgng)
			sudocmd pkg delete -R $@
			return ;;
112
		emerge)
113 114
			#sudocmd emerge --unmerge $@
			sudocmd emerge -aC $@
115 116
			return ;;
		pacman)
117
			sudocmd pacman -Rc $@
118 119
			return ;;
		yum-rpm)
120
			sudocmd yum remove $@
121
			return ;;
122 123 124
		dnf-rpm)
			sudocmd dnf remove $@
			return ;;
125 126 127
		snappy)
			sudocmd snappy uninstall $@
			return ;;
128
		zypper-rpm)
129
			sudocmd zypper remove --clean-deps $@
130 131
			return ;;
		mpkg)
132
			sudocmd mpkg remove $@
133
			return ;;
134 135 136
		conary)
			sudocmd conary erase $@
			return ;;
137
		npackd)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
138
			sudocmd npackdcl remove --package=$1
139
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
140 141 142
		nix)
			sudocmd nix-env --uninstall $@
			return ;;
143 144 145
		apk)
			sudocmd apk del $@
			return ;;
146 147 148
		guix)
			sudocmd guix package -r $@
			return ;;
149 150 151
		android)
			sudocmd pm uninstall $@
			return ;;
152
		chocolatey)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
153
			sudocmd chocolatey uninstall $@
154
			return ;;
155
		slackpkg)
156
			sudocmd /usr/sbin/slackpkg remove $@
157
			return ;;
158
		homebrew)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
159
			docmd brew remove $@
160
			return ;;
161 162 163
		aptcyg)
			sudocmd apt-cyg remove $@
			return ;;
164 165 166
		xbps)
			sudocmd xbps remove -R $@
			return ;;
167 168 169
		appget|winget)
			sudocmd $PMTYPE uninstall $@
			return ;;
170
		opkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
171
			# shellcheck disable=SC2046
172
			sudocmd opkg $(subst_option force -force-depends) remove $@
173
			return ;;
174
		*)
175
			fatal "Have no suitable command for $PMTYPE"
176
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
177
	esac
178 179
}

180 181 182
# TODO
epm_remove_nonint()
{
183 184
	warmup_bases

185
	case $PMTYPE in
186
		apt-dpkg)
187
			sudocmd apt-get -y --force-yes remove --purge $@
188
			return ;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
189 190 191
		aptitude-dpkg)
			sudocmd aptitude -y purge $@
			return ;;
192 193 194
		apt-rpm)
			sudocmd apt-get -y --force-yes remove $@
			return ;;
195
		packagekit)
196 197
			docmd pkcon remove --noninteractive $@
			return ;;
198
		urpm-rpm)
199
			sudocmd urpme --auto $@
200 201
			return ;;
		pacman)
202
			sudocmd pacman -Rc --noconfirm $@
203 204
			return ;;
		yum-rpm)
205
			sudocmd yum -y remove $@
206
			return ;;
207 208 209
		dnf-rpm)
			sudocmd dnf remove --assumeyes $@
			return ;;
210
		zypper-rpm)
211
			sudocmd zypper --non-interactive remove --clean-deps $@
212
			return ;;
213
		slackpkg)
214
			sudocmd /usr/sbin/slackpkg -batch=on -default_answer=yes remove $@
215
			return ;;
216 217 218
		pkgng)
			sudocmd pkg delete -y -R $@
			return ;;
219 220
		opkg)
			sudocmd opkg -force-defaults remove $@
221
			return ;;
222 223 224
		appget|winget)
			sudocmd $PMTYPE uninstall -s $@
			return ;;
225 226 227
		xbps)
			sudocmd xbps remove -y $@
			return ;;
228 229
	esac
	return 5
230 231
}

232 233 234
epm_print_remove_command()
{
	case $PMTYPE in
235
		*-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
236
			echo "rpm -ev $nodeps $*"
237
			;;
238
		*-dpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
239
			echo "dpkg -P $*"
240
			;;
241 242 243
		packagekit-*)
			echo "pkcon remove --noninteractive $*"
			;;
244
		pkgsrc)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
245
			echo "pkg_delete -r $*"
246
			;;
247
		pkgng)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
248
			echo "pkg delete -R $*"
249
			;;
250
		pacman)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
251
			echo "pacman -R $*"
252
			;;
253
		emerge)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
254
			echo "emerge --unmerge $*"
255 256
			;;
		slackpkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
257
			echo "/sbin/removepkg $*"
258
			;;
259 260
		opkg)
			echo "opkg remove $*"
261
			;;
262
		aptcyg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
263
			echo "apt-cyg remove $*"
264
			;;
265
		xbps)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
266
			echo "xbps remove -y $*"
267
			;;
268
		appget|winget)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
269
			echo "$PMTYPE uninstall -s $*"
270
			;;
271
		*)
272
			fatal "Have no suitable appropriate remove command for $PMTYPE"
273 274 275 276
			;;
	esac
}

277 278 279

epm_remove()
{
280 281 282 283 284
	if [ -n "$show_command_only" ] ; then
		epm_print_remove_command $pkg_filenames
		return
	fi

285
	if [ "$DISTRNAME" = "ALTLinux" ] || [ "$DISTRNAME" = "ALTServer" ] ; then
286 287 288 289 290
		load_helper epm-sh-altlinux
		if tasknumber "$pkg_names" >/dev/null ; then
			assure_exists apt-repo
			pkg_names="$(get_task_packages $pkg_names)"
		fi
291 292
	fi

293
	# TODO: fix pkg_names override
294 295
	# get full package name(s) from the package file(s)
	[ -n "$pkg_files" ] && pkg_names="$pkg_names $(epm query $pkg_files)"
296
	pkg_files=''
297

298 299
	if [ -z "$pkg_names" ] ; then
		warning "no package(s) to remove."
300
		return
301
	fi
302 303
	# remove according current arch (if x86_64) by default
	pkg_names="$(echo $pkg_names | exp_with_arch_suffix)"
304 305 306 307

	if [ -n "$dryrun" ] ; then
		info "Packages for removing:"
		echo "$pkg_names"
308 309 310 311 312
		case $PMTYPE in
			apt-rpm)
				nodeps="--test"
				APTOPTIONS="--simulate"
				;;
313 314 315 316
			apt-deb)
				nodeps="--simulate"
				APTOPTIONS="--simulate"
				;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
317
			*)
318 319 320
				return
				;;
		esac
321 322
	fi

323 324 325 326
	if [ -n "$skip_missed" ] ; then
		pkg_names="$(get_only_installed_packages $pkg_names)"
	fi

327
	epm_remove_low $pkg_names && return
328 329
	local STATUS=$?

330
	if [ -n "$direct" ] || [ -n "$nodeps" ] || [ "$STATUS" = "$RPMISNOTINSTALLED" ]; then
331
		[ -n "$force" ] || return $STATUS
332
	fi
333

334 335
	# get package name for hi level package management command (with version if supported and if possible)
	pkg_names=$(__epm_get_hilevel_name $pkg_names)
336 337

	if [ -n "$non_interactive" ] ; then
338
		epm_remove_nonint $pkg_names
339 340 341 342 343
		local RET=$?
		# if not separate command, use usual command
		[ "$RET" = "5" ] || return $RET
	fi

344
	epm_remove_names $pkg_names
345
}