sup rev 36

Improve download function
author Christophe Lincoln <pankso@slitaz.org>
date Tue Feb 28 19:35:14 2017 +0100 (2017-02-28)
parents 4f9243868cc0
children 08132ef79601
files README libsup.sh sup sup-demo/receip
line diff
     1.1 --- a/README	Tue Feb 28 05:18:43 2017 +0100
     1.2 +++ b/README	Tue Feb 28 19:35:14 2017 +0100
     1.3 @@ -30,7 +30,15 @@
     1.4       
     1.5    3) Play :-)
     1.6       $ sup help
     1.7 -  
     1.8 +
     1.9 +
    1.10 +Cook SUP packages to be uploaded to the server
    1.11 +--------------------------------------------------------------------------------
    1.12 +Sup lets users upload packages to the server. To cook your first one you may want to
    1.13 +use the sup 'new' command:
    1.14 +
    1.15 +  $ sup -n
    1.16 +
    1.17  
    1.18  What goes where ?
    1.19  --------------------------------------------------------------------------------
    1.20 @@ -96,6 +104,7 @@
    1.21  More directories into files/ can be added according to the needs and goals
    1.22  of the packages.
    1.23  
    1.24 +
    1.25  Receipt variables and function
    1.26  --------------------------------------------------------------------------------
    1.27  Here are the variables used or usable in a sup receip as well as the uniq function.
    1.28 @@ -115,14 +124,15 @@
    1.29  receip to warn or prompt users to make an action.
    1.30  
    1.31  
    1.32 -Cook sup packages to be uploaded to the server
    1.33 +Colors shems for sup tools
    1.34  --------------------------------------------------------------------------------
    1.35 -Sup lets users upload packages to the server. To cook your first one you may want to
    1.36 -use the sup-demo package:
    1.37 -
    1.38 -  $ sup cook --init
    1.39 -  $ cp -rf /usr/share/sup/wok/sup-demo ~/.local/share/sup/wok
    1.40 -  $ sup cook sup-demo
    1.41 +Here are the colors guide line used in the cmdline tool:
    1.42 + 
    1.43 +  [036/36]  Cyan for installed/mirror 
    1.44 +  [035/35]  Magenta for cook/wok
    1.45 +  [033/33]  Yellow for commands/functions
    1.46 +  
    1.47 +The CGI/Web hug services match SCN/SliTaz network graphical style.
    1.48  
    1.49  
    1.50  Mime type & Icon
     2.1 --- a/libsup.sh	Tue Feb 28 05:18:43 2017 +0100
     2.2 +++ b/libsup.sh	Tue Feb 28 19:35:14 2017 +0100
     2.3 @@ -32,16 +32,43 @@
     2.4  
     2.5  # Set ttysize= # stty will not work if called from GTK box or CGI script
     2.6  tty_size() {
     2.7 -	local size=$(stty size | cut -d " " -f 2)
     2.8 -	[ "$size" ] && local size=80
     2.9 -	echo ${size}
    2.10 +	if ! stty size | cut -d " " -f 2; then
    2.11 +		echo 80
    2.12 +	fi
    2.13  }
    2.14  
    2.15 -# Minimal Busybox wget output. Usage: download "name" "url"
    2.16 +# Pretty Busybox wget output. Usage: download [name] [url] [dest]
    2.17 +# Download in current dir in $3 is not set
    2.18  download() {
    2.19 -	gettext "Fetching"; echo -n $(colorize 30 " db → ")
    2.20 -	wget "$2" 2>&1 | tail -n 2 | awk '{printf $3}' \
    2.21 -		| sed s'!*!-!'g
    2.22 +	name="$1"
    2.23 +	url="$2"
    2.24 +	dest=${3:-$(pwd)}
    2.25 +	file=$(basename $url)
    2.26 +	trap "echo -e '\nExiting...'; rm -f ${dest%/}/$file" SIGINT INT TERM
    2.27 +	# i18n
    2.28 +	dl="$(gettext 'Downloading')"
    2.29 +	
    2.30 +	# Get download name chars to adjust progress bar placement
    2.31 +	char="$(echo $dl $name | wc -L)"
    2.32 +	in=$((3 + ${char}))
    2.33 +	
    2.34 +	echo -n "$dl $(colorize 035 $name →)"
    2.35 +	wget -c -P ${dest} "$url" 2>&1 | while read file pct progress null
    2.36 +	do
    2.37 +		case "$progress" in
    2.38 +			"|"*) echo -n "$(indent ${in} $progress)" | sed s'!*!-!'g ;;
    2.39 +		esac
    2.40 +	done
    2.41 +	
    2.42 +	case "$?" in
    2.43 +		0)
    2.44 +			# Handle 4.0K 46.0M
    2.45 +			size="$(du -mh ${dest%/}/$(basename $2) | awk '{print $1}')"
    2.46 +			char="$(echo $size | wc -L)"
    2.47 +			in=$((7 + ${char}))
    2.48 +			indent $(($(tty_size) - ${in})) "[ $(colorize 035 $size) ]" ;;
    2.49 +		1) status ;;
    2.50 +	esac
    2.51  }
    2.52  
    2.53  # Extract a sup package: extract_sup "/path/to/pkg.sup"
    2.54 @@ -88,7 +115,8 @@
    2.55  	done
    2.56  	
    2.57  	newline
    2.58 -	gettext "Installing package:"; colorize 36 " $PACKAGE $VERSION"
    2.59 +	echo -n "$(colorize 33 $(gettext 'Installing package:'))"
    2.60 +	colorize 36 " $PACKAGE $VERSION"
    2.61  	log "$(gettext 'Installing package:') $PACKAGE $VERSION"
    2.62  	separator
    2.63  	
    2.64 @@ -120,8 +148,9 @@
    2.65  	echo "md5_sum=\"${md5sum}\"" >> receip
    2.66  	
    2.67  	# Now we need a place to store package data and set $sup_size
    2.68 -	echo -n "$(colorize 036 "$(gettext 'Installing files:') \
    2.69 -$(wc -l files.list | cut -d " " -f 1)")"
    2.70 +	echo -n "$(colorize 036 $(gettext 'Installing files:'))"
    2.71 +	echo -n " $(colorize 033 $(wc -l files.list | cut -d ' ' -f 1))"
    2.72 +	
    2.73  	data="${installed}/${PACKAGE}"
    2.74  	mkdir -p ${data}
    2.75  	
    2.76 @@ -143,14 +172,16 @@
    2.77  	. ${installed}/${pkg}/receip
    2.78  	
    2.79  	newline
    2.80 -	gettext "Removing package:"; colorize 36 " $PACKAGE $VERSION"
    2.81 +	echo -n "$(colorize 33 $(gettext 'Removing package:'))"
    2.82 +	colorize 36 " $PACKAGE $VERSION"
    2.83  	log "$(gettext 'Removing package:') $PACKAGE $VERSION"
    2.84  	separator
    2.85  	
    2.86  	gettext "Files to remove :"
    2.87 -	files="$(wc -l ${files_list} | cut -d ' ' -f 1)" in=8
    2.88 -	[ "$files" -gt "10" ] && in=9 && [ "$files" -gt "100" ] && in=10
    2.89 -	indent $(($(tty_size) - ${in})) "[ $(colorize 33 $files) ]"
    2.90 +	files="$(wc -l ${files_list} | cut -d ' ' -f 1)"
    2.91 +	char="$(echo $files | wc -L)"
    2.92 +	in=$((7 + ${char}))
    2.93 +	indent $(($(tty_size) - ${in})) "[ $(colorize 033 $files) ]"
    2.94  	
    2.95  	# Remove all files
    2.96  	for file in $(cat $files_list)
     3.1 --- a/sup	Tue Feb 28 05:18:43 2017 +0100
     3.2 +++ b/sup	Tue Feb 28 19:35:14 2017 +0100
     3.3 @@ -5,8 +5,6 @@
     3.4  # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
     3.5  # Author: Christophe Lincoln <pankso@slitaz.org>
     3.6  #
     3.7 -# Colors: installed 036/36; wok/package 035/35; commands/functions 033/33
     3.8 -#
     3.9  
    3.10  # Get system wide libsup.sh or development version if working in Hg folder
    3.11  if [ -f "/usr/lib/slitaz/libsup.sh" ]; then
    3.12 @@ -107,19 +105,18 @@
    3.13  		# Handle --init
    3.14  		if [ "$init" ]; then
    3.15  			gettext "Initializing sup cook environment..."
    3.16 -			cook_init
    3.17 -			status && exit 0
    3.18 +			cook_init; status; exit 0
    3.19  		fi
    3.20  		
    3.21  		# Sanity check
    3.22  		check_pkg_arg "$1"
    3.23  		if [ ! -f "$receip" ]; then
    3.24 -			gettext "Can't find receip in wok for:"
    3.25 -			colorize 31 " $pkg" && exit 1
    3.26 +			gettext "Can't find receip in wok for:"; colorize 31 " $pkg"; exit 1
    3.27  		fi
    3.28  		
    3.29  		newline
    3.30 -		gettext "Building package:"; colorize 36 " $pkg"
    3.31 +		echo -n "$(colorize 33 $(gettext 'Building package:'))"
    3.32 +		colorize 35 " $pkg"
    3.33  		separator
    3.34  		rm -rf ${cache} && mkdir -p ${cache} ${cooked}
    3.35  		
    3.36 @@ -167,9 +164,27 @@
    3.37  		status
    3.38  		
    3.39  		size="$(du -sh $cooked/$supfile | cut -d "	" -f 1)"
    3.40 -		echo "$(gettext 'Packages:') $(colorize 35 $supfile) ($size)"
    3.41 +		echo "$(gettext 'Packages:') $(colorize 035 $supfile) ($size)"
    3.42  		rm -rf ${cache} && newline ;;
    3.43  	
    3.44 +	run)
    3.45 +		# Run sup_install() without any installation to help package creation
    3.46 +		pkg="$2"
    3.47 +		receip="$wok/$pkg/receip"
    3.48 +		
    3.49 +		# Sanity check
    3.50 +		check_pkg_arg "$1"
    3.51 +		if [ ! -f "$receip" ]; then
    3.52 +			gettext "Can't find receip in wok for:"; colorize 31 " $pkg"; exit 1
    3.53 +		fi
    3.54 +		
    3.55 +		newline
    3.56 +		echo "$(gettext 'Running') $(colorize 36 $pkg) $(colorize 035 sup_install)"
    3.57 +		separator
    3.58 +		
    3.59 +		echo "To be continued"
    3.60 +		;;
    3.61 +	
    3.62  	-n|new)
    3.63  		pkg="$2"
    3.64  		if [ ! "$pkg" ]; then
    3.65 @@ -198,16 +213,17 @@
    3.66  		# Sanity check
    3.67  		check_pkg_arg "$1"
    3.68  		if [ ! -f "$pkg" ]; then
    3.69 -			gettext "Can't find package file:"; colorize 35 " $pkg" && exit 1
    3.70 +			gettext "Can't find package file:"; colorize 31 " $pkg" && exit 1
    3.71  		fi
    3.72  		
    3.73  		newline
    3.74 -		gettext "Extract package:"; colorize 36 " $(basename $pkg)"
    3.75 +		echo -n "$(colorize 33 $(gettext 'Extract package:'))"
    3.76 +		colorize 36 " $(basename $pkg)"
    3.77  		separator
    3.78  
    3.79  		extract_sup "$pkg"
    3.80  		
    3.81 -		echo "$(gettext 'Packages:') $(colorize 33 $pkg) ($size)"
    3.82 +		echo "$(gettext 'Packages:') $(colorize 036 $pkg) ($size)"
    3.83  		newline ;;
    3.84  		 
    3.85  	-i|install)
    3.86 @@ -261,33 +277,38 @@
    3.87  		gettext "Checking packages.sql..."
    3.88  		db_sum=$(wget -q "${server}?sup=dbsum" -O -)
    3.89  		if echo "$db_sum  $pkgsdb" | md5sum -s -c -; then
    3.90 -			indent $(($(tty_size) - 13)) "[ $(colorize 36 synced) ]"
    3.91 +			indent $(($(tty_size) - 13)) "[ $(colorize 036 synced) ]"
    3.92  		else
    3.93  			[ "$online" ] && rm -f ${pkgsdb}
    3.94 -			indent $(($(tty_size) - 13)) "[ $(colorize 35 update) ]"
    3.95 +			indent $(($(tty_size) - 13)) "[ $(colorize 035 update) ]"
    3.96  		fi
    3.97  		
    3.98 +		# --force
    3.99 +		[ "$force" ] && rm -f ${pkgsdb}
   3.100 +		
   3.101  		# No packages.sql: get latest!
   3.102  		if [ ! -f "$pkgsdb" ] && [ "$online" ]; then
   3.103 -			gettext "Fetching db"; echo -n " → "
   3.104 -			wget -P ${supdb} ${mirror}/packages.sql 2>&1 | tail -n 2 \
   3.105 -				| awk '{printf $3}'; status
   3.106 +			
   3.107 +			download "db" "${mirror}/packages.sql" "${supdb}"
   3.108  			
   3.109  			timestamp=$(sqlite3 ${pkgsdb} 'SELECT timestamp FROM info' \
   3.110  				| awk '{printf $1 " " $2}')
   3.111  			gettext "Database timestamp:"
   3.112 -			indent $(($(tty_size) - 23)) "[ $(boldify $timestamp) ]"
   3.113 +			indent $(($(tty_size) - 23)) "[ $(colorize 30 $timestamp) ]"
   3.114  		fi
   3.115  		
   3.116  		# Scan installed packages
   3.117 -		count=$(ls $installed | wc -l) in=8
   3.118 -		[ "$count" -gt "10" ] && in=9 && [ "$count" -gt "100" ] && in=10
   3.119 +		count="$(ls $installed | wc -l)"
   3.120 +		char="$(echo $count | wc -L)"
   3.121 +		in=$((7 + ${char}))
   3.122 +		
   3.123  		gettext "Installed packages:"
   3.124 -		indent $(($(tty_size) - ${in})) "[ $(colorize 33 $count) ]"
   3.125 +		indent $(($(tty_size) - ${in})) "[ $(colorize 033 $count) ]"
   3.126  		cat << EOT
   3.127  
   3.128 -$(boldify Package $(indent 30 Version) $(indent 52 Status))
   3.129 ---------------------------------------------------------------------------------
   3.130 +$(boldify $(gettext "Package") $(indent 30 $(gettext "Version")) \
   3.131 +$(indent 60 $(gettext "Status")))
   3.132 +$(separator "-")
   3.133  EOT
   3.134  		for pkg in $(ls $installed)
   3.135  		do
   3.136 @@ -300,21 +321,20 @@
   3.137  				vers=$(sqlite3 ${pkgsdb} \
   3.138  					"SELECT version FROM pkgs WHERE name = '$pkg'")
   3.139  				if [ "$VERSION" != "$vers" ]; then
   3.140 -					status="$(gettext "update")"
   3.141 +					status="$(colorize 032 $(gettext "update"))"
   3.142  				else
   3.143 -					status="$(gettext "rebuilt")"
   3.144 +					status="$(colorize 035 $(gettext "rebuilt"))"
   3.145  				fi
   3.146 -				echo "$pkg $(indent 30 $VERSION) $(indent 52 $status)"
   3.147 +				echo "$pkg $(indent 30 $VERSION) $(indent 60 $status)"
   3.148  				echo "$pkg" >> ${cache}/updates
   3.149  			fi
   3.150  		done
   3.151  		
   3.152  		if [ -f "$cache/updates" ]; then
   3.153  			count=$(cat $cache/updates | wc -l)
   3.154 -			cat << EOT
   3.155 ---------------------------------------------------------------------------------
   3.156 -EOT
   3.157 +			separator "-"
   3.158  			gettext "Available updates:"; colorize 32 " $count"
   3.159 +			newline
   3.160  			# --install
   3.161  			if [ "$install" ]; then
   3.162  				for pkg in $(cat $cache/updates); do
   3.163 @@ -345,7 +365,7 @@
   3.164  		boldify " $installed_nb"
   3.165  		separator
   3.166  		for pkg in $(ls ${installed}); do
   3.167 -			. ${wok}/${pkg}/receip
   3.168 +			. ${installed}/${pkg}/receip
   3.169  			echo "$pkg $(indent 26 $VERSION) $(indent 42 $MAINTAINER)"
   3.170  		done
   3.171  		if [ "$installed_nb" == 0 ]; then
     4.1 --- a/sup-demo/receip	Tue Feb 28 05:18:43 2017 +0100
     4.2 +++ b/sup-demo/receip	Tue Feb 28 19:35:14 2017 +0100
     4.3 @@ -3,9 +3,9 @@
     4.4  PACKAGE="sup-demo"
     4.5  VERSION="0.1"
     4.6  SHORT_DESC="Short package description and information"
     4.7 -MAINTAINER="devel@slitaz.org"
     4.8 +MAINTAINER="pankso@slitaz.org"
     4.9  LICENSE="BSD"
    4.10 -WEB_SITE="http://www.slitaz.org/"
    4.11 +WEB_SITE="http://scn.slitaz.org/?sup"
    4.12  
    4.13  # Sup dependencies will be automatically downloaded but system wide 
    4.14  # deps must be manually installed by root.