NBash

Форк
0
360 строк · 10.2 Кб
1
#!/bin/sh
2
#
3
# Copyright (C) 2012, 2017, 2018, 2021  Etersoft
4
# Copyright (C) 2012, 2017, 2018, 2021  Vitaly Lipatov <lav@etersoft.ru>
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#
19

20
load_helper epm-check_updated_repo
21

22
__epm_print_excluded()
23
{
24
    local pkgs="$1"
25
    local fullpkgs="$2"
26
    local excluded
27
    excluded="$(estrlist exclude "$pkgs" "$fullpkgs")"
28
    if [ -n "$excluded" ] ; then
29
        echo "Skipped manually installed:"
30
        estrlist union $excluded
31
    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=$(apt-cache list-nodeps | grep -E -- "$libexclude" )
46
    pkgs=$(skip_manually_installed $fullpkgs)
47

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

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

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

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

65

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

72
    return 0
73
}
74

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

83
__epm_autoremove_altrpm_lib()
84
{
85
    local pkgs fullpkgs
86

87
    local flag=''
88
    local opt="$1"
89
    local libgrep=''
90
    info
91
    case "$opt" in
92
        libs)
93
            info "Removing all non -devel/-debuginfo libs packages not need by anything..."
94
            local develrule='-(devel|devel-static)$'
95
            libgrep='^(lib|bzlib|zlib)'
96
            ;;
97
        i586-libs)
98
            info "Removing all non -devel/-debuginfo i586-libs packages not need by anything..."
99
            local develrule='-(devel|devel-static)$'
100
            libgrep='^(i586-lib|i586-bzlib|i586-zlib)'
101
            ;;
102
        devel)
103
            info "Removing all non -debuginfo libs packages (-devel too) not need by anything..."
104
            local develrule='-(NONONO)$'
105
            libgrep='^(lib|bzlib|zlib)'
106
            ;;
107
        *)
108
            fatal "Internal error: unsupported opt $opt"
109
    esac
110

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

121
    if [ -n "$dryrun" ] ; then
122
        info "Packages for autoremoving:"
123
        echo "$pkgs"
124
        __epm_print_excluded "$pkgs" "$fullpkgs"
125
        return 0
126
    fi
127

128
    if [ -n "$pkgs" ] ; then
129
        info "The command we will run:"
130
        showcmd rpm -v -e $pkgs
131
        __epm_print_excluded "$pkgs" "$fullpkgs"
132
        confirm_info "We will remove unused (without dependencies) packages above."
133

134
        sudocmd rpm -v -e $pkgs && flag=1
135
    fi
136

137
    if [ -n "$flag" ] ; then
138
        info ""
139
        info "call again for next cycle until all libs will be removed"
140
        __epm_autoremove_altrpm_lib $opt
141
    fi
142

143
    return 0
144
}
145

146

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

149
__epm_autoremove_altrpm()
150
{
151
    local i
152
    load_helper epm-packages
153
    assure_exists /usr/share/apt/scripts/list-nodeps.lua apt-scripts
154

155
    if [ -z "$pkg_names" ] ; then
156
        pkg_names="$epm_autoremove_default_groups"
157
    elif [ "$pkg_names" = "python" ] ; then
158
        pkg_names="python2 python3"
159
    fi
160

161
    for i in $pkg_names ; do
162
        case $i in
163
        libs)
164
            __epm_autoremove_altrpm_lib libs
165
            ;;
166
        i586-libs)
167
            __epm_autoremove_altrpm_lib i586-libs
168
            ;;
169
        debuginfo)
170
            __epm_autoremove_altrpm_package_group '-debuginfo-'
171
            ;;
172
        devel)
173
            __epm_autoremove_altrpm_package_group '^(rpm-build-|gcc-|glibc-devel-)'
174
            ;;
175
        python2)
176
            __epm_autoremove_altrpm_pp '^(python-module-|python-modules-)'
177
            ;;
178
        python3)
179
            __epm_autoremove_altrpm_pp '^(python3-module-|python3-modules-)'
180
            ;;
181
        php)
182
            __epm_autoremove_altrpm_pp '^(php7-|php5-|php8-)'
183
            ;;
184
        gem)
185
            __epm_autoremove_altrpm_pp '^(gem-)'
186
            ;;
187
        ruby)
188
            __epm_autoremove_altrpm_pp '^(ruby-)'
189
            ;;
190
        perl)
191
            __epm_autoremove_altrpm_pp '^(perl-)'
