cookutils annotate modules/compressor @ rev 1021

Tiny edits
author Paul Issott <paul@slitaz.org>
date Fri Dec 29 13:05:43 2017 +0000 (2017-12-29)
parents dc7238a11470
children be4a05be1a6f
rev   line source
al@865 1 #!/bin/sh
al@865 2 #
al@865 3 # compressor - module of the SliTaz Cook
al@865 4 # Copyright (C) SliTaz GNU/Linux - GNU GPL v3
al@865 5 #
al@865 6
al@865 7 . /usr/lib/slitaz/libcook.sh
al@865 8
al@868 9 # Compressor cache stuff
al@868 10
al@873 11 comp_cache_root='/home/slitaz/cache/cook'
al@869 12 mkdir -p "$comp_cache_root"
al@868 13 cache_stat=$(mktemp)
al@868 14
al@868 15 # Cache notes.
al@868 16 # Do not do the same job twice. Getting the file from the cache is much faster
al@868 17 # than compressing the file one more time. In addition, this cache is trying not
paul@1006 18 # to take extra space using the hardlinks. Although the files from the cache
paul@870 19 # without reference to itself should be removed periodically.
al@868 20
al@868 21
al@868 22
al@868 23
al@865 24 #
al@865 25 # Functions
al@865 26 #
al@865 27
al@865 28
al@912 29 # tazpkg install command
al@912 30
al@912 31 tpi() {
al@912 32 tazpkg -gi --quiet --local --cookmode $1
al@912 33 }
al@912 34
al@912 35
al@881 36 # Working with time (with hundredths precision)
al@865 37
al@881 38 get_time() {
al@881 39 cut -d" " -f2 /proc/uptime
al@881 40 }
al@865 41
al@881 42 calc_time() {
al@881 43 # L10n: 's' is for seconds, 'm' is for minutes
al@881 44 awk -va="$1" -vb="$(get_time)" -vs="$(_ 's')" -vm="$(_ 'm')" '
al@881 45 BEGIN{
al@881 46 time = b - a;
al@881 47 if (time < 30)
al@881 48 printf("%.2f%s\n", time, s);
al@881 49 else
al@881 50 printf("%.2f%s ~ %.0f%s\n", time, s, time / 60, m);
al@881 51 }'
al@865 52 }
al@865 53
al@865 54
al@865 55 # Compressor mini summary
al@865 56
al@865 57 comp_summary() {
al@998 58 # "$time0" "$size0" "$size1" "$log_file"
al@865 59 status
al@865 60 [ "$2" -eq 0 ] && return
al@881 61 saving=$(awk -va="$2" -vb="$3" 'BEGIN{ printf("%.0f\n", (a - b) / 1024) }')
al@868 62 cache_msg=''
al@868 63 if [ -s "$cache_stat" ]; then
al@868 64 cache_msg=$(_n ' Cache hit: %d/%d.' "$(fgrep '+' $cache_stat | wc -l)" "$(wc -l < $cache_stat)")
al@868 65 echo -n > $cache_stat
al@868 66 fi
al@868 67 _ ' Time: %s. Size: %s B -> %s B. Save: %s KB.%s' \
al@881 68 "$(calc_time $1)" "$2" "$3" "$saving" "$cache_msg"
al@998 69
al@998 70 if [ -s "$4" ]; then
al@998 71 _ 'Cleaner warnings and errors:'
al@998 72 awk '{printf " %s\n", $0;}' "$4"
al@998 73 echo
al@998 74 fi
al@998 75 # Clean log
al@998 76 [ ! -f "$4" ] || rm "$4"
al@865 77 }
al@865 78
al@865 79
al@1020 80 # Find ELF files
al@1020 81
al@1020 82 find_elf() {
al@1020 83 local i ifs="$IFS"
al@1020 84 IFS=$'\n'
al@1020 85 find $fs -type f \
al@1020 86 | while read i; do
al@1020 87 # output of `readelf -h <file> is human-readable information,
paul@1021 88 # we are interested in the next line:
al@1020 89 # Type: EXEC (Executable file)
al@1020 90 # or
al@1020 91 # Type: DYN (Shared object file)
al@1020 92 if [ "$(readelf -h "$i" 2>/dev/null \
al@1020 93 | sed -n '/Type:/ s|.*: *\([^ ]*\) .*|\1|p')" == "$1" ]; then
al@1020 94 echo "$i" # $1 = { EXEC | DYN }
al@1020 95 fi
al@1020 96 done
al@1020 97 IFS="$ifs"
al@1020 98 }
al@1020 99
al@1020 100
al@865 101 # Calculating different sizes
al@865 102
al@865 103 sizes() {
al@1020 104 local ifs="$IFS"; IFS=$'\n'
al@865 105 case $1 in
al@865 106 man) find $install/usr/share/man -type f -exec ls -l \{\} \; ;;
al@865 107 png) find $install -type f -name '*.png' -exec ls -l \{\} \; ;;
al@865 108 svg) find $install -type f -name '*.svg' -exec ls -l \{\} \; ;;
al@865 109 xml) find $install -type f \( -name '*.ui' -o -name '*.glade' \) -exec ls -l \{\} \; ;;
al@865 110 des) find $install -type f -name '*.desktop' -exec ls -l \{\} \; ;;
al@865 111 mo1) find $install -type f -name '*.mo' -exec ls -l \{\} \; ;;
al@865 112 loc) find $install/usr/share/i18n/locales -type f -exec ls -l \{\} \; ;;
al@865 113 mo2) find $fs/usr/share/locale -type f -name '*.mo' -exec ls -l \{\} \; ;;
al@872 114 gz) find $install -type f -name '*.gz' ! -path '*/share/man/*' -exec ls -l \{\} \; ;;
al@954 115 zip) find $install -type f -name '*.zip' -exec ls -l \{\} \; ;;
al@1020 116 strip)
al@1020 117 {
al@1020 118 find_elf EXEC
al@1020 119 find_elf DYN
al@1020 120 find $fs -type f \( -name '*.a' -o -name '*.pyc' -o -name '*.pyo' \
al@1020 121 -o -name '.packlist' -o -name '*.pm' -o -name '*.pl' -o -name '*.pod' \)
al@1020 122 } \
al@1020 123 | xargs ls -l
al@1020 124 ;;
al@865 125 esac | awk '{s+=$5}END{print s}'
al@1020 126 IFS="$ifs"
al@865 127 }
al@865 128
al@865 129
al@868 130 # Query cache for already existing compressed file; substitute original file with the cached
al@868 131 # compressed one if so.
al@868 132 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
al@868 133
al@868 134 query_cache() {
al@868 135 md5=$(md5sum "$2")
al@868 136 cachefile="$comp_cache_root/$1/${md5%% *}"
al@868 137 echo "$cachefile"
al@868 138 if [ -f "$cachefile" ]; then
al@868 139 ln -f "$cachefile" "$2"
al@868 140 echo '+' >> "$cache_stat"
al@868 141 else
al@868 142 echo '-' >> "$cache_stat"
al@868 143 false
al@868 144 fi
al@868 145 }
al@868 146
al@868 147
al@868 148 # Store compressed file to the cache
al@868 149 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
al@868 150
al@868 151 store_cache() {
al@868 152 mkdir -p "${1%/*}"
al@868 153 mv "$2" "$1"
al@868 154 ln "$1" "$2"
al@868 155 }
al@868 156
al@868 157
al@865 158 # Function to compress all man pages
al@865 159 # Compressing can be disabled with COOKOPTS="!manz"
al@865 160
al@865 161 compress_manpages() {
al@881 162 time0=$(get_time)
al@865 163 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
al@874 164 manpath="$install/usr/share/man"
al@865 165 [ -d "$manpath" ] || return
al@865 166 size0=$(sizes man); [ -z "$size0" ] && return
al@865 167
al@936 168 tpi advancecomp-static
al@867 169
al@865 170 action 'Compressing man pages...'
al@865 171
al@865 172 # We'll use only Gzip compression, so decompress other formats first
al@865 173 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
al@865 174 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
al@865 175
al@865 176 # Fast compress with gzip
al@874 177 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
al@865 178
al@865 179 # Fix symlinks
al@874 180 for i in $(find $manpath -type l); do
al@865 181 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
al@865 182 link=$(echo $i | sed 's|\.[gbx]z2*$||')
al@865 183 rm $i; ln -s $dest.gz $link.gz
al@865 184 done
al@865 185
al@865 186 # Recompress with advdef (it can't compress, only recompress)
al@998 187 cleaner_log="$(mktemp)"
al@876 188 IFS=$'\n'
al@874 189 for i in $(find $manpath -type f); do
al@868 190 if ! cached_path=$(query_cache mangz "$i"); then
al@998 191 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
al@998 192 out="$(advdef -z4q "$i")"
al@998 193 if [ -n "$out" ]; then
al@998 194 echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log"
al@998 195 mv -f "$i.orig$$" "$i" # restore the original
al@998 196 else
al@998 197 store_cache "$cached_path" "$i"
al@998 198 rm -f "$i.orig$$" # clean
al@998 199 fi
al@868 200 fi
al@865 201 done
al@865 202
al@998 203 comp_summary "$time0" "$size0" "$(sizes man)" "$cleaner_log"
al@865 204 }
al@865 205
al@865 206
al@872 207 # Function to recompress all gzip archives
al@872 208 # Recompressing can be disabled with COOKOPTS="!gz"
al@872 209
al@872 210 recompress_gz() {
al@881 211 time0=$(get_time)
al@872 212 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
al@872 213 size0=$(sizes gz); [ -z "$size0" ] && return
al@872 214
al@936 215 tpi advancecomp-static
al@872 216
al@872 217 action 'Recompressing gzip files...'
al@872 218
al@872 219 # Recompress with advdef
al@998 220 cleaner_log="$(mktemp)"
al@876 221 IFS=$'\n'
al@872 222 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
al@872 223 if ! cached_path=$(query_cache gz "$i"); then
al@998 224 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
al@998 225 out="$(advdef -z4q "$i")"
al@998 226 if [ -n "$out" ]; then
al@998 227 echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log"
al@998 228 mv -f "$i.orig$$" "$i" # restore the original
al@998 229 else
al@998 230 store_cache "$cached_path" "$i"
al@998 231 rm -f "$i.orig$$" # clean
al@998 232 fi
al@872 233 fi
al@872 234 done
al@872 235
al@998 236 comp_summary "$time0" "$size0" "$(sizes gz)" "$cleaner_log"
al@872 237 }
al@872 238
al@872 239
al@954 240 # Function to recompress all zip archives
al@954 241 # Recompressing can be disabled with COOKOPTS="!zip"
al@954 242
al@954 243 recompress_zip() {
al@954 244 time0=$(get_time)
al@954 245 [ "${COOKOPTS/!zip/}" != "$COOKOPTS" ] && return
al@954 246 size0=$(sizes zip); [ -z "$size0" ] && return
al@954 247
al@954 248 tpi advancecomp-static
al@954 249
al@954 250 action 'Recompressing zip files...'
al@954 251
al@954 252 # Recompress with advzip
al@998 253 cleaner_log="$(mktemp)"
al@954 254 IFS=$'\n'
al@954 255 for i in $(find $install -type f -name '*.zip'); do
al@954 256 if ! cached_path=$(query_cache zip "$i"); then
al@998 257 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
al@998 258 out="$(advzip -z3qk "$i")" # '-4' is more than two orders slower; use '-3'
al@998 259 if [ -n "$out" ]; then
al@998 260 echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log"
al@998 261 mv -f "$i.orig$$" "$i" # restore the original
al@998 262 else
al@998 263 store_cache "$cached_path" "$i"
al@998 264 rm -f "$i.orig$$" # clean
al@998 265 fi
al@954 266 fi
al@954 267 done
al@954 268
al@998 269 comp_summary "$time0" "$size0" "$(sizes zip)" "$cleaner_log"
al@954 270 }
al@954 271
al@954 272
al@865 273 # Function used after compile_rules() to compress all png images
al@865 274 # Compressing can be disabled with COOKOPTS="!pngz"
al@865 275
al@865 276 compress_png() {
al@881 277 time0=$(get_time)
al@865 278 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
al@865 279 size0=$(sizes png); [ -z "$size0" ] && return
al@865 280
al@865 281 use_pq=true
al@865 282 use_op=true
al@865 283 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
al@865 284 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
al@932 285 $use_pq && tpi pngquant-static
al@932 286 $use_op && tpi optipng-static
al@865 287
al@867 288 action 'Compressing png images...'
al@865 289
al@865 290 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
al@868 291 [ -z "$oplevel" ] && oplevel='2'
al@868 292
al@868 293 cache_section="png$oplevel"
al@868 294 $use_pq && cache_section="${cache_section}p"
al@868 295 $use_op && cache_section="${cache_section}o"
al@868 296
al@865 297 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
al@865 298
al@998 299 cleaner_log="$(mktemp)"
al@998 300 pq_opt='--skip-if-larger' # Sublime Text is mad about `if` in $(), so put it separately
al@978 301 IFS=$'\n'
al@978 302 for i in $(find $install -type f -name '*.png'); do
al@998 303 unset IFS iserror
al@868 304 if ! cached_path=$(query_cache $cache_section "$i"); then
al@998 305 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
al@998 306 if $use_pq; then
al@998 307 out="$(pngquant -f $pq_opt --ext .png --speed 1 "$i" 2>&1)"
al@998 308 if [ -n "$out" ]; then
al@998 309 echo "$i (pngquant):"$'\n'"$out"$'\n' >> "$cleaner_log"
al@998 310 iserror='yes'
al@998 311 fi
al@998 312 fi
al@998 313 if $use_op && [ -z "$iserror" ]; then
al@998 314 out="$(optipng -quiet -strip all -o$oplevel "$i" 2>&1)"
al@998 315 if [ -n "$out" ]; then
al@998 316 echo "$i (optipng):"$'\n'"$out"$'\n' >> "$cleaner_log"
al@998 317 iserror='yes'
al@998 318 fi
al@998 319 fi
al@998 320 if [ -n "$iserror" ]; then
al@998 321 mv -f "$i.orig$$" "$i" # restore the original
al@998 322 else
al@998 323 store_cache "$cached_path" "$i"
al@998 324 rm -f "$i.orig$$" # clean
al@998 325 fi
al@868 326 fi
al@865 327 done
al@865 328
al@998 329 comp_summary "$time0" "$size0" "$(sizes png)" "$cleaner_log"
al@865 330 }
al@865 331
al@865 332
al@865 333 # Function used after compile_rules() to compress all svg images
al@865 334 # Compressing can be disabled with COOKOPTS="!svgz"
al@865 335
al@865 336 compress_svg() {
al@881 337 time0=$(get_time)
al@865 338 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
al@865 339 size0=$(sizes svg); [ -z "$size0" ] && return
al@865 340
al@912 341 tpi svgcleaner
al@867 342
al@865 343 action 'Compressing svg images...'
al@865 344
al@959 345 [ "${COOKOPTS/!svgextra/}" == "$COOKOPTS" ] &&
al@959 346 opts="--apply-transform-to-paths yes --coordinates-precision 1 --paths-coordinates-precision 1"
al@959 347
al@865 348 cleaner_log="$(mktemp)"
al@959 349 for i in $(IFS=$'\n' find $install -type f -name '*.svg'); do
al@959 350 out="$(unset IFS; svgcleaner "$i" "$i" --copy-on-error --quiet \
al@959 351 --multipass --remove-unresolved-classes no $opts 2>&1)"
al@998 352 [ -z "$out" ] || echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log"
al@865 353 done
al@865 354
al@998 355 comp_summary "$time0" "$size0" "$(sizes svg)" "$cleaner_log"
al@865 356 }
al@865 357
al@865 358
al@865 359 # Function used after compile_rules() to shrink all *.ui and *.glade files:
al@865 360 # remove insignificant spaces and comments
al@865 361 # Compressing can be disabled with COOKOPTS="!uiz"
al@865 362
al@865 363 compress_ui() {
al@865 364 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
al@865 365 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
al@865 366
al@912 367 tpi xmlstarlet
al@867 368
al@865 369 action 'Compressing ui files...'
al@867 370
al@865 371 size0=$(sizes xml)
al@881 372 time0=$(get_time)
al@865 373 temp_ui="$(mktemp)"
al@998 374 cleaner_log="$(mktemp)"
al@876 375 IFS=$'\n'
al@865 376 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
al@998 377 out="$(xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui")"
al@998 378 if [ -n "$out" ]; then
al@998 379 echo "$ui:"$'\n'"$out"$'\n' >> "$cleaner_log"
al@998 380 else
al@998 381 cat "$temp_ui" > "$ui"
al@998 382 fi
al@865 383 done
al@865 384
al@998 385 comp_summary "$time0" "$size0" "$(sizes xml)" "$cleaner_log"
al@865 386 rm "$temp_ui"
al@865 387 }
al@865 388
al@865 389
al@865 390 # Get list of supported locales...
al@865 391
al@865 392 get_supported_locales() {
al@865 393 lpc='/slitaz-i18n/stuff/locale-pack.conf'
al@865 394 if [ -e "$WOK$lpc" ]; then
al@865 395 # ... from package in the local wok
al@865 396 . "$WOK$lpc"
al@865 397 else
al@865 398 # ... from Hg
al@865 399 temp_conf=$(mktemp)
al@865 400 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
al@865 401 if [ -s $temp_conf ]; then
al@865 402 . $temp_conf
al@865 403 else
al@865 404 # Give up and use hardcoded list
al@865 405 LOCALE_PACK="ar ca cs da de el en es fi fr hr hu id is it ja nb nl nn pl pt \
al@865 406 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
al@865 407 fi
al@865 408 rm $temp_conf
al@865 409 fi
al@865 410 echo $LOCALE_PACK
al@865 411 }
al@865 412
al@865 413
al@865 414 # Fix common errors and warnings in the .desktop files
al@865 415 # Fixing can be disabled with COOKOPTS="!fixdesktops"
al@865 416
al@865 417 fix_desktop_files() {
al@865 418 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
al@876 419 deskpath="$install/usr/share/applications"
al@876 420 [ -d "$deskpath" ] || return
al@876 421 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
al@865 422
al@865 423 size0=$(sizes des)
al@881 424 time0=$(get_time)
al@865 425
al@865 426 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
al@936 427 tpi desktop-file-validate-static
al@865 428 fi
al@865 429
al@865 430 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
al@865 431 # Default value is "" (empty). That means for us that we'll use the full
al@865 432 # list of supported locales here.
al@865 433 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
al@865 434
al@876 435 IFS=$'\n'
al@876 436 for desktop in $(find $deskpath -type f -name '*.desktop'); do
al@865 437 cp "$desktop" "$desktop.orig"
al@865 438
al@865 439 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
al@865 440 sdft "$desktop" -i
al@865 441
al@865 442 # Fix common errors in .desktop file
al@865 443 fix-desktop-file "$desktop"
al@865 444
al@865 445 # Strip unsupported locales from .desktop file
al@865 446 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
al@865 447 sdft "$desktop" -i -k "$LOCALE"
al@865 448
al@865 449 # Extra-strip
al@865 450 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
al@865 451 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
al@865 452
al@865 453 if [ -n "$QA" ]; then
al@865 454 # Check the rest of errors, warnings and tips
al@865 455 _ 'QA: Checking %s...' "$(basename $desktop)"
al@1002 456 busybox diff "$desktop.orig" "$desktop" | sed 's!^!|!'
al@865 457 desktop-file-validate "$desktop" | busybox fold -s
al@865 458 echo
al@865 459 fi
al@865 460
al@865 461 rm "$desktop.orig"
al@865 462 done
al@865 463
al@998 464 comp_summary "$time0" "$size0" "$(sizes des)" '/dev/null'
al@865 465 }
al@865 466
al@865 467
paul@879 468 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
al@865 469 # to the translation (msgid = msgstr)
al@865 470 # Normalization can be disabled with COOKOPTS="!monorm"
al@865 471
al@865 472 normalize_mo() {
al@865 473 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
al@865 474 [ -z "$(find $install -type f -name '*.mo')" ] && return
al@865 475
al@867 476 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
al@912 477 tpi gettext
al@867 478 # Gconv modules (convert to UTF-8)
al@912 479 tpi glibc-locale
al@867 480
al@865 481 action 'Normalizing mo files...'
al@867 482
al@865 483 size0=$(sizes mo1)
al@881 484 time0=$(get_time)
al@865 485
al@865 486 # Process all existing *.mo files
al@998 487 cleaner_log="$(mktemp)"
al@876 488 IFS=$'\n'
al@865 489 for mo in $(find "$install" -type f -name '*.mo'); do
al@865 490 tmpfile="$(mktemp)"
al@865 491
al@998 492 # put ANY errors of {msgunfmt,msguniq,msgconv} to $out. FIXME?
al@998 493 out="$({ msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'; } 2>&1)"
al@998 494 if [ -n "$out" ]; then
al@998 495 # using literal $'\n' here instead of using `echo -e "...\n..."` because
al@998 496 # $out may contain escapes ('\r', '\v') that we should print as-is
al@998 497 echo "$mo:"$'\n'"$out"$'\n' >> "$cleaner_log"
al@998 498 continue # proceed to next file
al@998 499 fi
al@998 500
al@865 501 # add newline
al@865 502 echo >> "$tmpfile"
al@865 503
al@865 504 # get Plural-Forms
al@865 505 awk '
al@865 506 BEGIN { skip = ""; }
al@865 507 {
al@865 508 if (! skip) {
al@865 509 s = $0;
al@865 510 gsub(/^[^\"]*\"/, "", s);
al@865 511 gsub(/\"$/, "", s);
al@865 512 printf("%s", s);
al@865 513 }
al@865 514 if (! $0) skip = "yes";
al@865 515 }
al@865 516 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
al@865 517
al@865 518 if ! grep -q 'msgid_plural' "$tmpfile"; then
al@865 519 echo > "$tmpfile.pf"
al@865 520 fi
al@865 521
al@865 522 # main
al@865 523 awk -v pf="$(cat "$tmpfile.pf")" '
al@865 524 function clean() {
al@865 525 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
al@865 526 }
al@865 527
al@865 528 function getstring() {
al@865 529 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
al@865 530 s = $0;
al@865 531 gsub(/^[^\"]*\"/, "", s);
al@865 532 gsub(/\"$/, "", s);
al@865 533 return s;
al@865 534 }
al@865 535
al@865 536 BEGIN {
al@865 537 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
al@865 538 if (pf)
al@865 539 printf("\"%s\\n\"\n", pf);
al@865 540 printf("\n");
al@865 541 skip = 1;
al@865 542 clean();
al@865 543 }
al@865 544
al@865 545 {
al@865 546 # Skip the entire header
al@865 547 if (!skip) {
al@865 548 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
al@865 549 mode = $1;
al@865 550 if ($1 == "msgstr[0]") mode = "msgstr0";
al@865 551 if ($1 == "msgstr[1]") mode = "msgstr1";
al@865 552 if ($1 == "msgstr[2]") mode = "msgstr2";
al@865 553 if ($1 == "msgstr[3]") mode = "msgstr3";
al@865 554 if ($1 == "msgstr[4]") mode = "msgstr4";
al@865 555 if ($1 == "msgstr[5]") mode = "msgstr5";
al@865 556
al@865 557 if (mode == "msgctxt") msgctxt = msgctxt getstring();
al@865 558 if (mode == "msgid") msgid = msgid getstring();
al@865 559 if (mode == "msgstr") msgstr = msgstr getstring();
al@865 560 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
al@865 561 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
al@865 562 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
al@865 563 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
al@865 564 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
al@865 565 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
al@865 566 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
al@865 567
al@865 568 if (! $0) {
al@865 569 if (msgid != msgstr) {
al@865 570 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
al@865 571 printf("msgid \"%s\"\n", msgid);
al@865 572 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
al@865 573 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
al@865 574 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
al@865 575 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
al@865 576 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
al@865 577 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
al@865 578 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
al@865 579 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
al@865 580 printf("\n");
al@865 581 }
al@865 582 clean();
al@865 583 }
al@865 584 }
al@865 585 if ($0 == "") skip = "";
al@865 586 }
al@865 587 ' "$tmpfile" > "$tmpfile.awk"
al@865 588
al@998 589 out="$(msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 2>&1)"
al@998 590 if [ -n "$out" ]; then
al@998 591 echo "$mo (msgfmt):"$'\n'"$out"$'\n' >> "$cleaner_log"
al@998 592 continue # proceed to next file
al@998 593 fi
al@865 594
al@865 595 if [ -s "$tmpfile.mo" ]; then
al@865 596 rm "$mo"; mv "$tmpfile.mo" "$mo"
al@865 597 else
al@998 598 _ 'Error processing %s' "$mo" >> "$cleaner_log"
al@998 599 echo >> "$cleaner_log"
al@865 600 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
al@865 601 fi
al@865 602
al@865 603 # Clean
al@865 604 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
al@865 605 done
al@865 606
al@998 607 comp_summary "$time0" "$size0" "$(sizes mo1)" "$cleaner_log"
al@865 608 }
al@865 609
al@865 610
al@865 611 # Strip locale definitions: normalize whitespace and remove comments
al@865 612 # Stripping can be disabled with COOKOPTS="!locdef"
al@865 613
al@865 614 strip_locale_def() {
al@865 615 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
al@865 616 [ ! -d "$install/usr/share/i18n/locales" ] && return
al@865 617 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
al@865 618
al@865 619 action 'Stripping locale definitions...'
al@865 620 size0=$(sizes loc)
al@881 621 time0=$(get_time)
al@865 622
al@865 623 for i in $(find $install/usr/share/i18n/locales -type f); do
al@865 624 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
al@865 625 done
al@865 626
al@865 627 comp_summary "$time0" "$size0" "$(sizes loc)"
al@865 628 }
al@865 629
al@865 630
al@865 631 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
al@865 632 # as removing unneeded files like in Python packages. Cross compiled binaries
al@865 633 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
al@865 634 # Stripping can be disabled with COOKOPTS="!strip"
al@865 635
al@865 636 strip_package() {
al@865 637 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
al@865 638
al@1020 639 local i ifs="$IFS"
al@1020 640 IFS=$'\n'
al@1020 641
al@865 642 case "$ARCH" in
al@865 643 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
al@865 644 *) export STRIP='strip' ;;
al@865 645 esac
al@865 646 action 'Executing strip on all files...'
al@865 647 size0=0
al@865 648 size1=0
al@881 649 time0=$(get_time)
al@1020 650 oldsize=$(sizes strip)
al@865 651
al@1020 652 # Strip executable files & shared libraries
al@1020 653 while read i; do
al@1020 654 $STRIP -s "$i" 2>/dev/null
al@1020 655 done <<EOT
al@1020 656 $(find_elf EXEC; find_elf DYN)
al@1020 657 EOT
al@865 658
al@1020 659 # Strip static libraries
al@1020 660 find $fs -name '*.a' -exec $STRIP -d '{}' 2>/dev/null \;
al@955 661
al@955 662 # Nullify timestamps of files in ar archives
al@957 663 # Skip empty 8-byte archives (hi, musl-libc package)
al@955 664 whereami=$(pwd)
al@957 665 find $fs -name '*.a' -type f -size +8c | \
al@955 666 while read i; do
al@955 667 tempdir=$(mktemp -d); cd $tempdir
al@955 668 ar -x $i; ar -crD $(basename $i) *
al@955 669 mv -f $tempdir/$(basename $i) $i
al@955 670 rm -rf $tempdir
al@955 671 done
al@955 672 cd $whereami; unset whereami
al@899 673
al@899 674 # Remove Python *.pyc and *.pyo
al@865 675 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
al@865 676
al@899 677 # Remove both with the empty subfolders:
al@899 678 # 1. Perl perllocal.pod and .packlist (unconditionally)
al@899 679 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
al@899 680 # 2. Perl *.pod (if not disabled)
al@899 681 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
al@899 682 perlfiles="$perlfiles $(find $fs -type f -name '*.pod')"
al@899 683 echo "$perlfiles" | xargs rm -f 2>/dev/null
al@899 684 echo "$perlfiles" | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' | xargs rmdir -p 2>/dev/null
al@899 685
al@1020 686 # Strip documentation inside Perl files (*.pm and *.pl) (if not disabled)
al@899 687 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
al@899 688 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
al@899 689
al@1020 690 newsize=$(sizes strip)
al@865 691
al@1020 692 comp_summary "$time0" "$oldsize" "$newsize"
al@1020 693 IFS="$ifs"
al@865 694 }
al@865 695
al@865 696
al@865 697 # Strip unsupported locales (.mo files)
al@865 698
al@865 699 strip_mo_i18n() {
al@865 700 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
al@865 701
al@865 702 [ ! -d "$fs/usr/share/locale" ] && return
al@865 703 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
al@865 704
al@865 705 action 'Thin out translation files...'
al@865 706 size0=$(sizes mo2)
al@881 707 time0=$(get_time)
al@865 708
al@865 709 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
al@865 710 # Default value is "" (empty). That means for us that we'll use the full
al@865 711 # list of supported locales here.
al@865 712 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
al@865 713
al@865 714 # Existing locales
al@865 715 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
al@865 716
al@865 717 # Thin out the list of existing locales. At the end there will be only locales that need
al@865 718 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
al@865 719 for keep_locale in $LOCALE; do
al@865 720 elocales=${elocales//$keep_locale}
al@865 721 done
al@865 722
al@865 723 # Remove the unsupported locales
al@865 724 for rem_locale in $elocales; do
al@998 725 [ ! -d "$fs/usr/share/locale/$rem_locale" ] ||
al@865 726 rm -r "$fs/usr/share/locale/$rem_locale"
al@865 727 done
al@865 728
al@865 729 comp_summary "$time0" "$size0" "$(sizes mo2)"
al@865 730 }
al@865 731
al@865 732
al@865 733
al@865 734
al@865 735 case $1 in
al@865 736 install)
al@865 737 # Compressors working in the $install
al@865 738 case "$ARCH" in
al@865 739 arm*) ;;
al@865 740 *)
al@872 741 recompress_gz
al@954 742 recompress_zip
al@865 743 compress_manpages
al@865 744 compress_png
al@865 745 compress_svg
al@865 746 compress_ui
al@865 747 ;;
al@865 748 esac
al@865 749
al@865 750 fix_desktop_files
al@865 751 normalize_mo
al@865 752 strip_locale_def
al@865 753 ;;
al@865 754 fs)
al@865 755 # Compressors working in the $fs
al@865 756 strip_package
al@865 757 strip_mo_i18n
al@865 758 ;;
al@865 759 esac
al@868 760
al@868 761 # Clean
al@868 762 rm "$cache_stat"
al@869 763 find $comp_cache_root -type f -links -2 -delete