aboutsummaryrefslogtreecommitdiffstats
path: root/System/apt-remove-orphaned-bin-packages.sh
blob: bbb7cd39ca9955173098d9c6b3dcfd7c40ce1fc9 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
74
75
76
77
78
79
80
81
82
83
84
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
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
242
243
244
245
246
247
248
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/bin/bash
########################################################################################################################
# apt-remove-orphaned-bin-packages.sh
########################################################################################################################
#
# All rights reserved Ⓒ 2017-2022 sdxlive.com
#
# Written by Jean-Christophe Manciot <jcmanciot@sdxlive.com>
#
# Licensed under a GPLv3 License.
# You may not use this file except in compliance with the License. You may obtain a copy of the License at
#
#    https://www.gnu.org/licenses/gpl-3.0.md
#
# The licensor cannot revoke these freedoms as long as you follow the license terms.
#
# Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. 
# You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
#
########################################################################################################################

########################################################################################################################
#
# Removing/purging all orphaned installed binary lib packages, i.e. all lib packages that have no packages depending on them.
# It is possible to pass a perl regular expression to narrow the search for installed binary packages which need to be 
# removed.
#
########################################################################################################################
#
# Requirements:
# ------------
#
#  - We assume that apt alongside all its dependencies is already installed.
#
########################################################################################################################
#
# Parameters:
# ----------
#
# - <purge_enabled>: boolean
#       * true|yes: the binary packages will be purged (removed alongside their configuration files)
#       * false|no: the binary packages will be removed (their configuration files will be kept)
#                   (default)
# - <auto>: boolean
#       * true|yes: the binary packages will be removed/purged without any user's confirmation
#                   Use with caution.
#       * false|no: the binary packages will be removed/purged only with user's confirmation for each binary package
#                   (default)
# - <perl_regex_include_pattern>: optional perl regular expression used with 'grep -P --' to narrow the search for the  
#                                 matched installed binary packages. Some special characters may need to be escaped.
#                                 Use with caution: setting <auto> to false is strongly advised when using 
#                                 <perl_regex_include_pattern>.
# - <perl_regex_exclude_pattern>: optional perl regular expression used with 'grep -Pv --' to exclude some binary packages
#                                 from the search. Some special characters may need to be escaped.
#                                 Use with caution: setting <auto> to false is strongly advised when using 
#                                 <perl_regex_exclude_pattern>.
#
#######################################################################################################################
#
# Usage example:
# -------------
# 
# apt-remove-orphaned-bin-packages.sh true true
#
########################################################################################################################
# set -x
# Expanding aliases
shopt -s expand_aliases

purge_enabled=$1
auto=$2
perl_regex_include_pattern=$3
perl_regex_exclude_pattern=$4
PARAMETERS_NUMBER=0

case "$#" in
        $PARAMETERS_NUMBER)
                purge_enabled=false
                auto=false
                perl_regex_include_pattern=''
                perl_regex_exclude_pattern=''
                ;;
        $((PARAMETERS_NUMBER+1)))
                case "$1" in
                        -h|--help)
                                end_of_help_message_line_number=$(awk '/^# set -x/{ print NR; exit }' $(which apt-remove-orphaned-bin-packages.sh))
                                ((end_of_help_message_line_number--))
                                if [[ (-n $(apt-cache -q=0 2>&1 policy zenity | grep Installed)) && (! ($(apt-cache -q=0 2>&1 policy zenity | grep Installed) =~ none)) ]]; then
                                        awk -v var=$end_of_help_message_line_number 'NR >= 18 && NR <= var' $(which apt-remove-orphaned-bin-packages.sh) | zenity --text-info --title "apt-remove-orphaned-bin-packages.sh help" --width 1350 --height 1000
                                else
                                        awk -v var=$end_of_help_message_line_number 'NR >= 18 && NR <= var' $(which apt-remove-orphaned-bin-packages.sh)
                                fi
                                exit 1
                                ;;
                        *)
                                auto=false
                                perl_regex_include_pattern=''
                                perl_regex_exclude_pattern=''
                                ;;
                esac
                ;;
        $((PARAMETERS_NUMBER+2)))
                perl_regex_include_pattern=''
                perl_regex_exclude_pattern=''
                ;;
        $((PARAMETERS_NUMBER+3)))
                perl_regex_exclude_pattern=''
                ;;
        $((PARAMETERS_NUMBER+4)))
                ;;
        *)
                echo -e ''$_{1..180}'\b-'
                echo Wrong number of parameters: $(basename $0) $@
                echo -e ''$_{1..180}'\b-'
                exit 1
                ;;
esac

# Escaping the '$.*?/|\^(){}+@[]' characters for later grep -P searched string
# Letters, digits and '-' must not be escaped
alias escape_grep_pcre_search_string=$'sed -e \'s|[]$.*?/|\^(){}+@[]|\\\&|g\''
# '

