spk rev 67

libspk.sh and spk-add: better but not finished download function, spk: add option --get and command clean
author Christophe Lincoln <pankso@slitaz.org>
date Fri May 18 19:23:52 2012 +0200 (2012-05-18)
parents 63fb720dc777
children d1790128faac
files lib/libspk.sh spk spk-add spk-mirror
line diff
     1.1 --- a/lib/libspk.sh	Fri May 18 06:36:19 2012 +0200
     1.2 +++ b/lib/libspk.sh	Fri May 18 19:23:52 2012 +0200
     1.3 @@ -99,33 +99,61 @@
     1.4  # Check if package is on main or extra mirror.
     1.5  mirrored_pkg() {
     1.6  	local name=$1
     1.7 -	local find=$(grep "^$name |" $pkgsdesc $extradb/*/*.desc 2>/dev/null)
     1.8 -	[ -n "$find" ]
     1.9 +	#local find=$(grep "^$name |" $pkgsdesc $extradb/*/*.desc 2>/dev/null)
    1.10 +	for desc in $(find $extradb $pkgsdesc -name packages.desc); do
    1.11 +		if grep -q "^$name |" $desc; then
    1.12 +			db=$(dirname $desc)
    1.13 +			mirrored=$(grep "^$name |" $desc)
    1.14 +			mirror=$(cat $db/mirror)
    1.15 +			break
    1.16 +		fi
    1.17 +	done
    1.18  }
    1.19  
    1.20  # Download a file trying all mirrors
    1.21 -# Parameters: package/file
    1.22 +# Usage: file [url|path]
    1.23  #
    1.24 -# We should do much better here, give priority to extra, then try
    1.25 -# main mirror, then try others official mirrors. The case $file is
    1.26 -# not needed since we use same URL for list or packages.
    1.27 +# Priority to extra is done by mirrored_pkg wich try first to find the
    1.28 +# packages in extra mirror, then on official.
    1.29  #
    1.30  download() {
    1.31  	local file=$1
    1.32 -	local mirror="$(cat $mirrorurl)"
    1.33 +	local uri="${2%/}"
    1.34 +	local pwd=$(pwd)
    1.35  	[ "$quiet" ] && local quiet="-q"
    1.36 -	case "$file" in
    1.37 -		*.tazpkg)
    1.38 -			[ "$quiet" ] || echo "URL: ${mirror%/}/"
    1.39 -			gettext "Downloading:"; boldify " $file"
    1.40 -			wget $quiet -c ${mirror%/}/$file
    1.41 -			if [ ! -f "$file" ]; then
    1.42 -				gettext "ERROR: Missing package:"; boldify "$package"
    1.43 -				newline && exit 1
    1.44 -			fi ;;
    1.45 -		ID|packages.*|files.list.lzma)
    1.46 -			echo "TODO" ;;
    1.47 -	esac
    1.48 +	[ "$cache" ] && local pwd=$CACHE_DIR
    1.49 +	[ "$forced" ] && rm -f $pwd/$file
    1.50 +	debug "download file: $file"
    1.51 +	debug "DB: $db"
    1.52 +	# Local mirror ? End by cd to cache, we may be installind. If --get
    1.53 +	# was used we dl/copy in the current dir.
    1.54 +	if [ -f "$uri/$file" ]; then
    1.55 +		[ "$quiet" ] || echo "URI: $uri/"
    1.56 +		[ "$get" ] || pwd=$CACHE_DIR
    1.57 +		gettext "Local mirror:"; boldify " $file"
    1.58 +		gettext "Copying file to:"; colorize " $pwd" 34
    1.59 +		cp -f $uri/$file $pwd
    1.60 +		cd $pwd && return 0
    1.61 +	fi
    1.62 +	# In cache ? Root can use --cache to set destdir.
    1.63 +	cd $CACHE_DIR
    1.64 +	if [ -f "$CACHE_DIR/$file" ]; then
    1.65 +		gettext "Using cache:"; colorize " ${file%.tazpkg}" 34
    1.66 +		return 0
    1.67 +	else
    1.68 +		[ "$quiet" ] || echo "URL: $uri/"
    1.69 +		gettext "Downloading:"; boldify " $file"
    1.70 +		gettext "Destination:"; colorize " $pwd" 34
    1.71 +		if [ -f "$pwd/$file" ]; then
    1.72 +			echo "File exist: $pwd/$file" && return 0
    1.73 +		fi
    1.74 +		wget $quiet -c $uri/$file -O $CACHE_DIR/$file
    1.75 +	fi
    1.76 +	# Be sure the file was fetched.
    1.77 +	if [ ! -f "$pwd/$file" ] || [ ! -f "$CACHE_DIR/$file" ]; then
    1.78 +		gettext "ERROR: Missing file:"; colorize "$file" 31
    1.79 +		newline && exit 1
    1.80 +	fi
    1.81  }
    1.82  
    1.83  # Return the full package name, search in all packages.desc and break when
    1.84 @@ -133,7 +161,6 @@
    1.85  full_package() {
    1.86  	for desc in $(find $extradb $pkgsdesc -name packages.desc); do
    1.87  		local line="$(grep "^$1 |" $desc)"
    1.88 -		local db=$(dirname $desc)
    1.89  		if grep -q "^$1 |" $desc; then
    1.90  			IFS="|"
    1.91  			echo $line | busybox awk '{print $1 "-" $2 ".tazpkg"}'
     2.1 --- a/spk	Fri May 18 06:36:19 2012 +0200
     2.2 +++ b/spk	Fri May 18 19:23:52 2012 +0200
     2.3 @@ -6,7 +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 +#. /usr/lib/slitaz/libspk.sh
     2.9 +. lib/libspk.sh
    2.10  
    2.11  #
    2.12  # Functions
    2.13 @@ -24,13 +25,17 @@
    2.14  $(boldify $(gettext "Commands:"))
    2.15    info        $(gettext "Display path, mirror and other stats")
    2.16    activity    $(gettext "Display packages activities")
    2.17 +  clean       $(gettext "Clean cache and temporary files")
    2.18  
    2.19  $(boldify $(gettext "Options:"))
    2.20    --add       $(gettext "Install packages if mirrored")
    2.21    --rm        $(gettext "Remove installed packages")
    2.22 +  --get       $(gettext "Download packages specified on cmdline")
    2.23    --block     $(gettext "Add packages to the blocked list")
    2.24    --unblock   $(gettext "Remove packages from the blocked list")
    2.25    --log       $(gettext "Show package install and upgrade log")
    2.26 +  --cache     $(gettext "Used with --get to download in cache")
    2.27 +  --forced    $(gettext "Force packages reinstall or download")
    2.28    --root=     $(gettext "Set the root file system path")
    2.29    --debug     $(gettext "Display some usefull debug information")
    2.30  
    2.31 @@ -72,17 +77,41 @@
    2.32  		cat $activity | tail -n $lines
    2.33  		separator && newline
    2.34  		exit 0 ;;
    2.35 +	clean)
    2.36 +		newline
    2.37 +		boldify "Spk Clean"
    2.38 +		separator
    2.39 +		size=$(du -sh $CACHE_DIR | awk '{print $1}')
    2.40 +		gettext "Cleaning cache:"; echo -n " $CACHE_DIR ($size)"
    2.41 +		rm -rf $CACHE_DIR/* && status
    2.42 +		gettext "Cleaning tmp  :"; echo -n " $(dirname $tmpdir)"
    2.43 +		rm -rf $(dirname $tmpdir) && status
    2.44 +		separator && newline
    2.45 +		exit 0 ;;
    2.46  esac
    2.47  
    2.48  #
    2.49  # Handle packages: spk package1 ... packageN
    2.50  #
    2.51  
    2.52 -debug "cmdline: $@"
    2.53 +#debug "cmdline: $@"
    2.54  count=0
    2.55  
    2.56  for pkg in $@
    2.57  do
    2.58 +	# Handle general: --options
    2.59 +	case " $@ " in
    2.60 +		*\ --get\ *)
    2.61 +			# We want: package-version.tazpkg
    2.62 +			package_full=$(full_package $pkg)
    2.63 +			mirrored_pkg $pkg
    2.64 +			[ "$mirrored" ] || continue
    2.65 +			[ "$count" == 0 ] && newline
    2.66 +			count=$(($count + 1))
    2.67 +			download $package_full $mirror
    2.68 +			unset mirrored mirror db
    2.69 +			newline && continue ;;
    2.70 +	esac
    2.71  	# Installed ?
    2.72  	if [ -d "$installed/$pkg" ]; then
    2.73  		# Handle: --options
    2.74 @@ -130,7 +159,7 @@
    2.75  					separator
    2.76  					cat $logdir/$pkg/up.log
    2.77  				else
    2.78 -					gettext "Any upgrade log for:"; boldify " $pkg"
    2.79 +					colorize "$(gettext "Any upgrade log for:"; echo " $pkg")" 36
    2.80  					newline
    2.81  				fi && continue ;;
    2.82  		esac
    2.83 @@ -146,9 +175,9 @@
    2.84  		continue
    2.85  	fi
    2.86  	# Mirrored ?
    2.87 -	mirrored=$(grep "^$pkg |" $pkgsdesc)
    2.88 +	mirrored_pkg $pkg
    2.89  	if [ "$mirrored" ]; then
    2.90 -		# Handle: --options
    2.91 +		# Handle mirrored: --options
    2.92  		case " $@ " in
    2.93  			*\ --add\ *)
    2.94  				spk-add $pkg --count=$count
    2.95 @@ -167,6 +196,7 @@
    2.96  		separator && newline
    2.97  		continue
    2.98  	fi
    2.99 +	unset mirrored
   2.100  	# Skip options such as --confirm or unknow package
   2.101  	case "$pkg" in
   2.102  		--*) continue ;;
     3.1 --- a/spk-add	Fri May 18 06:36:19 2012 +0200
     3.2 +++ b/spk-add	Fri May 18 19:23:52 2012 +0200
     3.3 @@ -288,10 +288,10 @@
     3.4  	local package_name="$1"
     3.5  
     3.6  	# Check if get-Package
     3.7 -	if mirrored_pkg get-$package_name; then
     3.8 -		package_name="get-$package_name"
     3.9 -		exec=true
    3.10 -	fi
    3.11 +	#if mirrored_pkg get-$package_name; then
    3.12 +		#package_name="get-$package_name"
    3.13 +		#exec=true
    3.14 +	#fi
    3.15  
    3.16  	# Check if package is mirrored
    3.17  	if ! mirrored_pkg $package_name; then
    3.18 @@ -302,25 +302,29 @@
    3.19  	# We want: package-version.tazpkg
    3.20  	local package_full=$(full_package $package_name)
    3.21  
    3.22 -	cd $CACHE_DIR || exit 1
    3.23 -	if [ -f "$package_full" ]; then
    3.24 -		[ "$count" == 1 ] && newline
    3.25 -		gettext "Using cache:"; colorize " ${package_full%.tazpkg}" 34
    3.26 +	#mirrored_pkg $package_name
    3.27 +	debug "spk-add package: $package_name"
    3.28 +	download "$package_full" $mirror
    3.29  
    3.30 -		# Check package download was finished
    3.31 -		if ! tail -c 2k $package_full | fgrep -q 00000000TRAILER; then
    3.32 -			gettext "Continuing download of:"; echo " $package_name"
    3.33 -			download "$package_full"
    3.34 -		fi
    3.35 +	#cd $CACHE_DIR || exit 1
    3.36 +	#if [ -f "$package_full" ]; then
    3.37 +		#[ "$count" == 1 ] && newline
    3.38 +		#gettext "Using cache:"; colorize " ${package_full%.tazpkg}" 34
    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 +		## Check package download was finished
    3.45 +		#if ! tail -c 2k $package_full | fgrep -q 00000000TRAILER; then
    3.46 +			#gettext "Continuing download of:"; echo " $package_name"
    3.47 +			#download "$package_full"
    3.48  		#fi
    3.49 -	else
    3.50 -		newline && download "$package_full"
    3.51 -	fi
    3.52 +
    3.53 +		## Check that the package has the correct checksum
    3.54 +		##if [ "$($CHECKSUM $package_full)" != "$(fgrep \"  $package_full\" $pkgsmd5)" ]; then
    3.55 +		##	rm -f $package.tazpkg
    3.56 +		##	download "$package_full"
    3.57 +		##fi
    3.58 +	#else
    3.59 +		#newline && download "$package_full" $mirror
    3.60 +	#fi
    3.61  
    3.62  	# Create package path early to avoid dependencies loop
    3.63  	mkdir -p $tmpdir
     4.1 --- a/spk-mirror	Fri May 18 06:36:19 2012 +0200
     4.2 +++ b/spk-mirror	Fri May 18 19:23:52 2012 +0200
     4.3 @@ -153,7 +153,7 @@
     4.4  do
     4.5  	[ -d "$url" ] && url="dir:$url"
     4.6  	case "$url" in
     4.7 -		http://*|ftp://)
     4.8 +		http://*|ftp://|local)
     4.9  			[ "$count" == 0 ] && newline
    4.10  			count=$(($count + 1))
    4.11  			url="${url%/}/"