tazpkg diff modules/recharge @ rev 834

Remove all --options from positional parameters; make "recharge" module; re-make "up" and "search" commands.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Aug 14 16:55:14 2015 +0300 (2015-08-14)
parents
children 4fbdffec7f48
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/recharge	Fri Aug 14 16:55:14 2015 +0300
     1.3 @@ -0,0 +1,192 @@
     1.4 +#!/bin/sh
     1.5 +# TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
     1.6 +# recharge - TazPkg module
     1.7 +# Recharge packages databases from a mirror
     1.8 +
     1.9 +
    1.10 +# Options:
    1.11 +#   [main|<repository>]				Repository name to recharge (all if empty)
    1.12 +
    1.13 +# Environment variables:
    1.14 +#   root							Root of the packages DB
    1.15 +#   UA								User Agent string ("TazPkg-<version>")
    1.16 +
    1.17 +
    1.18 +# Connect function libraries
    1.19 +. /lib/libtaz.sh
    1.20 +# Get TazPkg configuration variables
    1.21 +. "$root/etc/slitaz/slitaz.conf"
    1.22 +. "$root/etc/slitaz/tazpkg.conf"
    1.23 +PKGS_DB="$root$PKGS_DB"
    1.24 +
    1.25 +# Fallback actions
    1.26 +mkdir -p "$PKGS_DB"
    1.27 +[ ! -e "$PKGS_DB/mirror" ] && echo "$ONLINE_PKGS" > "$PKGS_DB/mirror"
    1.28 +
    1.29 +
    1.30 +# Functions
    1.31 +# ---------
    1.32 +
    1.33 +# Download a file from specified mirror
    1.34 +
    1.35 +download_from() {
    1.36 +	# input: "<mirror_url>+" "file_name"
    1.37 +	local i
    1.38 +	for i in $1; do
    1.39 +		case "$i" in
    1.40 +			# Mirror URL can have a trailing slash or not.
    1.41 +			http://* | https://* | ftp://*)
    1.42 +				busybox wget -c -q -T 30 -U $UA ${i%/}/$2 2>/dev/null && break ;;
    1.43 +			*)
    1.44 +				ln -sf ${i%/}/$2 . && break ;;
    1.45 +		esac
    1.46 +	done
    1.47 +}
    1.48 +
    1.49 +
    1.50 +# When recharging errors occur
    1.51 +
    1.52 +recharging_failed() {
    1.53 +	# Restore database from bak files
    1.54 +	action 'Restoring database files...'
    1.55 +	[ -e 'ID'  -a ! -e 'ID.bak' ]  && rm ID
    1.56 +	[ -e 'IDs' -a ! -e 'IDs.bak' ] && rm IDs
    1.57 +	for file in $(ls $1/*.bak); do
    1.58 +		mv -f $file ${file%.bak}
    1.59 +	done
    1.60 +	status
    1.61 +
    1.62 +	footer "$(colorize 31 "$(_ 'Recharging failed')")"
    1.63 +}
    1.64 +
    1.65 +
    1.66 +
    1.67 +
    1.68 +REPO="$1"
    1.69 +
    1.70 +# What to recharge: main, or all, or selected undigest
    1.71 +case "$REPO" in
    1.72 +	main) repo_to_recharge="$PKGS_DB";;
    1.73 +	'')   repo_to_recharge="$PKGS_DB $PKGS_DB/undigest/*";;
    1.74 +	*)    repo_to_recharge="$PKGS_DB/undigest/$REPO"
    1.75 +		if [ ! -d "$repo_to_recharge" ]; then
    1.76 +			_ "Repository \"%s\" doesn't exist." "$repo_to_recharge" >&2
    1.77 +			exit 1
    1.78 +		fi
    1.79 +		;;
    1.80 +esac
    1.81 +
    1.82 +for path in $repo_to_recharge; do
    1.83 +	[ ! -f $path/mirror ] && continue	# skip
    1.84 +	cd $path
    1.85 +	mirror="$(cat mirror)"
    1.86 +
    1.87 +	# Repository name
    1.88 +	if [ "$path" == "$PKGS_DB" ]; then
    1.89 +		repo_name='Main'
    1.90 +	else
    1.91 +		repo_name="$(_n 'Undigest %s' "$(basename "$path")")"
    1.92 +	fi
    1.93 +
    1.94 +	title 'Recharging repository "%s"' "$repo_name"
    1.95 +
    1.96 +	# Don't let ID be a symlink when using local repository.
    1.97 +	if [ -h ID  ]; then mv -f ID  ID.lnk;  cat ID.lnk  > ID;  rm ID.lnk;  fi
    1.98 +	if [ -h IDs ]; then mv -f IDs IDs.lnk; cat IDs.lnk > IDs; rm IDs.lnk; fi
    1.99 +
   1.100 +	[ -f ID ]  && mv ID  ID.bak					# Compatibility with "old" ID
   1.101 +	[ -f IDs ] && mv IDs IDs.bak
   1.102 +	download_from "$mirror" IDs
   1.103 +	[ -e 'IDs' ] && awk '{print $1}' IDs > ID	# Compatibility with "old" ID
   1.104 +
   1.105 +	# Check if recharging is needed
   1.106 +	if [ -f 'IDs' ] && cmp -s IDs IDs.bak; then
   1.107 +		action 'Checking...'; status			# "Fake" message
   1.108 +		footer "$(_ 'Repository "%s" is up to date.' "$repo_name")"
   1.109 +		rm IDs.bak ID.bak
   1.110 +		continue
   1.111 +	fi
   1.112 +	rm IDs.bak ID.bak 2>/dev/null
   1.113 +
   1.114 +	[ -e 'IDs' ] && _ 'Database timestamp: %s' "$(date -d "@$(awk '{print $2}' IDs)" "+%x %R")"
   1.115 +
   1.116 +	action 'Creating backup of the last packages list...'
   1.117 +	for i in packages.desc packages.$SUM packages.txt packages.list \
   1.118 +		packages.equiv files.list.lzma extra.list mirrors packages.info; do
   1.119 +		[ -f "$i" ] && mv -f $i $i.bak 2>/dev/null
   1.120 +	done
   1.121 +	:; status
   1.122 +
   1.123 +	# Download and extract bundle: extra.list, mirrors, files-list.md5,
   1.124 +	#   packages.{info,desc,md5,txt,list,equiv}
   1.125 +	bundle='bundle.tar.lzma'
   1.126 +	action 'Getting "%s"...' $bundle
   1.127 +	download_from "$mirror" $bundle
   1.128 +	status
   1.129 +	if [ -f "$bundle" ]; then
   1.130 +		busybox tar -xaf $bundle; rm $bundle
   1.131 +	else
   1.132 +		recharging_failed $path; continue
   1.133 +	fi
   1.134 +
   1.135 +	# Download files.list.lzma
   1.136 +	files_local='files.list.lzma'; files_remote='files-list.lzma'
   1.137 +	if [ -e "$files_local.bak" ]; then
   1.138 +		md5sum $files_local.bak | awk '{printf $1}' > files-list.md5.bak
   1.139 +		if cmp -s files-list.md5 files-list.md5.bak; then
   1.140 +			mv $files_local.bak $files_remote
   1.141 +		else
   1.142 +			action 'Getting "%s"...' $files_remote
   1.143 +			download_from "$mirror" $files_remote
   1.144 +			status
   1.145 +		fi
   1.146 +	else
   1.147 +		action 'Getting "%s"...' $files_remote
   1.148 +		download_from "$mirror" $files_remote
   1.149 +		status
   1.150 +	fi
   1.151 +
   1.152 +	if [ ! -e "$files_remote" ]; then
   1.153 +		recharging_failed $path; continue
   1.154 +	fi
   1.155 +	mv -f $files_remote $files_local
   1.156 +
   1.157 +	# Remove old database files (but packages.list.bak, extra.list.bak)
   1.158 +	for i in packages.desc packages.$SUM packages.txt packages.equiv \
   1.159 +		files.list.lzma mirrors packages.info files-list.md5; do
   1.160 +		[ -f "$i.bak" ] && rm $i.bak 2>/dev/null
   1.161 +	done
   1.162 +
   1.163 +	footer "$(_ 'Last database is ready to use.')"
   1.164 +
   1.165 +	# Check diff
   1.166 +	if [ -f 'packages.list.bak' ]; then
   1.167 +		diff -u packages.list.bak packages.list | grep ^+[a-z] > packages.diff
   1.168 +		rm packages.list.bak
   1.169 +		if [ -f 'extra.list.bak' ]; then
   1.170 +			if [ -f 'extra.list' ]; then
   1.171 +				awk -F'|' '{print $1 " (extra)"}' extra.list > extra.list1
   1.172 +				awk -F'|' '{print $1 " (extra)"}' extra.list.bak > extra.list1.bak
   1.173 +				diff -u extra.list1.bak extra.list1 | grep ^+[a-z] >> packages.diff
   1.174 +				rm extra.list.bak extra.list1 extra.list1.bak
   1.175 +			else
   1.176 +				mv extra.list.bak extra.list
   1.177 +			fi
   1.178 +		fi
   1.179 +		sed -i s/+// packages.diff
   1.180 +
   1.181 +		new_pkgs=$(wc -l < packages.diff)
   1.182 +		if [ "$new_pkgs" -gt 0 ]; then
   1.183 +			title 'Mirrored packages diff'
   1.184 +			cat packages.diff
   1.185 +			footer "$(emsg "$(_p \
   1.186 +			'%s new package on the mirror.' \
   1.187 +			'%s new packages on the mirror.' $new_pkgs \
   1.188 +			"<c 32>$new_pkgs</c>")")"
   1.189 +		fi
   1.190 +	else
   1.191 +		longline "$(_ "Note that next time you recharge the list, a list of \
   1.192 +differences will be displayed to show new and upgradeable packages.")"
   1.193 +	fi
   1.194 +done
   1.195 +newline