tazpkg annotate modules/recharge @ rev 836

Add module "upgrade"; make bullet-proof code to work with "root" ("recharge" and "upgrade" modules); re-make depends/rdepends commands.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sat Aug 15 17:41:08 2015 +0300 (2015-08-15)
parents f1c82b2d20aa
children a02e36d44d06
rev   line source
al@834 1 #!/bin/sh
al@834 2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
al@834 3 # recharge - TazPkg module
al@834 4 # Recharge packages databases from a mirror
al@834 5
al@834 6
al@834 7 # Options:
al@834 8 # [main|<repository>] Repository name to recharge (all if empty)
al@834 9
al@834 10 # Environment variables:
al@834 11 # root Root of the packages DB
al@834 12 # UA User Agent string ("TazPkg-<version>")
al@834 13
al@834 14
al@834 15 # Connect function libraries
al@834 16 . /lib/libtaz.sh
al@834 17
al@836 18
al@836 19
al@836 20
al@836 21 # Get TazPkg working environment
al@836 22 # ------------------------------
al@836 23
al@836 24 [ ! -d "$root/etc/slitaz" ] && mkdir -p "$root/etc/slitaz"
al@836 25 [ ! -e "$root/etc/slitaz/slitaz.conf" ] && cp /etc/slitaz/slitaz.conf "$root/etc/slitaz"
al@836 26 [ ! -e "$root/etc/slitaz/tazpkg.conf" ] && cp /etc/slitaz/tazpkg.conf "$root/etc/slitaz"
al@836 27 [ ! -e "$root/etc/slitaz-release" ] && echo 'cooking' > "$root/etc/slitaz-release"
al@836 28
al@836 29 # Read configuration
al@836 30 if [ -n "$root" ]; then
al@836 31 # Patch external conf files to correctly handle --root value
al@836 32 slitaz_conf=$(mktemp); cp "$root/etc/slitaz/slitaz.conf" $slitaz_conf
al@836 33 tazpkg_conf=$(mktemp); cp "$root/etc/slitaz/tazpkg.conf" $tazpkg_conf
al@836 34 sed -i "s| /| $root/|g; s|\"/|\"$root/|g" $slitaz_conf $tazpkg_conf
al@836 35 . $slitaz_conf; . $tazpkg_conf; rm $slitaz_conf $tazpkg_conf
al@836 36 else
al@836 37 . /etc/slitaz/slitaz.conf; . /etc/slitaz/tazpkg.conf
al@836 38 fi
al@836 39
al@836 40 # Silent make missing files
al@834 41 mkdir -p "$PKGS_DB"
al@834 42 [ ! -e "$PKGS_DB/mirror" ] && echo "$ONLINE_PKGS" > "$PKGS_DB/mirror"
al@834 43
al@834 44
al@836 45
al@836 46
al@834 47 # Functions
al@834 48 # ---------
al@834 49
al@834 50 # Download a file from specified mirror
al@834 51
al@836 52 get_from_mirror() {
al@836 53 case "$mirror" in
al@836 54 http://* | https://* | ftp://*)
al@836 55 busybox wget -c -q -T 30 -U $UA "$mirror$1" 2>/dev/null;;
al@836 56 *)
al@836 57 ln -sf "$mirror$1" .;;
al@836 58 esac
al@836 59 status
al@834 60 }
al@834 61
al@834 62
al@834 63 # When recharging errors occur
al@834 64
al@834 65 recharging_failed() {
al@834 66 # Restore database from bak files
al@834 67 action 'Restoring database files...'
al@834 68 [ -e 'ID' -a ! -e 'ID.bak' ] && rm ID
al@834 69 [ -e 'IDs' -a ! -e 'IDs.bak' ] && rm IDs
al@834 70 for file in $(ls $1/*.bak); do
al@834 71 mv -f $file ${file%.bak}
al@834 72 done
al@834 73 status
al@834 74
al@834 75 footer "$(colorize 31 "$(_ 'Recharging failed')")"
al@834 76 }
al@834 77
al@834 78
al@834 79
al@834 80
al@834 81 REPO="$1"
al@834 82
al@834 83 # What to recharge: main, or all, or selected undigest
al@834 84 case "$REPO" in
al@834 85 main) repo_to_recharge="$PKGS_DB";;
al@834 86 '') repo_to_recharge="$PKGS_DB $PKGS_DB/undigest/*";;
al@834 87 *) repo_to_recharge="$PKGS_DB/undigest/$REPO"
al@834 88 if [ ! -d "$repo_to_recharge" ]; then
al@834 89 _ "Repository \"%s\" doesn't exist." "$repo_to_recharge" >&2
al@834 90 exit 1
al@834 91 fi
al@834 92 ;;
al@834 93 esac
al@834 94
al@834 95 for path in $repo_to_recharge; do
al@834 96 [ ! -f $path/mirror ] && continue # skip
al@834 97 cd $path
al@836 98 # Mirror URL will have a trailing slash
al@836 99 mirror="$(cat mirror)"; mirror="${mirror%/}/"
al@834 100
al@834 101 # Repository name
al@834 102 if [ "$path" == "$PKGS_DB" ]; then
al@834 103 repo_name='Main'
al@834 104 else
al@834 105 repo_name="$(_n 'Undigest %s' "$(basename "$path")")"
al@834 106 fi
al@834 107
al@834 108 title 'Recharging repository "%s"' "$repo_name"
al@834 109
al@834 110 # Don't let ID be a symlink when using local repository.
al@834 111 if [ -h ID ]; then mv -f ID ID.lnk; cat ID.lnk > ID; rm ID.lnk; fi
al@834 112 if [ -h IDs ]; then mv -f IDs IDs.lnk; cat IDs.lnk > IDs; rm IDs.lnk; fi
al@834 113
al@834 114 [ -f ID ] && mv ID ID.bak # Compatibility with "old" ID
al@834 115 [ -f IDs ] && mv IDs IDs.bak
al@836 116 action 'Checking...'
al@836 117 get_from_mirror IDs
al@836 118
al@834 119 [ -e 'IDs' ] && awk '{print $1}' IDs > ID # Compatibility with "old" ID
al@836 120 [ -e 'IDs' ] && _ 'Database timestamp: %s' "$(date -d "@$(awk '{print $2}' IDs)" "+%x %R")"
al@834 121
al@834 122 # Check if recharging is needed
al@834 123 if [ -f 'IDs' ] && cmp -s IDs IDs.bak; then
al@834 124 footer "$(_ 'Repository "%s" is up to date.' "$repo_name")"
al@834 125 rm IDs.bak ID.bak
al@834 126 continue
al@834 127 fi
al@834 128 rm IDs.bak ID.bak 2>/dev/null
al@834 129
al@834 130 action 'Creating backup of the last packages list...'
al@834 131 for i in packages.desc packages.$SUM packages.txt packages.list \
al@834 132 packages.equiv files.list.lzma extra.list mirrors packages.info; do
al@834 133 [ -f "$i" ] && mv -f $i $i.bak 2>/dev/null
al@834 134 done
al@836 135 :; status # Always "[ Done ]"
al@834 136
al@834 137 # Download and extract bundle: extra.list, mirrors, files-list.md5,
al@834 138 # packages.{info,desc,md5,txt,list,equiv}
al@834 139 bundle='bundle.tar.lzma'
al@834 140 action 'Getting "%s"...' $bundle
al@836 141 get_from_mirror $bundle
al@834 142 if [ -f "$bundle" ]; then
al@834 143 busybox tar -xaf $bundle; rm $bundle
al@834 144 else
al@834 145 recharging_failed $path; continue
al@834 146 fi
al@834 147
al@834 148 # Download files.list.lzma
al@834 149 files_local='files.list.lzma'; files_remote='files-list.lzma'
al@834 150 if [ -e "$files_local.bak" ]; then
al@834 151 md5sum $files_local.bak | awk '{printf $1}' > files-list.md5.bak
al@834 152 if cmp -s files-list.md5 files-list.md5.bak; then
al@834 153 mv $files_local.bak $files_remote
al@834 154 else
al@834 155 action 'Getting "%s"...' $files_remote
al@836 156 get_from_mirror $files_remote
al@834 157 fi
al@834 158 else
al@834 159 action 'Getting "%s"...' $files_remote
al@836 160 get_from_mirror $files_remote
al@834 161 fi
al@834 162
al@834 163 if [ ! -e "$files_remote" ]; then
al@834 164 recharging_failed $path; continue
al@834 165 fi
al@834 166 mv -f $files_remote $files_local
al@834 167
al@834 168 # Remove old database files (but packages.list.bak, extra.list.bak)
al@834 169 for i in packages.desc packages.$SUM packages.txt packages.equiv \
al@834 170 files.list.lzma mirrors packages.info files-list.md5; do
al@834 171 [ -f "$i.bak" ] && rm $i.bak 2>/dev/null
al@834 172 done
al@834 173
al@834 174 footer "$(_ 'Last database is ready to use.')"
al@834 175
al@834 176 # Check diff
al@834 177 if [ -f 'packages.list.bak' ]; then
al@834 178 diff -u packages.list.bak packages.list | grep ^+[a-z] > packages.diff
al@834 179 rm packages.list.bak
al@834 180 if [ -f 'extra.list.bak' ]; then
al@834 181 if [ -f 'extra.list' ]; then
al@834 182 awk -F'|' '{print $1 " (extra)"}' extra.list > extra.list1
al@834 183 awk -F'|' '{print $1 " (extra)"}' extra.list.bak > extra.list1.bak
al@834 184 diff -u extra.list1.bak extra.list1 | grep ^+[a-z] >> packages.diff
al@834 185 rm extra.list.bak extra.list1 extra.list1.bak
al@834 186 else
al@834 187 mv extra.list.bak extra.list
al@834 188 fi
al@834 189 fi
al@834 190 sed -i s/+// packages.diff
al@834 191
al@834 192 new_pkgs=$(wc -l < packages.diff)
al@834 193 if [ "$new_pkgs" -gt 0 ]; then
al@834 194 title 'Mirrored packages diff'
al@834 195 cat packages.diff
al@834 196 footer "$(emsg "$(_p \
al@834 197 '%s new package on the mirror.' \
al@834 198 '%s new packages on the mirror.' $new_pkgs \
al@834 199 "<c 32>$new_pkgs</c>")")"
al@834 200 fi
al@834 201 else
al@834 202 longline "$(_ "Note that next time you recharge the list, a list of \
al@834 203 differences will be displayed to show new and upgradeable packages.")"
al@834 204 fi
al@834 205 done
al@834 206 newline