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

178 179 180
# TODO
epm_remove_nonint()
{
181 182
	warmup_bases

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

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

275 276 277

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

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

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

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

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

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

325
	epm_remove_low $pkg_names && return
326 327
	local STATUS=$?

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

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

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

342
	epm_remove_names $pkg_names
343
}