-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpaccache-service-manager
More file actions
executable file
·264 lines (227 loc) · 9.49 KB
/
paccache-service-manager
File metadata and controls
executable file
·264 lines (227 loc) · 9.49 KB
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
#!/bin/bash
#
# Simple service manager for pacman cache cleaner.
#
# Requires packages:
# pacman-contrib (for paccache)
# yad (for yad)
# polkit (for pkexec)
#
source /usr/share/endeavouros/scripts/eos-script-lib-yad || exit 1
export -f eos_yad
AssignIconVariables_in_eos_bash_shared
PaccacheOptions_for_keep() {
local opts=""
local lopts="dryrun,move:,remove,arch:,cachedir:,force,help,ignore:,keep:,min-atime:,min-mtime:,nocolor,null,quiet,uninstalled,verbose,version"
local sopts="dm:ra:c:i:fhk:zquvV"
opts="$(/usr/bin/getopt -o="$sopts" --longoptions "$lopts" --name "$progname" -- "$@")" || {
$OptFunc -h
return 1
}
eval set -- "$opts"
while true ; do
case "$1" in
--) shift; break ;;
# these options we simply ignore or don't want:
-d|--dryrun|-m|--move) DIE "$FUNCNAME: paccache option $1 was not expected" ;;
-a|--arch|-c|--cachedir|-i|--ignore|--min-atime|--min-mtime) shift ;;
-f|--force|-h|--help|--nocolor|-z|--null|-q|--quiet|-u|--uninstalled|-v|--verbose|-V|--version) ;;
# this is what we are looking for:
-r|--remove) current_keepcount=3 ;;
-k|--keep) current_keepcount="$2"
[ "${current_keepcount//[0-9]/}" ] && DIE "cannot parse paccache options.\nFile:\n $s\nLine:\n $exec_start_line"
shift
;;
esac
shift
done
}
DIE() {
local msg="$1"
shift
if false ; then
echo "Error: $msg" | eos_yad --text-info --image=$ICO_ERROR --title="$progname error" \
--wrap --width=500 --height=300 --button=yad-quit:1 "$@"
else
eos_yad --form --image=$ICO_ERROR --title="$progname error" --text="Error: $msg" --button=yad-quit:1 --scroll "$@"
fi
exit 1
}
GetCurrentValues() {
local t="$timer"
local s="$service"
if [ ! -r $s ] ; then
# service is only under /usr/lib/... so get current values from there
t="$dir2/$pt"
s="$dir2/paccache.service"
local why="Is pacman-contrib installed?"
test -r $t || DIE "sorry, $t does not exist. $why"
test -r $s || DIE "sorry, $s does not exist. $why"
fi
current_period="$(grep "^OnCalendar=" $t | cut -d '=' -f 2)"
current_delete_uninstalled=FALSE
# Note: the ExecStart= line can be a bit tricky.
current_keepcount="$(grep "^ExecStart=.*-rk" $s | tail -n1 | sed -E 's|.*-rk([^ ;]*).*|\1|')" # only get the * of '-rk*' from the ExecStart= line
if [ -z "$current_keepcount" ] ; then
current_keepcount="$(grep "^ExecStart=.*keep=" $s | tail -n1 | sed -E 's|.*keep=([^ ;]*).*|\1|')" # only get the * of 'keep=*' from the ExecStart= line
fi
[ "$current_keepcount" ] || current_keepcount=3
# current_keepcount=$(eval echo ${current_keepcount})
# PaccacheOptions_for_keep $current_keepcount
local arg args=()
for arg in $current_keepcount ; do
case "$arg" in
'$PACCACHE_ARGS') args+=($PACCACHE_ARGS) ;;
*) args+=(-k "$arg") ;;
esac
done
PaccacheOptions_for_keep "${args[@]}"
}
Usage() {
cat <<EOF
Usage: $progname [options]
Options:
--defaults Enable manager with default values (run $period, keep latest $keepcount).
-v, --verbose Increase verbosity (= see more terminal output).
-h, --help This help.
EOF
[ -n "$1" ] && exit "$1"
}
AddCommand() {
local show=yes
case "$1" in
--silent) show=no; shift ;;
esac
local command="$1"
cmds+=" ; $command"
if [ "$verbose" = "yes" ] && [ "$show" = "yes" ] ; then
echo "$command" >> $vfile
fi
}
_paccache_cleaner_manager()
{
local progname=paccache-service-manager
eos_assert_deps $progname yad || return 1
local dir=/etc/systemd/system
local dir2=/usr/lib/systemd/system # current values may be only here...
local service=$dir/paccache.service
local timer=$dir/paccache.timer
local current_period
local current_keepcount
local current_delete_uninstalled
local pt="paccache.timer"
local cmd_yad
local cmds=":" cmds2=() cmd_period cmd_ruk=""
local xx
local period_vals=""
local txt
local image=$ICO_DISK # drive-harddisk # disk-utility
local result=""
local field_nr=1
local indent="<tt> </tt>"
local options=""
local defaults=no
local period=weekly keepcount=2 delete_uninstalled=FALSE
local first_time=no
local verbose=no
local vfile="" # temporary file for showing all commands in verbose mode
local keepcount_max=100
local -r cachedir=$(GetCacheDir)
local du="$(LANG=C /usr/bin/du --si -s "$cachedir" 2>/dev/null | /usr/bin/awk '{print $1}' | sed 's|\([KMGT]\)$| \1B|')"
local df="$(LANG=C /usr/bin/df --si | /usr/bin/grep -w / | /usr/bin/awk '{print $4}' | sed 's|\([KMGT]\)$| \1B|')"
if [ ! -r $service ] ; then
first_time=yes
fi
case "$1" in
-v | --verbose) options+=" -v"
options="${options# }" # drop leading space
verbose=yes
vfile="$(mktemp)"
;;
-h | --help) Usage 0
;;
--defaults) defaults=yes
;;
esac
GetCurrentValues
if [ "$defaults" = "no" ] ; then
txt="<b>Modifies the service that cleans up pacman cache periodically.\n"
txt+="Below you'll see settings and their current values.\n\n"
txt+="Current status:\n"
txt+="${indent}Package cache usage: $du\n"
txt+="${indent}Free space on disk: $df</b>\n"
txt+="\n"
# List possible period values, and set the default
for xx in daily weekly monthly ; do
test -n "$period_vals" && period_vals+="!" # separator
test "$xx" = "$current_period" && period_vals+="^" # default
period_vals+="$xx"
done
cmd_yad=(
eos_yad --form --width=500 --image=$image
--title='Pacman cache cleaner service manager'
--text="$txt"
--field='Cleaning period!How often the package cache will be cleaned':CB "$period_vals"
--field='Number of the latest package versions to keep!Keep only the selected number of each package':NUM "$current_keepcount"
--field='Remove uninstalled but still cached packages now!One time operation':CHK "$current_delete_uninstalled"
)
result="$("${cmd_yad[@]}" 2>/dev/null)"
[ -n "$result" ] || return # package kde-gtk-config should remove possible gtk related yad warnings...
# New values
period="$( echo "$result" | cut -d '|' -f $field_nr)" ; ((++field_nr))
keepcount="$( echo "$result" | cut -d '|' -f $field_nr)" ; ((++field_nr))
delete_uninstalled="$(echo "$result" | cut -d '|' -f $field_nr)" ; ((++field_nr))
# ad hoc value checks
[ "$(echo "$keepcount" | sed 's|[0-9]||g')" = "" ] || DIE "sorry, keep count value '$keepcount' is not a number"
[ $keepcount -le $keepcount_max ] || DIE "sorry, keep count value '$keepcount' is more than $keepcount_max and not supported"
case "$period" in
daily | weekly | monthly) ;;
*) DIE "sorry, cleaning period '$period' is unsupported" ;;
esac
case "$delete_uninstalled" in
TRUE | FALSE) ;;
*) DIE "sorry, value '$delete_uninstalled' for removing uninstalled packages is unsupported" ;;
esac
fi
# Build the final paccache command list.
# Clean up cache now and periodically
if [ "$delete_uninstalled" = "TRUE" ] ; then
cmd_ruk="paccache $options -ruk0"
fi
if [ "$keepcount $period $delete_uninstalled" != "$current_keepcount $current_period $current_delete_uninstalled" ] || [ "$first_time" = "yes" ] ; then
cmd_period="paccache $options -rk$keepcount"
if [ ! -r $service ] ; then
AddCommand "cp $dir2/$pt $timer"
AddCommand "cp $dir2/paccache.service $service"
fi
# Allow ExecStart line with both /usr/bin/paccache or just paccache.
AddCommand "sed -i $service -e 's|^ExecStart=paccache .*$|ExecStart=$cmd_period|' -e 's|^ExecStart=/usr/bin/paccache .*$|ExecStart=$cmd_period|'"
AddCommand "sed -i $timer -e 's|^OnCalendar=.*$|OnCalendar=$period|'"
AddCommand "systemctl enable --now $pt"
AddCommand "systemctl daemon-reload"
fi
if [ -n "$cmd_ruk" ] ; then
AddCommand "$cmd_ruk"
fi
# Execute the command list.
if [ "$cmds" != ":" ] ; then
AddCommand --silent "echo Service reloaded."
if [ "$verbose" = "yes" ] ; then
# pkexec bash -c "cat '$vfile' ; $cmds"
RunInTerminal "cat \"$vfile\"; $EOS_ROOTER \"$cmds\""
rm -f "$vfile"
else
# pkexec bash -c "$cmds"
if [ "$verbose" = "yes" ] ; then
RunInTerminal "cat \"$vfile\"; $EOS_ROOTER \"$cmds\""
else
RunInTerminal "echo \"Running selected commands.\"; $EOS_ROOTER \"$cmds\""
fi
fi
else
# echo "No change to settings."
local msg="Nothing to do (settings not changed)"
eos_yad --form --image=$ICO_INFO --title="$progname" --text="$msg" --button=yad-quit:1 --scroll
fi
}
_paccache_cleaner_manager "$@"