cookutils view modules/compressor @ rev 1103

modules/compressor: gettext package was reorganized; lighttpd/cooker.css: smaller patch icon
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sat Dec 01 11:36:38 2018 +0200 (2018-12-01)
parents 8e94062e3045
children 3c6c68093522
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 # Compressor cache stuff
11 comp_cache_root='/home/slitaz/cache/cook'
12 mkdir -p "$comp_cache_root"
13 cache_stat=$(mktemp)
15 # Cache notes.
16 # Do not do the same job twice. Getting the file from the cache is much faster
17 # than compressing the file one more time. In addition, this cache is trying not
18 # to take extra space using the hardlinks. Although the files from the cache
19 # without reference to itself should be removed periodically.
24 #
25 # Functions
26 #
29 # tazpkg install command
31 tpi() {
32 tazpkg -gi --quiet --local --cookmode $1
33 }
36 # Working with time (with hundredths precision)
38 get_time() {
39 cut -d" " -f2 /proc/uptime
40 }
42 calc_time() {
43 # L10n: 's' is for seconds, 'm' is for minutes
44 awk -va="$1" -vb="$(get_time)" -vs="$(_ 's')" -vm="$(_ 'm')" '
45 BEGIN{
46 time = b - a;
47 if (time < 30)
48 printf("%.2f%s\n", time, s);
49 else
50 printf("%.2f%s ~ %.0f%s\n", time, s, time / 60, m);
51 }'
52 }
55 # Compressor mini summary
57 comp_summary() {
58 # "$time0" "$size0" "$size1" "$log_file"
59 status
60 [ "$2" -eq 0 ] && return
61 saving=$(awk -va="$2" -vb="$3" 'BEGIN{ printf("%.0f\n", (a - b) / 1024) }')
62 cache_msg=''
63 if [ -s "$cache_stat" ]; then
64 cache_msg=$(_n ' Cache hit: %d/%d.' "$(fgrep '+' $cache_stat | wc -l)" "$(wc -l < $cache_stat)")
65 echo -n > $cache_stat
66 fi
67 _ ' Time: %s. Size: %s B -> %s B. Save: %s KB.%s' \
68 "$(calc_time $1)" "$2" "$3" "$saving" "$cache_msg"
70 if [ -s "$4" ]; then
71 _ 'Compressor warnings and errors:'
72 awk '{printf " %s\n", $0;}' "$4"
73 echo
74 fi
75 # Clean log
76 [ ! -f "$4" ] || rm "$4"
77 }
80 # Find ELF files
82 find_elf() {
83 local i ifs="$IFS"
84 IFS=$'\n'
85 find $fs -type f \
86 | while read i; do
87 # output of `readelf -h <file> is human-readable information,
88 # we are interested in the next line:
89 # Type: EXEC (Executable file)
90 # or
91 # Type: DYN (Shared object file)
92 if [ "$(readelf -h "$i" 2>/dev/null \
93 | sed -n '/Type:/ s|.*: *\([^ ]*\) .*|\1|p')" == "$1" ]; then
94 echo "$i" # $1 = { EXEC | DYN }
95 fi
96 done
97 IFS="$ifs"
98 }
101 # Calculating different sizes
103 sizes() {
104 local ifs="$IFS"; IFS=$'\n'
105 case $1 in
106 man) find $install/usr/share/man -type f -exec ls -l \{\} \; ;;
107 png) find $install -type f -name '*.png' -exec ls -l \{\} \; ;;
108 svg) find $install -type f -name '*.svg' -exec ls -l \{\} \; ;;
109 gif) find $install -type f -name '*.gif' -exec ls -l \{\} \; ;;
110 xml) find $install -type f \( -name '*.ui' -o -name '*.glade' \) -exec ls -l \{\} \; ;;
111 des) find $install -type f -name '*.desktop' -exec ls -l \{\} \; ;;
112 mo1) find $install -type f -name '*.mo' -exec ls -l \{\} \; ;;
113 loc) find $install/usr/share/i18n/locales -type f -exec ls -l \{\} \; ;;
114 mo2) find $fs/usr/share/locale -type f -name '*.mo' -exec ls -l \{\} \; ;;
115 gz) find $install -type f -name '*.gz' ! -path '*/share/man/*' -exec ls -l \{\} \; ;;
116 zip) find $install -type f -name '*.zip' -exec ls -l \{\} \; ;;
117 css) find $install -type f -name '*.css' -exec ls -l \{\} \; ;;
118 strip)
119 {
120 find_elf EXEC
121 find_elf DYN
122 find $fs -type f \( -name '*.a' -o -name '*.pyc' -o -name '*.pyo' \
123 -o -name '.packlist' -o -name '*.pm' -o -name '*.pl' -o -name '*.pod' \)
124 } \
125 | tr '\n' '\0' \
126 | xargs -0 ls -l
127 ;;
128 esac | awk '{s+=$5}END{print s}'
129 IFS="$ifs"
130 }
133 # Query cache for already existing compressed file; substitute original file with the cached
134 # compressed one if so.
135 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
137 query_cache() {
138 md5=$(md5sum "$2")
139 cachefile="$comp_cache_root/$1/${md5%% *}"
140 echo "$cachefile"
141 owner=$(stat -c%u:%g "$2")
142 perms=$(stat -c%a "$2")
143 if [ -f "$cachefile" ]; then
144 ln -f "$cachefile" "$2"
145 chown $owner "$2"
146 chmod $perms "$2"
147 echo '+' >> "$cache_stat"
148 else
149 echo '-' >> "$cache_stat"
150 false
151 fi
152 }
155 # Store compressed file to the cache
156 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
158 store_cache() {
159 mkdir -p "${1%/*}"
160 mv "$2" "$1"
161 ln "$1" "$2"
162 }
165 # Function to compress all man pages
166 # Compressing can be disabled with COOKOPTS="!manz"
168 compress_manpages() {
169 time0=$(get_time)
170 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
171 manpath="$install/usr/share/man"
172 [ -d "$manpath" ] || return
173 size0=$(sizes man); [ -z "$size0" ] && return
175 tpi advancecomp-static
177 action 'Compressing man pages...'
179 # We'll use only Gzip compression, so decompress other formats first
180 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
181 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
183 # Fast compress with gzip
184 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
186 # Fix symlinks
187 for i in $(find $manpath -type l); do
188 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
189 link=$(echo $i | sed 's|\.[gbx]z2*$||')
190 rm $i; ln -s $dest.gz $link.gz
191 done
193 # Recompress with advdef (it can't compress, only recompress)
194 the_log="$(mktemp)"
195 if which advdef >/dev/null; then
196 IFS=$'\n'
197 for i in $(find $manpath -type f); do
198 if ! cached_path=$(query_cache mangz "$i"); then
199 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
200 out="$(advdef -z4q "$i")"
201 if [ -n "$out" ]; then
202 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
203 mv -f "$i.orig$$" "$i" # restore the original
204 else
205 store_cache "$cached_path" "$i"
206 rm -f "$i.orig$$" # clean
207 fi
208 fi
209 done
210 else
211 echo 'Warning: advdef not found.' > "$the_log"
212 fi
214 comp_summary "$time0" "$size0" "$(sizes man)" "$the_log"
215 }
218 # Function to recompress all gzip archives
219 # Recompressing can be disabled with COOKOPTS="!gz"
221 recompress_gz() {
222 time0=$(get_time)
223 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
224 size0=$(sizes gz); [ -z "$size0" ] && return
226 tpi advancecomp-static
228 action 'Recompressing gzip files...'
230 # Recompress with advdef
231 the_log="$(mktemp)"
232 if which advdef >/dev/null; then
233 IFS=$'\n'
234 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
235 if ! cached_path=$(query_cache gz "$i"); then
236 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
237 out="$(advdef -z4q "$i")"
238 if [ -n "$out" ]; then
239 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
240 mv -f "$i.orig$$" "$i" # restore the original
241 else
242 store_cache "$cached_path" "$i"
243 rm -f "$i.orig$$" # clean
244 fi
245 fi
246 done
247 else
248 echo 'Warning: advdef not found.' > "$the_log"
249 fi
251 comp_summary "$time0" "$size0" "$(sizes gz)" "$the_log"
252 }
255 # Function to recompress all zip archives
256 # Recompressing can be disabled with COOKOPTS="!zip"
258 recompress_zip() {
259 time0=$(get_time)
260 [ "${COOKOPTS/!zip/}" != "$COOKOPTS" ] && return
261 size0=$(sizes zip); [ -z "$size0" ] && return
263 tpi advancecomp-static
265 action 'Recompressing zip files...'
267 # Recompress with advzip
268 the_log="$(mktemp)"
269 if which advzip >/dev/null; then
270 IFS=$'\n'
271 for i in $(find $install -type f -name '*.zip'); do
272 if ! cached_path=$(query_cache zip "$i"); then
273 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
274 out="$(advzip -z3qk "$i")" # '-4' is more than two orders slower; use '-3'
275 if [ -n "$out" ]; then
276 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
277 mv -f "$i.orig$$" "$i" # restore the original
278 else
279 store_cache "$cached_path" "$i"
280 rm -f "$i.orig$$" # clean
281 fi
282 fi
283 done
284 else
285 echo 'Warning: advzip not found.' > "$the_log"
286 fi
288 comp_summary "$time0" "$size0" "$(sizes zip)" "$the_log"
289 }
292 # Function used after compile_rules() to compress all png images
293 # Compressing can be disabled with COOKOPTS="!pngz"
295 compress_png() {
296 time0=$(get_time)
297 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
298 size0=$(sizes png); [ -z "$size0" ] && return
300 use_pq=true
301 use_op=true
302 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
303 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
304 $use_pq && tpi pngquant-static
305 $use_op && tpi optipng-static
307 action 'Compressing png images...'
309 the_log="$(mktemp)"
310 $use_pq && if ! which pngquant >/dev/null; then
311 echo 'Warning: pngquant not found.' > "$the_log"
312 use_pq=false
313 fi
314 $use_op && if ! which optipng >/dev/null; then
315 echo 'Warning: optipng not found.' >> "$the_log"
316 use_op=false
317 fi
319 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
320 [ -z "$oplevel" ] && oplevel='2'
322 cache_section="png$oplevel"
323 $use_pq && cache_section="${cache_section}p"
324 $use_op && cache_section="${cache_section}o"
326 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
328 pq_opt='--skip-if-larger' # Sublime Text is mad about `if` in $(), so put it separately
329 IFS=$'\n'
330 for i in $(find $install -type f -name '*.png'); do
331 unset IFS iserror
332 if ! cached_path=$(query_cache $cache_section "$i"); then
333 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
334 if $use_pq; then
335 out="$(pngquant -f $pq_opt --ext .png --speed 1 "$i" 2>&1)"
336 if [ -n "$out" ]; then
337 echo "$i (pngquant):"$'\n'"$out"$'\n' >> "$the_log"
338 iserror='yes'
339 [ -e "$i.tmp" ] && rm "$i.tmp" # zero-size file remains on pngquant fail
340 fi
341 fi
342 if $use_op && [ -z "$iserror" ]; then
343 out="$(optipng -quiet -strip all -o$oplevel "$i" 2>&1)"
344 if [ -n "$out" ]; then
345 echo "$i (optipng):"$'\n'"$out"$'\n' >> "$the_log"
346 iserror='yes'
347 fi
348 fi
349 if [ -n "$iserror" ]; then
350 mv -f "$i.orig$$" "$i" # restore the original
351 else
352 store_cache "$cached_path" "$i"
353 rm -f "$i.orig$$" # clean
354 fi
355 fi
356 done
358 comp_summary "$time0" "$size0" "$(sizes png)" "$the_log"
359 }
362 # Function used after compile_rules() to compress all svg images
363 # Compressing can be disabled with COOKOPTS="!svgz"
365 compress_svg() {
366 time0=$(get_time)
367 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
368 size0=$(sizes svg); [ -z "$size0" ] && return
370 tpi svgcleaner
372 action 'Compressing svg images...'
374 the_log="$(mktemp)"
375 if which svgcleaner >/dev/null; then
376 [ "${COOKOPTS/!svgextra/}" == "$COOKOPTS" ] &&
377 opts="--apply-transform-to-paths yes --coordinates-precision 1 --paths-coordinates-precision 1"
379 for i in $(IFS=$'\n' find $install -type f -name '*.svg'); do
380 out="$(unset IFS; svgcleaner "$i" "$i" --copy-on-error --quiet \
381 --multipass --remove-unresolved-classes no $opts 2>&1)"
382 [ -z "$out" ] || echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
383 done
384 else
385 echo 'Warning: svgcleaner not found.' > "$the_log"
386 fi
388 comp_summary "$time0" "$size0" "$(sizes svg)" "$the_log"
389 }
392 # Function used after compile_rules() to compress all gif images
393 # Compressing can be disabled with COOKOPTS="!gifz"
395 compress_gif() {
396 time0=$(get_time)
397 [ "${COOKOPTS/!gifz/}" != "$COOKOPTS" ] && return
398 size0=$(sizes gif); [ -z "$size0" ] && return
400 tpi gifsicle
402 action 'Compressing gif images...'
404 the_log="$(mktemp)"
405 if which gifsicle >/dev/null; then
406 IFS=$'\n'
407 for i in $(find $install -type f -name '*.gif'); do
408 if ! cached_path=$(query_cache gif "$i"); then
409 unset IFS
410 # use intermediate file, if all well ($?=0), then substitute the original
411 if gifsicle -O3 "$i" -o "$i.$$" >> "$the_log" 2>&1; then
412 if [ -s "$i.$$" ]; then
413 mv "$i.$$" "$i"
414 store_cache "$cached_path" "$i"
415 fi
416 else
417 rm "$i.$$"
418 fi
419 fi
420 done
421 else
422 echo 'Warning: gifsicle not found.' > "$the_log"
423 fi
425 comp_summary "$time0" "$size0" "$(sizes gif)" "$the_log"
426 }
429 # Function used after compile_rules() to shrink all *.ui and *.glade files:
430 # remove insignificant spaces and comments
431 # Compressing can be disabled with COOKOPTS="!uiz"
433 compress_ui() {
434 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
435 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
437 tpi xmlstarlet
439 action 'Compressing ui files...'
441 the_log="$(mktemp)"
442 if which xmlstarlet >/dev/null; then
443 size0=$(sizes xml)
444 time0=$(get_time)
445 temp_ui="$(mktemp)"
446 IFS=$'\n'
447 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
448 out="$(xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui")"
449 if [ -n "$out" ]; then
450 echo "$ui:"$'\n'"$out"$'\n' >> "$the_log"
451 else
452 cat "$temp_ui" > "$ui"
453 fi
454 done
455 else
456 echo 'Warning: xmlstarlet not found.' > "$the_log"
457 fi
459 comp_summary "$time0" "$size0" "$(sizes xml)" "$the_log"
460 rm "$temp_ui"
461 }
464 # Function used after compile_rules() to shrink all *.css files:
465 # remove insignificant spaces and comments
466 # Compressing can be disabled with COOKOPTS="!cssz"
468 compress_css() {
469 [ "${COOKOPTS/!cssz/}" != "$COOKOPTS" ] && return
470 size0=$(sizes css); [ -z "$size0" ] && return
472 tpi rcssmin
474 action 'Compressing CSS files...'
476 time0=$(get_time)
477 temp_css="$(mktemp)"
478 IFS=$'\n'
479 for css in $(find $install -type f -name '*.css'); do
480 python -mrcssmin < "$css" > "$temp_css"
481 cat "$temp_css" > "$css"
482 done
483 unset IFS
485 comp_summary "$time0" "$size0" "$(sizes css)" '/dev/null'
486 rm "$temp_css"
487 }
490 # Get list of supported locales...
492 get_supported_locales() {
493 lpc='/slitaz-i18n/stuff/locale-pack.conf'
494 if [ -e "$WOK$lpc" ]; then
495 # ... from package in the local wok
496 . "$WOK$lpc"
497 else
498 # ... from Hg
499 temp_conf=$(mktemp)
500 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
501 if [ -s $temp_conf ]; then
502 . $temp_conf
503 else
504 # Give up and use hardcoded list
505 LOCALE_PACK="ar ca cs da de el en es fi fr hr hu id is it ja nb nl nn pl pt \
506 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
507 fi
508 rm $temp_conf
509 fi
510 echo $LOCALE_PACK
511 }
514 # Fix common errors and warnings in the .desktop files
515 # Fixing can be disabled with COOKOPTS="!fixdesktops"
517 fix_desktop_files() {
518 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
519 deskpath="$install/usr/share/applications"
520 [ -d "$deskpath" ] || return
521 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
523 size0=$(sizes des)
524 time0=$(get_time)
526 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
527 tpi desktop-file-validate-static
528 fi
530 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
531 # Default value is "" (empty). That means for us that we'll use the full
532 # list of supported locales here.
533 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
535 IFS=$'\n'
536 for desktop in $(find $deskpath -type f -name '*.desktop'); do
537 cp "$desktop" "$desktop.orig"
539 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
540 sdft "$desktop" -i
542 # Fix common errors in .desktop file
543 fix-desktop-file "$desktop"
545 # Strip unsupported locales from .desktop file
546 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
547 sdft "$desktop" -i -k "$LOCALE"
549 # Extra-strip
550 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
551 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
553 if [ -n "$QA" ]; then
554 # Check the rest of errors, warnings and tips
555 _ 'QA: Checking %s...' "$(basename $desktop)"
556 busybox diff "$desktop.orig" "$desktop" | sed 's!^!|!'
557 if which desktop-file-validate >/dev/null; then
558 desktop-file-validate "$desktop" | busybox fold -s
559 else
560 echo 'Warning: desktop-file-validate not found.'
561 fi
562 echo
563 fi
565 rm "$desktop.orig"
566 done
568 comp_summary "$time0" "$size0" "$(sizes des)" '/dev/null'
569 }
572 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
573 # to the translation (msgid = msgstr)
574 # Normalization can be disabled with COOKOPTS="!monorm"
576 normalize_mo() {
577 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
578 [ -z "$(find $install -type f -name '*.mo')" ] && return
580 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
581 tpi gettext-dev
582 # Gconv modules (convert to UTF-8)
583 tpi glibc-locale
585 action 'Normalizing mo files...'
587 the_log="$(mktemp)"
588 to_continue=true
589 for i in msgunfmt msguniq msgconv msgfmt; do
590 if ! which $i >/dev/null; then
591 echo "Warning: $i not found. Normalizing aborted" > "$the_log"
592 to_continue=false
593 fi
594 done
596 size0=$(sizes mo1)
597 time0=$(get_time)
599 # Process all existing *.mo files
600 IFS=$'\n'
601 $to_continue &&
602 for mo in $(find "$install" -type f -name '*.mo'); do
603 tmpfile="$(mktemp)"
605 # put ANY errors of {msgunfmt,msguniq,msgconv} to $out. FIXME?
606 out="$({ msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'; } 2>&1)"
607 if [ -n "$out" ]; then
608 # using literal $'\n' here instead of using `echo -e "...\n..."` because
609 # $out may contain escapes ('\r', '\v') that we should print as-is
610 echo "$mo:"$'\n'"$out"$'\n' >> "$the_log"
611 continue # proceed to next file
612 fi
614 # add newline
615 echo >> "$tmpfile"
617 # get Plural-Forms
618 awk '
619 BEGIN { skip = ""; }
620 {
621 if (! skip) {
622 s = $0;
623 gsub(/^[^\"]*\"/, "", s);
624 gsub(/\"$/, "", s);
625 printf("%s", s);
626 }
627 if (! $0) skip = "yes";
628 }
629 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
631 if ! grep -q 'msgid_plural' "$tmpfile"; then
632 echo > "$tmpfile.pf"
633 fi
635 # main
636 awk -v pf="$(cat "$tmpfile.pf")" '
637 function clean() {
638 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
639 }
641 function getstring() {
642 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
643 s = $0;
644 gsub(/^[^\"]*\"/, "", s);
645 gsub(/\"$/, "", s);
646 return s;
647 }
649 BEGIN {
650 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
651 if (pf)
652 printf("\"%s\\n\"\n", pf);
653 printf("\n");
654 skip = 1;
655 clean();
656 }
658 {
659 # Skip the entire header
660 if (!skip) {
661 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
662 mode = $1;
663 if ($1 == "msgstr[0]") mode = "msgstr0";
664 if ($1 == "msgstr[1]") mode = "msgstr1";
665 if ($1 == "msgstr[2]") mode = "msgstr2";
666 if ($1 == "msgstr[3]") mode = "msgstr3";
667 if ($1 == "msgstr[4]") mode = "msgstr4";
668 if ($1 == "msgstr[5]") mode = "msgstr5";
670 if (mode == "msgctxt") msgctxt = msgctxt getstring();
671 if (mode == "msgid") msgid = msgid getstring();
672 if (mode == "msgstr") msgstr = msgstr getstring();
673 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
674 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
675 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
676 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
677 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
678 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
679 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
681 if (! $0) {
682 if (msgid != msgstr) {
683 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
684 printf("msgid \"%s\"\n", msgid);
685 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
686 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
687 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
688 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
689 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
690 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
691 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
692 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
693 printf("\n");
694 }
695 clean();
696 }
697 }
698 if ($0 == "") skip = "";
699 }
700 ' "$tmpfile" > "$tmpfile.awk"
702 out="$(msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 2>&1)"
703 if [ -n "$out" ]; then
704 echo "$mo (msgfmt):"$'\n'"$out"$'\n' >> "$the_log"
705 continue # proceed to next file
706 fi
708 if [ -s "$tmpfile.mo" ]; then
709 rm "$mo"; mv "$tmpfile.mo" "$mo"
710 else
711 _ 'Error processing %s' "$mo" >> "$the_log"
712 echo >> "$the_log"
713 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
714 fi
716 # Clean
717 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
718 done
720 comp_summary "$time0" "$size0" "$(sizes mo1)" "$the_log"
721 }
724 # Strip locale definitions: normalize whitespace and remove comments
725 # Stripping can be disabled with COOKOPTS="!locdef"
727 strip_locale_def() {
728 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
729 [ ! -d "$install/usr/share/i18n/locales" ] && return
730 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
732 action 'Stripping locale definitions...'
733 size0=$(sizes loc)
734 time0=$(get_time)
736 for i in $(find $install/usr/share/i18n/locales -type f); do
737 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
738 done
740 comp_summary "$time0" "$size0" "$(sizes loc)"
741 }
744 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
745 # as removing unneeded files like in Python packages. Cross compiled binaries
746 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
747 # Stripping can be disabled with COOKOPTS="!strip"
749 strip_package() {
750 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
752 local i ifs="$IFS"
753 IFS=$'\n'
755 case "$ARCH" in
756 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
757 *) export STRIP='strip' ;;
758 esac
759 action 'Executing strip on all files...'
760 size0=0
761 size1=0
762 time0=$(get_time)
763 oldsize=$(sizes strip)
766 # GNU strip (GNU Binutils)
767 # -p --preserve-dates Copy modified/access timestamps to the output
768 # -s --strip-all Remove all symbols and relocation information
769 # --strip-unneeded Remove all symbols not needed by relocations
770 # -D --enable-deterministic-archives Produce deterministic output when stripping archives
771 # -g -S -d --strip-debug Remove all debugging symbols & sections
773 # Strip executable files
774 while read i; do
775 $STRIP -ps "$i" 2>/dev/null
776 done <<EOT
777 $(find_elf EXEC)
778 EOT
780 # Strip shared libraries
781 while read i; do
782 case $i in
783 *.dbg) ;; # skip library.so.*.dbg debugging symbols
784 *) $STRIP -p --strip-unneeded "$i" 2>/dev/null;;
785 esac
786 done <<EOT
787 $(find_elf DYN)
788 EOT
790 # Strip static libraries
791 # See also: https://wiki.debian.org/ReproducibleBuilds/TimestampsInStaticLibraries
792 find $fs -name '*.a' -exec $STRIP -pdD '{}' 2>/dev/null \;
795 # Remove Python *.pyc and *.pyo
796 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
798 # Remove both with the empty subfolders:
799 # 1. Perl perllocal.pod and .packlist (unconditionally)
800 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
801 # 2. Perl *.pod (if not disabled)
802 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
803 perlfiles="$perlfiles"$'\n'"$(find $fs -type f -name '*.pod')"
804 echo "$perlfiles" | sort -u | xargs rm -f 2>/dev/null
805 echo "$perlfiles" | sort -u | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' \
806 | xargs rmdir -p --ignore-fail-on-non-empty 2>/dev/null
808 # Strip documentation inside Perl files (*.pm and *.pl) (if not disabled)
809 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
810 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
812 newsize=$(sizes strip)
814 comp_summary "$time0" "$oldsize" "$newsize"
815 IFS="$ifs"
816 }
819 # Strip unsupported locales (.mo files)
821 strip_mo_i18n() {
822 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
824 [ ! -d "$fs/usr/share/locale" ] && return
825 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
827 action 'Thin out translation files...'
828 size0=$(sizes mo2)
829 time0=$(get_time)
831 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
832 # Default value is "" (empty). That means for us that we'll use the full
833 # list of supported locales here.
834 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
836 # Existing locales
837 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
839 # Thin out the list of existing locales. At the end there will be only locales that need
840 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
841 for keep_locale in $LOCALE; do
842 elocales=${elocales//$keep_locale}
843 done
845 # Remove the unsupported locales
846 for rem_locale in $elocales; do
847 [ ! -d "$fs/usr/share/locale/$rem_locale" ] ||
848 rm -r "$fs/usr/share/locale/$rem_locale"
849 done
851 comp_summary "$time0" "$size0" "$(sizes mo2)"
852 }
857 case $1 in
858 install)
859 # Compressors working in the $install
860 case "$ARCH" in
861 arm*) ;;
862 *)
863 recompress_gz
864 recompress_zip
865 compress_manpages
866 compress_png
867 compress_svg
868 compress_gif
869 compress_ui
870 compress_css
871 ;;
872 esac
874 fix_desktop_files
875 normalize_mo
876 strip_locale_def
877 ;;
878 fs)
879 # Compressors working in the $fs
880 strip_package
881 strip_mo_i18n
882 ;;
883 esac
885 # Clean
886 rm "$cache_stat"
887 find $comp_cache_root -type f -links -2 -delete