spk rev 81

Add spk-up: lets update the system or individual packages, priority work (unlike tazpkg)
author Christophe Lincoln <pankso@slitaz.org>
date Thu May 24 17:02:36 2012 +0200 (2012-05-24)
parents 3f204d3bf901
children ee3163f781b3
files spk-up
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/spk-up	Thu May 24 17:02:36 2012 +0200
     1.3 @@ -0,0 +1,285 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# Spk-up - Update packages. Read the README before adding or
     1.7 +# modifing any code in spk!
     1.8 +#
     1.9 +# Copyright (C) SliTaz GNU/Linux - BSD License
    1.10 +# Author: See AUTHORS files
    1.11 +#
    1.12 +. /usr/lib/slitaz/libspk.sh
    1.13 +
    1.14 +#
    1.15 +# Functions
    1.16 +#
    1.17 +
    1.18 +# Help and usage
    1.19 +usage() {
    1.20 +	name=$(basename $0)
    1.21 +	cat << EOT
    1.22 +
    1.23 +$(boldify $(gettext "Usage:")) $name [packages|--options]
    1.24 +
    1.25 +$(gettext "Update packages lists and upgrade the system")
    1.26 +
    1.27 +$(boldify $(gettext "Options:"))
    1.28 +  --list       $(gettext "Recharges the packages lists")
    1.29 +  --add        $(gettext "Install upgrade automaticaly")
    1.30 +  --forced     $(gettext "Force recharging the lists")
    1.31 +  --mirror=    $(gettext "Specify a mirror to check")
    1.32 +
    1.33 +$(boldify $(gettext "Examples:"))
    1.34 +  $name package1 package2
    1.35 +  $name --list --forced --mirror=main
    1.36 +
    1.37 +EOT
    1.38 +	exit 0
    1.39 +}
    1.40 +
    1.41 +# Headers fo system or packages update
    1.42 +up_headers() {
    1.43 +	newline
    1.44 +	boldify $(gettext "Package") \
    1.45 +	$(echo -n $(indent 28 $(gettext "Version"))) \
    1.46 +	$(echo -n $(indent 48 $(gettext "Status"))) \
    1.47 +	$(echo -n $(indent 68 $(gettext "Mirror")))
    1.48 +	separator
    1.49 +}
    1.50 +
    1.51 +# Recharges all list from one mirror or all mirrors
    1.52 +recharge_lists() {
    1.53 +	check_root
    1.54 +	newline
    1.55 +	boldify $(gettext "Updating packages lists")
    1.56 +	separator
    1.57 +	check="$extradb/*/mirror $PKGS_DB/mirror"
    1.58 +	if [ "$mirror" ]; then
    1.59 +		check=$extradb/$mirror/mirror
    1.60 +		[ "$mirror" == "main" ] && check=$PKGS_DB/mirror
    1.61 +	fi
    1.62 +	for mirror in $check; do
    1.63 +		[ -f "$mirror" ] || continue
    1.64 +		# Skip local mirror
    1.65 +		[ ! $(readlink $(dirname $mirror)/packages.desc) ] || continue
    1.66 +		# Get want a mirror name and download url
    1.67 +		name=$(basename $(dirname $mirror))
    1.68 +		url=$(cat $mirror)
    1.69 +		lists="packages.list packages.md5 packages.desc packages.equiv files.list.lzma"
    1.70 +		[ "$(dirname $mirror)" == "$PKGS_DB" ] && name="main"
    1.71 +		[ "$quiet" ] && quiet="-q"
    1.72 +
    1.73 +		gettext "Checking mirror:"; colorize 34 " $name"
    1.74 +		cd $(dirname $mirror)
    1.75 +
    1.76 +		# ID
    1.77 +		[ -f "ID" ] || echo "$$" > ID
    1.78 +		mv ID ID.bak
    1.79 +		wget -q ${url%/}/ID
    1.80 +		debug "ID: $(cat ID)"
    1.81 +		debug "ID.bak: $(cat ID.bak)"
    1.82 +		if [ $(cat ID) == $(cat ID.bak) ] && [ ! "$forced" ]; then
    1.83 +			gettext "Mirror is up-to-date"; newline
    1.84 +			continue
    1.85 +		fi
    1.86 +
    1.87 +		# Backup and get all lists
    1.88 +		for list in $lists
    1.89 +		do
    1.90 +			[ -f "$list" ] && cp -f $list $list.bak
    1.91 +			debug "url: ${url%/}/$list"
    1.92 +			rm -f $list
    1.93 +			busybox wget $quiet ${url%/}/$list
    1.94 +		done
    1.95 +	done
    1.96 +	separator
    1.97 +	newline
    1.98 +}
    1.99 +
   1.100 +# Repo priority: local, extras then official
   1.101 +priority() {
   1.102 +	extras=$(ls $extradb | sed s"/local//")
   1.103 +	for i in $extras; do
   1.104 +		extras="$extradb/$i"
   1.105 +	done
   1.106 +	[ -d "$extradb/local" ] && localdb="$extradb/local"
   1.107 +	echo "$localdb $extras $PKGS_DB"
   1.108 +}
   1.109 +
   1.110 +# Types: blocked, new build or new version
   1.111 +up_type() {
   1.112 +	# Jump to next repository if pkg doesn't exists in this one.
   1.113 +	grep -q "^$PACKAGE |" $dbdesc || continue
   1.114 +
   1.115 +	echo -n "$PACKAGE"
   1.116 +	echo -n $(indent 28 "$VERSION")
   1.117 +
   1.118 +	# Mirror name
   1.119 +	case $repo in
   1.120 +		$PKGS_DB) reponame=$(gettext "Official") ;;
   1.121 +		*) reponame=$(gettext "Extra") ;;
   1.122 +	esac
   1.123 +	# Local have priority
   1.124 +	if [ -d "$localdb" ] && $(grep -q "^$PACKAGE |" $extradb/local/packages.desc); then
   1.125 +		#local_count=$(($local_count + 1))
   1.126 +		reponame=$(gettext "Local")
   1.127 +	fi
   1.128 +
   1.129 +	# Blocked
   1.130 +	if $(grep -qs "^$PACKAGE" $blocked); then
   1.131 +		blocked_count=$(($blocked_count + 1))
   1.132 +		echo -n $(colorize 31 $(indent 48 $(gettext "Blocked")))
   1.133 +		indent 68 "$reponame"
   1.134 +		break
   1.135 +	fi
   1.136 +
   1.137 +	new=$(grep "^$PACKAGE |" $dbdesc | awk '{print $3}')
   1.138 +
   1.139 +	if [ "$VERSION" == "$new" ]; then
   1.140 +		build_count=$(($build_count + 1))
   1.141 +		echo -n $(colorize 34 $(indent 48 $(gettext "New build")))
   1.142 +	else
   1.143 +		echo -n $(colorize 32 $(indent 48 $(gettext "New") $new))
   1.144 +	fi
   1.145 +	indent 68 "$reponame"
   1.146 +	echo "$PACKAGE" >> $pkgsup
   1.147 +}
   1.148 +
   1.149 +# Check if we have an upgrade for a package
   1.150 +check_pkgup() {
   1.151 +	unset_receipt
   1.152 +	. $pkg/receipt
   1.153 +	sum=$(fgrep "  $PACKAGE-${VERSION}$EXTRAVERSION.tazpkg" \
   1.154 +		$installed.$SUM | awk '{print $1}')
   1.155 +
   1.156 +	# Skip up-to-date local packages
   1.157 +	if [ -d "$extradb/local" ] && fgrep -q "$sum  $PACKAGE-" $extradb/local/packages.$SUM; then
   1.158 +		continue
   1.159 +	fi
   1.160 +
   1.161 +	for repo in $(priority); do
   1.162 +		dbdesc=$repo/packages.desc
   1.163 +		dbsum=$repo/packages.$SUM
   1.164 +
   1.165 +		# Sum match or not ?
   1.166 +		if ! fgrep -q "$sum  $PACKAGE-" $dbsum; then
   1.167 +			up_type
   1.168 +			break
   1.169 +		fi
   1.170 +	done
   1.171 +}
   1.172 +
   1.173 +# Log and install an upgradable package.
   1.174 +install_up() {
   1.175 +	mirrored_pkg $pkg
   1.176 +	vers=$(echo "$mirrored" | awk '{print $3}')
   1.177 +	mkdir -p $logdir/$pkg
   1.178 +	echo "$(date '+%Y-%m-%d %H:%M') : Upgrade to $vers" >> $logdir/$pkg/up.log
   1.179 +	spk-add $pkg --forced
   1.180 +}
   1.181 +
   1.182 +#
   1.183 +# Handle packages and --options
   1.184 +#
   1.185 +
   1.186 +count=0
   1.187 +
   1.188 +for arg in $@
   1.189 +do
   1.190 +	case "$arg" in
   1.191 +		*usage|*help) usage ;;
   1.192 +		--list) recharge_lists && exit 0 ;;
   1.193 +		--*) continue ;;
   1.194 +		*)
   1.195 +			pkg=$arg
   1.196 +			system=no
   1.197 +			[ "$count" == 0 ] && up_headers
   1.198 +			if [ -f "$installed/$pkg/receipt" ]; then
   1.199 +				count=$(($count +1))
   1.200 +				cd $installed
   1.201 +				check=$(check_pkgup)
   1.202 +				if [ "$check" != "" ]; then
   1.203 +					echo "$check"
   1.204 +					[ "$add" ] && install_up
   1.205 +				else
   1.206 +					if $(grep -qs "^${pkg}$" $blocked); then
   1.207 +						echo -n $(colorize 31 "$pkg $VERSION ")
   1.208 +						gettext "is blocked"; newline
   1.209 +					else
   1.210 +						echo -n $(boldify "$pkg $VERSION ")
   1.211 +						gettext "is up-to-date. Version"; newline
   1.212 +					fi
   1.213 +				fi
   1.214 +			fi ;;
   1.215 +	esac
   1.216 +done
   1.217 +
   1.218 +# Skip system-wide upgrade if some packages was updated individually.
   1.219 +if [ "$system" ]; then
   1.220 +	[ "$add" ] || newline
   1.221 +	exit 0
   1.222 +fi
   1.223 +
   1.224 +#
   1.225 +# Check all mirrors list and upgrade system.
   1.226 +#
   1.227 +
   1.228 +time=$(date +%s)
   1.229 +build_count=0
   1.230 +blocked_count=0
   1.231 +
   1.232 +up_headers
   1.233 +cd $installed
   1.234 +newline > $pkgsup
   1.235 +
   1.236 +# Check all installed packages
   1.237 +for pkg in *
   1.238 +do
   1.239 +	check_pkgup
   1.240 +done
   1.241 +
   1.242 +# Remove empty line and count
   1.243 +sed -i /^$/d $pkgsup
   1.244 +upnb=$(cat $pkgsup | wc -l)
   1.245 +pkgs=$(ls | wc -l)
   1.246 +time=$(($(date +%s) - $time))
   1.247 +
   1.248 +if [ "$upnb" == 0 ]; then
   1.249 +	gettext "System is up-to-date..."; newline
   1.250 +fi
   1.251 +separator
   1.252 +echo -n "$pkgs "; gettext "installed packages scanned in"; echo " ${time}s"
   1.253 +newline
   1.254 +
   1.255 +# Summary
   1.256 +boldify $(gettext "Packages upgrade summary")
   1.257 +separator
   1.258 +gettext "New version :"; colorize 32 " $(($upnb - $build_count))"
   1.259 +gettext "New build   :"; colorize 34 " $build_count"
   1.260 +gettext "Blocked     :"; colorize 31 " $blocked_count"
   1.261 +separator
   1.262 +newline
   1.263 +
   1.264 +# Pkgs to upgrade ? Skip, let --add/--install them all or ask user
   1.265 +if [ "$upnb" -gt 0 ]; then
   1.266 +	if [ "$add" ] || [ "$install" ]; then
   1.267 +		continue
   1.268 +	else
   1.269 +		gettext "Do you wish to upgrade now"
   1.270 +		if ! confirm; then
   1.271 +			gettext "Upgrade cancelled"
   1.272 +			echo -e "\n" && exit 0
   1.273 +		fi
   1.274 +	fi
   1.275 +	# Install and log all upgrade
   1.276 +	for pkg in $(cat $pkgsup)
   1.277 +	do
   1.278 +		install_up
   1.279 +	done
   1.280 +	# List is generated each time and must be cleaned so
   1.281 +	# tazpkg-notify dont find upgrade anymore.
   1.282 +	rm $pkgsup && touch $pkgsup
   1.283 +	newline
   1.284 +	gettext "Handeled upgrades:"; colorize 32 " $upnb"
   1.285 +	newline
   1.286 +fi
   1.287 +
   1.288 +exit 0