cookutils view modules/compressor @ rev 910

modules/compressor: fix installing local tools.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue May 30 12:08:36 2017 +0300 (2017-05-30)
parents ea11d3c96873
children b6b815ca6bb9
line source
1 #!/bin/sh
2 #
3 # compressor - module of the SliTaz Cook
4 # Copyright (C) SliTaz GNU/Linux - GNU GPL v3
5 #
7 . /usr/lib/slitaz/libcook.sh
9 # tazpkg install command
10 tpi='tazpkg -gi --quiet --local --cookmode'
13 # Compressor cache stuff
15 comp_cache_root='/home/slitaz/cache/cook'
16 mkdir -p "$comp_cache_root"
17 cache_stat=$(mktemp)
19 # Cache notes.
20 # Do not do the same job twice. Getting the file from the cache is much faster
21 # than compressing the file one more time. In addition, this cache is trying not
22 # to take extra space, using the hardlinks. Although the files from the cache
23 # without reference to itself should be removed periodically.
28 #
29 # Functions
30 #
33 # Working with time (with hundredths precision)
35 get_time() {
36 cut -d" " -f2 /proc/uptime
37 }
39 calc_time() {
40 # L10n: 's' is for seconds, 'm' is for minutes
41 awk -va="$1" -vb="$(get_time)" -vs="$(_ 's')" -vm="$(_ 'm')" '
42 BEGIN{
43 time = b - a;
44 if (time < 30)
45 printf("%.2f%s\n", time, s);
46 else
47 printf("%.2f%s ~ %.0f%s\n", time, s, time / 60, m);
48 }'
49 }
52 # Compressor mini summary
54 comp_summary() {
55 # "$time0" "$size0" "$size1"
56 status
57 [ "$2" -eq 0 ] && return
58 saving=$(awk -va="$2" -vb="$3" 'BEGIN{ printf("%.0f\n", (a - b) / 1024) }')
59 cache_msg=''
60 if [ -s "$cache_stat" ]; then
61 cache_msg=$(_n ' Cache hit: %d/%d.' "$(fgrep '+' $cache_stat | wc -l)" "$(wc -l < $cache_stat)")
62 echo -n > $cache_stat
63 fi
64 _ ' Time: %s. Size: %s B -> %s B. Save: %s KB.%s' \
65 "$(calc_time $1)" "$2" "$3" "$saving" "$cache_msg"
66 }
69 # Calculating different sizes
71 sizes() {
72 case $1 in
73 man) find $install/usr/share/man -type f -exec ls -l \{\} \; ;;
74 png) find $install -type f -name '*.png' -exec ls -l \{\} \; ;;
75 svg) find $install -type f -name '*.svg' -exec ls -l \{\} \; ;;
76 xml) find $install -type f \( -name '*.ui' -o -name '*.glade' \) -exec ls -l \{\} \; ;;
77 des) find $install -type f -name '*.desktop' -exec ls -l \{\} \; ;;
78 mo1) find $install -type f -name '*.mo' -exec ls -l \{\} \; ;;
79 loc) find $install/usr/share/i18n/locales -type f -exec ls -l \{\} \; ;;
80 mo2) find $fs/usr/share/locale -type f -name '*.mo' -exec ls -l \{\} \; ;;
81 gz) find $install -type f -name '*.gz' ! -path '*/share/man/*' -exec ls -l \{\} \; ;;
82 str) find $fs -type f \( -name '*.so*' -o -name '*.a' -o -name '*.pyc' -o -name '*.pyo' \
83 -o -name '.packlist' -o -name '*.pm' -o -name '*.pl' -o -name '*.pod' \) -exec ls -l \{\} \; ;;
84 esac | awk '{s+=$5}END{print s}'
85 }
88 # Query cache for already existing compressed file; substitute original file with the cached
89 # compressed one if so.
90 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
92 query_cache() {
93 md5=$(md5sum "$2")
94 cachefile="$comp_cache_root/$1/${md5%% *}"
95 echo "$cachefile"
96 if [ -f "$cachefile" ]; then
97 ln -f "$cachefile" "$2"
98 echo '+' >> "$cache_stat"
99 else
100 echo '-' >> "$cache_stat"
101 false
102 fi
103 }
106 # Store compressed file to the cache
107 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
109 store_cache() {
110 mkdir -p "${1%/*}"
111 mv "$2" "$1"
112 ln "$1" "$2"
113 }
116 # Function to compress all man pages
117 # Compressing can be disabled with COOKOPTS="!manz"
119 compress_manpages() {
120 time0=$(get_time)
121 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
122 manpath="$install/usr/share/man"
123 [ -d "$manpath" ] || return
124 size0=$(sizes man); [ -z "$size0" ] && return
126 $tpi advancecomp
128 action 'Compressing man pages...'
130 # We'll use only Gzip compression, so decompress other formats first
131 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
132 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
134 # Fast compress with gzip
135 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
137 # Fix symlinks
138 for i in $(find $manpath -type l); do
139 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
140 link=$(echo $i | sed 's|\.[gbx]z2*$||')
141 rm $i; ln -s $dest.gz $link.gz
142 done
144 # Recompress with advdef (it can't compress, only recompress)
145 IFS=$'\n'
146 for i in $(find $manpath -type f); do
147 if ! cached_path=$(query_cache mangz "$i"); then
148 advdef -z4q "$i"
149 store_cache "$cached_path" "$i"
150 fi
151 done
153 comp_summary "$time0" "$size0" "$(sizes man)"
154 }
157 # Function to recompress all gzip archives
158 # Recompressing can be disabled with COOKOPTS="!gz"
160 recompress_gz() {
161 time0=$(get_time)
162 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
163 size0=$(sizes gz); [ -z "$size0" ] && return
165 $tpi advancecomp
167 action 'Recompressing gzip files...'
169 # Recompress with advdef
170 IFS=$'\n'
171 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
172 if ! cached_path=$(query_cache gz "$i"); then
173 advdef -z4q "$i"
174 store_cache "$cached_path" "$i"
175 fi
176 done
178 comp_summary "$time0" "$size0" "$(sizes gz)"
179 }
182 # Function used after compile_rules() to compress all png images
183 # Compressing can be disabled with COOKOPTS="!pngz"
185 compress_png() {
186 time0=$(get_time)
187 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
188 size0=$(sizes png); [ -z "$size0" ] && return
190 use_pq=true
191 use_op=true
192 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
193 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
194 $use_pq && $tpi pngquant
195 $use_op && $tpi optipng
197 action 'Compressing png images...'
199 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
200 [ -z "$oplevel" ] && oplevel='2'
202 cache_section="png$oplevel"
203 $use_pq && cache_section="${cache_section}p"
204 $use_op && cache_section="${cache_section}o"
206 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
208 IFS=$'\n'
209 for i in $(find $install -type f -name '*.png'); do
210 if ! cached_path=$(query_cache $cache_section "$i"); then
211 $use_pq && pngquant -f --skip-if-larger --ext .png --speed 1 "$i"
212 $use_op && optipng -quiet -strip all -o$oplevel "$i"
213 store_cache "$cached_path" "$i"
214 fi
215 done
217 comp_summary "$time0" "$size0" "$(sizes png)"
218 }
221 # Function used after compile_rules() to compress all svg images
222 # Compressing can be disabled with COOKOPTS="!svgz"
224 compress_svg() {
225 time0=$(get_time)
226 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
227 size0=$(sizes svg); [ -z "$size0" ] && return
229 $tpi svgcleaner
231 action 'Compressing svg images...'
233 cleaner_log="$(mktemp)"
234 IFS=$'\n'
235 for i in $(find $install -type f -name '*.svg'); do
236 echo -n "$i: " >> "$cleaner_log"
237 svgcleaner "$i" "$i" --remove-unresolved-classes false --quiet true >> "$cleaner_log"
238 done
240 comp_summary "$time0" "$size0" "$(sizes svg)"
242 sed -i '/: $/d' "$cleaner_log"
243 if [ -s "$cleaner_log" ]; then
244 _ 'Cleaner warnings and errors:'
245 awk '{printf " %s\n", $0;}' "$cleaner_log"
246 echo
247 fi
248 rm "$cleaner_log"
249 }
252 # Function used after compile_rules() to shrink all *.ui and *.glade files:
253 # remove insignificant spaces and comments
254 # Compressing can be disabled with COOKOPTS="!uiz"
256 compress_ui() {
257 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
258 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
260 $tpi xmlstarlet
262 action 'Compressing ui files...'
264 size0=$(sizes xml)
265 time0=$(get_time)
266 temp_ui="$(mktemp)"
267 IFS=$'\n'
268 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
269 xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui"
270 cat "$temp_ui" > "$ui"
271 done
273 comp_summary "$time0" "$size0" "$(sizes xml)"
274 rm "$temp_ui"
275 }
278 # Get list of supported locales...
280 get_supported_locales() {
281 lpc='/slitaz-i18n/stuff/locale-pack.conf'
282 if [ -e "$WOK$lpc" ]; then
283 # ... from package in the local wok
284 . "$WOK$lpc"
285 else
286 # ... from Hg
287 temp_conf=$(mktemp)
288 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
289 if [ -s $temp_conf ]; then
290 . $temp_conf
291 else
292 # Give up and use hardcoded list
293 LOCALE_PACK="ar ca cs da de el en es fi fr hr hu id is it ja nb nl nn pl pt \
294 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
295 fi
296 rm $temp_conf
297 fi
298 echo $LOCALE_PACK
299 }
302 # Fix common errors and warnings in the .desktop files
303 # Fixing can be disabled with COOKOPTS="!fixdesktops"
305 fix_desktop_files() {
306 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
307 deskpath="$install/usr/share/applications"
308 [ -d "$deskpath" ] || return
309 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
311 size0=$(sizes des)
312 time0=$(get_time)
314 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
315 $tpi desktop-file-utils-extra
316 fi
318 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
319 # Default value is "" (empty). That means for us that we'll use the full
320 # list of supported locales here.
321 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
323 IFS=$'\n'
324 for desktop in $(find $deskpath -type f -name '*.desktop'); do
325 cp "$desktop" "$desktop.orig"
327 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
328 sdft "$desktop" -i
330 # Fix common errors in .desktop file
331 fix-desktop-file "$desktop"
333 # Strip unsupported locales from .desktop file
334 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
335 sdft "$desktop" -i -k "$LOCALE"
337 # Extra-strip
338 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
339 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
341 if [ -n "$QA" ]; then
342 # Check the rest of errors, warnings and tips
343 _ 'QA: Checking %s...' "$(basename $desktop)"
344 diff "$desktop.orig" "$desktop"
345 desktop-file-validate "$desktop" | busybox fold -s
346 echo
347 fi
349 rm "$desktop.orig"
350 done
352 comp_summary "$time0" "$size0" "$(sizes des)"
353 }
356 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
357 # to the translation (msgid = msgstr)
358 # Normalization can be disabled with COOKOPTS="!monorm"
360 normalize_mo() {
361 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
362 [ -z "$(find $install -type f -name '*.mo')" ] && return
364 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
365 $tpi gettext
366 # Gconv modules (convert to UTF-8)
367 $tpi glibc-locale
369 action 'Normalizing mo files...'
371 size0=$(sizes mo1)
372 time0=$(get_time)
374 # Process all existing *.mo files
375 IFS=$'\n'
376 for mo in $(find "$install" -type f -name '*.mo'); do
377 tmpfile="$(mktemp)"
379 msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'
380 # add newline
381 echo >> "$tmpfile"
383 # get Plural-Forms
384 awk '
385 BEGIN { skip = ""; }
386 {
387 if (! skip) {
388 s = $0;
389 gsub(/^[^\"]*\"/, "", s);
390 gsub(/\"$/, "", s);
391 printf("%s", s);
392 }
393 if (! $0) skip = "yes";
394 }
395 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
397 if ! grep -q 'msgid_plural' "$tmpfile"; then
398 echo > "$tmpfile.pf"
399 fi
401 # main
402 awk -v pf="$(cat "$tmpfile.pf")" '
403 function clean() {
404 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
405 }
407 function getstring() {
408 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
409 s = $0;
410 gsub(/^[^\"]*\"/, "", s);
411 gsub(/\"$/, "", s);
412 return s;
413 }
415 BEGIN {
416 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
417 if (pf)
418 printf("\"%s\\n\"\n", pf);
419 printf("\n");
420 skip = 1;
421 clean();
422 }
424 {
425 # Skip the entire header
426 if (!skip) {
427 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
428 mode = $1;
429 if ($1 == "msgstr[0]") mode = "msgstr0";
430 if ($1 == "msgstr[1]") mode = "msgstr1";
431 if ($1 == "msgstr[2]") mode = "msgstr2";
432 if ($1 == "msgstr[3]") mode = "msgstr3";
433 if ($1 == "msgstr[4]") mode = "msgstr4";
434 if ($1 == "msgstr[5]") mode = "msgstr5";
436 if (mode == "msgctxt") msgctxt = msgctxt getstring();
437 if (mode == "msgid") msgid = msgid getstring();
438 if (mode == "msgstr") msgstr = msgstr getstring();
439 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
440 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
441 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
442 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
443 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
444 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
445 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
447 if (! $0) {
448 if (msgid != msgstr) {
449 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
450 printf("msgid \"%s\"\n", msgid);
451 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
452 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
453 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
454 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
455 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
456 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
457 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
458 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
459 printf("\n");
460 }
461 clean();
462 }
463 }
464 if ($0 == "") skip = "";
465 }
466 ' "$tmpfile" > "$tmpfile.awk"
468 msgfmt "$tmpfile.awk" -o "$tmpfile.mo"
470 if [ -s "$tmpfile.mo" ]; then
471 rm "$mo"; mv "$tmpfile.mo" "$mo"
472 else
473 _ 'Error processing %s' "$mo"
474 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
475 fi
477 # Clean
478 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
479 done
481 comp_summary "$time0" "$size0" "$(sizes mo1)"
482 }
485 # Strip locale definitions: normalize whitespace and remove comments
486 # Stripping can be disabled with COOKOPTS="!locdef"
488 strip_locale_def() {
489 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
490 [ ! -d "$install/usr/share/i18n/locales" ] && return
491 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
493 action 'Stripping locale definitions...'
494 size0=$(sizes loc)
495 time0=$(get_time)
497 for i in $(find $install/usr/share/i18n/locales -type f); do
498 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
499 done
501 comp_summary "$time0" "$size0" "$(sizes loc)"
502 }
505 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
506 # as removing unneeded files like in Python packages. Cross compiled binaries
507 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
508 # Stripping can be disabled with COOKOPTS="!strip"
510 strip_package() {
511 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
513 case "$ARCH" in
514 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
515 *) export STRIP='strip' ;;
516 esac
517 action 'Executing strip on all files...'
518 size0=0
519 size1=0
520 time0=$(get_time)
522 # Strip executable files
523 for dir in $fs/bin $fs/sbin $fs/usr/bin $fs/usr/sbin $fs/usr/games; do
524 if [ -d "$dir" ]; then
525 oldsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
526 find $dir -type f -exec $STRIP -s '{}' 2>/dev/null \;
527 newsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
528 size0=$((size0 + oldsize)); size1=$((size1 + newsize))
529 fi
530 done
532 oldsize=$(sizes str)
534 # Strip shared and static libraries
535 find $fs -name '*.so*' -exec $STRIP -s '{}' 2>/dev/null \;
536 find $fs -name '*.a' -exec $STRIP --strip-debug '{}' 2>/dev/null \;
538 # Remove Python *.pyc and *.pyo
539 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
541 # Remove both with the empty subfolders:
542 # 1. Perl perllocal.pod and .packlist (unconditionally)
543 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
544 # 2. Perl *.pod (if not disabled)
545 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
546 perlfiles="$perlfiles $(find $fs -type f -name '*.pod')"
547 echo "$perlfiles" | xargs rm -f 2>/dev/null
548 echo "$perlfiles" | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' | xargs rmdir -p 2>/dev/null
550 # Strip Perl files (*.pm and *.pl) from documentation inside (if not disabled)
551 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
552 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
554 newsize=$(sizes str)
556 comp_summary "$time0" "$((size0 + oldsize))" "$((size1 + newsize))"
557 }
560 # Strip unsupported locales (.mo files)
562 strip_mo_i18n() {
563 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
565 [ ! -d "$fs/usr/share/locale" ] && return
566 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
568 action 'Thin out translation files...'
569 size0=$(sizes mo2)
570 time0=$(get_time)
572 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
573 # Default value is "" (empty). That means for us that we'll use the full
574 # list of supported locales here.
575 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
577 # Existing locales
578 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
580 # Thin out the list of existing locales. At the end there will be only locales that need
581 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
582 for keep_locale in $LOCALE; do
583 elocales=${elocales//$keep_locale}
584 done
586 # Remove the unsupported locales
587 for rem_locale in $elocales; do
588 [ -d "$fs/usr/share/locale/$rem_locale" ] &&
589 rm -r "$fs/usr/share/locale/$rem_locale"
590 done
592 comp_summary "$time0" "$size0" "$(sizes mo2)"
593 }
598 case $1 in
599 install)
600 # Compressors working in the $install
601 case "$ARCH" in
602 arm*) ;;
603 *)
604 recompress_gz
605 compress_manpages
606 compress_png
607 compress_svg
608 compress_ui
609 ;;
610 esac
612 fix_desktop_files
613 normalize_mo
614 strip_locale_def
615 ;;
616 fs)
617 # Compressors working in the $fs
618 strip_package
619 strip_mo_i18n
620 ;;
621 esac
623 # Clean
624 rm "$cache_stat"
625 find $comp_cache_root -type f -links -2 -delete