dpkg-lock.sh apt update
# Installing dependencies needed to play an alert or a completion sound when necessary
dpkg-lock.sh apt -y install alsa-utils emacspeak pidgin-data

# Orphan binary lib packages
sorted_bin_package_names=()
if [[ -n "$perl_regex_exclude_pattern" ]]; then
        readarray -t sorted_bin_package_names < <(deborphaned --force-hold --nice-mode 2>/dev/null | cut -d ':' -f 1 | grep -Pv -- "$perl_regex_exclude_pattern" | grep -P -- "$perl_regex_include_pattern" | sort -u)
else
        readarray -t sorted_bin_package_names < <(deborphaned --force-hold --nice-mode 2>/dev/null | cut -d ':' -f 1 | grep -P -- "$perl_regex_include_pattern" | sort -u)
fi

removed_bin_package_names_versions=()
not_removed_bin_package_names_versions=()

log=~/.output.log

for ((s=0; s<${#sorted_bin_package_names[@]}; s++))
do
        bin_package_name="${sorted_bin_package_names[${s}]}"
        if [[ -z "$bin_package_name" ]]; then
                continue
        fi
        grep_esc_bin_package_name="$(echo "$bin_package_name" | escape_grep_pcre_search_string)"

        # Thanks to dash-o for his/her help
        # https://stackoverflow.com/questions/58806323/how-to-capture-the-output-of-a-bash-command-which-prompts-for-a-users-confirmat
        # Redirecting stdout/stderr to a log file
        echo &> $log
        # Background process
        (
                # Waiting for some keywords in $log
                while ! grep -q -P 'continue\?|Removing|following packages have unmet dependencies' $log ; do sleep 2 ; done ; 
        ) &

        bin_package_installed_version=$(apt-cache -q=0 2>&1 policy "$bin_package_name" | grep Installed | sed -E 's|^.*?: (.*)$|\1|g')

        case $purge_enabled in
                true|yes)
                        printf "\n"
                        echo -e ''$_{1..180}'\b='
                        echo "Purging: ${bin_package_name}=${bin_package_installed_version}"
                        echo -e ''$_{1..180}'\b='
                        case $auto in
                                true|yes)
                                        dpkg-lock.sh apt --show-progress --assume-yes --allow-change-held-packages 2>&1 purge "$bin_package_name" | tee -a $log
                                        return_code=$?
                                        if [[ $return_code -eq 0 ]]; then
                                                removed_bin_package_names_versions=("${removed_bin_package_names_versions[@]}" "${bin_package_name}=${bin_package_installed_version}")
                                        else
                                                aplay /usr/share/emacs/site-lisp/emacspeak/sounds/classic/alert-user.wav
                                                exit 1
                                        fi
                                        ;;
                                false|no)
                                        dpkg-lock.sh apt --show-progress 2>&1 purge "$bin_package_name" | tee -a $log
                                        return_code=$?
                                        if [[ $return_code -eq 0 ]]; then
                                                removed_bin_package_names_versions=("${removed_bin_package_names_versions[@]}" "${bin_package_name}=${bin_package_installed_version}")
                                        elif [[ $return_code -ne 1 ]]; then
                                                aplay /usr/share/emacs/site-lisp/emacspeak/sounds/classic/alert-user.wav
                                                exit 1
                                        fi
                                        ;;
                                *)
                                        printf "\n"
                                        echo -e ''$_{1..180}'\b-'
                                        echo "Unknown auto: $auto"
                                        echo -e ''$_{1..180}'\b-'
                                        exit 1
                                        ;;
                        esac
                        ;;
                false|no)
                        printf "\n"
                        echo -e ''$_{1..180}'\b='
                        echo "Removing: ${bin_package_name}=${bin_package_installed_version}"
                        echo -e ''$_{1..180}'\b='
                        case $auto in
                                true|yes)
                                        dpkg-lock.sh apt --show-progress --assume-yes --allow-change-held-packages 2>&1 remove "$bin_package_name" | tee -a $log
                                        if [[ $return_code -eq 0 ]]; then
                                                removed_bin_package_names_versions=("${removed_bin_package_names_versions[@]}" "${bin_package_name}=${bin_package_installed_version}")
                                        else
                                                aplay /usr/share/emacs/site-lisp/emacspeak/sounds/classic/alert-user.wav
                                                exit 1
                                        fi
                                        ;;
                                false|no)
                                        dpkg-lock.sh apt --show-progress 2>&1 remove "$bin_package_name" | tee -a $log
                                        return_code=$?
                                        if [[ $return_code -eq 0 ]]; then
                                                removed_bin_package_names_versions=("${removed_bin_package_names_versions[@]}" "${bin_package_name}=${bin_package_installed_version}")
                                        elif [[ $return_code -ne 1 ]]; then
                                                aplay /usr/share/emacs/site-lisp/emacspeak/sounds/classic/alert-user.wav
                                                exit 1
                                        fi
                                        ;;
                                *)
                                        printf "\n"
                                        echo -e ''$_{1..180}'\b-'
                                        echo "Unknown auto: $auto"
                                        echo -e ''$_{1..180}'\b-'
                                        exit 1
                                        ;;
                        esac
                        ;;
                *)
                        printf "\n"
                        echo -e ''$_{1..180}'\b-'
                        echo "Unknown purge_enabled: $purge_enabled"
                        echo -e ''$_{1..180}'\b-'
                        exit 1
                        ;;
        esac

        # Waiting for background process to do its job
        wait

        if [[ -n $(grep "following packages have unmet dependencies" $log) ]]; then
                case $purge_enabled in
                        true|yes)
                                echo -e ''$_{1..180}'\b='
                                echo "Couldn't purge: ${bin_package_name}=${bin_package_installed_version}"
                                echo -e ''$_{1..180}'\b='
                                ;;
                        false|no)
                                echo -e ''$_{1..180}'\b='
                                echo "Couldn't remove: ${bin_package_name}=${bin_package_installed_version}"
                                echo -e ''$_{1..180}'\b='
                                ;;
                esac
                not_removed_bin_package_names_versions=("${not_removed_bin_package_names_versions[@]}" "${bin_package_name}=${bin_package_installed_version}")
        elif [[ -n "$(grep Removing $log)" ]]; then
                # $bin_package_name has been removed/purged potentially alongside others due to dependencies
                removed_bin_packages_string=$(awk "/^The following packages will be REMOVED/{flag=1; next} /^[0-9]+ upgraded, [0-9]+ newly installed, [0-9]+ to remove and [0-9]+ not upgraded/{flag=0} flag" "$log" | head -n 1 | sed 's|*||g' | sed -E "s|^[[:blank:]]+(.*)$|\1|")
                removed_bin_packages_array=()
                # Transforming a list of blank-separated strings into an array
                IFS=' ' read -r -a removed_bin_packages_array <<< "$removed_bin_packages_string"

                for removed_bin_package_name in "${removed_bin_packages_array[@]}"
                do
                        grep_esc_removed_bin_package_name="$(echo "$removed_bin_package_name" | escape_grep_pcre_search_string)"
                        removed_bin_package_index=$(printf "%s\n" "${sorted_bin_package_names[@]}" | grep -nP "^${grep_esc_removed_bin_package_name}$" | cut -d ':' -f 1)
                        ((removed_bin_package_index--))
                        # Removing $removed_bin_package_name from ${sorted_bin_package_names[@]} array
                        # Beware that the unset creates a gap in the array but modifies its size
                        # It also messes with the index if the removed cell index is lower than or equal to the current cell index
                        unset "sorted_bin_package_names[${removed_bin_package_index}]"
                        # Rebuilding the array to fill the gap
                        sorted_bin_package_names=("${sorted_bin_package_names[@]}")

                        if [[ "$removed_bin_package_name" == "$bin_package_name" ]]; then
                                # Otherwise, the next package in ${sorted_bin_package_names[@]} array will be skipped
                                ((s--))
                        else
                                removed_bin_package_names_versions=("${removed_bin_package_names_versions[@]}" "${removed_bin_package_name}}")
                        fi
                done
        fi
done

if [[ -e $log ]]; then
        rm -f $log
fi

if [[ ${#removed_bin_package_names_versions[@]} -ne 0 ]]; then
        printf "\n"
        echo -e ''$_{1..180}'\b='
        case $purge_enabled in
                true|yes)
                        echo "The following binary packages have been purged:"
                        ;;
                false|no)
                        echo "The following binary packages have been removed:"
                        ;;
        esac
        echo -e ''$_{1..180}'\b='
        printf "%s\n" "${removed_bin_package_names_versions[@]}" | sort -uV | sed -E 's|=| <--> |g'
fi

if [[ ${#not_removed_bin_package_names_versions[@]} -ne 0 ]]; then
        printf "\n"
        echo -e ''$_{1..180}'\b='
        case $purge_enabled in
                true|yes)
                        echo "The following binary packages couldn't be purged:"
                        ;;
                false|no)
                        echo "The following binary packages couldn't be removed:"
                        ;;
        esac
        echo -e ''$_{1..180}'\b='
        printf "%s\n" "${not_removed_bin_package_names_versions[@]}" | sort -uV | sed -E 's|=| <--> |g'
fi

aplay /usr/share/sounds/purple/login.wav
exit 0