cookutils diff modules/compressor @ rev 998
lighttpd/index.cgi: improve titling; modules/compressor: show file names for warnings/errors, restore original if error, store in cache if no errors.
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Sat Nov 04 04:02:57 2017 +0200 (2017-11-04) |
parents | b7b907696891 |
children | 8190be7ad294 |
line diff
1.1 --- a/modules/compressor Fri Oct 13 14:01:28 2017 +0300 1.2 +++ b/modules/compressor Sat Nov 04 04:02:57 2017 +0200 1.3 @@ -55,7 +55,7 @@ 1.4 # Compressor mini summary 1.5 1.6 comp_summary() { 1.7 - # "$time0" "$size0" "$size1" 1.8 + # "$time0" "$size0" "$size1" "$log_file" 1.9 status 1.10 [ "$2" -eq 0 ] && return 1.11 saving=$(awk -va="$2" -vb="$3" 'BEGIN{ printf("%.0f\n", (a - b) / 1024) }') 1.12 @@ -66,6 +66,14 @@ 1.13 fi 1.14 _ ' Time: %s. Size: %s B -> %s B. Save: %s KB.%s' \ 1.15 "$(calc_time $1)" "$2" "$3" "$saving" "$cache_msg" 1.16 + 1.17 + if [ -s "$4" ]; then 1.18 + _ 'Cleaner warnings and errors:' 1.19 + awk '{printf " %s\n", $0;}' "$4" 1.20 + echo 1.21 + fi 1.22 + # Clean log 1.23 + [ ! -f "$4" ] || rm "$4" 1.24 } 1.25 1.26 1.27 @@ -146,15 +154,23 @@ 1.28 done 1.29 1.30 # Recompress with advdef (it can't compress, only recompress) 1.31 + cleaner_log="$(mktemp)" 1.32 IFS=$'\n' 1.33 for i in $(find $manpath -type f); do 1.34 if ! cached_path=$(query_cache mangz "$i"); then 1.35 - advdef -z4q "$i" 1.36 - store_cache "$cached_path" "$i" 1.37 + cp -a "$i" "$i.orig$$" # save the original if something goes wrong 1.38 + out="$(advdef -z4q "$i")" 1.39 + if [ -n "$out" ]; then 1.40 + echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log" 1.41 + mv -f "$i.orig$$" "$i" # restore the original 1.42 + else 1.43 + store_cache "$cached_path" "$i" 1.44 + rm -f "$i.orig$$" # clean 1.45 + fi 1.46 fi 1.47 done 1.48 1.49 - comp_summary "$time0" "$size0" "$(sizes man)" 1.50 + comp_summary "$time0" "$size0" "$(sizes man)" "$cleaner_log" 1.51 } 1.52 1.53 1.54 @@ -171,15 +187,23 @@ 1.55 action 'Recompressing gzip files...' 1.56 1.57 # Recompress with advdef 1.58 + cleaner_log="$(mktemp)" 1.59 IFS=$'\n' 1.60 for i in $(find $install -type f -name '*.gz' ! -path '*/share/man/*'); do 1.61 if ! cached_path=$(query_cache gz "$i"); then 1.62 - advdef -z4q "$i" 1.63 - store_cache "$cached_path" "$i" 1.64 + cp -a "$i" "$i.orig$$" # save the original if something goes wrong 1.65 + out="$(advdef -z4q "$i")" 1.66 + if [ -n "$out" ]; then 1.67 + echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log" 1.68 + mv -f "$i.orig$$" "$i" # restore the original 1.69 + else 1.70 + store_cache "$cached_path" "$i" 1.71 + rm -f "$i.orig$$" # clean 1.72 + fi 1.73 fi 1.74 done 1.75 1.76 - comp_summary "$time0" "$size0" "$(sizes gz)" 1.77 + comp_summary "$time0" "$size0" "$(sizes gz)" "$cleaner_log" 1.78 } 1.79 1.80 1.81 @@ -196,15 +220,23 @@ 1.82 action 'Recompressing zip files...' 1.83 1.84 # Recompress with advzip 1.85 + cleaner_log="$(mktemp)" 1.86 IFS=$'\n' 1.87 for i in $(find $install -type f -name '*.zip'); do 1.88 if ! cached_path=$(query_cache zip "$i"); then 1.89 - advzip -z3qk "$i" # -4 is more than 2 orders slower 1.90 - store_cache "$cached_path" "$i" 1.91 + cp -a "$i" "$i.orig$$" # save the original if something goes wrong 1.92 + out="$(advzip -z3qk "$i")" # '-4' is more than two orders slower; use '-3' 1.93 + if [ -n "$out" ]; then 1.94 + echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log" 1.95 + mv -f "$i.orig$$" "$i" # restore the original 1.96 + else 1.97 + store_cache "$cached_path" "$i" 1.98 + rm -f "$i.orig$$" # clean 1.99 + fi 1.100 fi 1.101 done 1.102 1.103 - comp_summary "$time0" "$size0" "$(sizes zip)" 1.104 + comp_summary "$time0" "$size0" "$(sizes zip)" "$cleaner_log" 1.105 } 1.106 1.107 1.108 @@ -234,17 +266,37 @@ 1.109 1.110 [ "$oplevel" == '8' ] && oplevel='7 -zm1-9' 1.111 1.112 + cleaner_log="$(mktemp)" 1.113 + pq_opt='--skip-if-larger' # Sublime Text is mad about `if` in $(), so put it separately 1.114 IFS=$'\n' 1.115 for i in $(find $install -type f -name '*.png'); do 1.116 - unset IFS 1.117 + unset IFS iserror 1.118 if ! cached_path=$(query_cache $cache_section "$i"); then 1.119 - $use_pq && pngquant -f --skip-if-larger --ext .png --speed 1 "$i" 1.120 - $use_op && optipng -quiet -strip all -o$oplevel "$i" 1.121 - store_cache "$cached_path" "$i" 1.122 + cp -a "$i" "$i.orig$$" # save the original if something goes wrong 1.123 + if $use_pq; then 1.124 + out="$(pngquant -f $pq_opt --ext .png --speed 1 "$i" 2>&1)" 1.125 + if [ -n "$out" ]; then 1.126 + echo "$i (pngquant):"$'\n'"$out"$'\n' >> "$cleaner_log" 1.127 + iserror='yes' 1.128 + fi 1.129 + fi 1.130 + if $use_op && [ -z "$iserror" ]; then 1.131 + out="$(optipng -quiet -strip all -o$oplevel "$i" 2>&1)" 1.132 + if [ -n "$out" ]; then 1.133 + echo "$i (optipng):"$'\n'"$out"$'\n' >> "$cleaner_log" 1.134 + iserror='yes' 1.135 + fi 1.136 + fi 1.137 + if [ -n "$iserror" ]; then 1.138 + mv -f "$i.orig$$" "$i" # restore the original 1.139 + else 1.140 + store_cache "$cached_path" "$i" 1.141 + rm -f "$i.orig$$" # clean 1.142 + fi 1.143 fi 1.144 done 1.145 1.146 - comp_summary "$time0" "$size0" "$(sizes png)" 1.147 + comp_summary "$time0" "$size0" "$(sizes png)" "$cleaner_log" 1.148 } 1.149 1.150 1.151 @@ -267,17 +319,10 @@ 1.152 for i in $(IFS=$'\n' find $install -type f -name '*.svg'); do 1.153 out="$(unset IFS; svgcleaner "$i" "$i" --copy-on-error --quiet \ 1.154 --multipass --remove-unresolved-classes no $opts 2>&1)" 1.155 - [ -z "$out" ] || echo -e "$i:\n$out\n" >> "$cleaner_log" 1.156 + [ -z "$out" ] || echo "$i:"$'\n'"$out"$'\n' >> "$cleaner_log" 1.157 done 1.158 1.159 - comp_summary "$time0" "$size0" "$(sizes svg)" 1.160 - 1.161 - if [ -s "$cleaner_log" ]; then 1.162 - _ 'Cleaner warnings and errors:' 1.163 - awk '{printf " %s\n", $0;}' "$cleaner_log" 1.164 - echo 1.165 - fi 1.166 - rm "$cleaner_log" 1.167 + comp_summary "$time0" "$size0" "$(sizes svg)" "$cleaner_log" 1.168 } 1.169 1.170 1.171 @@ -296,13 +341,18 @@ 1.172 size0=$(sizes xml) 1.173 time0=$(get_time) 1.174 temp_ui="$(mktemp)" 1.175 + cleaner_log="$(mktemp)" 1.176 IFS=$'\n' 1.177 for ui in $(find $install -type f \( -name '*.ui' -o -name '*.glade' \) ); do 1.178 - xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui" 1.179 - cat "$temp_ui" > "$ui" 1.180 + out="$(xmlstarlet c14n --without-comments "$ui" | xmlstarlet sel -B -t -c '*' > "$temp_ui")" 1.181 + if [ -n "$out" ]; then 1.182 + echo "$ui:"$'\n'"$out"$'\n' >> "$cleaner_log" 1.183 + else 1.184 + cat "$temp_ui" > "$ui" 1.185 + fi 1.186 done 1.187 1.188 - comp_summary "$time0" "$size0" "$(sizes xml)" 1.189 + comp_summary "$time0" "$size0" "$(sizes xml)" "$cleaner_log" 1.190 rm "$temp_ui" 1.191 } 1.192 1.193 @@ -381,7 +431,7 @@ 1.194 rm "$desktop.orig" 1.195 done 1.196 1.197 - comp_summary "$time0" "$size0" "$(sizes des)" 1.198 + comp_summary "$time0" "$size0" "$(sizes des)" '/dev/null' 1.199 } 1.200 1.201 1.202 @@ -404,11 +454,20 @@ 1.203 time0=$(get_time) 1.204 1.205 # Process all existing *.mo files 1.206 + cleaner_log="$(mktemp)" 1.207 IFS=$'\n' 1.208 for mo in $(find "$install" -type f -name '*.mo'); do 1.209 tmpfile="$(mktemp)" 1.210 1.211 - msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8' 1.212 + # put ANY errors of {msgunfmt,msguniq,msgconv} to $out. FIXME? 1.213 + out="$({ msgunfmt "$mo" | msguniq | msgconv -o "$tmpfile" -t 'UTF-8'; } 2>&1)" 1.214 + if [ -n "$out" ]; then 1.215 + # using literal $'\n' here instead of using `echo -e "...\n..."` because 1.216 + # $out may contain escapes ('\r', '\v') that we should print as-is 1.217 + echo "$mo:"$'\n'"$out"$'\n' >> "$cleaner_log" 1.218 + continue # proceed to next file 1.219 + fi 1.220 + 1.221 # add newline 1.222 echo >> "$tmpfile" 1.223 1.224 @@ -497,12 +556,17 @@ 1.225 } 1.226 ' "$tmpfile" > "$tmpfile.awk" 1.227 1.228 - msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 1.229 + out="$(msgfmt "$tmpfile.awk" -o "$tmpfile.mo" 2>&1)" 1.230 + if [ -n "$out" ]; then 1.231 + echo "$mo (msgfmt):"$'\n'"$out"$'\n' >> "$cleaner_log" 1.232 + continue # proceed to next file 1.233 + fi 1.234 1.235 if [ -s "$tmpfile.mo" ]; then 1.236 rm "$mo"; mv "$tmpfile.mo" "$mo" 1.237 else 1.238 - _ 'Error processing %s' "$mo" 1.239 + _ 'Error processing %s' "$mo" >> "$cleaner_log" 1.240 + echo >> "$cleaner_log" 1.241 [ -e "$tmpfile.mo" ] && rm "$tmpfile.mo" 1.242 fi 1.243 1.244 @@ -510,7 +574,7 @@ 1.245 rm "$tmpfile" "$tmpfile.pf" "$tmpfile.awk" 1.246 done 1.247 1.248 - comp_summary "$time0" "$size0" "$(sizes mo1)" 1.249 + comp_summary "$time0" "$size0" "$(sizes mo1)" "$cleaner_log" 1.250 } 1.251 1.252 1.253 @@ -629,7 +693,7 @@ 1.254 1.255 # Remove the unsupported locales 1.256 for rem_locale in $elocales; do 1.257 - [ -d "$fs/usr/share/locale/$rem_locale" ] && 1.258 + [ ! -d "$fs/usr/share/locale/$rem_locale" ] || 1.259 rm -r "$fs/usr/share/locale/$rem_locale" 1.260 done 1.261