tazpkg view modules/remove @ rev 931

Add module/bb: manage Busybox applets.
Remove function definitions that are now in the /lib/libtaz.sh (die, im).
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sun Jan 08 11:24:12 2017 +0200 (2017-01-08)
parents fd96c4f9a889
children 50421cb50644
line source
1 #!/bin/sh
2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
3 # remove - TazPkg module
4 # Remove packages
7 # Connect function libraries
8 . /lib/libtaz.sh
10 # Get TazPkg working environment
11 . @@MODULES@@/getenv
16 # Log activity
18 log_pkg() {
19 [ -w "$LOG" ] &&
20 echo "$(date +'%F %T') - $1 - $PACKAGE ($VERSION$EXTRAVERSION)" >> "$LOG"
21 }
24 # Block of receipt function callers
25 # Why? "Bad" receipt sourcing can redefine some vital TazPkg variables.
26 # Few receipts function should be patched now.
28 # Input: $1 = path to the receipt to be processed
30 call_pre_remove() {
31 local tmp
32 if grep -q '^pre_remove()' "$1"; then
33 action 'Execute pre-remove commands...'
34 tmp="$(mktemp)"
35 cp "$1" "$tmp"
36 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
37 ( . "$tmp"; pre_remove "$root" )
38 status
39 rm "$tmp"
40 fi
41 }
43 call_post_remove() {
44 local tmp
45 if grep -q '^post_remove()' "$1"; then
46 action 'Execute post-remove commands...'
47 tmp="$(mktemp)"
48 cp "$1" "$tmp"
49 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
50 ( . "$tmp"; post_remove "$root" )
51 status
52 rm "$tmp"
53 fi
54 }
57 # return possible name for a virtual package name
59 virtual_pkg() {
60 # input: $1 virtual package name
61 # $2 repository db directory
62 # output: display possible package name
64 debug "\nvirtual_pkg('$1', '$2')"
65 local i
66 unset IFS
67 for i in $(grep -hs "^$1=" "$2/packages.equiv" | sed "s/^$1=//"); do
68 if echo $i | fgrep -q : ; then
69 # format 'alternative:newname'
70 # if alternative is installed then substitute newname
71 if [ -f $INSTALLED/${i%:*}/receipt ]; then
72 # substitute package dependency
73 echo ${i#*:}
74 return
75 fi
76 elif ! grep -q "^$1 " "$2/packages.info" || [ -f "$INSTALLED/$i/receipt" ]; then
77 # unconditional substitution
78 echo $i
79 return
80 fi
81 done
82 # the real package name
83 echo $1
84 }
89 for rep in $PRIORITY; do
90 [ ! -f "$rep/packages.info" ] && continue
91 PACKAGE="$(virtual_pkg "$1" "$rep")"
92 [ "$PACKAGE" != "$1" ] && break
93 done
95 if [ ! -f "$INSTALLED/$PACKAGE/receipt" ]; then
96 newline; _ 'Package "%s" is not installed.' "$PACKAGE"
97 exit 1
98 fi
100 . "$INSTALLED/$PACKAGE/receipt"
102 # Info #1: dependent packages (to be processed later)
103 ALTERED="$(awk -F$'\t' -vp=" $PACKAGE " 'index(" " $8 " ", p) { printf " %s\n", $1 }' "$PKGS_DB/installed.info")"
105 if [ -n "$ALTERED" ]; then
106 _ 'The following packages depend on package "%s":' "$PACKAGE"
107 echo "$ALTERED"
108 fi
110 # Info #2: changed packages (to be processed later)
111 REFRESH=$(cd "$INSTALLED"; grep -sl "^$PACKAGE$" */modifiers)
113 if [ -n "$REFRESH" ]; then
114 _ 'The following packages have been modified by package "%s":' "$PACKAGE"
115 for i in $REFRESH; do
116 echo " ${i%/modifiers}"
117 done
118 fi
120 # Confirmation
121 if [ -n "$noconfirm" ] || im && [ -z "$auto" ]; then
122 confirm "$(_ 'Remove package "%s" (%s)? (y/N)' "$PACKAGE" "$VERSION$EXTRAVERSION")"
123 if [ "$?" -ne 0 ]; then
124 newline; _ 'Uninstallation of package "%s" cancelled.' "$PACKAGE"
125 exit 0
126 fi
127 fi
128 # We are here: non-interactive mode, or --auto, or --yes, or answer 'y'
130 # Removing package
131 title 'Removing package "%s"' "$PACKAGE"
133 # Unblock package quietly; otherwise:
134 # 1. We can no longer install the package one more time - because it is blocked
135 # 2. We can no longer unblock the package - because it is not installed
136 tazpkg -u "$PACKAGE" --nowarning | grep -v '^$'
138 # [1/5] Pre-remove commands
139 call_pre_remove "$INSTALLED/$PACKAGE/receipt"
142 # [2/5] Removing files
143 action 'Removing all files installed...'
145 # NOTE: package 'faenza-icon-theme' install time: 12s; removing time ~ 11min on my system o_O
146 # After optimization: 3s! (Long) for-loops are (big) evil ;)
148 # NOTE: many packages contains filenames with spaces:
149 # lzcat /var/lib/tazpkg/files.list.lzma | awk -F" " '{if(NF>2)print $1}' | sed 's|:$||' | uniq
150 # Redefine IFS to only-new-line field separator:
151 IFS=$'\n'
153 files2remove="$(mktemp)"
155 debug '\nDetermine which files to remove...'
156 if [ -f "$INSTALLED/$PACKAGE/modifiers" ]; then
157 debug ' (modifiers detected)'
159 mods="$(mktemp)"
160 for mod in $(cat "$INSTALLED/$PACKAGE/modifiers"); do
161 cat "$INSTALLED/$mod/files.list" >> "$mods" 2>/dev/null
162 done
164 awk -vroot="$root" -vfl="$INSTALLED/$PACKAGE/files.list" '
165 {
166 if (FILENAME == fl)
167 f[$0] = 1;
168 else
169 f[$0] = "";
170 }
171 END {
172 for (i in f) {
173 if (f[i] == 1) printf "%s%s\n", root, i;
174 }
175 }' "$INSTALLED/$PACKAGE/files.list" "$mods" > "$files2remove"
176 rm "$mods"
177 else
178 debug ' (modifiers not detected)'
180 awk -vroot="$root" '{ printf "%s%s\n", root, $0; }' \
181 "$INSTALLED/$PACKAGE/files.list" > "$files2remove"
182 fi
184 debug 'Removing files...'
185 xargs rm -f < "$files2remove"
187 debug 'Removing folders...'
188 awk '
189 BEGIN {
190 FS = "/"; OFS = "/";
191 }
192 {
193 # removing filename beyond the last "/"
194 $NF = "";
195 if (! a[$0]) {
196 a[$0] = 1; print;
197 }
198 }' "$files2remove" | xargs rmdir -p 2>/dev/null
200 rm "$files2remove"
201 unset IFS
203 status
205 # [3/5] Post-remove commands
206 call_post_remove "$INSTALLED/$PACKAGE/receipt"
207 # restore Busybox applets
208 . @@MODULES@@/bb restore
211 # [4/5] Update system databases
212 fl="$INSTALLED/$PACKAGE/files.list"; upd=0
214 fgrep /usr/share/applications/ "$fl" | fgrep -q .desktop && udesk='yes'
215 fgrep -q /usr/share/mime "$fl" && umime='yes'
216 fgrep -q /usr/share/icon/hicolor "$fl" && uicon='yes'
217 fgrep /usr/share/glib-2.0/schemas "$fl" | fgrep -q .xml && uschm='yes'
218 fgrep /usr/lib/gdk-pixbuf "$fl" | fgrep -q .so && upixb='yes'
219 if fgrep -q /lib/modules "$fl"; then
220 ukrnl='yes'
221 if fgrep -q /kernel/fs/ "$fl"; then
222 ukrnlfs='yes'
223 fi
224 fi
226 if [ -n "$udesk$umime$uicon$uschm$upixb$ukrnl" ]; then
227 action 'Update system databases...'
228 upd=1
229 fi
231 # package 'desktop-file-utils'
232 [ -n "$udesk" ] && chroot "$root/" /usr/bin/update-desktop-database /usr/share/applications 2>/dev/null
233 # package 'shared-mime-info'
234 [ -n "$umime" ] && chroot "$root/" /usr/bin/update-mime-database /usr/share/mime
235 # packages 'gtk+', 'gtk+3'
236 [ -n "$uicon" ] && chroot "$root/" /usr/bin/gtk-update-icon-cache /usr/share/icons/hicolor
237 # package 'glib'
238 # hide messages like next because they are unresolved (we may to patch glib to hide them, almost the same)
239 # warning: Schema '*' has path '*'. Paths starting with '/apps/', '/desktop/' or '/system/' are deprecated.
240 [ -n "$uschm" ] && chroot "$root/" /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas 2>&1 | fgrep -v '/apps/'
241 # package 'gdk-pixbuf'
242 [ -n "$upixb" ] && chroot "$root/" /usr/bin/gdk-pixbuf-query-loaders --update-cache
244 if [ -n "$ukrnlfs" ]; then
245 for i in $(awk -F/ '{if($6=="fs" && $8~$7)print $7}' "$fl" | sort -u); do
246 grep -i "/^$i\$/d" "$root/etc/filesystems"
247 done
248 fi
249 # packages 'busybox', 'kmod', 'depmod'
250 [ -n "$ukrnl" ] && grep '/lib/modules' "$fl" | cut -d'/' -f4 | uniq | xargs chroot "$root/" /sbin/depmod -a
252 [ "$upd" -eq 1 ] && status
255 # [5/5] Remove package receipt and remove it from databases
256 action 'Removing package receipt...'
257 rm -rf "$INSTALLED/$PACKAGE"
258 sed -i "/ $PACKAGE-$VERSION$EXTRAVERSION.tazpkg$/d" "$PKGS_DB/installed.$SUM"
259 sed -i "/^$PACKAGE /d" "$PKGS_DB/installed.info"
260 status
262 footer "$(_ 'Package "%s" (%s) removed.' "$PACKAGE" "$VERSION$EXTRAVERSION")"
264 # Log this activity
265 log_pkg Removed
267 # Stop if non-interactive mode and no --auto option
268 if ! im && [ -z "$auto" ]; then exit 0; fi
270 # Process dependent packages
271 if [ -n "$ALTERED" ]; then
272 if [ -n "$auto" ]; then
273 answer=0
274 else
275 confirm "$(_ 'Remove packages depending on package "%s"? (y/N)' "$PACKAGE")"
276 answer=$?
277 fi