sup rev 34

Lots of improvments, implement 'update' cmdline tool handle SQLite packages db
author Christophe Lincoln <pankso@slitaz.org>
date Tue Feb 28 04:58:01 2017 +0100 (2017-02-28)
parents 454e319df9a6
children 4f9243868cc0
files libsup.sh sup
line diff
     1.1 --- a/libsup.sh	Tue Feb 28 00:01:30 2017 +0100
     1.2 +++ b/libsup.sh	Tue Feb 28 04:58:01 2017 +0100
     1.3 @@ -15,9 +15,8 @@
     1.4  installed="$supdb/installed"
     1.5  activity="$cache/activity.log"
     1.6  
     1.7 -host="http://localhost/~pankso/cgi-bin/scn/"
     1.8 -#host="http://scn.slitaz.org/"
     1.9 -mirror="${host}content/sup/packages"
    1.10 +server="http://scn.slitaz.org/"
    1.11 +mirror="${server}content/sup/packages"
    1.12  pkgsdb="$supdb/packages.sql"
    1.13  
    1.14  wok="$supdb/wok"
    1.15 @@ -38,6 +37,13 @@
    1.16  	echo ${size}
    1.17  }
    1.18  
    1.19 +# Minimal Busybox wget output. Usage: download "name" "url"
    1.20 +download() {
    1.21 +	gettext "Fetching"; echo -n $(colorize 30 " db → ")
    1.22 +	wget "$2" 2>&1 | tail -n 2 | awk '{printf $3}' \
    1.23 +		| sed s'!*!-!'g
    1.24 +}
    1.25 +
    1.26  # Extract a sup package: extract_sup "/path/to/pkg.sup"
    1.27  extract_sup() {
    1.28  	pkg="$(basename ${1%.sup})"
    1.29 @@ -58,10 +64,9 @@
    1.30  install_sup() {
    1.31  	pkg="$(basename ${1%.sup})"
    1.32  	supfile="$(readlink -f ${1%.sup}.sup)"
    1.33 -	cache="$cache/install"
    1.34  	
    1.35 -	rm -rf ${cache} && mkdir ${cache} 
    1.36 -	cp ${supfile} ${cache} && cd ${cache}
    1.37 +	rm -rf ${cache}/install && mkdir ${cache}/install
    1.38 +	cp ${supfile} ${cache}/install && cd ${cache}/install
    1.39  	
    1.40  	# Get receip for deps
    1.41  	cpio -i receip --quiet < ${supfile}
    1.42 @@ -75,7 +80,7 @@
    1.43  			echo "Missing dependency:"; colorize 35 " $dep"
    1.44  		fi
    1.45  	done
    1.46 -	. /etc/slitaz/slitaz.conf
    1.47 +	. /etc/slitaz/slitaz.conf # PKGS_DB
    1.48  	for dep in ${DEPENDS}; do
    1.49  		if [ ! "$PKGS_DB/installed/$dep" ]; then
    1.50  			echo "Missing dependency:"; colorize 35 " $dep"
    1.51 @@ -88,6 +93,7 @@
    1.52  	separator
    1.53  	
    1.54  	# Extract and source receip first to check deps
    1.55 +	md5sum=$(md5sum $supfile | awk '{print $1}')
    1.56  	extract_sup "$supfile"
    1.57  	
    1.58  	# Execute sup_install() in files/ tree so packages maintainers just
    1.59 @@ -95,7 +101,7 @@
    1.60  	cd files
    1.61  	if grep -q "^sup_install" ../receip; then
    1.62  		gettext "Executing install function:"
    1.63 -		indent $(($(tty_size) - 18)) "[ $(colorize 33 sup_install) ]"
    1.64 +		indent $(($(tty_size) - 18)) "[ $(colorize 033 sup_install) ]"
    1.65  		sup_install
    1.66  	fi
    1.67  	
    1.68 @@ -103,18 +109,19 @@
    1.69  	if [ "$verbose" ]; then
    1.70  		gettext "Creating the list of installed files..."; echo
    1.71  	fi
    1.72 -	files_list="${cache}/${PACKAGE}-${VERSION}/files.list"
    1.73 +	files_list="${cache}/install/${PACKAGE}-${VERSION}/files.list"
    1.74  	find . -type f -print > ${files_list}
    1.75  	find . -type l -print >> ${files_list}
    1.76  	sed -i s/'^.'/''/g ${files_list}
    1.77  	
    1.78  	# Back to pkg tree
    1.79 -	cd ${cache}/${PACKAGE}-${VERSION}
    1.80 +	cd ${cache}/install/${PACKAGE}-${VERSION}
    1.81  	echo "sup_size=\"$(du -sh files | cut -d "	" -f 1)\"" >> receip
    1.82 +	echo "md5_sum=\"${md5sum}\"" >> receip
    1.83  	
    1.84  	# Now we need a place to store package data and set $sup_size
    1.85 -	gettext "Installing files:"
    1.86 -	echo -n "$(colorize 35 " $(wc -l files.list | cut -d " " -f 1)")"
    1.87 +	echo -n "$(colorize 036 "$(gettext 'Installing files:') \
    1.88 +$(wc -l files.list | cut -d " " -f 1)")"
    1.89  	data="${installed}/${PACKAGE}"
    1.90  	mkdir -p ${data}
    1.91  	
    1.92 @@ -123,8 +130,10 @@
    1.93  	done
    1.94  	for file in $(ls -A files); do
    1.95  		cp -rf files/${file} ${HOME}
    1.96 -	done && status
    1.97 -	newline && rm -rf ${cache}
    1.98 +	done
    1.99 +	status
   1.100 +	
   1.101 +	newline && rm -rf ${cache}/install
   1.102  }
   1.103  
   1.104  # Remove a sup package
   1.105 @@ -139,8 +148,9 @@
   1.106  	separator
   1.107  	
   1.108  	gettext "Files to remove :"
   1.109 -	indent $(($(tty_size) - 8)) \
   1.110 -		"[ $(colorize 33 $(wc -l ${files_list} | cut -d ' ' -f 1)) ]"
   1.111 +	files="$(wc -l ${files_list} | cut -d ' ' -f 1)" in=8
   1.112 +	[ "$files" -gt "10" ] && in=9 && [ "$files" -gt "100" ] && in=10
   1.113 +	indent $(($(tty_size) - ${in})) "[ $(colorize 33 $files) ]"
   1.114  	
   1.115  	# Remove all files
   1.116  	for file in $(cat $files_list)
     2.1 --- a/sup	Tue Feb 28 00:01:30 2017 +0100
     2.2 +++ b/sup	Tue Feb 28 04:58:01 2017 +0100
     2.3 @@ -5,7 +5,7 @@
     2.4  # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
     2.5  # Author: Christophe Lincoln <pankso@slitaz.org>
     2.6  #
     2.7 -# Colors: installed 36; wok/package 35; commands/functions 33
     2.8 +# Colors: installed 036/36; wok/package 035/35; commands/functions 033/33
     2.9  #
    2.10  
    2.11  # Get system wide libsup.sh or development version if working in Hg folder
    2.12 @@ -165,7 +165,7 @@
    2.13  		status
    2.14  		
    2.15  		size="$(du -sh $cooked/$supfile | cut -d "	" -f 1)"
    2.16 -		echo "$(gettext 'Packages:') $(colorize 33 $supfile) ($size)"
    2.17 +		echo "$(gettext 'Packages:') $(colorize 35 $supfile) ($size)"
    2.18  		rm -rf ${cache} && newline ;;
    2.19  	
    2.20  	-n|new)
    2.21 @@ -227,12 +227,23 @@
    2.22  		fi
    2.23  		
    2.24  		# Sup on mirror
    2.25 -		echo "TODO: check for package on mirror"
    2.26 +		[ ! -f "${pkgsdb}" ] && sup -u
    2.27  		
    2.28 +		if sqlite3 ${pkgsdb} "SELECT name FROM pkgs WHERE name = '$pkg'" >/dev/null
    2.29 +		then
    2.30 +			vers=$(sqlite3 ${pkgsdb} "SELECT version FROM pkgs WHERE name = '$pkg'")
    2.31 +			mkdir -p ${cache}/wget && cd ${cache}/wget
    2.32 +			# Download
    2.33 +			newline
    2.34 +			download "$pkg" "${mirror}/${pkg}-${vers}.sup"
    2.35 +			install_sup "${pkg}-${vers}.sup"
    2.36 +			rm -rf ${cache}/wget && exit 0
    2.37 +		fi
    2.38  		gettext "Can't find sup package:"; colorize 31 " $pkg" ;;
    2.39  	
    2.40  	-u|update)
    2.41  		# Update packages
    2.42 +		online=""
    2.43  		newline
    2.44  		colorize 33 $(gettext "SUP Update")
    2.45  		separator
    2.46 @@ -240,22 +251,79 @@
    2.47  		# Skip HTTP/1.1 404 Not Found: 2>/dev/null
    2.48  		if ! wget -q -T 5 --spider ${mirror}/packages.sql 2>/dev/null; then
    2.49  			gettext "Mirror is unreachable"; newline
    2.50 +		else
    2.51 +			online=0
    2.52  		fi
    2.53  		
    2.54  		# Newer packages.sql ?
    2.55 -		wget -q "http://localhost/~pankso/cgi-bin/scn/?sup=dbsum" -O -
    2.56 +		gettext "Checking packages.sql..."
    2.57 +		db_sum=$(wget -q "${server}?sup=dbsum" -O -)
    2.58 +		if echo "$db_sum  $pkgsdb" | md5sum -s -c -; then
    2.59 +			indent $(($(tty_size) - 13)) "[ $(colorize 36 synced) ]"
    2.60 +		else
    2.61 +			[ "$online" ] && rm -f ${pkgsdb}
    2.62 +			indent $(($(tty_size) - 13)) "[ $(colorize 35 update) ]"
    2.63 +		fi
    2.64  		
    2.65 +		# No packages.sql: get latest!
    2.66 +		if [ ! -f "$pkgsdb" ] && [ "$online" ]; then
    2.67 +			gettext "Fetching db"; echo -n " → "
    2.68 +			wget -P ${supdb} ${mirror}/packages.sql 2>&1 | tail -n 2 \
    2.69 +				| awk '{printf $3}'; status
    2.70 +			
    2.71 +			timestamp=$(sqlite3 ${pkgsdb} 'SELECT timestamp FROM info' \
    2.72 +				| awk '{printf $1 " " $2}')
    2.73 +			gettext "Database timestamp:"
    2.74 +			indent $(($(tty_size) - 23)) "[ $(boldify $timestamp) ]"
    2.75 +		fi
    2.76  		
    2.77 -		rm -f ${pkgsdb}
    2.78 -		wget -P ${supdb} ${mirror}/packages.sql
    2.79 -		sqlite3 ${pkgsdb} 'SELECT timestamp FROM info' \
    2.80 -			| awk '{printf "Timestamp : %s %s\n",$1,$2 }'
    2.81 -		
    2.82 -		for pkg in $(ls $wok); do
    2.83 -			echo "$pkg"
    2.84 +		# Scan installed packages
    2.85 +		count=$(ls $installed | wc -l) in=8
    2.86 +		[ "$count" -gt "10" ] && in=9 && [ "$count" -gt "100" ] && in=10
    2.87 +		gettext "Installed packages:"
    2.88 +		indent $(($(tty_size) - ${in})) "[ $(colorize 33 $count) ]"
    2.89 +		cat << EOT
    2.90 +
    2.91 +$(boldify Package $(indent 30 Version) $(indent 52 Status))
    2.92 +--------------------------------------------------------------------------------
    2.93 +EOT
    2.94 +		for pkg in $(ls $installed)
    2.95 +		do
    2.96 +			. ${installed}/${pkg}/receip
    2.97 +			db_sum=$(sqlite3 ${pkgsdb} \
    2.98 +				"SELECT md5sum FROM pkgs WHERE name = '$pkg'")
    2.99 +			
   2.100 +			# MD5sum not matching: new version our new build ?
   2.101 +			if [ "$md5_sum" != "$db_sum" ]; then
   2.102 +				vers=$(sqlite3 ${pkgsdb} \
   2.103 +					"SELECT version FROM pkgs WHERE name = '$pkg'")
   2.104 +				if [ "$VERSION" != "$vers" ]; then
   2.105 +					status="$(gettext "update")"
   2.106 +				else
   2.107 +					status="$(gettext "rebuilt")"
   2.108 +				fi
   2.109 +				echo "$pkg $(indent 30 $VERSION) $(indent 52 $status)"
   2.110 +				echo "$pkg" >> ${cache}/updates
   2.111 +			fi
   2.112  		done
   2.113  		
   2.114 -		newline ;;
   2.115 +		if [ -f "$cache/updates" ]; then
   2.116 +			count=$(cat $cache/updates | wc -l)
   2.117 +			cat << EOT
   2.118 +--------------------------------------------------------------------------------
   2.119 +EOT
   2.120 +			gettext "Available updates:"; colorize 32 " $count"
   2.121 +			# --install
   2.122 +			if [ "$install" ]; then
   2.123 +				for pkg in $(cat $cache/updates); do
   2.124 +					sup -i ${pkg}
   2.125 +				done
   2.126 +			fi
   2.127 +		else
   2.128 +			gettext "Packages are up-to-date"; echo
   2.129 +			newline
   2.130 +		fi 
   2.131 +		rm -f ${cache}/updates ;;
   2.132  	
   2.133  	-r|remove)
   2.134  		# Remove a package