epm-remove 10.3 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-requires
24
load_helper epm-sh-warmup
25
load_helper epm-sh-install
26

27 28
RPMISNOTINSTALLED=202

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


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

41
    warmup_lowbase
42

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

87 88
epm_remove_names()
{
89
    [ -z "$1" ] && return
Vitaly Lipatov's avatar
Vitaly Lipatov committed
90

91
    warmup_bases
92

93
    local APTOPTIONS="$(subst_option non_interactive -y)"
94

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

197 198 199
# TODO
epm_remove_nonint()
{
200
    warmup_bases
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    case $PMTYPE in
        apt-dpkg)
            sudocmd apt-get -y --force-yes remove --purge $@
            return ;;
        aptitude-dpkg)
            sudocmd aptitude -y purge $@
            return ;;
        apt-rpm)
            sudocmd apt-get -y --force-yes remove $@
            return ;;
        packagekit)
            docmd pkcon remove --noninteractive $@
            return ;;
        urpm-rpm)
            sudocmd urpme --auto $@
            return ;;
        pacman)
            sudocmd pacman -Rc --noconfirm $@
            return ;;
        yum-rpm)
            sudocmd yum -y remove $@
            return ;;
        dnf-rpm)
            sudocmd dnf remove --assumeyes $@
            return ;;
        zypper-rpm)
            sudocmd zypper --non-interactive remove --clean-deps $@
            return ;;
        slackpkg)
            sudocmd /usr/sbin/slackpkg -batch=on -default_answer=yes remove $@
            return ;;
        pkgng)
            sudocmd pkg delete -y -R $@
            return ;;
        opkg)
            sudocmd opkg -force-defaults remove $@
            return ;;
        eopkg)
            sudocmd eopkg $(subst_option nodeps --ignore-dependency) --yes-all remove $@
            return ;;
242 243 244
        pisi)
            sudocmd pisi $(subst_option nodeps --ignore-dependency) --yes-all remove $@
            return ;;
245 246 247 248 249 250 251 252
        appget|winget)
            sudocmd $PMTYPE uninstall -s $@
            return ;;
        xbps)
            sudocmd xbps remove -y $@
            return ;;
    esac
    return 5
253 254
}

255 256
epm_print_remove_command()
{
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    case $PMTYPE in
        *-rpm)
            echo "rpm -ev $nodeps $*"
            ;;
        *-dpkg)
            echo "dpkg -P $*"
            ;;
        packagekit-*)
            echo "pkcon remove --noninteractive $*"
            ;;
        pkgsrc)
            echo "pkg_delete -r $*"
            ;;
        pkgng)
            echo "pkg delete -R $*"
            ;;
        pacman)
            echo "pacman -R $*"
            ;;
        emerge)
            echo "emerge --unmerge $*"
            ;;
        slackpkg)
            echo "/sbin/removepkg $*"
            ;;
        opkg)
            echo "opkg remove $*"
            ;;
        eopkg)
            echo "eopkg remove $*"
            ;;
288 289 290
        pisi)
            echo "pisi remove $*"
            ;;
291 292 293 294 295 296 297 298 299 300
        aptcyg)
            echo "apt-cyg remove $*"
            ;;
        xbps)
            echo "xbps remove -y $*"
            ;;
        appget|winget)
            echo "$PMTYPE uninstall -s $*"
            ;;
        *)
301
            fatal 'Have no suitable appropriate remove command for $PMTYPE'
302 303
            ;;
    esac
304 305
}

306 307 308

epm_remove()
{
309 310 311 312
    if [ -n "$show_command_only" ] ; then
        epm_print_remove_command $pkg_filenames
        return
    fi
313

Vitaly Lipatov's avatar
Vitaly Lipatov committed
314 315
    # TODO: add support for --no-scripts to all cases

316 317 318 319 320 321 322
    if [ "$BASEDISTRNAME" = "alt" ] ; then
        load_helper epm-sh-altlinux
        if tasknumber "$pkg_names" >/dev/null ; then
            assure_exists apt-repo
            pkg_names="$(get_task_packages $pkg_names)"
        fi
    fi
323

324 325 326 327
    if [ -n "$manual_requires" ] ; then
        local pkg_names="$pkg_names $(short=1 epm_requires $pkg_names)"
    fi

328 329 330 331
    # TODO: fix pkg_names override
    # get full package name(s) from the package file(s)
    [ -n "$pkg_files" ] && pkg_names="$pkg_names $(epm query $pkg_files)"
    pkg_files=''
332

333 334 335 336 337 338
    if [ -z "$pkg_names" ] ; then
        warning "no package(s) to remove."
        return
    fi
    # remove according current arch (if x86_64) by default
    pkg_names="$(echo $pkg_names | exp_with_arch_suffix)"
339

340 341 342 343 344 345 346 347 348 349 350 351 352
    if [ -n "$dryrun" ] ; then
        info "Packages for removing:"
        echo "$pkg_names"
        case $PMTYPE in
            apt-rpm)
                nodeps="--test"
                APTOPTIONS="--simulate"
                ;;
            apt-deb)
                nodeps="--simulate"
                APTOPTIONS="--simulate"
                ;;
            *)
353
                fatal 'don'\''t yet support --simulate for $PMTYPE'
354 355 356 357
                return
                ;;
        esac
    fi
358

359 360 361
    if [ -n "$skip_missed" ] ; then
        pkg_names="$(get_only_installed_packages $pkg_names)"
    fi
362

363 364
    epm_remove_low $pkg_names && return
    local STATUS=$?
365

366 367 368
    if [ -n "$direct" ] || [ -n "$nodeps" ] || [ "$STATUS" = "$RPMISNOTINSTALLED" ]; then
        [ -n "$force" ] || return $STATUS
    fi
369

Vitaly Lipatov's avatar
Vitaly Lipatov committed
370 371
    # TODO: FIX
    # нужно удалить все пакеты, которые зависят от удаляемого
372
    if [ -n "$noscripts" ] ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
373
        #warning "It is not recommended to remove a few packages with disabled scripts simultaneously."
374 375 376
        fatal "We can't allow packages removing on hi level when --noscripts is used."
    fi

377 378
    # get package name for hi level package management command (with version if supported and if possible)
    pkg_names=$(__epm_get_hilevel_name $pkg_names)
379

380 381 382 383 384 385
    if [ -n "$non_interactive" ] ; then
        epm_remove_nonint $pkg_names
        local RET=$?
        # if not separate command, use usual command
        [ "$RET" = "5" ] || return $RET
    fi
386

387
    epm_remove_names $pkg_names
388
}