cookutils view modules/compressor @ rev 1016

lighttpd/index.cgi: fix coloring (php log)
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Dec 01 15:09:18 2017 +0200 (2017-12-01)
parents 8190be7ad294
children dc7238a11470
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 _ 'Cleaner warnings and errors:'
72 awk '{printf " %s\n", $0;}' "$4"
73 echo
74 fi
75 # Clean log
76 [ ! -f "$4" ] || rm "$4"
77 }
80 # Calculating different sizes
82 sizes() {
83 case $1 in
84 man) find $install/usr/share/man -type f -exec ls -l \{\} \; ;;
85 png) find $install -type f -name '*.png' -exec ls -l \{\} \; ;;
86 svg) find $install -type f -name '*.svg' -exec ls -l \{\} \; ;;
87 xml) find $install -type f \( -name '*.ui' -o -name '*.glade' \) -exec ls -l \{\} \; ;;
88 des) find $install -type f -name '*.desktop' -exec ls -l \{\} \; ;;
89 mo1) find $install -type f -name '*.mo' -exec ls -l \{\} \; ;;
90 loc) find $install/usr/share/i18n/locales -type f -exec ls -l \{\} \; ;;
91 mo2) find $fs/usr/share/locale -type f -name '*.mo' -exec ls -l \{\} \; ;;
92 gz) find $install -type f -name '*.gz' ! -path '*/share/man/*' -exec ls -l \{\} \; ;;
93 zip) find $install -type f -name '*.zip' -exec ls -l \{\} \; ;;
94 str) find $fs -type f \( -name '*.so*' -o -name '*.a' -o -name '*.pyc' -o -name '*.pyo' \
95 -o -name '.packlist' -o -name '*.pm' -o -name '*.pl' -o -name '*.pod' \) -exec ls -l \{\} \; ;;
96 esac | awk '{s+=$5}END{print s}'
97 }
100 # Query cache for already existing compressed file; substitute original file with the cached
101 # compressed one if so.
102 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
104 query_cache() {
105 md5=$(md5sum "$2")
106 cachefile="$comp_cache_root/$1/${md5%% *}"
107 echo "$cachefile"
108 if [ -f "$cachefile" ]; then
109 ln -f "$cachefile" "$2"
110 echo '+' >> "$cache_stat"
111 else
112 echo '-' >> "$cache_stat"
113 false
114 fi
115 }
118 # Store compressed file to the cache
119 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
121 store_cache() {
122 mkdir -p "${1%/*}"
123 mv "$2" "$1"
124 ln "$1" "$2"
125 }
128 # Function to compress all man pages
129 # Compressing can be disabled with COOKOPTS="!manz"
131 compress_manpages() {
132 time0=$(get_time)
133 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
134 manpath="$install/usr/share/man"
135 [ -d "$manpath" ] || return
136 size0=$(sizes man); [ -z "$size0" ] && return
138 tpi advancecomp-static
140 action 'Compressing man pages...'
142 # We'll use only Gzip compression, so decompress other formats first
143 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
144 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
146 # Fast compress with gzip
147 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
149 # Fix symlinks
150 for i in $(find $manpath -type l); do
151 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
152 link=$(echo $i | sed 's|\.[gbx]z2*$||')
153 rm $i; ln -s $dest.gz $link.gz
154 done
156 # Recompress with advdef (it can't compress, only recompress)
157 cleaner_log="$(mktemp)"
158 IFS=$'\n'
159 for i in $(find $manpath -type f); do
160 if ! cached_path=$(query_cache mangz "$i"); then
161 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
162 out="$(advdef -z4q "$i")"
163 if [ -n "$out" ]; then
164 echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log"
165 mv -f "$i.orig$$" "$i" # restore the original
166 else
167 store_cache "$cached_path" "$i"
168 rm -f "$i.orig$$" # clean
169 fi
170 fi
171 done
173 comp_summary "$time0" "$size0" "$(sizes man)" "$cleaner_log"
174 }
177 # Function to recompress all gzip archives
178 # Recompressing can be disabled with COOKOPTS="!gz"
180 recompress_gz() {
181 time0=$(get_time)
182 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
183 size0=$(sizes gz); [ -z "$size0" ] && return
185 tpi advancecomp-static
187 action 'Recompressing gzip files...'
189 # Recompress with advdef
190 cleaner_log="$(mktemp)"
191 IFS=$'\n'
192 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
193 if ! cached_path=$(query_cache gz "$i"); then
194 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
195 out="$(advdef -z4q "$i")"
196 if [ -n "$out" ]; then
197 echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log"
198 mv -f "$i.orig$$" "$i" # restore the original
199 else
200 store_cache "$cached_path" "$i"
201 rm -f "$i.orig$$" # clean
202 fi
203 fi
204 done
206 comp_summary "$time0" "$size0" "$(sizes gz)" "$cleaner_log"
207 }
210 # Function to recompress all zip archives
211 # Recompressing can be disabled with COOKOPTS="!zip"
213 recompress_zip() {
214 time0=$(get_time)
215 [ "${COOKOPTS/!zip/}" != "$COOKOPTS" ] && return
216 size0=$(sizes zip); [ -z "$size0" ] && return
218 tpi advancecomp-static
220 action 'Recompressing zip files...'
222 # Recompress with advzip
223 cleaner_log="$(mktemp)"
224 IFS=$'\n'
225 for i in $(find $install -type f -name '*.zip'); do
226 if ! cached_path=$(query_cache zip "$i"); then
227 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
228 out="$(advzip -z3qk "$i")" # '-4' is more than two orders slower; use '-3'
229 if [ -n "$out" ]; then
230 echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log"
231 mv -f "$i.orig$$" "$i" # restore the original
232 else
233 store_cache "$cached_path" "$i"
234 rm -f "$i.orig$$" # clean
235 fi
236 fi
237 done
239 comp_summary "$time0" "$size0" "$(sizes zip)" "$cleaner_log"
240 }
243 # Function used after compile_rules() to compress all png images
244 # Compressing can be disabled with COOKOPTS="!pngz"
246 compress_png() {
247 time0=$(get_time)
248 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
249 size0=$(sizes png); [ -z "$size0" ] && return
251 use_pq=true
252 use_op=true
253 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
254 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
255 $use_pq && tpi pngquant-static
256 $use_op && tpi optipng-static
258 action 'Compressing png images...'
260 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
261 [ -z "$oplevel" ] && oplevel='2'
263 cache_section="png$oplevel"
264 $use_pq && cache_section="${cache_section}p"
265 $use_op && cache_section="${cache_section}o"
267 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
269 cleaner_log="$(mktemp)"
270 pq_opt='--skip-if-larger' # Sublime Text is mad about `if` in $(), so put it separately
271 IFS=$'\n'
272 for i in $(find $install -type f -name '*.png'); do
273 unset IFS iserror
274 if ! cached_path=$(query_cache $cache_section "$i"); then
275 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
276 if $use_pq; then
277 out="$(pngquant -f $pq_opt --ext .png --speed 1 "$i" 2>&1)"
278 if [ -n "$out" ]; then
279 echo "$i (pngquant):"$'\n'"$out"$'\n' >> "$cleaner_log"
280 iserror='yes'
281 fi
282 fi
283 if $use_op && [ -z "$iserror" ]; then
284 out="$(optipng -quiet -strip all -o$oplevel "$i" 2>&1)"
285 if [ -n "$out" ]; then
286 echo "$i (optipng):"$'\n'"$out"$'\n' >> "$cleaner_log"
287 iserror='yes'
288 fi
289 fi
290 if [ -n "$iserror" ]; then
291 mv -f "$i.orig$$" "$i" # restore the original
292 else
293 store_cache "$cached_path" "$i"
294 rm -f "$i.orig$$" # clean
295 fi
296 fi
297 done
299 comp_summary "$time0" "$size0" "$(sizes png)" "$cleaner_log"
300 }
303 # Function used after compile_rules() to compress all svg images
304 # Compressing can be disabled with COOKOPTS="!svgz"
306 compress_svg() {
307 time0=$(get_time)
308 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
309 size0=$(sizes svg); [ -z "$size0" ] && return
311 tpi svgcleaner
313 action 'Compressing svg images...'
315 [ "${COOKOPTS/!svgextra/}" == "$COOKOPTS" ] &&
316 opts="--apply-transform-to-paths yes --coordinates-precision 1 --paths-coordinates-precision 1"
318 cleaner_log="$(mktemp)"
319 for i in $(IFS=$'\n' find $install -type f -name '*.svg'); do
320 out="$(unset IFS; svgcleaner "$i" "$i" --copy-on-error --quiet \
321 --multipass --remove-unresolved-classes no $opts 2>&1)"
322 [ -z "$out" ] || echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log"
323 done
325 comp_summary "$time0" "$size0" "$(sizes svg)" "$cleaner_log"
326 }
329 # Function used after compile_rules() to shrink all *.ui and *.glade files:
330 # remove insignificant spaces and comments
331 # Compressing can be disabled with COOKOPTS="!uiz"
333 compress_ui() {
334 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
335 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
337 tpi xmlstarlet
339 action 'Compressing ui files...'
341 size0=$(sizes xml)
342 time0=$(get_time)
343 temp_ui="$(mktemp)"
344 cleaner_log="$(mktemp)"
345 IFS=$'\n'
346 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
347 out="$(xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui")"
348 if [ -n "$out" ]; then
349 echo "$ui:"$'\n'"$out"$'\n' >> "$cleaner_log"
350 else
351 cat "$temp_ui" > "$ui"
352 fi
353 done
355 comp_summary "$time0" "$size0" "$(sizes xml)" "$cleaner_log"
356 rm "$temp_ui"
357 }
360 # Get list of supported locales...
362 get_supported_locales() {
363 lpc='/slitaz-i18n/stuff/locale-pack.conf'
364 if [ -e "$WOK$lpc" ]; then
365 # ... from package in the local wok
366 . "$WOK$lpc"
367 else
368 # ... from Hg
369 temp_conf=$(mktemp)
370 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
371 if [ -s $temp_conf ]; then
372 . $temp_conf
373 else
374 # Give up and use hardcoded list
375 LOCALE_PACK="ar ca cs da de el en es fi fr hr hu id is it ja nb nl nn pl pt \
376 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
377 fi
378 rm $temp_conf
379 fi
380 echo $LOCALE_PACK
381 }
384 # Fix common errors and warnings in the .desktop files
385 # Fixing can be disabled with COOKOPTS="!fixdesktops"
387 fix_desktop_files() {
388 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
389 deskpath="$install/usr/share/applications"
390 [ -d "$deskpath" ] || return
391 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
393 size0=$(sizes des)
394 time0=$(get_time)
396 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
397 tpi desktop-file-validate-static
398 fi
400 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
401 # Default value is "" (empty). That means for us that we'll use the full
402 # list of supported locales here.
403 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
405 IFS=$'\n'
406 for desktop in $(find $deskpath -type f -name '*.desktop'); do
407 cp "$desktop" "$desktop.orig"
409 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
410 sdft "$desktop" -i
412 # Fix common errors in .desktop file
413 fix-desktop-file "$desktop"
415 # Strip unsupported locales from .desktop file
416 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
417 sdft "$desktop" -i -k "$LOCALE"
419 # Extra-strip
420 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
421 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
423 if [ -n "$QA" ]; then
424 # Check the rest of errors, warnings and tips
425 _ 'QA: Checking %s...' "$(basename $desktop)"
426 busybox diff "$desktop.orig" "$desktop" | sed 's!^!|!'
427 desktop-file-validate "$desktop" | busybox fold -s
428 echo
429 fi
431 rm "$desktop.orig"
432 done
434 comp_summary "$time0" "$size0" "$(sizes des)" '/dev/null'
435 }
438 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
439 # to the translation (msgid = msgstr)
440 # Normalization can be disabled with COOKOPTS="!monorm"
442 normalize_mo() {
443 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
444 [ -z "$(find $install -type f -name '*.mo')" ] && return
446 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
447 tpi gettext
448 # Gconv modules (convert to UTF-8)
449 tpi glibc-locale
451 action 'Normalizing mo files...'
453 size0=$(sizes mo1)
454 time0=$(get_time)
456 # Process all existing *.mo files
457 cleaner_log="$(mktemp)"
458 IFS=$'\n'
459 for mo in $(find "$install" -type f -name '*.mo'); do
460 tmpfile="$(mktemp)"
462 # put ANY errors of {msgunfmt,msguniq,msgconv} to $out. FIXME?
463 out="$({ msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'; } 2>&1)"
464 if [ -n "$out" ]; then
465 # using literal $'\n' here instead of using `echo -e "...\n..."` because
466 # $out may contain escapes ('\r', '\v') that we should print as-is
467 echo "$mo:"$'\n'"$out"$'\n' >> "$cleaner_log"
468 continue # proceed to next file
469 fi
471 # add newline
472 echo >> "$tmpfile"
474 # get Plural-Forms
475 awk '
476 BEGIN { skip = ""; }
477 {
478 if (! skip) {
479 s = $0;
480 gsub(/^[^\"]*\"/, "", s);
481 gsub(/\"$/, "", s);
482 printf("%s", s);
483 }
484 if (! $0) skip = "yes";
485 }
486 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
488 if ! grep -q 'msgid_plural' "$tmpfile"; then
489 echo > "$tmpfile.pf"
490 fi
492 # main
493 awk -v pf="$(cat "$tmpfile.pf")" '
494 function clean() {
495 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
496 }
498 function getstring() {
499 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
500 s = $0;
501 gsub(/^[^\"]*\"/, "", s);
502 gsub(/\"$/, "", s);
503 return s;
504 }
506 BEGIN {
507 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
508 if (pf)
509 printf("\"%s\\n\"\n", pf);
510 printf("\n");
511 skip = 1;
512 clean();
513 }
515 {
516 # Skip the entire header
517 if (!skip) {
518 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
519 mode = $1;
520 if ($1 == "msgstr[0]") mode = "msgstr0";
521 if ($1 == "msgstr[1]") mode = "msgstr1";
522 if ($1 == "msgstr[2]") mode = "msgstr2";
523 if ($1 == "msgstr[3]") mode = "msgstr3";
524 if ($1 == "msgstr[4]") mode = "msgstr4";
525 if ($1 == "msgstr[5]") mode = "msgstr5";
527 if (mode == "msgctxt") msgctxt = msgctxt getstring();
528 if (mode == "msgid") msgid = msgid getstring();
529 if (mode == "msgstr") msgstr = msgstr getstring();
530 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
531 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
532 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
533 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
534 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
535 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
536 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
538 if (! $0) {
539 if (msgid != msgstr) {
540 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
541 printf("msgid \"%s\"\n", msgid);
542 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
543 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
544 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
545 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
546 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
547 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
548 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
549 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
550 printf("\n");
551 }
552 clean();
553 }
554 }
555 if ($0 == "") skip = "";
556 }
557 ' "$tmpfile" > "$tmpfile.awk"
559 out="$(msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 2>&1)"
560 if [ -n "$out" ]; then
561 echo "$mo (msgfmt):"$'\n'"$out"$'\n' >> "$cleaner_log"
562 continue # proceed to next file
563 fi
565 if [ -s "$tmpfile.mo" ]; then
566 rm "$mo"; mv "$tmpfile.mo" "$mo"
567 else
568 _ 'Error processing %s' "$mo" >> "$cleaner_log"
569 echo >> "$cleaner_log"
570 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
571 fi
573 # Clean
574 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
575 done
577 comp_summary "$time0" "$size0" "$(sizes mo1)" "$cleaner_log"
578 }
581 # Strip locale definitions: normalize whitespace and remove comments
582 # Stripping can be disabled with COOKOPTS="!locdef"
584 strip_locale_def() {
585 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
586 [ ! -d "$install/usr/share/i18n/locales" ] && return
587 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
589 action 'Stripping locale definitions...'
590 size0=$(sizes loc)
591 time0=$(get_time)
593 for i in $(find $install/usr/share/i18n/locales -type f); do
594 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
595 done
597 comp_summary "$time0" "$size0" "$(sizes loc)"
598 }
601 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
602 # as removing unneeded files like in Python packages. Cross compiled binaries
603 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
604 # Stripping can be disabled with COOKOPTS="!strip"
606 strip_package() {
607 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
609 case "$ARCH" in
610 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
611 *) export STRIP='strip' ;;
612 esac
613 action 'Executing strip on all files...'
614 size0=0
615 size1=0
616 time0=$(get_time)
618 # Strip executable files
619 for dir in $fs/bin $fs/sbin $fs/usr/bin $fs/usr/sbin $fs/usr/games; do
620 if [ -d "$dir" ]; then
621 oldsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
622 find $dir -type f -exec $STRIP -s '{}' 2>/dev/null \;
623 newsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
624 size0=$((size0 + oldsize)); size1=$((size1 + newsize))
625 fi
626 done
628 oldsize=$(sizes str)
630 # Strip shared and static libraries
631 find $fs -name '*.so*' -exec $STRIP -s '{}' 2>/dev/null \;
632 find $fs -name '*.a' -exec $STRIP -d '{}' 2>/dev/null \;
634 # Nullify timestamps of files in ar archives
635 # Skip empty 8-byte archives (hi, musl-libc package)
636 whereami=$(pwd)
637 find $fs -name '*.a' -type f -size +8c | \
638 while read i; do
639 tempdir=$(mktemp -d); cd $tempdir
640 ar -x $i; ar -crD $(basename $i) *
641 mv -f $tempdir/$(basename $i) $i
642 rm -rf $tempdir
643 done
644 cd $whereami; unset whereami
646 # Remove Python *.pyc and *.pyo
647 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
649 # Remove both with the empty subfolders:
650 # 1. Perl perllocal.pod and .packlist (unconditionally)
651 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
652 # 2. Perl *.pod (if not disabled)
653 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
654 perlfiles="$perlfiles $(find $fs -type f -name '*.pod')"
655 echo "$perlfiles" | xargs rm -f 2>/dev/null
656 echo "$perlfiles" | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' | xargs rmdir -p 2>/dev/null
658 # Strip Perl files (*.pm and *.pl) from documentation inside (if not disabled)
659 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
660 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
662 newsize=$(sizes str)
664 comp_summary "$time0" "$((size0 + oldsize))" "$((size1 + newsize))"
665 }
668 # Strip unsupported locales (.mo files)
670 strip_mo_i18n() {
671 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
673 [ ! -d "$fs/usr/share/locale" ] && return
674 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
676 action 'Thin out translation files...'
677 size0=$(sizes mo2)
678 time0=$(get_time)
680 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
681 # Default value is "" (empty). That means for us that we'll use the full
682 # list of supported locales here.
683 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
685 # Existing locales
686 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
688 # Thin out the list of existing locales. At the end there will be only locales that need
689 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
690 for keep_locale in $LOCALE; do
691 elocales=${elocales//$keep_locale}
692 done
694 # Remove the unsupported locales
695 for rem_locale in $elocales; do
696 [ ! -d "$fs/usr/share/locale/$rem_locale" ] ||
697 rm -r "$fs/usr/share/locale/$rem_locale"
698 done
700 comp_summary "$time0" "$size0" "$(sizes mo2)"
701 }
706 case $1 in
707 install)
708 # Compressors working in the $install
709 case "$ARCH" in
710 arm*) ;;
711 *)
712 recompress_gz
713 recompress_zip
714 compress_manpages
715 compress_png
716 compress_svg
717 compress_ui
718 ;;
719 esac
721 fix_desktop_files
722 normalize_mo
723 strip_locale_def
724 ;;
725 fs)
726 # Compressors working in the $fs
727 strip_package
728 strip_mo_i18n
729 ;;
730 esac
732 # Clean
733 rm "$cache_stat"
734 find $comp_cache_root -type f -links -2 -delete