tazpkg annotate modules/remove @ rev 860

remove: using "rmdir -p" instead of loop (thanks Lucas Levrel)
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sat Nov 14 14:41:30 2015 +0200 (2015-11-14)
parents d62dc010e0ee
children 1362693564d1
rev   line source
al@840 1 #!/bin/sh
al@840 2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
al@840 3 # remove - TazPkg module
al@840 4 # Remove packages
al@840 5
al@840 6
al@840 7 # Connect function libraries
al@840 8 . /lib/libtaz.sh
al@840 9
al@840 10 # Get TazPkg working environment
al@840 11 . @@MODULES@@/getenv
al@840 12
al@840 13
al@840 14
al@840 15
al@840 16 # Log activity
al@840 17
al@840 18 log_pkg() {
al@840 19 [ -w "$LOG" ] &&
al@840 20 echo "$(date +'%F %T') - $1 - $PACKAGE ($VERSION$EXTRAVERSION)" >> "$LOG"
al@840 21 }
al@840 22
al@840 23
al@840 24 # Interactive mode
al@840 25
al@840 26 im() { tty -s; }
al@840 27
al@840 28
al@844 29 # Block of receipt function callers
al@844 30 # Why? "Bad" receipt sourcing can redefine some vital TazPkg variables.
al@844 31 # Few receipts function should be patched now.
al@844 32
al@844 33 # Input: $1 = path to the receipt to be processed
al@844 34
al@844 35 call_pre_remove() {
al@844 36 local tmp
al@844 37 if grep -q '^pre_remove()' "$1"; then
al@844 38 action 'Execute pre-remove commands...'
al@844 39 tmp="$(mktemp)"
al@844 40 cp "$1" "$tmp"
al@844 41 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
al@844 42 ( . "$tmp"; pre_remove "$root" )
al@844 43 status
al@844 44 rm "$tmp"
al@844 45 fi
al@844 46 }
al@844 47
al@844 48 call_post_remove() {
al@844 49 local tmp
al@844 50 if grep -q '^post_remove()' "$1"; then
al@844 51 action 'Execute post-remove commands...'
al@844 52 tmp="$(mktemp)"
al@844 53 cp "$1" "$tmp"
al@844 54 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
al@844 55 ( . "$tmp"; post_remove "$root" )
al@844 56 status
al@844 57 rm "$tmp"
al@844 58 fi
al@844 59 }
al@844 60
al@844 61
al@840 62
al@840 63
al@840 64 PACKAGE="$1"
al@840 65
al@840 66 if [ ! -f "$INSTALLED/$PACKAGE/receipt" ]; then
al@840 67 newline; _ 'Package "%s" is not installed.' "$PACKAGE"
al@840 68 exit 1
al@840 69 fi
al@840 70
al@840 71 . "$INSTALLED/$PACKAGE/receipt"
al@840 72
al@840 73 # Info #1: dependent packages (to be processed later)
al@840 74 ALTERED="$(awk -F$'\t' -vp=" $PACKAGE " 'index(" " $8 " ", p) { printf " %s\n", $1 }' "$PKGS_DB/installed.info")"
al@840 75
al@840 76 if [ -n "$ALTERED" ]; then
al@840 77 _ 'The following packages depend on package "%s":' "$PACKAGE"
al@840 78 echo "$ALTERED"
al@840 79 fi
al@840 80
al@840 81 # Info #2: changed packages (to be processed later)
al@840 82 REFRESH=$(cd "$INSTALLED"; grep -sl "^$PACKAGE$" */modifiers)
al@840 83
al@840 84 if [ -n "$REFRESH" ]; then
al@840 85 _ 'The following packages have been modified by package "%s":' "$PACKAGE"
al@840 86 for i in $REFRESH; do
al@840 87 echo " ${i%/modifiers}"
al@840 88 done
al@840 89 fi
al@840 90
al@840 91 # Confirmation
al@840 92 if im && [ -z "$auto" ]; then
al@840 93 confirm "$(_ 'Remove package "%s" (%s)? (y/N)' "$PACKAGE" "$VERSION$EXTRAVERSION")"
al@840 94 if [ "$?" -ne 0 ]; then
al@840 95 newline; _ 'Uninstallation of package "%s" cancelled.' "$PACKAGE"
al@840 96 exit 0
al@840 97 fi
al@840 98 fi
al@840 99 # We are here: non-interactive mode, or --auto, or answer 'y'
al@840 100
al@840 101 # Removing package
al@840 102 title 'Removing package "%s"' "$PACKAGE"
al@840 103
al@840 104 # [1/4] Pre-remove commands
al@844 105 call_pre_remove "$INSTALLED/$PACKAGE/receipt"
al@844 106
al@840 107
al@840 108 # [2/4] Removing files
al@840 109 action 'Removing all files installed...'
al@857 110
al@857 111 # NOTE: package 'faenza-icon-theme' install time: 12s; removing time ~ 11min on my system o_O
al@860 112 # After optimization: 3s! (Long) for-loops are (big) evil ;)
al@857 113
al@857 114 # NOTE: many packages contains filenames with spaces:
al@857 115 # lzcat /var/lib/tazpkg/files.list.lzma | awk -F" " '{if(NF>2)print $1}' | sed 's|:$||' | uniq
al@857 116 # Redefine IFS to only-new-line field separator:
al@857 117 IFS=$'\n'
al@857 118
al@857 119 files2remove="$(mktemp)"
al@857 120
al@857 121 debug '\nDetermine which files to remove...'
al@840 122 if [ -f "$INSTALLED/$PACKAGE/modifiers" ]; then
al@857 123 debug ' (modifiers detected)'
al@857 124
al@857 125 mods="$(mktemp)"
al@857 126 for mod in $(cat "$INSTALLED/$PACKAGE/modifiers"); do
al@857 127 cat "$INSTALLED/$mod/files.list" >> "$mods" 2>/dev/null
al@840 128 done
al@857 129
al@857 130 awk -vroot="$root" -vfl="$INSTALLED/$PACKAGE/files.list" '
al@857 131 {
al@857 132 if (FILENAME == fl)
al@857 133 f[$0] = 1;
al@857 134 else
al@857 135 f[$0] = "";
al@857 136 }
al@857 137 END {
al@857 138 for (i in f) {
al@857 139 if (f[i] == 1) printf "%s%s\n", root, i;
al@857 140 }
al@857 141 }' "$INSTALLED/$PACKAGE/files.list" "$mods" > "$files2remove"
al@857 142 rm "$mods"
al@840 143 else
al@857 144 debug ' (modifiers not detected)'
al@857 145
al@857 146 awk -vroot="$root" '{ printf "%s%s\n", root, $0; }' \
al@857 147 "$INSTALLED/$PACKAGE/files.list" > "$files2remove"
al@857 148 fi
al@857 149
al@857 150 debug 'Removing files...'
al@857 151 xargs rm -f < "$files2remove"
al@857 152
al@860 153 debug 'Removing folders...'
al@857 154 awk '
al@857 155 BEGIN {
al@857 156 FS = "/"; OFS = "/";
al@857 157 }
al@857 158 {
al@857 159 # removing filename beyond the last "/"
al@857 160 $NF = "";
al@857 161 if (! a[$0]) {
al@857 162 a[$0] = 1; print;
al@857 163 }
al@860 164 }' "$files2remove" | xargs rmdir -p 2>/dev/null
al@857 165
al@860 166 rm "$files2remove"
al@857 167 unset IFS
al@857 168
al@840 169 status
al@840 170
al@840 171 # [3/4] Post-remove commands
al@844 172 call_post_remove "$INSTALLED/$PACKAGE/receipt"
al@840 173
al@840 174 # [4/4] Remove package receipt and remove it from databases
al@840 175 action 'Removing package receipt...'
al@840 176 rm -rf "$INSTALLED/$PACKAGE"
al@840 177 sed -i "/ $PACKAGE-$VERSION$EXTRAVERSION.tazpkg$/d" "$PKGS_DB/installed.$SUM"
al@840 178 sed -i "/^$PACKAGE /d" "$PKGS_DB/installed.info"
al@840 179 status
al@840 180
al@840 181 footer "$(_ 'Package "%s" (%s) removed.' "$PACKAGE" "$VERSION$EXTRAVERSION")"
al@840 182
al@840 183 # Log this activity
al@840 184 log_pkg Removed
al@840 185
al@840 186 # Stop if non-interactive mode and no --auto option
al@840 187 if ! im && [ -z "$auto" ]; then exit 0; fi
al@840 188
al@840 189 # Process dependent packages
al@840 190 if [ -n "$ALTERED" ]; then
al@840 191 if [ -n "$auto" ]; then
al@840 192 answer=0
al@840 193 else
al@840 194 confirm "$(_ 'Remove packages depending on package "%s"? (y/N)' "$PACKAGE")"
al@840 195 answer=$?
al@840 196 fi
al@840 197 if [ "$answer" -eq 0 ]; then
al@840 198 for i in $ALTERED; do
al@840 199 if [ -d "$INSTALLED/$i" ]; then
al@840 200 tazpkg remove $i
al@840 201 fi
al@840 202 done
al@840 203 fi
al@840 204 fi
al@840 205
al@840 206 # Process changed packages
al@840 207 if [ -n "$REFRESH" ]; then
al@840 208 if [ -n "$auto" ]; then
al@840 209 answer=0
al@840 210 else
al@840 211 confirm "$(_ 'Reinstall packages modified by package "%s"? (y/N)' "$PACKAGE")"
al@840 212 answer=$?
al@840 213 fi
al@840 214 if [ "$answer" -eq 0 ]; then
al@840 215 for i in $REFRESH; do
al@840 216 if [ "$(wc -l < "$INSTALLED/$i")" -gt 1 ]; then
al@849 217 _ 'Package "%s" was modified by "%s" and other packages. It will not be reinstalled.' \
al@849 218 "${i%/modifiers}" "$PACKAGE"
al@849 219 _ 'Check "%s" for reinstallation.' "$INSTALLED/$i"
al@849 220
al@840 221 continue
al@840 222 fi
al@840 223 rm -r "$INSTALLED/$i"
al@840 224 tazpkg get-install ${i%/modifiers} --forced
al@840 225 done
al@840 226 fi
al@840 227 fi
al@840 228