cookutils rev 834

cook: remake compressors (few more to come), introduce COOKOPTS.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Nov 14 07:01:25 2016 +0200 (2016-11-14)
parents cf394b12c320
children cb0d015fce20
files cook doc/cookopts.txt
line diff
     1.1 --- a/cook	Mon Nov 14 02:48:39 2016 +0200
     1.2 +++ b/cook	Mon Nov 14 07:01:25 2016 +0200
     1.3 @@ -349,6 +349,23 @@
     1.4  }
     1.5  
     1.6  
     1.7 +# Display time.
     1.8 +
     1.9 +disp_time() {
    1.10 +	local sec div min
    1.11 +	sec="$1"
    1.12 +	div=$(( ($1 + 30) / 60))
    1.13 +	case $div in
    1.14 +		0) min='';;
    1.15 +		# L10n: 'm' is for minutes (approximate cooking time)
    1.16 +		*) min=$(_n ' ~ %dm' "$div");;
    1.17 +	esac
    1.18 +
    1.19 +	# L10n: 's' is for seconds (cooking time)
    1.20 +	_ '%ds%s' "$sec" "$min"
    1.21 +}
    1.22 +
    1.23 +
    1.24  # Display cooked package summary.
    1.25  
    1.26  summary() {
    1.27 @@ -360,14 +377,6 @@
    1.28  	files=$(cat $WOK/$pkg/taz/$pkg-*/files.list | wc -l)
    1.29  	[ -n "$TARBALL" ] && srcsize=$(du -sh $SRC/$TARBALL | awk '{print $1}')
    1.30  
    1.31 -	sec="$time"
    1.32 -	div=$(( ($time + 30) / 60))
    1.33 -	case $div in
    1.34 -		0) min='';;
    1.35 -		# L10n: 'm' is for minutes (approximate cooking time)
    1.36 -		*) min=$(_n '~ %dm' "$div");;
    1.37 -	esac
    1.38 -
    1.39  	_ 'Summary for: %s' "$PACKAGE $VERSION"
    1.40  	separator
    1.41  
    1.42 @@ -380,7 +389,7 @@
    1.43  	_ 'Compressed  : %s' "$size"
    1.44  	_ 'Files       : %s' "$files"
    1.45  	# L10n: 's' is for seconds (cooking time)
    1.46 -	_ 'Cook time   : %ds %s' "$sec" "$min"
    1.47 +	_ 'Cook time   : %s' "$(disp_time "$time")"
    1.48  	_ 'Cook date   : %s' "$(date "$(_ '+%%F %%R')")"
    1.49  	_ 'Host arch   : %s' "$ARCH"
    1.50  	separator
    1.51 @@ -428,8 +437,8 @@
    1.52  		fi
    1.53  	fi
    1.54  
    1.55 -	# Generic pixmaps copy can be disabled with GENERIC_PIXMAPS="no"
    1.56 -	if [ "$GENERIC_PIXMAPS" != 'no' ]; then
    1.57 +	# Generic pixmaps copy can be disabled with COOKOPTS="!pixmaps" (or GENERIC_PIXMAPS="no")
    1.58 +	if [ "${COOKOPTS/!pixmaps/}" == "$COOKOPTS" -a "$GENERIC_PIXMAPS" != 'no' ]; then
    1.59  		if [ -d "$install/usr/share/pixmaps" ]; then
    1.60  			mkdir -p $fs/usr/share/pixmaps
    1.61  			if [ -f "$install/usr/share/pixmaps/$PACKAGE.png" ]; then
    1.62 @@ -449,8 +458,8 @@
    1.63  	fi
    1.64  
    1.65  	# Desktop entry (.desktop).
    1.66 -	# Generic desktop entry copy can be disabled with GENERIC_MENUS="no"
    1.67 -	if [ "$GENERIC_MENUS" != 'no' ]; then
    1.68 +	# Generic desktop entry copy can be disabled with COOKOPTS="!menus" (or GENERIC_MENUS="no")
    1.69 +	if [ "${COOKOPTS/!menus/}" == "$COOKOPTS" -a "$GENERIC_MENUS" != 'no' ]; then
    1.70  		if [ -d "$install/usr/share/applications" ] && [ -z "$WANTED" ]; then
    1.71  			mkdir -p $fs/usr/share
    1.72  			cp -a $install/usr/share/applications $fs/usr/share
    1.73 @@ -476,14 +485,14 @@
    1.74  
    1.75  
    1.76  # Fix common errors and warnings in the .desktop files
    1.77 +# Fixing can be disabled with COOKOPTS="!fixdesktops"
    1.78  
    1.79  fix_desktop_files() {
    1.80 +	[ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
    1.81  	[ -z "$(find $install -type f -name '*.desktop')" ] && return
    1.82  
    1.83  	if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
    1.84 -		action 'Installing dep (web/cache): %s' 'desktop-file-utils-extra'
    1.85  		tazpkg -gi desktop-file-utils-extra --quiet
    1.86 -		status
    1.87  	fi
    1.88  
    1.89  	for desktop in $(find $install -type f -name '*.desktop'); do
    1.90 @@ -508,30 +517,61 @@
    1.91  }
    1.92  
    1.93  
    1.94 +# Compressor mini summary
    1.95 +
    1.96 +comp_summary() {
    1.97 +	local time=$(($2 - $1))
    1.98 +	local saving=$(( ($3 - $4) / 1024 ))
    1.99 +	_ '  Time: %s. Size: %s B -> %s B. Save: %s KB' "$(disp_time $time)" "$3" "$4" "$saving"
   1.100 +}
   1.101 +
   1.102 +
   1.103  # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
   1.104  # as removing unneeded files like in Python packages. Cross compiled binaries
   1.105  # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
   1.106 +# Stripping can be disabled with COOKOPTS="!strip"
   1.107  
   1.108  strip_package() {
   1.109 +	[ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
   1.110 +
   1.111  	case "$ARCH" in
   1.112  		arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
   1.113  		*)           export STRIP='strip' ;;
   1.114  	esac
   1.115  	action 'Executing strip on all files...'
   1.116 +	local size0=0 size1=0 oldsize newsize
   1.117 +	local time0=$(date +%s)
   1.118 +
   1.119 +	# Strip executable files
   1.120  	for dir in $fs/bin $fs/sbin $fs/usr/bin $fs/usr/sbin $fs/usr/games; do
   1.121  		if [ -d "$dir" ]; then
   1.122 +			oldsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
   1.123  			find $dir -type f -exec $STRIP -s '{}' 2>/dev/null \;
   1.124 +			newsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
   1.125 +			size0=$((size0 + oldsize)); size1=$((size1 + newsize))
   1.126  		fi
   1.127  	done
   1.128 +
   1.129 +	# Strip shared and static libraries
   1.130 +	# Remove Python *.pyc and *.pyo, Perl perllocal.pod and .packlist
   1.131 +	oldsize=$(find $fs -type f \( \
   1.132 +		-name '*.so*' -o -name '*.a' -o \
   1.133 +		-name '*.pyc' -o -name '*.pyo' -o \
   1.134 +		-name 'perllocal.pod' -o -name '.packlist' \) -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
   1.135 +
   1.136  	find $fs -name '*.so*' -exec $STRIP -s '{}' 2>/dev/null \;
   1.137  	find $fs -name '*.a' -exec $STRIP --strip-debug '{}' 2>/dev/null \;
   1.138 +	find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
   1.139 +	find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \) -delete 2>/dev/null
   1.140 +
   1.141 +	newsize=$(find $fs -type f \( \
   1.142 +		-name '*.so*' -o -name '*.a' -o \) -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
   1.143 +
   1.144 +	size0=$((size0 + oldsize)); size1=$((size1 + newsize))
   1.145 +
   1.146 +	local time1=$(date +%s)
   1.147  	status
   1.148 -
   1.149 -	# Remove Python .pyc and .pyo from packages.
   1.150 -	find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
   1.151 -
   1.152 -	# Remove Perl perllocal.pod and .packlist from packages.
   1.153 -	find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \) -delete 2>/dev/null
   1.154 +	comp_summary "$time0" "$time1" "$size0" "$size1"
   1.155  }
   1.156  
   1.157  
   1.158 @@ -568,9 +608,13 @@
   1.159  	fi
   1.160  }
   1.161  
   1.162 +
   1.163  # Function to compress all man pages
   1.164 +# Compressing can be disabled with COOKOPTS="!manz"
   1.165  
   1.166  compress_manpages() {
   1.167 +	[ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
   1.168 +
   1.169  	case "$ARCH" in
   1.170  		arm*) return;; # While SliTaz-arm miss `advancecomp`
   1.171  	esac
   1.172 @@ -581,6 +625,8 @@
   1.173  
   1.174  	action 'Compressing man pages...'
   1.175  
   1.176 +	local size0=$(find $install/usr/share/man -type f -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.177 +	local time0=$(date +%s)
   1.178  	# We'll use only Gzip compression, so decompress other formats first
   1.179  	find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
   1.180  	find $manpath -type f -name '*.xz'  -exec unxz    \{\} \;
   1.181 @@ -601,7 +647,80 @@
   1.182  		advdef -z4q $i
   1.183  	done
   1.184  
   1.185 +	local size1=$(find $install/usr/share/man -type f -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.186 +	local time1=$(date +%s)
   1.187  	status
   1.188 +	comp_summary "$time0" "$time1" "$size0" "$size1"
   1.189 +}
   1.190 +
   1.191 +
   1.192 +# Function used after compile_rules() to compress all png images
   1.193 +# Compressing can be disabled with COOKOPTS="!pngz"
   1.194 +
   1.195 +cook_compress_png() {
   1.196 +	[ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
   1.197 +	case "$ARCH" in
   1.198 +		arm*) return;; # While SliTaz-arm miss `pngquant` and `optipng`
   1.199 +	esac
   1.200 +	[ -z "$(find $install -type f -name '*.png')" ] && return
   1.201 +
   1.202 +	action 'Compressing png images...'
   1.203 +	local size0=$(find $install -type f -name '*.png' -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.204 +	local time0=$(date +%s)
   1.205 +
   1.206 +	local use_pq=true use_op=true
   1.207 +	[ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
   1.208 +	[ "${COOKOPTS/!optipng/}"  != "$COOKOPTS" ] && use_op=false
   1.209 +
   1.210 +	$use_pq && tazpkg -gi pngquant --quiet
   1.211 +	$use_op && tazpkg -gi optipng --quiet
   1.212 +
   1.213 +	local oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
   1.214 +	[ -z "$oplevel" ]     && oplevel='2'
   1.215 +	[ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
   1.216 +
   1.217 +	for i in $(find $install -type f -name '*.png'); do
   1.218 +		$use_pq && pngquant -f --skip-if-larger --ext .png --speed 1 "$i"
   1.219 +		$use_op && optipng -quiet -strip all -o$oplevel "$i"
   1.220 +	done
   1.221 +
   1.222 +	local size1=$(find $install -type f -name '*.png' -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.223 +	local time1=$(date +%s)
   1.224 +	status
   1.225 +	comp_summary "$time0" "$time1" "$size0" "$size1"
   1.226 +}
   1.227 +
   1.228 +
   1.229 +# Function used after compile_rules() to compress all svg images
   1.230 +# Compressing can be disabled with COOKOPTS="!svgz"
   1.231 +
   1.232 +cook_compress_svg() {
   1.233 +	[ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
   1.234 +	case "$ARCH" in
   1.235 +		arm*) return;; # While SliTaz-arm miss `svgcleaner`
   1.236 +	esac
   1.237 +	[ -z "$(find $install -type f -name '*.svg')" ] && return
   1.238 +
   1.239 +	action 'Compressing svg images...'
   1.240 +	local size0=$(find $install -type f -name '*.svg' -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.241 +	local time0=$(date +%s)
   1.242 +	tazpkg -gi svgcleaner --quiet
   1.243 +	cleaner_log="$(mktemp)"
   1.244 +	for i in $(find $install -type f -name '*.svg'); do
   1.245 +		echo -n "$i: " >> "$cleaner_log"
   1.246 +		svgcleaner "$i" "$i" --remove-unresolved-classes false --quiet true >> "$cleaner_log"
   1.247 +	done
   1.248 +	local size1=$(find $install -type f -name '*.svg' -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.249 +	local time1=$(date +%s)
   1.250 +	status
   1.251 +	comp_summary "$time0" "$time1" "$size0" "$size1"
   1.252 +	sed -i '/: $/d' "$cleaner_log"
   1.253 +	if [ -s "$cleaner_log" ]; then
   1.254 +		echo 'Cleaner warnings and errors:'
   1.255 +		awk '{printf "  %s\n", $0;}' "$cleaner_log"
   1.256 +		echo
   1.257 +	fi
   1.258 +	rm "$cleaner_log"
   1.259  }
   1.260  
   1.261  
   1.262 @@ -819,7 +938,10 @@
   1.263  	fi
   1.264  
   1.265  	# Actions to do after compiling the package
   1.266 +	footer
   1.267  	compress_manpages
   1.268 +	cook_compress_png
   1.269 +	cook_compress_svg
   1.270  	footer
   1.271  
   1.272  	# Execute testsuite.
   1.273 @@ -830,6 +952,7 @@
   1.274  	fi
   1.275  
   1.276  	fix_desktop_files
   1.277 +
   1.278  	update_installed_cook_diff force
   1.279  }
   1.280  
   1.281 @@ -1233,46 +1356,6 @@
   1.282  }
   1.283  
   1.284  
   1.285 -# Function to use in compile_rules() to compress all png images
   1.286 -
   1.287 -cook_compress_png() {
   1.288 -	case "$ARCH" in
   1.289 -		arm*) return;; # While SliTaz-arm miss `pngquant` and `optipng`
   1.290 -	esac
   1.291 -
   1.292 -	action 'Compressing png images...'
   1.293 -	local size0=$(find $install -type f -name '*.png' -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.294 -	tazpkg -gi pngquant --quiet
   1.295 -	tazpkg -gi optipng --quiet
   1.296 -	for i in $(find $install -type f -name '*.png'); do
   1.297 -		pngquant -f --skip-if-larger --ext .png --speed 1 "$i"
   1.298 -		optipng -quiet -strip all -o7 -zm1-9 "$i"
   1.299 -	done
   1.300 -	local size1=$(find $install -type f -name '*.png' -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.301 -	status
   1.302 -	echo "  Size: $size0 B -> $size1 B"
   1.303 -}
   1.304 -
   1.305 -
   1.306 -# Function to use in compile_rules() to compress all svg images
   1.307 -
   1.308 -cook_compress_svg() {
   1.309 -	case "$ARCH" in
   1.310 -		arm*) return;; # While SliTaz-arm miss `svgcleaner`
   1.311 -	esac
   1.312 -
   1.313 -	action 'Compressing svg images...'
   1.314 -	local size0=$(find $install -type f -name '*.svg' -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.315 -	tazpkg -gi svgcleaner --quiet
   1.316 -	for i in $(find $install -type f -name '*.svg'); do
   1.317 -		svgcleaner "$i" "$i" --remove-unresolved-classes false
   1.318 -	done
   1.319 -	local size1=$(find $install -type f -name '*.svg' -exec ls -l \{\} \; | awk '{s+=$5}END{print s}')
   1.320 -	status
   1.321 -	echo "  Size: $size0 B -> $size1 B"
   1.322 -}
   1.323 -
   1.324 -
   1.325  # Function to use in genpkg_rules() to copy specified files from $install to $fs
   1.326  
   1.327  cook_copy_files() {
   1.328 @@ -1290,6 +1373,23 @@
   1.329  }
   1.330  
   1.331  
   1.332 +# Function to use in genpkg_rules() to copy specified folders from $install to $fs
   1.333 +
   1.334 +cook_copy_folders() {
   1.335 +	action 'Copying folders...'
   1.336 +	cd $install
   1.337 +	local i j
   1.338 +	for i in $@; do
   1.339 +		for j in $(find . -name $i -type d); do
   1.340 +			mkdir -p $fs$(dirname ${j#.})
   1.341 +			cp -a $j $fs/${j#.}
   1.342 +		done
   1.343 +	done
   1.344 +	cd - >/dev/null
   1.345 +	status
   1.346 +}
   1.347 +
   1.348 +
   1.349  # Function to use in genpkg_rules() to copy hicolor icons in specified sizes
   1.350  # (default: 16 and 48) from $install to $fs
   1.351  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/doc/cookopts.txt	Mon Nov 14 07:01:25 2016 +0200
     2.3 @@ -0,0 +1,51 @@
     2.4 +You can use a variable COOKOPTS in the receipts to adjust certain aspects of the
     2.5 +preparation of a package. Multiple options are allowed, that can be written
     2.6 +together or separated by spaces, tabs or any other symbols.
     2.7 +
     2.8 +Example of use:
     2.9 +
    2.10 +    COOKOPTS="!menus !pngz"
    2.11 +
    2.12 +
    2.13 +Currently, the following options are recognized:
    2.14 +
    2.15 +!pixmaps
    2.16 +    Don't copy icons from a /usr/share/pixmaps folder as well as from stuff
    2.17 +    folder (the same action that made obsolete option `GENERIC_PIXMAPS="no"`).
    2.18 +
    2.19 +!menus
    2.20 +    Don't copy desktop files from a /usr/share/applications folder (the same
    2.21 +    action that made obsolete option `GENERIC_MENUS="no"`).
    2.22 +
    2.23 +!fixdesktops
    2.24 +    Don't fix common errors and warnings in the .desktop files.
    2.25 +
    2.26 +!strip
    2.27 +    Don't strip executable files, shared and static libraries, as well as remove
    2.28 +    Python (*.pyc and *.pyo) and Perl (perllocal.pod and .packlist) files.
    2.29 +
    2.30 +!manz
    2.31 +    Don't compress all man pages.
    2.32 +
    2.33 +!pngz
    2.34 +    Don't compress all png images. Image compression allows you to save some
    2.35 +    space, but it takes a lot of time.
    2.36 +
    2.37 +!pngquant
    2.38 +    Don't use `pngquant` to compress png images. Note, `pngquant` produces
    2.39 +    indexed images (max 256 colors) which can be inappropriate in some cases:
    2.40 +    a few programs (such as SLiM) will not recognize this compressed PNG file
    2.41 +    format.
    2.42 +
    2.43 +!optipng
    2.44 +    Don't use `optipng` to compress png images. Note, `optipng` is lossless png
    2.45 +    compressor. Using `pngquant` and `optipng` in conjunction allows to save
    2.46 +    more space.
    2.47 +
    2.48 +op0 to op8
    2.49 +    Use specified `optipng` optimization level. The higher the level, the slower
    2.50 +    compression. Note, this option does not have an exclamation mark, because it
    2.51 +    means "don't" and here is "do" otherwise.
    2.52 +
    2.53 +!svgz
    2.54 +    Don't compress all svg images.