cookutils view modules/compressor @ rev 876

modules/compressor: allow spaces in filenames; fix_desktop_files() only in /usr/share/applications.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Thu Feb 16 02:18:58 2017 +0200 (2017-02-16)
parents ec4508cc348e
children 3f0ca4492ec7
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
10 # Compressor cache stuff
12 comp_cache_root='/home/slitaz/cache/cook'
13 mkdir -p "$comp_cache_root"
14 cache_stat=$(mktemp)
16 # Cache notes.
17 # Do not do the same job twice. Getting the file from the cache is much faster
18 # than compressing the file one more time. In addition, this cache is trying not
19 # to take extra space, using the hardlinks. Although the files from the cache
20 # without reference to itself should be removed periodically.
25 #
26 # Functions
27 #
30 # Display time.
32 disp_time() {
33 div=$(( ($1 + 30) / 60))
34 case $div in
35 0) min='';;
36 # L10n: 'm' is for minutes (approximate time)
37 *) min=$(_n ' ~ %dm' "$div");;
38 esac
40 # L10n: 's' is for seconds (cooking time)
41 _ '%ds%s' "$1" "$min"
42 }
45 # Compressor mini summary
47 comp_summary() {
48 # "$time0" "$size0" "$size1"
49 time1=$(date +%s)
50 status
51 [ "$2" -eq 0 ] && return
52 time=$(($time1 - $1))
53 saving=$(( ($2 - $3) / 1024 ))
54 cache_msg=''
55 if [ -s "$cache_stat" ]; then
56 cache_msg=$(_n ' Cache hit: %d/%d.' "$(fgrep '+' $cache_stat | wc -l)" "$(wc -l < $cache_stat)")
57 echo -n > $cache_stat
58 fi
59 _ ' Time: %s. Size: %s B -> %s B. Save: %s KB.%s' \
60 "$(disp_time $time)" "$2" "$3" "$saving" "$cache_msg"
61 }
64 # Calculating different sizes
66 sizes() {
67 case $1 in
68 man) find $install/usr/share/man -type f -exec ls -l \{\} \; ;;
69 png) find $install -type f -name '*.png' -exec ls -l \{\} \; ;;
70 svg) find $install -type f -name '*.svg' -exec ls -l \{\} \; ;;
71 xml) find $install -type f \( -name '*.ui' -o -name '*.glade' \) -exec ls -l \{\} \; ;;
72 des) find $install -type f -name '*.desktop' -exec ls -l \{\} \; ;;
73 mo1) find $install -type f -name '*.mo' -exec ls -l \{\} \; ;;
74 loc) find $install/usr/share/i18n/locales -type f -exec ls -l \{\} \; ;;
75 mo2) find $fs/usr/share/locale -type f -name '*.mo' -exec ls -l \{\} \; ;;
76 gz) find $install -type f -name '*.gz' ! -path '*/share/man/*' -exec ls -l \{\} \; ;;
77 esac | awk '{s+=$5}END{print s}'
78 }
81 # Query cache for already existing compressed file; substitute original file with the cached
82 # compressed one if so.
83 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
85 query_cache() {
86 md5=$(md5sum "$2")
87 cachefile="$comp_cache_root/$1/${md5%% *}"
88 echo "$cachefile"
89 if [ -f "$cachefile" ]; then
90 ln -f "$cachefile" "$2"
91 echo '+' >> "$cache_stat"
92 else
93 echo '-' >> "$cache_stat"
94 false
95 fi
96 }
99 # Store compressed file to the cache
100 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
102 store_cache() {
103 mkdir -p "${1%/*}"
104 mv "$2" "$1"
105 ln "$1" "$2"
106 }
109 # Function to compress all man pages
110 # Compressing can be disabled with COOKOPTS="!manz"
112 compress_manpages() {
113 time0=$(date +%s)
114 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
115 manpath="$install/usr/share/man"
116 [ -d "$manpath" ] || return
117 size0=$(sizes man); [ -z "$size0" ] && return
119 tazpkg -gi advancecomp --quiet --cookmode
121 action 'Compressing man pages...'
123 # We'll use only Gzip compression, so decompress other formats first
124 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
125 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
127 # Fast compress with gzip
128 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
130 # Fix symlinks
131 for i in $(find $manpath -type l); do
132 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
133 link=$(echo $i | sed 's|\.[gbx]z2*$||')
134 rm $i; ln -s $dest.gz $link.gz
135 done
137 # Recompress with advdef (it can't compress, only recompress)
138 IFS=$'\n'
139 for i in $(find $manpath -type f); do
140 if ! cached_path=$(query_cache mangz "$i"); then
141 advdef -z4q "$i"
142 store_cache "$cached_path" "$i"
143 fi
144 done
146 comp_summary "$time0" "$size0" "$(sizes man)"
147 }
150 # Function to recompress all gzip archives
151 # Recompressing can be disabled with COOKOPTS="!gz"
153 recompress_gz() {
154 time0=$(date +%s)
155 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
156 size0=$(sizes gz); [ -z "$size0" ] && return
158 tazpkg -gi advancecomp --quiet --cookmode
160 action 'Recompressing gzip files...'
162 # Recompress with advdef
163 IFS=$'\n'
164 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
165 if ! cached_path=$(query_cache gz "$i"); then
166 advdef -z4q "$i"
167 store_cache "$cached_path" "$i"
168 fi
169 done
171 comp_summary "$time0" "$size0" "$(sizes gz)"
172 }
175 # Function used after compile_rules() to compress all png images
176 # Compressing can be disabled with COOKOPTS="!pngz"
178 compress_png() {
179 time0=$(date +%s)
180 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
181 size0=$(sizes png); [ -z "$size0" ] && return
183 use_pq=true
184 use_op=true
185 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
186 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
187 $use_pq && tazpkg -gi pngquant --quiet --cookmode
188 $use_op && tazpkg -gi optipng --quiet --cookmode
190 action 'Compressing png images...'
192 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
193 [ -z "$oplevel" ] && oplevel='2'
195 cache_section="png$oplevel"
196 $use_pq && cache_section="${cache_section}p"
197 $use_op && cache_section="${cache_section}o"
199 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
201 IFS=$'\n'
202 for i in $(find $install -type f -name '*.png'); do
203 if ! cached_path=$(query_cache $cache_section "$i"); then
204 $use_pq && pngquant -f --skip-if-larger --ext .png --speed 1 "$i"
205 $use_op && optipng -quiet -strip all -o$oplevel "$i"
206 store_cache "$cached_path" "$i"
207 fi
208 done
210 comp_summary "$time0" "$size0" "$(sizes png)"
211 }
214 # Function used after compile_rules() to compress all svg images
215 # Compressing can be disabled with COOKOPTS="!svgz"
217 compress_svg() {
218 time0=$(date +%s)
219 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
220 size0=$(sizes svg); [ -z "$size0" ] && return
222 tazpkg -gi svgcleaner --quiet --cookmode
224 action 'Compressing svg images...'
226 cleaner_log="$(mktemp)"
227 IFS=$'\n'
228 for i in $(find $install -type f -name '*.svg'); do
229 echo -n "$i: " >> "$cleaner_log"
230 svgcleaner "$i" "$i" --remove-unresolved-classes false --quiet true >> "$cleaner_log"
231 done
233 comp_summary "$time0" "$size0" "$(sizes svg)"
235 sed -i '/: $/d' "$cleaner_log"
236 if [ -s "$cleaner_log" ]; then
237 _ 'Cleaner warnings and errors:'
238 awk '{printf " %s\n", $0;}' "$cleaner_log"
239 echo
240 fi
241 rm "$cleaner_log"
242 }
245 # Function used after compile_rules() to shrink all *.ui and *.glade files:
246 # remove insignificant spaces and comments
247 # Compressing can be disabled with COOKOPTS="!uiz"
249 compress_ui() {
250 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
251 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
253 tazpkg -gi xmlstarlet --quiet --cookmode
255 action 'Compressing ui files...'
257 size0=$(sizes xml)
258 time0=$(date +%s)
259 temp_ui="$(mktemp)"
260 IFS=$'\n'
261 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
262 xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui"
263 cat "$temp_ui" > "$ui"
264 done
266 comp_summary "$time0" "$size0" "$(sizes xml)"
267 rm "$temp_ui"
268 }
271 # Get list of supported locales...
273 get_supported_locales() {
274 lpc='/slitaz-i18n/stuff/locale-pack.conf'
275 if [ -e "$WOK$lpc" ]; then
276 # ... from package in the local wok
277 . "$WOK$lpc"
278 else
279 # ... from Hg
280 temp_conf=$(mktemp)
281 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
282 if [ -s $temp_conf ]; then
283 . $temp_conf
284 else
285 # Give up and use hardcoded list
286 LOCALE_PACK="ar ca cs da de el en es fi fr hr hu id is it ja nb nl nn pl pt \
287 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
288 fi
289 rm $temp_conf
290 fi
291 echo $LOCALE_PACK
292 }
295 # Fix common errors and warnings in the .desktop files
296 # Fixing can be disabled with COOKOPTS="!fixdesktops"
298 fix_desktop_files() {
299 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
300 deskpath="$install/usr/share/applications"
301 [ -d "$deskpath" ] || return
302 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
304 size0=$(sizes des)
305 time0=$(date +%s)
307 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
308 tazpkg -gi desktop-file-utils-extra --quiet --cookmode
309 fi
311 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
312 # Default value is "" (empty). That means for us that we'll use the full
313 # list of supported locales here.
314 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
316 IFS=$'\n'
317 for desktop in $(find $deskpath -type f -name '*.desktop'); do
318 cp "$desktop" "$desktop.orig"
320 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
321 sdft "$desktop" -i
323 # Fix common errors in .desktop file
324 fix-desktop-file "$desktop"
326 # Strip unsupported locales from .desktop file
327 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
328 sdft "$desktop" -i -k "$LOCALE"
330 # Extra-strip
331 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
332 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
334 if [ -n "$QA" ]; then
335 # Check the rest of errors, warnings and tips
336 _ 'QA: Checking %s...' "$(basename $desktop)"
337 diff "$desktop.orig" "$desktop"
338 desktop-file-validate "$desktop" | busybox fold -s
339 echo
340 fi
342 rm "$desktop.orig"
343 done
345 comp_summary "$time0" "$size0" "$(sizes des)"
346 }
349 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really added
350 # to the translation (msgid = msgstr)
351 # Normalization can be disabled with COOKOPTS="!monorm"
353 normalize_mo() {
354 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
355 [ -z "$(find $install -type f -name '*.mo')" ] && return
357 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
358 tazpkg -gi gettext --quiet --cookmode
359 # Gconv modules (convert to UTF-8)
360 tazpkg -gi glibc-locale --quiet --cookmode
362 action 'Normalizing mo files...'
364 size0=$(sizes mo1)
365 time0=$(date +%s)
367 # Process all existing *.mo files
368 IFS=$'\n'
369 for mo in $(find "$install" -type f -name '*.mo'); do
370 tmpfile="$(mktemp)"
372 msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'
373 # add newline
374 echo >> "$tmpfile"
376 # get Plural-Forms
377 awk '
378 BEGIN { skip = ""; }
379 {
380 if (! skip) {
381 s = $0;
382 gsub(/^[^\"]*\"/, "", s);
383 gsub(/\"$/, "", s);
384 printf("%s", s);
385 }
386 if (! $0) skip = "yes";
387 }
388 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
390 if ! grep -q 'msgid_plural' "$tmpfile"; then
391 echo > "$tmpfile.pf"
392 fi
394 # main
395 awk -v pf="$(cat "$tmpfile.pf")" '
396 function clean() {
397 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
398 }
400 function getstring() {
401 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
402 s = $0;
403 gsub(/^[^\"]*\"/, "", s);
404 gsub(/\"$/, "", s);
405 return s;
406 }
408 BEGIN {
409 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
410 if (pf)
411 printf("\"%s\\n\"\n", pf);
412 printf("\n");
413 skip = 1;
414 clean();
415 }
417 {
418 # Skip the entire header
419 if (!skip) {
420 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
421 mode = $1;
422 if ($1 == "msgstr[0]") mode = "msgstr0";
423 if ($1 == "msgstr[1]") mode = "msgstr1";
424 if ($1 == "msgstr[2]") mode = "msgstr2";
425 if ($1 == "msgstr[3]") mode = "msgstr3";
426 if ($1 == "msgstr[4]") mode = "msgstr4";
427 if ($1 == "msgstr[5]") mode = "msgstr5";
429 if (mode == "msgctxt") msgctxt = msgctxt getstring();
430 if (mode == "msgid") msgid = msgid getstring();
431 if (mode == "msgstr") msgstr = msgstr getstring();
432 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
433 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
434 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
435 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
436 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
437 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
438 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
440 if (! $0) {
441 if (msgid != msgstr) {
442 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
443 printf("msgid \"%s\"\n", msgid);
444 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
445 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
446 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
447 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
448 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
449 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
450 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
451 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
452 printf("\n");
453 }
454 clean();
455 }
456 }
457 if ($0 == "") skip = "";
458 }
459 ' "$tmpfile" > "$tmpfile.awk"
461 msgfmt "$tmpfile.awk" -o "$tmpfile.mo"
463 if [ -s "$tmpfile.mo" ]; then
464 rm "$mo"; mv "$tmpfile.mo" "$mo"
465 else
466 _ 'Error processing %s' "$mo"
467 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
468 fi
470 # Clean
471 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
472 done
474 comp_summary "$time0" "$size0" "$(sizes mo1)"
475 }
478 # Strip locale definitions: normalize whitespace and remove comments
479 # Stripping can be disabled with COOKOPTS="!locdef"
481 strip_locale_def() {
482 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
483 [ ! -d "$install/usr/share/i18n/locales" ] && return
484 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
486 action 'Stripping locale definitions...'
487 size0=$(sizes loc)
488 time0=$(date +%s)
490 for i in $(find $install/usr/share/i18n/locales -type f); do
491 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
492 done
494 comp_summary "$time0" "$size0" "$(sizes loc)"
495 }
498 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
499 # as removing unneeded files like in Python packages. Cross compiled binaries
500 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
501 # Stripping can be disabled with COOKOPTS="!strip"
503 strip_package() {
504 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
506 case "$ARCH" in
507 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
508 *) export STRIP='strip' ;;
509 esac
510 action 'Executing strip on all files...'
511 size0=0
512 size1=0
513 time0=$(date +%s)
515 # Strip executable files
516 for dir in $fs/bin $fs/sbin $fs/usr/bin $fs/usr/sbin $fs/usr/games; do
517 if [ -d "$dir" ]; then
518 oldsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
519 find $dir -type f -exec $STRIP -s '{}' 2>/dev/null \;
520 newsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
521 size0=$((size0 + oldsize)); size1=$((size1 + newsize))
522 fi
523 done
525 # Strip shared and static libraries
526 # Remove Python *.pyc and *.pyo, Perl perllocal.pod and .packlist
527 oldsize=$(find $fs -type f \( \
528 -name '*.so*' -o -name '*.a' -o \
529 -name '*.pyc' -o -name '*.pyo' -o \
530 -name 'perllocal.pod' -o -name '.packlist' \) -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
532 find $fs -name '*.so*' -exec $STRIP -s '{}' 2>/dev/null \;
533 find $fs -name '*.a' -exec $STRIP --strip-debug '{}' 2>/dev/null \;
534 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
535 find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \) -delete 2>/dev/null
537 newsize=$(find $fs -type f \( \
538 -name '*.so*' -o -name '*.a' -o \) -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
540 comp_summary "$time0" "$((size0 + oldsize))" "$((size1 + newsize))"
541 }
544 # Strip unsupported locales (.mo files)
546 strip_mo_i18n() {
547 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
549 [ ! -d "$fs/usr/share/locale" ] && return
550 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
552 action 'Thin out translation files...'
553 size0=$(sizes mo2)
554 time0=$(date +%s)
556 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
557 # Default value is "" (empty). That means for us that we'll use the full
558 # list of supported locales here.
559 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
561 # Existing locales
562 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
564 # Thin out the list of existing locales. At the end there will be only locales that need
565 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
566 for keep_locale in $LOCALE; do
567 elocales=${elocales//$keep_locale}
568 done
570 # Remove the unsupported locales
571 for rem_locale in $elocales; do
572 [ -d "$fs/usr/share/locale/$rem_locale" ] &&
573 rm -r "$fs/usr/share/locale/$rem_locale"
574 done
576 comp_summary "$time0" "$size0" "$(sizes mo2)"
577 }
582 case $1 in
583 install)
584 # Compressors working in the $install
585 case "$ARCH" in
586 arm*) ;;
587 *)
588 recompress_gz
589 compress_manpages
590 compress_png
591 compress_svg
592 compress_ui
593 ;;
594 esac
596 fix_desktop_files
597 normalize_mo
598 strip_locale_def
599 ;;
600 fs)
601 # Compressors working in the $fs
602 strip_package
603 strip_mo_i18n
604 ;;
605 esac
607 # Clean
608 rm "$cache_stat"
609 find $comp_cache_root -type f -links -2 -delete