cookutils annotate modules/compressor @ rev 899

Some important changes:
* cook: better die handling; cook can now mark packages as broken by itself; less chatty zip extracting; rename internal function copy() to scopy(); new copy() is the main tool to copy files from $install to $fs (docs to come); automatic patching (if patches are in $stuff/patches and patch list is $stuff/patches/series); better local packages handling; update packages.info database after each successful build, virtual packages are accessible immediately after build.
* doc/cookopts.txt: added "!perlz" and "!rmpod" options description.
* modules/compressor: strip Perl files.
* modules/pkgdb: calculate estimated time for cook:pkgdb too (as well as for cook:package).
author Aleksej Bobylev <al.bobylev@gmail.com>
date Thu May 11 00:36:43 2017 +0300 (2017-05-11)
parents 897914bd4c94
children 39feb4e7243d
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@865 9
al@868 10 # Compressor cache stuff
al@868 11
al@873 12 comp_cache_root='/home/slitaz/cache/cook'
al@869 13 mkdir -p "$comp_cache_root"
al@868 14 cache_stat=$(mktemp)
al@868 15
al@868 16 # Cache notes.
al@868 17 # Do not do the same job twice. Getting the file from the cache is much faster
al@868 18 # than compressing the file one more time. In addition, this cache is trying not
al@868 19 # to take extra space, using the hardlinks. Although the files from the cache
paul@870 20 # without reference to itself should be removed periodically.
al@868 21
al@868 22
al@868 23
al@868 24
al@865 25 #
al@865 26 # Functions
al@865 27 #
al@865 28
al@865 29
al@881 30 # Working with time (with hundredths precision)
al@865 31
al@881 32 get_time() {
al@881 33 cut -d" " -f2 /proc/uptime
al@881 34 }
al@865 35
al@881 36 calc_time() {
al@881 37 # L10n: 's' is for seconds, 'm' is for minutes
al@881 38 awk -va="$1" -vb="$(get_time)" -vs="$(_ 's')" -vm="$(_ 'm')" '
al@881 39 BEGIN{
al@881 40 time = b - a;
al@881 41 if (time < 30)
al@881 42 printf("%.2f%s\n", time, s);
al@881 43 else
al@881 44 printf("%.2f%s ~ %.0f%s\n", time, s, time / 60, m);
al@881 45 }'
al@865 46 }
al@865 47
al@865 48
al@865 49 # Compressor mini summary
al@865 50
al@865 51 comp_summary() {
al@865 52 # "$time0" "$size0" "$size1"
al@865 53 status
al@865 54 [ "$2" -eq 0 ] && return
al@881 55 saving=$(awk -va="$2" -vb="$3" 'BEGIN{ printf("%.0f\n", (a - b) / 1024) }')
al@868 56 cache_msg=''
al@868 57 if [ -s "$cache_stat" ]; then
al@868 58 cache_msg=$(_n ' Cache hit: %d/%d.' "$(fgrep '+' $cache_stat | wc -l)" "$(wc -l < $cache_stat)")
al@868 59 echo -n > $cache_stat
al@868 60 fi
al@868 61 _ ' Time: %s. Size: %s B -> %s B. Save: %s KB.%s' \
al@881 62 "$(calc_time $1)" "$2" "$3" "$saving" "$cache_msg"
al@865 63 }
al@865 64
al@865 65
al@865 66 # Calculating different sizes
al@865 67
al@865 68 sizes() {
al@865 69 case $1 in
al@865 70 man) find $install/usr/share/man -type f -exec ls -l \{\} \; ;;
al@865 71 png) find $install -type f -name '*.png' -exec ls -l \{\} \; ;;
al@865 72 svg) find $install -type f -name '*.svg' -exec ls -l \{\} \; ;;
al@865 73 xml) find $install -type f \( -name '*.ui' -o -name '*.glade' \) -exec ls -l \{\} \; ;;
al@865 74 des) find $install -type f -name '*.desktop' -exec ls -l \{\} \; ;;
al@865 75 mo1) find $install -type f -name '*.mo' -exec ls -l \{\} \; ;;
al@865 76 loc) find $install/usr/share/i18n/locales -type f -exec ls -l \{\} \; ;;
al@865 77 mo2) find $fs/usr/share/locale -type f -name '*.mo' -exec ls -l \{\} \; ;;
al@872 78 gz) find $install -type f -name '*.gz' ! -path '*/share/man/*' -exec ls -l \{\} \; ;;
al@899 79 str) find $fs -type f \( -name '*.so*' -o -name '*.a' -o -name '*.pyc' -o -name '*.pyo' \
al@899 80 -o -name '.packlist' -o -name '*.pm' -o -name '*.pl' -o -name '*.pod' \) -exec ls -l \{\} \; ;;
al@865 81 esac | awk '{s+=$5}END{print s}'
al@865 82 }
al@865 83
al@865 84
al@868 85 # Query cache for already existing compressed file; substitute original file with the cached
al@868 86 # compressed one if so.
al@868 87 # $1: cache section (gz, mangz, png, etc.); $2: path to original file
al@868 88
al@868 89 query_cache() {
al@868 90 md5=$(md5sum "$2")
al@868 91 cachefile="$comp_cache_root/$1/${md5%% *}"
al@868 92 echo "$cachefile"
al@868 93 if [ -f "$cachefile" ]; then
al@868 94 ln -f "$cachefile" "$2"
al@868 95 echo '+' >> "$cache_stat"
al@868 96 else
al@868 97 echo '-' >> "$cache_stat"
al@868 98 false
al@868 99 fi
al@868 100 }
al@868 101
al@868 102
al@868 103 # Store compressed file to the cache
al@868 104 # $1: path to cache entry to be stored; $2: path to compressed file to be stored
al@868 105
al@868 106 store_cache() {
al@868 107 mkdir -p "${1%/*}"
al@868 108 mv "$2" "$1"
al@868 109 ln "$1" "$2"
al@868 110 }
al@868 111
al@868 112
al@865 113 # Function to compress all man pages
al@865 114 # Compressing can be disabled with COOKOPTS="!manz"
al@865 115
al@865 116 compress_manpages() {
al@881 117 time0=$(get_time)
al@865 118 [ "${COOKOPTS/!manz/}" != "$COOKOPTS" ] && return
al@874 119 manpath="$install/usr/share/man"
al@865 120 [ -d "$manpath" ] || return
al@865 121 size0=$(sizes man); [ -z "$size0" ] && return
al@865 122
al@867 123 tazpkg -gi advancecomp --quiet --cookmode
al@867 124
al@865 125 action 'Compressing man pages...'
al@865 126
al@865 127 # We'll use only Gzip compression, so decompress other formats first
al@865 128 find $manpath -type f -name '*.bz2' -exec bunzip2 \{\} \;
al@865 129 find $manpath -type f -name '*.xz' -exec unxz \{\} \;
al@865 130
al@865 131 # Fast compress with gzip
al@874 132 find $manpath -type f ! -name '*.gz' -exec gzip \{\} \;
al@865 133
al@865 134 # Fix symlinks
al@874 135 for i in $(find $manpath -type l); do
al@865 136 dest=$(readlink $i | sed 's|\.[gbx]z2*$||')
al@865 137 link=$(echo $i | sed 's|\.[gbx]z2*$||')
al@865 138 rm $i; ln -s $dest.gz $link.gz
al@865 139 done
al@865 140
al@865 141 # Recompress with advdef (it can't compress, only recompress)
al@876 142 IFS=$'\n'
al@874 143 for i in $(find $manpath -type f); do
al@868 144 if ! cached_path=$(query_cache mangz "$i"); then
al@868 145 advdef -z4q "$i"
al@868 146 store_cache "$cached_path" "$i"
al@868 147 fi
al@865 148 done
al@865 149
al@865 150 comp_summary "$time0" "$size0" "$(sizes man)"
al@865 151 }
al@865 152
al@865 153
al@872 154 # Function to recompress all gzip archives
al@872 155 # Recompressing can be disabled with COOKOPTS="!gz"
al@872 156
al@872 157 recompress_gz() {
al@881 158 time0=$(get_time)
al@872 159 [ "${COOKOPTS/!gz/}" != "$COOKOPTS" ] && return
al@872 160 size0=$(sizes gz); [ -z "$size0" ] && return
al@872 161
al@872 162 tazpkg -gi advancecomp --quiet --cookmode
al@872 163
al@872 164 action 'Recompressing gzip files...'
al@872 165
al@872 166 # Recompress with advdef
al@876 167 IFS=$'\n'
al@872 168 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do
al@872 169 if ! cached_path=$(query_cache gz "$i"); then
al@872 170 advdef -z4q "$i"
al@872 171 store_cache "$cached_path" "$i"
al@872 172 fi
al@872 173 done
al@872 174
al@872 175 comp_summary "$time0" "$size0" "$(sizes gz)"
al@872 176 }
al@872 177
al@872 178
al@865 179 # Function used after compile_rules() to compress all png images
al@865 180 # Compressing can be disabled with COOKOPTS="!pngz"
al@865 181
al@865 182 compress_png() {
al@881 183 time0=$(get_time)
al@865 184 [ "${COOKOPTS/!pngz/}" != "$COOKOPTS" ] && return
al@865 185 size0=$(sizes png); [ -z "$size0" ] && return
al@865 186
al@865 187 use_pq=true
al@865 188 use_op=true
al@865 189 [ "${COOKOPTS/!pngquant/}" != "$COOKOPTS" ] && use_pq=false
al@865 190 [ "${COOKOPTS/!optipng/}" != "$COOKOPTS" ] && use_op=false
al@867 191 $use_pq && tazpkg -gi pngquant --quiet --cookmode
al@867 192 $use_op && tazpkg -gi optipng --quiet --cookmode
al@865 193
al@867 194 action 'Compressing png images...'
al@865 195
al@865 196 oplevel=$(echo $COOKOPTS | grep 'op[0-8]' | sed 's|.*op\([0-8]\).*|\1|')
al@868 197 [ -z "$oplevel" ] && oplevel='2'
al@868 198
al@868 199 cache_section="png$oplevel"
al@868 200 $use_pq && cache_section="${cache_section}p"
al@868 201 $use_op && cache_section="${cache_section}o"
al@868 202
al@865 203 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9'
al@865 204
al@876 205 IFS=$'\n'
al@865 206 for i in $(find $install -type f -name '*.png'); do
al@868 207 if ! cached_path=$(query_cache $cache_section "$i"); then
al@868 208 $use_pq && pngquant -f --skip-if-larger --ext .png --speed 1 "$i"
al@868 209 $use_op && optipng -quiet -strip all -o$oplevel "$i"
al@868 210 store_cache "$cached_path" "$i"
al@868 211 fi
al@865 212 done
al@865 213
al@865 214 comp_summary "$time0" "$size0" "$(sizes png)"
al@865 215 }
al@865 216
al@865 217
al@865 218 # Function used after compile_rules() to compress all svg images
al@865 219 # Compressing can be disabled with COOKOPTS="!svgz"
al@865 220
al@865 221 compress_svg() {
al@881 222 time0=$(get_time)
al@865 223 [ "${COOKOPTS/!svgz/}" != "$COOKOPTS" ] && return
al@865 224 size0=$(sizes svg); [ -z "$size0" ] && return
al@865 225
al@867 226 tazpkg -gi svgcleaner --quiet --cookmode
al@867 227
al@865 228 action 'Compressing svg images...'
al@865 229
al@865 230 cleaner_log="$(mktemp)"
al@876 231 IFS=$'\n'
al@865 232 for i in $(find $install -type f -name '*.svg'); do
al@865 233 echo -n "$i: " >> "$cleaner_log"
al@865 234 svgcleaner "$i" "$i" --remove-unresolved-classes false --quiet true >> "$cleaner_log"
al@865 235 done
al@865 236
al@865 237 comp_summary "$time0" "$size0" "$(sizes svg)"
al@865 238
al@865 239 sed -i '/: $/d' "$cleaner_log"
al@865 240 if [ -s "$cleaner_log" ]; then
al@865 241 _ 'Cleaner warnings and errors:'
al@865 242 awk '{printf " %s\n", $0;}' "$cleaner_log"
al@865 243 echo
al@865 244 fi
al@865 245 rm "$cleaner_log"
al@865 246 }
al@865 247
al@865 248
al@865 249 # Function used after compile_rules() to shrink all *.ui and *.glade files:
al@865 250 # remove insignificant spaces and comments
al@865 251 # Compressing can be disabled with COOKOPTS="!uiz"
al@865 252
al@865 253 compress_ui() {
al@865 254 [ "${COOKOPTS/!uiz/}" != "$COOKOPTS" ] && return
al@865 255 [ -z "$(find $install -type f \( -name '*.ui' -o -name '*.glade' \) )" ] && return
al@865 256
al@867 257 tazpkg -gi xmlstarlet --quiet --cookmode
al@867 258
al@865 259 action 'Compressing ui files...'
al@867 260
al@865 261 size0=$(sizes xml)
al@881 262 time0=$(get_time)
al@865 263 temp_ui="$(mktemp)"
al@876 264 IFS=$'\n'
al@865 265 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do
al@865 266 xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui"
al@865 267 cat "$temp_ui" > "$ui"
al@865 268 done
al@865 269
al@865 270 comp_summary "$time0" "$size0" "$(sizes xml)"
al@865 271 rm "$temp_ui"
al@865 272 }
al@865 273
al@865 274
al@865 275 # Get list of supported locales...
al@865 276
al@865 277 get_supported_locales() {
al@865 278 lpc='/slitaz-i18n/stuff/locale-pack.conf'
al@865 279 if [ -e "$WOK$lpc" ]; then
al@865 280 # ... from package in the local wok
al@865 281 . "$WOK$lpc"
al@865 282 else
al@865 283 # ... from Hg
al@865 284 temp_conf=$(mktemp)
al@865 285 wget -q -O $temp_conf -T 10 "http://hg.slitaz.org/wok/raw-file/tip$lpc"
al@865 286 if [ -s $temp_conf ]; then
al@865 287 . $temp_conf
al@865 288 else
al@865 289 # Give up and use hardcoded list
al@865 290 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 291 pt_BR ro ru sl sv tr uk zh_CN zh_TW"
al@865 292 fi
al@865 293 rm $temp_conf
al@865 294 fi
al@865 295 echo $LOCALE_PACK
al@865 296 }
al@865 297
al@865 298
al@865 299 # Fix common errors and warnings in the .desktop files
al@865 300 # Fixing can be disabled with COOKOPTS="!fixdesktops"
al@865 301
al@865 302 fix_desktop_files() {
al@865 303 [ "${COOKOPTS/!fixdesktops/}" != "$COOKOPTS" ] && return
al@876 304 deskpath="$install/usr/share/applications"
al@876 305 [ -d "$deskpath" ] || return
al@876 306 [ -z "$(find $deskpath -type f -name '*.desktop')" ] && return
al@865 307
al@865 308 size0=$(sizes des)
al@881 309 time0=$(get_time)
al@865 310
al@865 311 if [ -n "$QA" -a -z "$(which desktop-file-validate)" ]; then
al@867 312 tazpkg -gi desktop-file-utils-extra --quiet --cookmode
al@865 313 fi
al@865 314
al@865 315 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
al@865 316 # Default value is "" (empty). That means for us that we'll use the full
al@865 317 # list of supported locales here.
al@865 318 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
al@865 319
al@876 320 IFS=$'\n'
al@876 321 for desktop in $(find $deskpath -type f -name '*.desktop'); do
al@865 322 cp "$desktop" "$desktop.orig"
al@865 323
al@865 324 # Sort out .desktop file (is prerequisite to correct working of `fix-desktop-file`)
al@865 325 sdft "$desktop" -i
al@865 326
al@865 327 # Fix common errors in .desktop file
al@865 328 fix-desktop-file "$desktop"
al@865 329
al@865 330 # Strip unsupported locales from .desktop file
al@865 331 [ "${COOKOPTS/!i18nz/}" == "$COOKOPTS" ] &&
al@865 332 sdft "$desktop" -i -k "$LOCALE"
al@865 333
al@865 334 # Extra-strip
al@865 335 [ "${COOKOPTS/!extradesktops/}" == "$COOKOPTS" ] &&
al@865 336 sdft "$desktop" -i -g -x -tf -r 'Keywords*' -o
al@865 337
al@865 338 if [ -n "$QA" ]; then
al@865 339 # Check the rest of errors, warnings and tips
al@865 340 _ 'QA: Checking %s...' "$(basename $desktop)"
al@865 341 diff "$desktop.orig" "$desktop"
al@865 342 desktop-file-validate "$desktop" | busybox fold -s
al@865 343 echo
al@865 344 fi
al@865 345
al@865 346 rm "$desktop.orig"
al@865 347 done
al@865 348
al@865 349 comp_summary "$time0" "$size0" "$(sizes des)"
al@865 350 }
al@865 351
al@865 352
paul@879 353 # Normalize all *.mo files: unconditionally convert to UTF-8; remove strings that are not really necessary
al@865 354 # to the translation (msgid = msgstr)
al@865 355 # Normalization can be disabled with COOKOPTS="!monorm"
al@865 356
al@865 357 normalize_mo() {
al@865 358 [ "${COOKOPTS/!monorm/}" != "$COOKOPTS" ] && return
al@865 359 [ -z "$(find $install -type f -name '*.mo')" ] && return
al@865 360
al@867 361 # Gettext functions: msgunfmt, msguniq, msgconv, msgfmt
al@867 362 tazpkg -gi gettext --quiet --cookmode
al@867 363 # Gconv modules (convert to UTF-8)
al@867 364 tazpkg -gi glibc-locale --quiet --cookmode
al@867 365
al@865 366 action 'Normalizing mo files...'
al@867 367
al@865 368 size0=$(sizes mo1)
al@881 369 time0=$(get_time)
al@865 370
al@865 371 # Process all existing *.mo files
al@876 372 IFS=$'\n'
al@865 373 for mo in $(find "$install" -type f -name '*.mo'); do
al@865 374 tmpfile="$(mktemp)"
al@865 375
al@865 376 msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'
al@865 377 # add newline
al@865 378 echo >> "$tmpfile"
al@865 379
al@865 380 # get Plural-Forms
al@865 381 awk '
al@865 382 BEGIN { skip = ""; }
al@865 383 {
al@865 384 if (! skip) {
al@865 385 s = $0;
al@865 386 gsub(/^[^\"]*\"/, "", s);
al@865 387 gsub(/\"$/, "", s);
al@865 388 printf("%s", s);
al@865 389 }
al@865 390 if (! $0) skip = "yes";
al@865 391 }
al@865 392 ' "$tmpfile" | sed 's|\\n|\n|g' | grep "^Plural-Forms:" > "$tmpfile.pf"
al@865 393
al@865 394 if ! grep -q 'msgid_plural' "$tmpfile"; then
al@865 395 echo > "$tmpfile.pf"
al@865 396 fi
al@865 397
al@865 398 # main
al@865 399 awk -v pf="$(cat "$tmpfile.pf")" '
al@865 400 function clean() {
al@865 401 mode = msgctxt = msgid = msgid_plural = msgstr = msgstr0 = msgstr1 = msgstr2 = msgstr3 = msgstr4 = msgstr5 = "";
al@865 402 }
al@865 403
al@865 404 function getstring() {
al@865 405 # Skip unquoted words at the beginning (msgid, msgstr...) and get string from inside quotes
al@865 406 s = $0;
al@865 407 gsub(/^[^\"]*\"/, "", s);
al@865 408 gsub(/\"$/, "", s);
al@865 409 return s;
al@865 410 }
al@865 411
al@865 412 BEGIN {
al@865 413 printf("msgid \"\"\nmsgstr \"\"\n\"Content-Type: text/plain; charset=UTF-8\\n\"\n");
al@865 414 if (pf)
al@865 415 printf("\"%s\\n\"\n", pf);
al@865 416 printf("\n");
al@865 417 skip = 1;
al@865 418 clean();
al@865 419 }
al@865 420
al@865 421 {
al@865 422 # Skip the entire header
al@865 423 if (!skip) {
al@865 424 if ($1 == "msgctxt" || $1 == "msgid" || $1 == "msgstr" || $1 == "msgid_plural")
al@865 425 mode = $1;
al@865 426 if ($1 == "msgstr[0]") mode = "msgstr0";
al@865 427 if ($1 == "msgstr[1]") mode = "msgstr1";
al@865 428 if ($1 == "msgstr[2]") mode = "msgstr2";
al@865 429 if ($1 == "msgstr[3]") mode = "msgstr3";
al@865 430 if ($1 == "msgstr[4]") mode = "msgstr4";
al@865 431 if ($1 == "msgstr[5]") mode = "msgstr5";
al@865 432
al@865 433 if (mode == "msgctxt") msgctxt = msgctxt getstring();
al@865 434 if (mode == "msgid") msgid = msgid getstring();
al@865 435 if (mode == "msgstr") msgstr = msgstr getstring();
al@865 436 if (mode == "msgid_plural") msgid_plural = msgid_plural getstring();
al@865 437 if (mode == "msgstr0") msgstr0 = msgstr0 getstring();
al@865 438 if (mode == "msgstr1") msgstr1 = msgstr1 getstring();
al@865 439 if (mode == "msgstr2") msgstr2 = msgstr2 getstring();
al@865 440 if (mode == "msgstr3") msgstr3 = msgstr3 getstring();
al@865 441 if (mode == "msgstr4") msgstr4 = msgstr4 getstring();
al@865 442 if (mode == "msgstr5") msgstr5 = msgstr5 getstring();
al@865 443
al@865 444 if (! $0) {
al@865 445 if (msgid != msgstr) {
al@865 446 if (msgctxt) printf("msgctxt \"%s\"\n", msgctxt);
al@865 447 printf("msgid \"%s\"\n", msgid);
al@865 448 if (msgid_plural) printf("msgid_plural \"%s\"\n", msgid_plural);
al@865 449 if (msgstr) printf("msgstr \"%s\"\n", msgstr);
al@865 450 if (msgstr0) printf("msgstr[0] \"%s\"\n", msgstr0);
al@865 451 if (msgstr1) printf("msgstr[1] \"%s\"\n", msgstr1);
al@865 452 if (msgstr2) printf("msgstr[2] \"%s\"\n", msgstr2);
al@865 453 if (msgstr3) printf("msgstr[3] \"%s\"\n", msgstr3);
al@865 454 if (msgstr4) printf("msgstr[4] \"%s\"\n", msgstr4);
al@865 455 if (msgstr5) printf("msgstr[5] \"%s\"\n", msgstr5);
al@865 456 printf("\n");
al@865 457 }
al@865 458 clean();
al@865 459 }
al@865 460 }
al@865 461 if ($0 == "") skip = "";
al@865 462 }
al@865 463 ' "$tmpfile" > "$tmpfile.awk"
al@865 464
al@865 465 msgfmt "$tmpfile.awk" -o "$tmpfile.mo"
al@865 466
al@865 467 if [ -s "$tmpfile.mo" ]; then
al@865 468 rm "$mo"; mv "$tmpfile.mo" "$mo"
al@865 469 else
al@865 470 _ 'Error processing %s' "$mo"
al@865 471 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo"
al@865 472 fi
al@865 473
al@865 474 # Clean
al@865 475 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk"
al@865 476 done
al@865 477
al@865 478 comp_summary "$time0" "$size0" "$(sizes mo1)"
al@865 479 }
al@865 480
al@865 481
al@865 482 # Strip locale definitions: normalize whitespace and remove comments
al@865 483 # Stripping can be disabled with COOKOPTS="!locdef"
al@865 484
al@865 485 strip_locale_def() {
al@865 486 [ "${COOKOPTS/!locdef/}" != "$COOKOPTS" ] && return
al@865 487 [ ! -d "$install/usr/share/i18n/locales" ] && return
al@865 488 [ -z "$(find $install/usr/share/i18n/locales -type f)" ] && return
al@865 489
al@865 490 action 'Stripping locale definitions...'
al@865 491 size0=$(sizes loc)
al@881 492 time0=$(get_time)
al@865 493
al@865 494 for i in $(find $install/usr/share/i18n/locales -type f); do
al@865 495 sed -i 's| | |g; s| *| |g; s|^ ||; /^%/d' $i
al@865 496 done
al@865 497
al@865 498 comp_summary "$time0" "$size0" "$(sizes loc)"
al@865 499 }
al@865 500
al@865 501
al@865 502 # Find and strip: --strip-all (-s) or --strip-debug on static libs as well
al@865 503 # as removing unneeded files like in Python packages. Cross compiled binaries
al@865 504 # must be stripped with cross-tools aka $ARCH-slitaz-*-strip
al@865 505 # Stripping can be disabled with COOKOPTS="!strip"
al@865 506
al@865 507 strip_package() {
al@865 508 [ "${COOKOPTS/!strip/}" != "$COOKOPTS" ] && return
al@865 509
al@865 510 case "$ARCH" in
al@865 511 arm*|x86_64) export STRIP="$HOST_SYSTEM-strip" ;;
al@865 512 *) export STRIP='strip' ;;
al@865 513 esac
al@865 514 action 'Executing strip on all files...'
al@865 515 size0=0
al@865 516 size1=0
al@881 517 time0=$(get_time)
al@865 518
al@865 519 # Strip executable files
al@865 520 for dir in $fs/bin $fs/sbin $fs/usr/bin $fs/usr/sbin $fs/usr/games; do
al@865 521 if [ -d "$dir" ]; then
al@865 522 oldsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
al@865 523 find $dir -type f -exec $STRIP -s '{}' 2>/dev/null \;
al@865 524 newsize=$(find $dir -type f -exec ls -l '{}' \; | awk '{s+=$5}END{print s}')
al@865 525 size0=$((size0 + oldsize)); size1=$((size1 + newsize))
al@865 526 fi
al@865 527 done
al@865 528
al@899 529 oldsize=$(sizes str)
al@899 530
al@865 531 # Strip shared and static libraries
al@865 532 find $fs -name '*.so*' -exec $STRIP -s '{}' 2>/dev/null \;
al@865 533 find $fs -name '*.a' -exec $STRIP --strip-debug '{}' 2>/dev/null \;
al@899 534
al@899 535 # Remove Python *.pyc and *.pyo
al@865 536 find $fs -type f \( -name '*.pyc' -o -name '*.pyo' \) -delete 2>/dev/null
al@865 537
al@899 538 # Remove both with the empty subfolders:
al@899 539 # 1. Perl perllocal.pod and .packlist (unconditionally)
al@899 540 local perlfiles="$(find $fs -type f \( -name 'perllocal.pod' -o -name '.packlist' \))"
al@899 541 # 2. Perl *.pod (if not disabled)
al@899 542 [ "${COOKOPTS/!rmpod/}" == "$COOKOPTS" ] &&
al@899 543 perlfiles="$perlfiles $(find $fs -type f -name '*.pod')"
al@899 544 echo "$perlfiles" | xargs rm -f 2>/dev/null
al@899 545 echo "$perlfiles" | awk 'BEGIN{FS=OFS="/"}{$NF="";print}' | xargs rmdir -p 2>/dev/null
al@899 546
al@899 547 # Strip Perl files (*.pm and *.pl) from documentation inside (if not disabled)
al@899 548 [ "${COOKOPTS/!perlz/}" == "$COOKOPTS" ] &&
al@899 549 find $fs -type f \( -name '*.pm' -o -name '*.pl' \) -exec sed -i '/^=/,/^=cut/d' '{}' \;
al@899 550
al@899 551 newsize=$(sizes str)
al@865 552
al@865 553 comp_summary "$time0" "$((size0 + oldsize))" "$((size1 + newsize))"
al@865 554 }
al@865 555
al@865 556
al@865 557 # Strip unsupported locales (.mo files)
al@865 558
al@865 559 strip_mo_i18n() {
al@865 560 [ "${COOKOPTS/!i18nz/}" != "$COOKOPTS" ] && return
al@865 561
al@865 562 [ ! -d "$fs/usr/share/locale" ] && return
al@865 563 [ -z "$(find $fs/usr/share/locale -type f -name '*.mo')" ] && return
al@865 564
al@865 565 action 'Thin out translation files...'
al@865 566 size0=$(sizes mo2)
al@881 567 time0=$(get_time)
al@865 568
al@865 569 # The variable $LOCALE is set in cook.conf and may be overridden in the receipt.
al@865 570 # Default value is "" (empty). That means for us that we'll use the full
al@865 571 # list of supported locales here.
al@865 572 [ -z "$LOCALE" ] && LOCALE=$(get_supported_locales)
al@865 573
al@865 574 # Existing locales
al@865 575 elocales=" $(ls -1 "$fs/usr/share/locale" | tr '\n' ' ') "
al@865 576
al@865 577 # Thin out the list of existing locales. At the end there will be only locales that need
al@865 578 # deleting (and the garbage like '_AU', _US', '_BR' leaving from 'en_AU', 'en_US', 'pt_BR'...)
al@865 579 for keep_locale in $LOCALE; do
al@865 580 elocales=${elocales//$keep_locale}
al@865 581 done
al@865 582
al@865 583 # Remove the unsupported locales
al@865 584 for rem_locale in $elocales; do
al@865 585 [ -d "$fs/usr/share/locale/$rem_locale" ] &&
al@865 586 rm -r "$fs/usr/share/locale/$rem_locale"
al@865 587 done
al@865 588
al@865 589 comp_summary "$time0" "$size0" "$(sizes mo2)"
al@865 590 }
al@865 591
al@865 592
al@865 593
al@865 594
al@865 595 case $1 in
al@865 596 install)
al@865 597 # Compressors working in the $install
al@865 598 case "$ARCH" in
al@865 599 arm*) ;;
al@865 600 *)
al@872 601 recompress_gz
al@865 602 compress_manpages
al@865 603 compress_png
al@865 604 compress_svg
al@865 605 compress_ui
al@865 606 ;;
al@865 607 esac
al@865 608
al@865 609 fix_desktop_files
al@865 610 normalize_mo
al@865 611 strip_locale_def
al@865 612 ;;
al@865 613 fs)
al@865 614 # Compressors working in the $fs
al@865 615 strip_package
al@865 616 strip_mo_i18n
al@865 617 ;;
al@865 618 esac
al@868 619
al@868 620 # Clean
al@868 621 rm "$cache_stat"
al@869 622 find $comp_cache_root -type f -links -2 -delete