tazpkg annotate modules/remove @ rev 844

Finish modularization. Beta release: still have few FIXMEs and TODOs.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Oct 05 03:53:47 2015 +0300 (2015-10-05)
parents a02e36d44d06
children b6daeaa95431
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 remove_with_path() {
al@840 17 # Avoid dirname errors by checking for argument.
al@840 18 [ -n "$1" ] || return
al@840 19
al@840 20 local dir
al@840 21 rm -f $1 2>/dev/null
al@840 22 dir="$1"
al@840 23 while [ "$dir" != "/" ]; do
al@840 24 dir="$(dirname "$dir")"
al@840 25 rmdir "$dir" 2>/dev/null || break
al@840 26 done
al@840 27 }
al@840 28
al@840 29
al@840 30 grepesc() {
al@840 31 sed 's/\[/\\[/g'
al@840 32 }
al@840 33
al@840 34
al@840 35 # Log activity
al@840 36
al@840 37 log_pkg() {
al@840 38 [ -w "$LOG" ] &&
al@840 39 echo "$(date +'%F %T') - $1 - $PACKAGE ($VERSION$EXTRAVERSION)" >> "$LOG"
al@840 40 }
al@840 41
al@840 42
al@840 43 # Interactive mode
al@840 44
al@840 45 im() { tty -s; }
al@840 46
al@840 47
al@844 48 # Block of receipt function callers
al@844 49 # Why? "Bad" receipt sourcing can redefine some vital TazPkg variables.
al@844 50 # Few receipts function should be patched now.
al@844 51
al@844 52 # Input: $1 = path to the receipt to be processed
al@844 53
al@844 54 call_pre_remove() {
al@844 55 local tmp
al@844 56 if grep -q '^pre_remove()' "$1"; then
al@844 57 action 'Execute pre-remove commands...'
al@844 58 tmp="$(mktemp)"
al@844 59 cp "$1" "$tmp"
al@844 60 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
al@844 61 ( . "$tmp"; pre_remove "$root" )
al@844 62 status
al@844 63 rm "$tmp"
al@844 64 fi
al@844 65 }
al@844 66
al@844 67 call_post_remove() {
al@844 68 local tmp
al@844 69 if grep -q '^post_remove()' "$1"; then
al@844 70 action 'Execute post-remove commands...'
al@844 71 tmp="$(mktemp)"
al@844 72 cp "$1" "$tmp"
al@844 73 sed -i 's|$1/*$INSTALLED|$INSTALLED|g' "$tmp"
al@844 74 ( . "$tmp"; post_remove "$root" )
al@844 75 status
al@844 76 rm "$tmp"
al@844 77 fi
al@844 78 }
al@844 79
al@844 80
al@840 81
al@840 82
al@840 83 PACKAGE="$1"
al@840 84
al@840 85 if [ ! -f "$INSTALLED/$PACKAGE/receipt" ]; then
al@840 86 newline; _ 'Package "%s" is not installed.' "$PACKAGE"
al@840 87 exit 1
al@840 88 fi
al@840 89
al@840 90 . "$INSTALLED/$PACKAGE/receipt"
al@840 91
al@840 92 # Info #1: dependent packages (to be processed later)
al@840 93 ALTERED="$(awk -F$'\t' -vp=" $PACKAGE " 'index(" " $8 " ", p) { printf " %s\n", $1 }' "$PKGS_DB/installed.info")"
al@840 94
al@840 95 if [ -n "$ALTERED" ]; then
al@840 96 _ 'The following packages depend on package "%s":' "$PACKAGE"
al@840 97 echo "$ALTERED"
al@840 98 fi
al@840 99
al@840 100 # Info #2: changed packages (to be processed later)
al@840 101 REFRESH=$(cd "$INSTALLED"; grep -sl "^$PACKAGE$" */modifiers)
al@840 102
al@840 103 if [ -n "$REFRESH" ]; then
al@840 104 _ 'The following packages have been modified by package "%s":' "$PACKAGE"
al@840 105 for i in $REFRESH; do
al@840 106 echo " ${i%/modifiers}"
al@840 107 done
al@840 108 fi
al@840 109
al@840 110 # Confirmation
al@840 111 if im && [ -z "$auto" ]; then
al@840 112 confirm "$(_ 'Remove package "%s" (%s)? (y/N)' "$PACKAGE" "$VERSION$EXTRAVERSION")"
al@840 113 if [ "$?" -ne 0 ]; then
al@840 114 newline; _ 'Uninstallation of package "%s" cancelled.' "$PACKAGE"
al@840 115 exit 0
al@840 116 fi
al@840 117 fi
al@840 118 # We are here: non-interactive mode, or --auto, or answer 'y'
al@840 119
al@840 120 # Removing package
al@840 121 title 'Removing package "%s"' "$PACKAGE"
al@840 122
al@840 123 # [1/4] Pre-remove commands
al@844 124 call_pre_remove "$INSTALLED/$PACKAGE/receipt"
al@844 125
al@840 126
al@840 127 # [2/4] Removing files
al@840 128 action 'Removing all files installed...'
al@840 129 if [ -f "$INSTALLED/$PACKAGE/modifiers" ]; then
al@840 130 for file in $(cat "$INSTALLED/$PACKAGE/files.list"); do
al@840 131 for mod in $(cat "$INSTALLED/$PACKAGE/modifiers"); do
al@840 132 [ -f "$INSTALLED/$mod/files.list" ] && \
al@840 133 [ $(grep "^$(echo $file | grepesc)$" "$INSTALLED/$mod/files.list" | wc -l) -gt 1 ] && \
al@840 134 continue 2
al@840 135 done
al@840 136 [ -n "$debug" ] && echo "remove_with_path $root$file"
al@840 137 remove_with_path $root$file
al@840 138 done
al@840 139 else
al@840 140 for file in $(cat "$INSTALLED/$PACKAGE/files.list"); do
al@840 141 [ -n "$debug" ] && echo "remove_with_path $root$file"
al@840 142 remove_with_path $root$file
al@840 143 done
al@840 144 fi
al@840 145 status
al@840 146
al@840 147 # [3/4] Post-remove commands
al@844 148 call_post_remove "$INSTALLED/$PACKAGE/receipt"
al@840 149
al@840 150 # [4/4] Remove package receipt and remove it from databases
al@840 151 action 'Removing package receipt...'
al@840 152 rm -rf "$INSTALLED/$PACKAGE"
al@840 153 sed -i "/ $PACKAGE-$VERSION$EXTRAVERSION.tazpkg$/d" "$PKGS_DB/installed.$SUM"
al@840 154 sed -i "/^$PACKAGE /d" "$PKGS_DB/installed.info"
al@840 155 status
al@840 156
al@840 157 footer "$(_ 'Package "%s" (%s) removed.' "$PACKAGE" "$VERSION$EXTRAVERSION")"
al@840 158
al@840 159 # Log this activity
al@840 160 log_pkg Removed
al@840 161
al@840 162 # Stop if non-interactive mode and no --auto option
al@840 163 if ! im && [ -z "$auto" ]; then exit 0; fi
al@840 164
al@840 165 # Process dependent packages
al@840 166 if [ -n "$ALTERED" ]; then
al@840 167 if [ -n "$auto" ]; then
al@840 168 answer=0
al@840 169 else
al@840 170 confirm "$(_ 'Remove packages depending on package "%s"? (y/N)' "$PACKAGE")"
al@840 171 answer=$?
al@840 172 fi
al@840 173 if [ "$answer" -eq 0 ]; then
al@840 174 for i in $ALTERED; do
al@840 175 if [ -d "$INSTALLED/$i" ]; then
al@840 176 tazpkg remove $i
al@840 177 fi
al@840 178 done
al@840 179 fi
al@840 180 fi
al@840 181
al@840 182 # Process changed packages
al@840 183 if [ -n "$REFRESH" ]; then
al@840 184 if [ -n "$auto" ]; then
al@840 185 answer=0
al@840 186 else
al@840 187 confirm "$(_ 'Reinstall packages modified by package "%s"? (y/N)' "$PACKAGE")"
al@840 188 answer=$?
al@840 189 fi
al@840 190 if [ "$answer" -eq 0 ]; then
al@840 191 for i in $REFRESH; do
al@840 192 if [ "$(wc -l < "$INSTALLED/$i")" -gt 1 ]; then
al@840 193 _ 'Check %s for reinstallation' "$INSTALLED/$i"
al@840 194 continue
al@840 195 fi
al@840 196 rm -r "$INSTALLED/$i"
al@840 197 tazpkg get-install ${i%/modifiers} --forced
al@840 198 done
al@840 199 fi
al@840 200 fi
al@840 201