192
            ;;
193
        libs-devel)
194
            __epm_autoremove_altrpm_lib devel
195
            ;;
196
        *)
197
            fatal "autoremove: unsupported '$i'. Use epm autoremove --help to list supported ones"
198
            ;;
199
        esac
200
    done
201

202
    return 0
203
}
204

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

225
Use
226
--auto|--assumeyes|--non-interactive  for non interactive mode
227
EOF
228
}
229

230

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

235
case $BASEDISTRNAME in
236
    "alt")
237
        if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "help" ] ; then
238
            epm_autoremove_print_help
239
            return 0
240
        fi
241

242
        if [ -z "$direct" ] ; then
243
            [ -n "$1" ] && fatal "Run autoremove without args or with --direct. Check epm autoremove --help to available commands."
244
            if epm installed sudo ; then
245
                epm mark manual sudo || fatal
246
            fi
247
            sudocmd apt-get $(subst_option non_interactive -y) autoremove $dryrun
248
            local RET=$?
249
            if [ "$RET" != 0 ] ; then
250
                echo
251
                info "Also you can run 'epm autoremove --direct' to use epm implementation of autoremove (see --help)"
252
                return
253
            fi
254
        else
255
            __epm_autoremove_altrpm "$@"
256
        fi
257

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

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

263
        docmd epm remove-old-kernels $dryrun
264

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

270
        return
271
        ;;
272
    *)
273
        ;;
274
esac
275

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

278
case $PMTYPE in
279
    apt-dpkg|aptitude-dpkg)
280
        sudocmd apt-get autoremove $(subst_option non_interactive -y) $dryrun
281
        ;;
282
    aura)
283
        if [ -n "$dryrun" ] ; then
284
            fatal "--dry-run is not supported yet"
285
        fi
286
        sudocmd aura -Oj
287
        ;;
288
    packagekit)
289
        docmd pkcon repair --autoremove
290
        ;;
291
    yum-rpm)
292
        # cleanup orphanes?
293
        while true ; do
294
            # shellcheck disable=SC2046
295
            docmd package-cleanup --leaves $(subst_option non_interactive --assumeyes)
296
            # FIXME: package-cleanup have to use stderr for errors
297
            local PKGLIST=$(package-cleanup -q --leaves | grep -v "^eepm-")
298
            [ -n "$PKGLIST" ] || break
299
            docmd epm remove $PKGLIST
300
        done
301
        ;;
302
    dnf-rpm)
303
        if [ -n "$dryrun" ] ; then
304
            fatal "--dry-run is not supported yet"
305
        fi
306
        sudocmd dnf autoremove
307
        ;;
308
    # see autoorhans
309
    #urpm-rpm)
310
    #    sudocmd urpme --auto-orphans
311
    #    ;;
312
    emerge)
313
        if [ -n "$dryrun" ] ; then
314
            fatal "--dry-run is not supported yet"
315
        fi
316
        sudocmd emerge --depclean
317
        assure_exists revdep-rebuild
318
        sudocmd revdep-rebuild
319
        ;;
320
    # see autoorhans
321
    #pacman)
322
    #    sudocmd pacman -Qdtq | sudocmd pacman -Rs -
323
    #    ;;
324
    slackpkg)
325
        # clean-system removes non official packages
326
        #sudocmd slackpkg clean-system
327
        ;;
328
    guix)
329
        sudocmd guix gc
330
        ;;
331
    pkgng)
332
        sudocmd pkg autoremove
333
        ;;
334
    zypper-rpm)
335
        # https://www.linux.org.ru/forum/desktop/11931830
336
        assure_exists zypper zypper 1.9.3
337
        sudocmd zypper packages --unneeded
338
        # FIXME: x86_64/i586 are duplicated
339
        local PKGLIST=$(zypper packages --unneeded | tail -n +5 | cut -d \| -f 3 | sort -u)
340
        showcmd epm remove --clean-deps $PKGLIST
341
        ;;
342
    xbps)
343
        if [ -n "$dryrun" ] ; then
344
            fatal "--dry-run is not supported yet"
345
        fi
346
        sudocmd xbps-remove -O
347
        ;;
348
    opkg)
349
        if [ -n "$dryrun" ] ; then
350
            sudocmd opkg --noaction --autoremove
351
        else
352
            sudocmd opkg --autoremove
353
        fi
354
        ;;
355
    *)
356
        fatal "Have no suitable command for $PMTYPE"
357
        ;;
358
esac
359

360
}
361

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.