cookutils rev 868

modules/compressor: make the caching system for *.man.gz and *.png.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sun Jan 22 16:18:08 2017 +0200 (2017-01-22)
parents 5f6be706ab4f
children 604b1ab4c992
files modules/compressor
line diff
     1.1 --- a/modules/compressor	Thu Jan 12 04:04:52 2017 +0200
     1.2 +++ b/modules/compressor	Sun Jan 22 16:18:08 2017 +0200
     1.3 @@ -7,6 +7,20 @@
     1.4  . /usr/lib/slitaz/libcook.sh
     1.5  
     1.6  
     1.7 +# Compressor cache stuff
     1.8 +
     1.9 +comp_cache_root='/var/cache/cook'
    1.10 +cache_stat=$(mktemp)
    1.11 +
    1.12 +# Cache notes.
    1.13 +# Do not do the same job twice. Getting the file from the cache is much faster
    1.14 +# than compressing the file one more time. In addition, this cache is trying not
    1.15 +# to take extra space, using the hardlinks. Although the files from the cache
    1.16 +# without reference to himself should be removed periodically (to be done).
    1.17 +
    1.18 +
    1.19 +
    1.20 +
    1.21  #
    1.22  # Functions
    1.23  #
    1.24 @@ -36,7 +50,13 @@
    1.25  	[ "$2" -eq 0 ] && return
    1.26  	time=$(($time1 - $1))
    1.27  	saving=$(( ($2 - $3) / 1024 ))
    1.28 -	_ '  Time: %s. Size: %s B -> %s B. Save: %s KB' "$(disp_time $time)" "$2" "$3" "$saving"
    1.29 +	cache_msg=''
    1.30 +	if [ -s "$cache_stat" ]; then
    1.31 +		cache_msg=$(_n ' Cache hit: %d/%d.' "$(fgrep '+' $cache_stat | wc -l)" "$(wc -l < $cache_stat)")
    1.32 +		echo -n > $cache_stat
    1.33 +	fi
    1.34 +	_ '  Time: %s. Size: %s B -> %s B. Save: %s KB.%s' \
    1.35 +		"$(disp_time $time)" "$2" "$3" "$saving" "$cache_msg"
    1.36  }
    1.37  
    1.38  
    1.39 @@ -56,6 +76,34 @@
    1.40  }
    1.41  
    1.42  
    1.43 +# Query cache for already existing compressed file; substitute original file with the cached
    1.44 +# compressed one if so.
    1.45 +# $1: cache section (gz, mangz, png, etc.); $2: path to original file
    1.46 +
    1.47 +query_cache() {
    1.48 +	md5=$(md5sum "$2")
    1.49 +	cachefile="$comp_cache_root/$1/${md5%% *}"
    1.50 +	echo "$cachefile"
    1.51 +	if [ -f "$cachefile" ]; then
    1.52 +		ln -f "$cachefile" "$2"
    1.53 +		echo '+' >> "$cache_stat"
    1.54 +	else
    1.55 +		echo '-' >> "$cache_stat"
    1.56 +		false
    1.57 +	fi
    1.58 +}
    1.59 +
    1.60 +
    1.61 +# Store compressed file to the cache
    1.62 +# $1: path to cache entry to be stored; $2: path to compressed file to be stored
    1.63 +
    1.64 +store_cache() {
    1.65 +	mkdir -p "${1%/*}"
    1.66 +	mv "$2" "$1"
    1.67 +	ln "$1" "$2"
    1.68 +}
    1.69 +
    1.70 +
    1.71  # Function to compress all man pages
    1.72  # Compressing can be disabled with COOKOPTS="!manz"
    1.73  
    1.74 @@ -86,7 +134,10 @@
    1.75  
    1.76  	# Recompress with advdef (it can't compress, only recompress)
    1.77  	for i in $(find $install/usr/share/man -type f); do
    1.78 -		advdef -z4q $i
    1.79 +		if ! cached_path=$(query_cache mangz "$i"); then
    1.80 +			advdef -z4q "$i"
    1.81 +			store_cache "$cached_path" "$i"
    1.82 +		fi
    1.83  	done
    1.84  
    1.85  	comp_summary "$time0" "$size0" "$(sizes man)"
    1.86 @@ -111,12 +162,20 @@
    1.87  	action 'Compressing png images...'
    1.88  
    1.89  	oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
    1.90 -	[ -z "$oplevel" ]     && oplevel='2'
    1.91 +	[ -z "$oplevel" ] && oplevel='2'
    1.92 +
    1.93 +	cache_section="png$oplevel"
    1.94 +	$use_pq && cache_section="${cache_section}p"
    1.95 +	$use_op && cache_section="${cache_section}o"
    1.96 +
    1.97  	[ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
    1.98  
    1.99  	for i in $(find $install -type f -name '*.png'); do
   1.100 -		$use_pq && pngquant -f --skip-if-larger --ext .png --speed 1 "$i"
   1.101 -		$use_op && optipng -quiet -strip all -o$oplevel "$i"
   1.102 +		if ! cached_path=$(query_cache $cache_section "$i"); then
   1.103 +			$use_pq && pngquant -f --skip-if-larger --ext .png --speed 1 "$i"
   1.104 +			$use_op && optipng -quiet -strip all -o$oplevel "$i"
   1.105 +			store_cache "$cached_path" "$i"
   1.106 +		fi
   1.107  	done
   1.108  
   1.109  	comp_summary "$time0" "$size0" "$(sizes png)"
   1.110 @@ -508,3 +567,6 @@
   1.111  		strip_mo_i18n
   1.112  		;;
   1.113  esac
   1.114 +
   1.115 +# Clean
   1.116 +rm "$cache_stat"