epm-autoremove 10.6 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
load_helper epm-check_updated_repo

__epm_print_excluded()
{
24 25 26 27 28
    local pkgs="$1"
    local fullpkgs="$2"
    local excluded
    excluded="$(estrlist exclude "$pkgs" "$fullpkgs")"
    if [ -n "$excluded" ] ; then
29
        message "Skipped manually installed:"
30 31
        estrlist union $excluded
    fi
32 33
}

34
__epm_autoremove_altrpm_pp()
35
{
36
    local pkgs fullpkgs
37

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

40
    local libexclude="$1"
41

42
    local flag=
43

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

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

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

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

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

65

66
    if [ -n "$flag" ] ; then
Boria138's avatar
Boria138 committed
67
        info
68 69 70
        info "call again for next cycle until all modules will be removed"
        __epm_autoremove_altrpm_pp "$libexclude"
    fi
71

72
    return 0
73 74
}

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

83 84
__epm_autoremove_altrpm_lib()
{
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    local pkgs fullpkgs

    local flag=''
    local opt="$1"
    local libgrep=''
    info
    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

    # https://www.altlinux.org/APT_в_ALT_Linux/Советы_по_использованию#apt-cache_list-nodeps
    showcmd "apt-cache list-nodeps | grep -E -- \"$libgrep\""
113
    fullpkgs=$(a= apt-cache list-nodeps | grep -E -- "$libgrep" \
114 115 116 117
        | sed -e "s/[-\.]32bit$//g" \
        | grep -E -v -- "$develrule" \
        | grep -E -v -- "-(debuginfo)$" \
        | grep -E -v -- "-(util|utils|tool|tools|plugin|daemon|help)$" \
118
        | grep -E -v -- "^(libva-|libvdpau-va-gl)$" \
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        | grep -E -v -- "^(libsystemd|libreoffice|libnss|libvirt-client|libvirt-daemon|libsasl2-plugin|eepm|distro_info)" )
    pkgs=$(skip_manually_installed $fullpkgs)

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

    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

    if [ -n "$flag" ] ; then
Boria138's avatar
Boria138 committed
139
        info
140 141 142 143 144
        info "call again for next cycle until all libs will be removed"
        __epm_autoremove_altrpm_lib $opt
    fi

    return 0
145 146
}

147

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

150 151
__epm_autoremove_altrpm()
{
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    local i
    load_helper epm-packages
    assure_exists /usr/share/apt/scripts/list-nodeps.lua apt-scripts

    if [ -z "$pkg_names" ] ; then
        pkg_names="$epm_autoremove_default_groups"
    elif [ "$pkg_names" = "python" ] ; then
        pkg_names="python2 python3"
    fi

    for i in $pkg_names ; do
        case $i in
        libs)
            __epm_autoremove_altrpm_lib libs
            ;;
        i586-libs)
            __epm_autoremove_altrpm_lib i586-libs
            ;;
        debuginfo)
            __epm_autoremove_altrpm_package_group '-debuginfo-'
            ;;
        devel)
            __epm_autoremove_altrpm_package_group '^(rpm-build-|gcc-|glibc-devel-)'
            ;;
        python2)
            __epm_autoremove_altrpm_pp '^(python-module-|python-modules-)'
            ;;
        python3)
            __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-)'
            ;;
        perl)
            __epm_autoremove_altrpm_pp '^(perl-)'
            ;;
        libs-devel)
            __epm_autoremove_altrpm_lib devel
            ;;
        *)
            fatal "autoremove: unsupported '$i'. Use epm autoremove --help to list supported ones"
            ;;
        esac
    done

    return 0
204 205
}

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

Use
--auto|--assumeyes|--non-interactive  for non interactive mode
228
'
229 230 231
}


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

236 237 238 239 240
    if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "help" ] ; then
        epm_autoremove_print_help
        return 0
    fi

241
case $BASEDISTRNAME in
242 243 244 245
    "alt")

        if [ -z "$direct" ] ; then
            [ -n "$1" ] && fatal "Run autoremove without args or with --direct. Check epm autoremove --help to available commands."
246 247 248
            if epm installed sudo ; then
                epm mark manual sudo || fatal
            fi
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
            sudocmd apt-get $(subst_option non_interactive -y) autoremove $dryrun
            local RET=$?
            if [ "$RET" != 0 ] ; then
                echo
                info "Also you can run 'epm autoremove --direct' to use epm implementation of autoremove (see --help)"
                return
            fi
        else
            __epm_autoremove_altrpm "$@"
        fi

        #[ -n "$dryrun" ] && return

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

        docmd epm remove-old-kernels $dryrun

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

        return
        ;;
274 275 276
    "astra")
        [ -n "$force" ] || fatal "It seems AstraLinux does no support autoremove correctly. You can rerun the command with --force option to get into trouble."
        ;;
277 278
    *)
        ;;
279 280
esac

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

283
case $PMTYPE in
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    apt-dpkg|aptitude-dpkg)
        sudocmd apt-get autoremove $(subst_option non_interactive -y) $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
302
            local PKGLIST=$(a= package-cleanup -q --leaves | grep -v "^eepm-")
303 304 305 306
            [ -n "$PKGLIST" ] || break
            docmd epm remove $PKGLIST
        done
        ;;
307
    dnf-rpm|dnf5-rpm)
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
        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
344
        local PKGLIST=$(a= zypper packages --unneeded | tail -n +5 | cut -d \| -f 3 | sort -u)
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
        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
        ;;
360 361 362 363 364 365 366
    eopkg)
        if [ -n "$dryrun" ] ; then
            sudocmd opkg --dry-run autoremove
        else
            sudocmd eopkg autoremove
        fi
        ;;
367
    *)
368
        fatal 'Have no suitable command for $PMTYPE'
369
        ;;
370 371 372
esac

}