cookutils annotate modules/compressor @ rev 1045

modules/compressor: *.png.tmp file may be missing on fail
author Aleksej Bobylev <al.bobylev@gmail.com>
date Wed Apr 04 11:44:07 2018 +0300 (2018-04-04)
parents c57d43c19d8a
children 0af059297232
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@1024 71 _ 'Compressor 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@1034 123 | tr '\n' '\0' \
al@1034 124 | xargs -0 ls -l
al@1020 125 ;;
al@865 126 esac | awk '{s+=$5}END{print s}'
al@1020 127 IFS="$ifs"
al@865 128 }
al@865 129
al@865 130
al@868 131 # Query cache for already existing compressed file; substitute original file with the cached
al@868 132 # compressed one if so.
al@868 133 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
al@868 134
al@868 135 query_cache() {
al@868 136 md5=$(md5sum "$2")
al@868 137 cachefile="$comp_cache_root/$1/${md5%% *}"
al@868 138 echo "$cachefile"
al@868 139 if [ -f "$cachefile" ]; then
al@868 140 ln -f "$cachefile" "$2"
al@868 141 echo '+' >> "$cache_stat"
al@868 142 else
al@868 143 echo '-' >> "$cache_stat"
al@868 144 false
al@868 145 fi
al@868 146 }
al@868 147
al@868 148
al@868 149 # Store compressed file to the cache
al@868 150 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
al@868 151
al@868 152 store_cache() {
al@868 153 mkdir -p "${1%/*}"
al@868 154 mv "$2" "$1"
al@868 155 ln "$1" "$2"
al@868 156 }
al@868 157
al@868 158
al@865 159 # Function to compress all man pages
al@865 160 # Compressing can be disabled with COOKOPTS="!manz"
al@865 161
al@865 162 compress_manpages() {
al@881 163 time0=$(get_time)
al@865 164 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
al@874 165 manpath="$install/usr/share/man"
al@865 166 [ -d "$manpath" ] || return
al@865 167 size0=$(sizes man); [ -z "$size0" ] && return
al@865 168
al@936 169 tpi advancecomp-static
al@867 170
al@865 171 action 'Compressing man pages...'
al@865 172
al@865 173 # We'll use only Gzip compression, so decompress other formats first
al@865 174 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
al@865 175 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
al@865 176
al@865 177 # Fast compress with gzip
al@874 178 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
al@865 179
al@865 180 # Fix symlinks
al@874 181 for i in $(find $manpath -type l); do
al@865 182 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
al@865 183 link=$(echo $i | sed 's|\.[gbx]z2*$||')
al@865 184 rm $i; ln -s $dest.gz $link.gz
al@865 185 done
al@865 186
al@865 187 # Recompress with advdef (it can't compress, only recompress)
al@1024 188 the_log="$(mktemp)"
al@1024 189 if which advdef >/dev/null; then
al@1024 190 IFS=$'\n'
al@1024 191 for i in $(find $manpath -type f); do
al@1024 192 if ! cached_path=$(query_cache mangz "$i"); then
al@1024 193 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
al@1024 194 out="$(advdef -z4q "$i")"
al@1024 195 if [ -n "$out" ]; then
al@1024 196 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
al@1024 197 mv -f "$i.orig$$" "$i" # restore the original
al@1024 198 else
al@1024 199 store_cache "$cached_path" "$i"
al@1024 200 rm -f "$i.orig$$" # clean
al@1024 201 fi
al@998 202 fi
al@1024 203 done
al@1024 204 else
al@1024 205 echo 'Warning: advdef not found.' > "$the_log"
al@1024 206 fi
al@865 207
al@1024 208 comp_summary "$time0" "$size0" "$(sizes man)" "$the_log"
al@865 209 }
al@865 210
al@865 211
al@872 212 # Function to recompress all gzip archives
al@872 213 # Recompressing can be disabled with COOKOPTS="!gz"
al@872 214
al@872 215 recompress_gz() {
al@881 216 time0=$(get_time)
al@872 217 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
al@872 218 size0=$(sizes gz); [ -z "$size0" ] && return
al@872 219
al@936 220 tpi advancecomp-static
al@872 221
al@872 222 action 'Recompressing gzip files...'
al@872 223
al@872 224 # Recompress with advdef
al@1024 225 the_log="$(mktemp)"
al@1024 226 if which advdef >/dev/null; then
al@1024 227 IFS=$'\n'
al@1024 228 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
al@1024 229 if ! cached_path=$(query_cache gz "$i"); then
al@1024 230 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
al@1024 231 out="$(advdef -z4q "$i")"
al@1024 232 if [ -n "$out" ]; then
al@1024 233 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
al@1024 234 mv -f "$i.orig$$" "$i" # restore the original
al@1024 235 else
al@1024 236 store_cache "$cached_path" "$i"
al@1024 237 rm -f "$i.orig$$" # clean
al@1024 238 fi
al@998 239 fi
al@1024 240 done
al@1024 241 else
al@1024 242 echo 'Warning: advdef not found.' > "$the_log"
al@1024 243 fi
al@872 244
al@1024 245 comp_summary "$time0" "$size0" "$(sizes gz)" "$the_log"
al@872 246 }
al@872 247
al@872 248
al@954 249 # Function to recompress all zip archives
al@954 250 # Recompressing can be disabled with COOKOPTS="!zip"
al@954 251
al@954 252 recompress_zip() {
al@954 253 time0=$(get_time)
al@954 254 [ "${COOKOPTS/!zip/}" != "$COOKOPTS" ] && return
al@954 255 size0=$(sizes zip); [ -z "$size0" ] && return
al@954 256
al@954 257 tpi advancecomp-static
al@954 258
al@954 259 action 'Recompressing zip files...'
al@954 260
al@954 261 # Recompress with advzip
al@1024 262 the_log="$(mktemp)"
al@1024 263 if which advzip >/dev/null; then
al@1024 264 IFS=$'\n'
al@1024 265 for i in $(find $install -type f -name '*.zip'); do
al@1024 266 if ! cached_path=$(query_cache zip "$i"); then
al@1024 267 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
al@1024 268 out="$(advzip -z3qk "$i")" # '-4' is more than two orders slower; use '-3'
al@1024 269 if [ -n "$out" ]; then
al@1024 270 echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
al@1024 271 mv -f "$i.orig$$" "$i" # restore the original
al@1024 272 else
al@1024 273 store_cache "$cached_path" "$i"
al@1024 274 rm -f "$i.orig$$" # clean
al@1024 275 fi
al@998 276 fi
al@1024 277 done
al@1024 278 else
al@1024 279 echo 'Warning: advzip not found.' > "$the_log"
al@1024 280 fi
al@954 281
al@1024 282 comp_summary "$time0" "$size0" "$(sizes zip)" "$the_log"
al@954 283 }
al@954 284
al@954 285
al@865 286 # Function used after compile_rules() to compress all png images
al@865 287 # Compressing can be disabled with COOKOPTS="!pngz"
al@865 288
al@865 289 compress_png() {
al@881 290 time0=$(get_time)
al@865 291 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
al@865 292 size0=$(sizes png); [ -z "$size0" ] && return
al@865 293
al@865 294 use_pq=true
al@865 295 use_op=true
al@865 296 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
al@865 297 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
al@932 298 $use_pq && tpi pngquant-static
al@932 299 $use_op && tpi optipng-static
al@865 300
al@867 301 action 'Compressing png images...'
al@865 302
al@1024 303 the_log="$(mktemp)"
al@1024 304 $use_pq && if ! which pngquant >/dev/null; then
al@1024 305 echo 'Warning: pngquant not found.' > "$the_log"
al@1024 306 use_pq=false
al@1024 307 fi
al@1024 308 $use_op && if ! which optipng >/dev/null; then
al@1024 309 echo 'Warning: optipng not found.' >> "$the_log"
al@1024 310 use_op=false
al@1024 311 fi
al@1024 312
al@865 313 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
al@868 314 [ -z "$oplevel" ] && oplevel='2'
al@868 315
al@868 316 cache_section="png$oplevel"
al@868 317 $use_pq && cache_section="${cache_section}p"
al@868 318 $use_op && cache_section="${cache_section}o"
al@868 319
al@865 320 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
al@865 321
al@998 322 pq_opt='--skip-if-larger' # Sublime Text is mad about `if` in $(), so put it separately
al@978 323 IFS=$'\n'
al@978 324 for i in $(find $install -type f -name '*.png'); do
al@998 325 unset IFS iserror
al@868 326 if ! cached_path=$(query_cache $cache_section "$i"); then
al@998 327 cp -a "$i" "$i.orig$$" # save the original if something goes wrong
al@998 328 if $use_pq; then
al@998 329 out="$(pngquant -f $pq_opt --ext .png --speed 1 "$i" 2>&1)"
al@998 330 if [ -n "$out" ]; then
al@1024 331 echo "$i (pngquant):"$'\n'"$out"$'\n' >> "$the_log"
al@998 332 iserror='yes'
al@1045 333 [ -e "$i.tmp" ] && rm "$i.tmp" # zero-size file remains on pngquant fail
al@998 334 fi
al@998 335 fi
al@998 336 if $use_op && [ -z "$iserror" ]; then
al@998 337 out="$(optipng -quiet -strip all -o$oplevel "$i" 2>&1)"
al@998 338 if [ -n "$out" ]; then
al@1024 339 echo "$i (optipng):"$'\n'"$out"$'\n' >> "$the_log"
al@998 340 iserror='yes'
al@998 341 fi
al@998 342 fi
al@998 343 if [ -n "$iserror" ]; then
al@998 344 mv -f "$i.orig$$" "$i" # restore the original
al@998 345 else
al@998 346 store_cache "$cached_path" "$i"
al@998 347 rm -f "$i.orig$$" # clean
al@998 348 fi
al@868 349 fi
al@865 350 done
al@865 351
al@1024 352 comp_summary "$time0" "$size0" "$(sizes png)" "$the_log"
al@865 353 }
al@865 354
al@865 355
al@865 356 # Function used after compile_rules() to compress all svg images
al@865 357 # Compressing can be disabled with COOKOPTS="!svgz"
al@865 358
al@865 359 compress_svg() {
al@881 360 time0=$(get_time)
al@865 361 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
al@865 362 size0=$(sizes svg); [ -z "$size0" ] && return
al@865 363
al@912 364 tpi svgcleaner
al@867 365
al@865 366 action 'Compressing svg images...'
al@865 367
al@1024 368 if which svgcleaner >/dev/null; then
al@1024 369 [ "${COOKOPTS/!svgextra/}" == "$COOKOPTS" ] &&
al@1024 370 opts="--apply-transform-to-paths yes --coordinates-precision 1 --paths-coordinates-precision 1"
al@959 371
al@1024 372 the_log="$(mktemp)"
al@1024 373 for i in $(IFS=$'\n' find $install -type f -name '*.svg'); do
al@1024 374 out="$(unset IFS; svgcleaner "$i" "$i" --copy-on-error --quiet \
al@1024 375 --multipass --remove-unresolved-classes no $opts 2>&1)"
al@1024 376 [ -z "$out" ] || echo "$i:"$'\n'"$out"$'\n' >> "$the_log"
al@1024 377 done
al@1024 378 else
al@1024 379 echo 'Warning: svgcleaner not found.' > "$the_log"
al@1024 380 fi
al@865 381
al@1024 382 comp_summary "$time0" "$size0" "$(sizes svg)" "$the_log"
al@865 383 }
al@865 384
al@865 385
al@865 386 # Function used after compile_rules() to shrink all *.ui and *.glade files:
al@865 387 # remove insignificant spaces and comments
al@865 388 # Compressing can be disabled with COOKOPTS="!uiz"
al@865 389
al@865 390 compress_ui() {
al@865 391 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
al@865 392 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
al@865 393
al@912 394 tpi xmlstarlet
al@867 395
al@865 396 action 'Compressing ui files...'
al@867 397
al@1024 398 if which xmlstarlet >/dev/null; then
al@1024 399 size0=$(sizes xml)
al@1024 400 time0=$(get_time)
al@1024 401 temp_ui="$(mktemp)"
al@1024 402 the_log="$(mktemp)"
al@1024 403 IFS=$'\n'
al@1024 404 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
al@1024 405 out="$(xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui")"
al@1024 406 if [ -n "$out" ]; then
al@1024 407 echo "$ui:"$'\n'"$out"$'\n' >> "$the_log"
al@1024 408 else
al@1024 409 cat "$temp_ui" > "$ui"
al@1024 410 fi
al@1024 411 done
al@1024 412 else
al@1024 413 echo 'Warning: xmlstarlet not found.' > "$the_log"
al@1024 414 fi
al@865 415
al@1024 416 comp_summary "$time0" "$size0" "$(sizes xml)" "$the_log"
al@865 417 rm "$temp_ui"
al@865 418 }
al@865 419
al@865 420
al@865 421 # Get list of supported locales...
al@865 422
al@865 423 get_supported_locales() {
al@865 424 lpc='/slitaz-i18n/stuff/locale-pack.conf'
al@865 425 if [ -e "$WOK$lpc" ]; then
al@865 426 # ... from package in the local wok
al@865 427 . "$WOK$lpc"
al@865 428 else
al@865 429 # ... from Hg
al@865 430 temp_conf=$(mktemp)
al@865 431 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
al@865 432 if [ -s $temp_conf ]; then
al@865 433 . $temp_conf
al@865 434 else
al@865 435 # Give up and use hardcoded list
al@865 436 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 437 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
al@865 438 fi
al@865 439 rm $temp_conf
al@865 440 fi
al@865 441 echo $LOCALE_PACK
al@865 442 }
al@865 443
al@865 444
al@865 445 # Fix common errors and warnings in the .desktop files
al@865 446 # Fixing can be disabled with COOKOPTS="!fixdesktops"
al@865 447
al@865 448 fix_desktop_files() {
al@865 449 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
al@876 450 deskpath="$install/usr/share/applications"
al@876 451 [ -d "$deskpath" ] || return
al@876 452 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
al@865 453
al@865 454 size0=$(sizes des)
al@881 455 time0=$(get_time)
al@865 456
al@865 457 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
al@936 458 tpi desktop-file-validate-static
al@865 459 fi
al@865 460
al@865 461 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
al@865 462 # Default value is "" (empty). That means for us that we'll use the full
al@865 463 # list of supported locales here.
al@865 464 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
al@865 465
al@876 466 IFS=$'\n'
al@876 467 for desktop in $(find $deskpath -type f -name '*.desktop'); do
al@865 468 cp "$desktop" "$desktop.orig"
al@865 469
al@865 470 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
al@865 471 sdft "$desktop" -i
al@865 472
al@865 473 # Fix common errors in .desktop file
al@865 474 fix-desktop-file "$desktop"
al@865 475
al@865 476 # Strip unsupported locales from .desktop file
al@865 477 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
al@865 478 sdft "$desktop" -i -k "$LOCALE"
al@865 479
al@865 480 # Extra-strip
al@865 481 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
al@865 482 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
al@865 483
al@865 484 if [ -n "$QA" ]; then
al@865 485 # Check the rest of errors, warnings and tips
al@865 486 _ 'QA: Checking %s...' "$(basename $desktop)"
al@1002 487 busybox diff "$desktop.orig" "$desktop" | sed 's!^!|!'
al@1036 488 if which desktop-file-validate >/dev/null; then
al@1024 489 desktop-file-validate "$desktop" | busybox fold -s
al@1024 490 else
al@1024 491 echo 'Warning: desktop-file-validate not found.'
al@1024 492 fi
al@865 493 echo
al@865 494 fi
al@865 495
al@865 496 rm "$desktop.orig"
al@865 497 done
al@865 498
al@998 499 comp_summary "$time0" "$size0" "$(sizes des)" '/dev/null'
al@865 500 }
al@865 501
al@865 502
paul@879 503 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
al@865 504 # to the translation (msgid = msgstr)
al@865 505 # Normalization can be disabled with COOKOPTS="!monorm"
al@865 506
al@865 507 normalize_mo() {
al@865 508 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
al@865 509 [ -z "$(find $install -type f -name '*.mo')" ] && return
al@865 510
al@867 511 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
al@912 512 tpi gettext
al@867 513 # Gconv modules (convert to UTF-8)
al@912 514 tpi glibc-locale
al@867 515
al@865 516 action 'Normalizing mo files...'
al@867 517
al@1024 518 the_log="$(mktemp)"
al@1024 519 to_continue=true
al@1024 520 for i in msgunfmt msguniq msgconv msgfmt; do
al@1024 521 if ! which $i >/dev/null; then
al@1024 522 echo "Warning: $i not found. Normalizing aborted" > "$the_log"
al@1024 523 to_continue=false
al@1024 524 fi
al@1024 525 done
al@1024 526
al@865 527 size0=$(sizes mo1)
al@881 528 time0=$(get_time)
al@865 529
al@865 530 # Process all existing *.mo files
al@876 531 IFS=$'\n'
al@1024 532 $to_continue &&
al@865 533 for mo in $(find "$install" -type f -name '*.mo'); do
al@865 534 tmpfile="$(mktemp)"
al@865 535
al@998 536 # put ANY errors of {msgunfmt,msguniq,msgconv} to $out. FIXME?
al@998 537 out="$({ msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'; } 2>&1)"
al@998 538 if [ -n "$out" ]; then
al@998 539 # using literal $'\n' here instead of using `echo -e "...\n..."` because
al@998 540 # $out may contain escapes ('\r', '\v') that we should print as-is
al@1024 541 echo "$mo:"$'\n'"$out"$'\n' >> "$the_log"
al@998 542 continue # proceed to next file
al@998 543 fi
al@998 544
al@865 545 # add newline
al@865 546 echo >> "$tmpfile"
al@865 547
al@865 548 # get Plural-Forms
al@865 549 awk '
al@865 550 BEGIN { skip = ""; }
al@865 551 {
al@865 552 if (! skip) {
al@865 553 s = $0;
al@865 554 gsub(/^[^\"]*\"/, "", s);
al@865 555 gsub(/\"$/, "", s);
al@865 556 printf("%s", s);
al@865 557 }
al@865 558 if (! $0) skip = "yes";
al@865 559 }
al@865 560 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
al@865 561
al@865 562 if ! grep -q 'msgid_plural' "$tmpfile"; then
al@865 563 echo > "$tmpfile.pf"
al@865 564 fi
al@865 565
al@865 566 # main
al@865 567 awk -v pf="$(cat "$tmpfile.pf")" '
al@865 568 function clean() {
al@865 569 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
al@865 570 }
al@865 571
al@865 572 function getstring() {
al@865 573 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
al@865 574 s = $0;
al@865 575 gsub(/^[^\"]*\"/, "", s);
al@865 576 gsub(/\"$/, "", s);
al@865 577 return s;
al@865 578 }
al@865 579
al@865 580 BEGIN {
al@865 581 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
al@865 582 if (pf)
al@865 583 printf("\"%s\\n\"\n", pf);
al@865 584 printf("\n");
al@865 585 skip = 1;
al@865 586 clean();
al@865 587 }
al@865 588
al@865 589 {
al@865 590 # Skip the entire header
al@865 591 if (!skip) {
al@865 592 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
al@865 593 mode = $1;
al@865 594 if ($1 == "msgstr[0]") mode = "msgstr0";
al@865 595 if ($1 == "msgstr[1]") mode = "msgstr1";
al@865 596 if ($1 == "msgstr[2]") mode = "msgstr2";
al@865 597 if ($1 == "msgstr[3]") mode = "msgstr3";
al@865 598 if ($1 == "msgstr[4]") mode = "msgstr4";
al@865 599 if ($1 == "msgstr[5]") mode = "msgstr5";
al@865 600
al@865 601 if (mode == "msgctxt") msgctxt = msgctxt getstring();
al@865 602 if (mode == "msgid") msgid = msgid getstring();
al@865 603 if (mode == "msgstr") msgstr = msgstr getstring();
al@865 604 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
al@865 605 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
al@865 606 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
al@865 607 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
al@865 608 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
al@865 609 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
al@865 610 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
al@865 611
al@865 612 if (! $0) {
al@865 613 if (msgid != msgstr) {
al@865 614 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
al@865 615 printf("msgid \"%s\"\n", msgid);
al@865 616 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
al@865 617 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
al@865 618 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
al@865 619 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
al@865 620 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
al@865 621 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
al@865 622 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
al@865 623 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
al@865 624 printf("\n");
al@865 625 }
al@865 626 clean();
al@865 627 }
al@865 628 }
al@865 629 if ($0 == "") skip = "";
al@865 630 }
al@865 631 ' "$tmpfile" > "$tmpfile.awk"
al@865 632
al@998 633 out="$(msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 2>&1)"
al@998 634 if [ -n "$out" ]; then
al@1024 635 echo "$mo (msgfmt):"$'\n'"$out"$'\n' >> "$the_log"
al@998 636 continue # proceed to next file
al@998 637 fi
al@865 638
al@865 639 if [ -s "$tmpfile.mo" ]; then
al@865 640 rm "$mo"; mv "$tmpfile.mo" "$mo"
al@865 641 else
al@1024 642 _ 'Error processing %s' "$mo" >> "$the_log"
al@1024 643 echo >> "$the_log"
al@865 644 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
al@865 645 fi
al@865 646
al@865 647 # Clean
al@865 648 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
al@865 649 done
al@865 650
al@1024 651 comp_summary "$time0" "$size0" "$(sizes mo1)" "$the_log"
al@865 652 }
al@865 653
al@865 654
al@865 655 # Strip locale definitions: normalize whitespace and remove comments
al@865 656 # Stripping can be disabled with COOKOPTS="!locdef"
al@865 657
al@865 658 strip_locale_def() {
al@865 659 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
al@865 660 [ ! -d "$install/usr/share/i18n/locales" ] && return
al@865 661 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
al@865 662
al@865 663 action 'Stripping locale definitions...'
al@865 664 size0=$(sizes loc)
al@881 665 time0=$(get_time)
al@865 666
al@865 667 for i in $(find $install/usr/share/i18n/locales -type f); do
al@865 668 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
al@865 669 done
al@865 670
al@865 671 comp_summary "$time0" "$size0" "$(sizes loc)"
al@865 672 }
al@865 673
al@865 674
al@865 675 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
al@865 676 # as removing unneeded files like in Python packages. Cross compiled binaries
al@865 677 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
al@865 678 # Stripping can be disabled with COOKOPTS="!strip"
al@865 679
al@865 680 strip_package() {
al@865 681 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
al@865 682
al@1020 683 local i ifs="$IFS"
al@1020 684 IFS=$'\n'
al@1020 685
al@865 686 case "$ARCH" in
al@865 687 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
al@865 688 *) export STRIP='strip' ;;
al@865 689 esac
al@865 690 action 'Executing strip on all files...'
al@865 691 size0=0
al@865 692 size1=0
al@881 693 time0=$(get_time)
al@1020 694 oldsize=$(sizes strip)
al@865 695
al@1027 696
al@1027 697 # GNU strip (GNU Binutils)
al@1027 698 # -p --preserve-dates Copy modified/access timestamps to the output
paul@1030 699 # -s --strip-all Remove all symbols and relocation information
al@1027 700 # --strip-unneeded Remove all symbols not needed by relocations
al@1027 701 # -D --enable-deterministic-archives Produce deterministic output when stripping archives
al@1027 702 # -g -S -d --strip-debug Remove all debugging symbols & sections
al@1027 703
al@1027 704 # Strip executable files
al@1020 705 while read i; do
al@1027 706 $STRIP -ps "$i" 2>/dev/null
al@1020 707 done <<EOT
al@1027 708 $(find_elf EXEC)
al@1027 709 EOT
al@1027 710
al@1027 711 # Strip shared libraries
al@1027 712 while read i; do
al@1027 713 case $i in
al@1027 714 *.dbg) ;; # skip library.so.*.dbg debugging symbols
al@1027 715 *) $STRIP -p --strip-unneeded "$i" 2>/dev/null;;
al@1027 716 esac
al@1027 717 done <<EOT
al@1027 718 $(find_elf DYN)
al@1020 719 EOT
al@865 720
al@1020 721 # Strip static libraries
al@1027 722 # See also: https://wiki.debian.org/ReproducibleBuilds/TimestampsInStaticLibraries
al@1027 723 find $fs -name '*.a' -exec $STRIP -pdD '{}' 2>/dev/null \;
al@955 724
al@899 725
al@899 726 # Remove Python *.pyc and *.pyo
al@865 727 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
al@865 728
al@899 729 # Remove both with the empty subfolders:
al@899 730 # 1. Perl perllocal.pod and .packlist (unconditionally)
al@899 731 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
al@899 732 # 2. Perl *.pod (if not disabled)
al@899 733 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
al@899 734 perlfiles="$perlfiles $(find $fs -type f -name '*.pod')"
al@899 735 echo "$perlfiles" | xargs rm -f 2>/dev/null
al@899 736 echo "$perlfiles" | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' | xargs rmdir -p 2>/dev/null
al@899 737
al@1020 738 # Strip documentation inside Perl files (*.pm and *.pl) (if not disabled)
al@899 739 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
al@899 740 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
al@899 741
al@1020 742 newsize=$(sizes strip)
al@865 743
al@1020 744 comp_summary "$time0" "$oldsize" "$newsize"
al@1020 745 IFS="$ifs"
al@865 746 }
al@865 747
al@865 748
al@865 749 # Strip unsupported locales (.mo files)
al@865 750
al@865 751 strip_mo_i18n() {
al@865 752 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
al@865 753
al@865 754 [ ! -d "$fs/usr/share/locale" ] && return
al@865 755 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
al@865 756
al@865 757 action 'Thin out translation files...'
al@865 758 size0=$(sizes mo2)
al@881 759 time0=$(get_time)
al@865 760
al@865 761 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
al@865 762 # Default value is "" (empty). That means for us that we'll use the full
al@865 763 # list of supported locales here.
al@865 764 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
al@865 765
al@865 766 # Existing locales
al@865 767 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
al@865 768
al@865 769 # Thin out the list of existing locales. At the end there will be only locales that need
al@865 770 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
al@865 771 for keep_locale in $LOCALE; do
al@865 772 elocales=${elocales//$keep_locale}
al@865 773 done
al@865 774
al@865 775 # Remove the unsupported locales
al@865 776 for rem_locale in $elocales; do
al@998 777 [ ! -d "$fs/usr/share/locale/$rem_locale" ] ||
al@865 778 rm -r "$fs/usr/share/locale/$rem_locale"
al@865 779 done
al@865 780
al@865 781 comp_summary "$time0" "$size0" "$(sizes mo2)"
al@865 782 }
al@865 783
al@865 784
al@865 785
al@865 786
al@865 787 case $1 in
al@865 788 install)
al@865 789 # Compressors working in the $install
al@865 790 case "$ARCH" in
al@865 791 arm*) ;;
al@865 792 *)
al@872 793 recompress_gz
al@954 794 recompress_zip
al@865 795 compress_manpages
al@865 796 compress_png
al@865 797 compress_svg
al@865 798 compress_ui
al@865 799 ;;
al@865 800 esac
al@865 801
al@865 802 fix_desktop_files
al@865 803 normalize_mo
al@865 804 strip_locale_def
al@865 805 ;;
al@865 806 fs)
al@865 807 # Compressors working in the $fs
al@865 808 strip_package
al@865 809 strip_mo_i18n
al@865 810 ;;
al@865 811 esac
al@868 812
al@868 813 # Clean
al@868 814 rm "$cache_stat"
al@869 815 find $comp_cache_root -type f -links -2 -delete