spk rev 68

bunch of improvments to download
author Christophe Lincoln <pankso@slitaz.org>
date Fri May 18 21:24:30 2012 +0200 (2012-05-18)
parents 885b195a5d69
children 0cb21eb5cf20
files lib/libspk.sh spk spk-add
line diff
     1.1 --- a/lib/libspk.sh	Fri May 18 19:23:52 2012 +0200
     1.2 +++ b/lib/libspk.sh	Fri May 18 21:24:30 2012 +0200
     1.3 @@ -110,6 +110,23 @@
     1.4  	done
     1.5  }
     1.6  
     1.7 +# Check if the download was sane
     1.8 +check_download() {
     1.9 +	debug "check_download: $file"
    1.10 +	if ! tail -c 2k $file | fgrep -q 00000000TRAILER; then
    1.11 +		gettext "Continuing download of:"; echo " $file"
    1.12 +		download "$file" $mirror
    1.13 +	fi
    1.14 +	# Check that the package has the correct checksum
    1.15 +	local msum=$(fgrep "  $package_full" $pkgsmd5)
    1.16 +	local sum=$($CHECKSUM $file)
    1.17 +	debug "mirror $SUM : $msum"
    1.18 +	debug "local $SUM  : $sum"
    1.19 +	if [ "$sum" != "$msum" ]; then
    1.20 +		rm -f $file && download "$file" $mirror
    1.21 +	fi
    1.22 +}
    1.23 +
    1.24  # Download a file trying all mirrors
    1.25  # Usage: file [url|path]
    1.26  #
    1.27 @@ -122,40 +139,52 @@
    1.28  	local pwd=$(pwd)
    1.29  	[ "$quiet" ] && local quiet="-q"
    1.30  	[ "$cache" ] && local pwd=$CACHE_DIR
    1.31 +	[ "$get" ] || local pwd=$CACHE_DIR
    1.32  	[ "$forced" ] && rm -f $pwd/$file
    1.33  	debug "download file: $file"
    1.34  	debug "DB: $db"
    1.35  	# Local mirror ? End by cd to cache, we may be installind. If --get
    1.36  	# was used we dl/copy in the current dir.
    1.37  	if [ -f "$uri/$file" ]; then
    1.38 -		[ "$quiet" ] || echo "URI: $uri/"
    1.39 -		[ "$get" ] || pwd=$CACHE_DIR
    1.40 -		gettext "Local mirror:"; boldify " $file"
    1.41 -		gettext "Copying file to:"; colorize " $pwd" 34
    1.42 +		[ "$verbose" ] && echo "URI: $uri/"
    1.43 +		gettext "Using local mirror:"; boldify " $file"
    1.44 +		[ "$verbose" ] && (gettext "Copying file to:"; colorize " $pwd" 34)
    1.45  		cp -f $uri/$file $pwd
    1.46  		cd $pwd && return 0
    1.47  	fi
    1.48  	# In cache ? Root can use --cache to set destdir.
    1.49 -	cd $CACHE_DIR
    1.50  	if [ -f "$CACHE_DIR/$file" ]; then
    1.51  		gettext "Using cache:"; colorize " ${file%.tazpkg}" 34
    1.52  		return 0
    1.53  	else
    1.54 -		[ "$quiet" ] || echo "URL: $uri/"
    1.55 -		gettext "Downloading:"; boldify " $file"
    1.56 -		gettext "Destination:"; colorize " $pwd" 34
    1.57 +		[ "$verbose" ] && echo "URL: $uri/"
    1.58 +		if [ "$db" == "$PKGS_DB" ]; then
    1.59 +			gettext "Using official mirror:"
    1.60 +		else
    1.61 +			gettext "Using extra mirror:"
    1.62 +		fi
    1.63 +		boldify " $file"
    1.64 +		[ "$verbose" ] && (gettext "Destination:"; colorize " $pwd" 34)
    1.65  		if [ -f "$pwd/$file" ]; then
    1.66  			echo "File exist: $pwd/$file" && return 0
    1.67  		fi
    1.68 +		# TODO: be a spider with wget -s to check if package is on mirror,
    1.69 +		# if not try all official mirrors ?
    1.70  		wget $quiet -c $uri/$file -O $CACHE_DIR/$file
    1.71 +		cd $CACHE_DIR && check_download
    1.72  	fi
    1.73  	# Be sure the file was fetched.
    1.74  	if [ ! -f "$pwd/$file" ] || [ ! -f "$CACHE_DIR/$file" ]; then
    1.75 -		gettext "ERROR: Missing file:"; colorize "$file" 31
    1.76 +		gettext "ERROR: Missing file:"; colorize " $file" 31
    1.77  		newline && exit 1
    1.78  	fi
    1.79  }
    1.80  
    1.81 +# Unser var set by mirrored_pkg
    1.82 +unset_mirrored() {
    1.83 +	unset mirrored mirror db pwd
    1.84 +}
    1.85 +
    1.86  # Return the full package name, search in all packages.desc and break when
    1.87  # first occurance is found: Usage: full_package pkgname
    1.88  full_package() {
     2.1 --- a/spk	Fri May 18 19:23:52 2012 +0200
     2.2 +++ b/spk	Fri May 18 21:24:30 2012 +0200
     2.3 @@ -6,8 +6,8 @@
     2.4  # Copyright (C) SliTaz GNU/Linux - BSD License
     2.5  # Author: See AUTHORS files
     2.6  #
     2.7 -#. /usr/lib/slitaz/libspk.sh
     2.8 -. lib/libspk.sh
     2.9 +. /usr/lib/slitaz/libspk.sh
    2.10 +#. lib/libspk.sh
    2.11  
    2.12  #
    2.13  # Functions
    2.14 @@ -109,7 +109,7 @@
    2.15  			[ "$count" == 0 ] && newline
    2.16  			count=$(($count + 1))
    2.17  			download $package_full $mirror
    2.18 -			unset mirrored mirror db
    2.19 +			unset_mirrored
    2.20  			newline && continue ;;
    2.21  	esac
    2.22  	# Installed ?
     3.1 --- a/spk-add	Fri May 18 19:23:52 2012 +0200
     3.2 +++ b/spk-add	Fri May 18 21:24:30 2012 +0200
     3.3 @@ -294,7 +294,14 @@
     3.4  	#fi
     3.5  
     3.6  	# Check if package is mirrored
     3.7 -	if ! mirrored_pkg $package_name; then
     3.8 +	#if ! mirrored_pkg $package_name; then
     3.9 +		#gettext "Could not find package on mirror:"
    3.10 +		#boldify " $package_name" && exit 1
    3.11 +	#fi
    3.12 +
    3.13 +	# Check if package is mirrored
    3.14 +	mirrored_pkg $package_name
    3.15 +	if [ ! "$mirrored" ]; then
    3.16  		gettext "Could not find package on mirror:"
    3.17  		boldify " $package_name" && exit 1
    3.18  	fi
    3.19 @@ -302,29 +309,11 @@
    3.20  	# We want: package-version.tazpkg
    3.21  	local package_full=$(full_package $package_name)
    3.22  
    3.23 -	#mirrored_pkg $package_name
    3.24 +	# Let fetch the package by download()
    3.25 +	[ "$count" == 1 ] && newline
    3.26  	debug "spk-add package: $package_name"
    3.27  	download "$package_full" $mirror
    3.28 -
    3.29 -	#cd $CACHE_DIR || exit 1
    3.30 -	#if [ -f "$package_full" ]; then
    3.31 -		#[ "$count" == 1 ] && newline
    3.32 -		#gettext "Using cache:"; colorize " ${package_full%.tazpkg}" 34
    3.33 -
    3.34 -		## Check package download was finished
    3.35 -		#if ! tail -c 2k $package_full | fgrep -q 00000000TRAILER; then
    3.36 -			#gettext "Continuing download of:"; echo " $package_name"
    3.37 -			#download "$package_full"
    3.38 -		#fi
    3.39 -
    3.40 -		## Check that the package has the correct checksum
    3.41 -		##if [ "$($CHECKSUM $package_full)" != "$(fgrep \"  $package_full\" $pkgsmd5)" ]; then
    3.42 -		##	rm -f $package.tazpkg
    3.43 -		##	download "$package_full"
    3.44 -		##fi
    3.45 -	#else
    3.46 -		#newline && download "$package_full" $mirror
    3.47 -	#fi
    3.48 +	cd $CACHE_DIR
    3.49  
    3.50  	# Create package path early to avoid dependencies loop
    3.51  	mkdir -p $tmpdir
    3.52 @@ -336,6 +325,7 @@
    3.53  	#[ "$exec" ] && chroot $root/ $package_name
    3.54  	[ "$exec" ] && $package_name $root
    3.55  	update_databases
    3.56 +	unset_mirrored
    3.57  }
    3.58  
    3.59  # Install all missing deps of a package.