3 # $Id: ddtp_i18n_check.sh 2535 2011-02-19 14:20:52Z nekral-guest $
5 # Copyright (C) 2008, 2011 Felipe Augusto van de Wiel <faw@funlabs.org>
6 # Copyright (C) 2008, 2009 Nicolas François <nicolas.francois@centraliens.net>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # On Debian systems, you can find the full text of the license in
14 # /usr/share/common-licenses/GPL-2
19 # This must be defined to either 0 or 1
20 # When DEBUG=0, fail after the first error.
21 # Otherwise, list all the errors.
24 # When DRY_RUN=0, generate the compressed version of the Translation-*
29 # If no argument indicates the PACKAGES_LISTS_DIR then use '.'
33 echo "Usage: $0 [options] <dists_parent_dir> [<packages_lists_directory>]" >&2
35 echo " --debug Debug mode: do not stop after the first error" >&2
36 echo " --dry-run Do not generate the compressed version of the " >&2
37 echo " Translation files">&2
54 echo "Empty parameter" >&2
59 if [ -z "$dists_parent_dir" ]; then
61 dists_parent_dir=${opt%/}
62 elif [ -z "$PACKAGES_LISTS_DIR" ]; then
63 PACKAGES_LISTS_DIR=$opt
65 echo "$0: Invalid option: $opt" >&2
71 PACKAGES_LISTS_DIR=${opt:-.}
73 if [ ! -d "$dists_parent_dir" ]; then
74 echo "missing dists_parent_dir, or not a directory" >&2
77 elif [ ! -d "$PACKAGES_LISTS_DIR" ]; then
78 echo "missing packages_lists_directory, or not a directory" >&2
87 # Original SHA256SUMS, generated by i18n.debian.net
88 CHECKSUMS="SHA256SUMS"
93 # These special files must exist on the top of dists_parent_dir
94 SPECIAL_FILES="$CHECKSUMS $TIMESTAMP $TIMESTAMP.gpg"
96 # Temporary working directory. We need a full path to reduce the
97 # complexity of checking CHECKSUMS and cleaning/removing TMPDIR
98 TEMP_WORK_DIR=$(mktemp -d -t ddtp_dinstall_tmpdir.XXXXXX)
104 # If it's trapped, something bad happened.
106 rm -rf "$TMP_WORK_DIR"
107 rm -f "$dists_parent_dir"/dists/*/main/i18n/Translation-*.bz2
108 rm -f "$dists_parent_dir"/dists/*/main/i18n/Index
111 trap trap_exit EXIT HUP INT QUIT TERM
113 is_filename_okay () {
116 # Check that the file in on an "i18n" directory
117 # This ensures that the Translation-$lang files are not e.g. in
118 # dists/etch/ or dists/etch/main/
119 ifo_d=$(basename $(dirname "$ifo_file"))
120 if [ "x$ifo_d" = "xi18n" ]; then
122 # Check that the file is named Translation-$lang
123 ifo_f=$(basename "$ifo_file")
125 Translation-[a-z][a-z][a-z]_[A-Z][A-Z]) return 0;;
126 Translation-[a-z][a-z]_[A-Z][A-Z]) return 0;;
127 Translation-[a-z][a-z][a-z]) return 0;;
128 Translation-[a-z][a-z]) return 0;;
135 # Check a directory name against a directory whitelist
140 "$dists_parent_dir") return 0;;
141 "$dists_parent_dir/dists") return 0;;
142 # TODO/FIXME: It is undecided how to update at stable/point-releases, so we
143 # don't allow files to $STABLE.
144 # "$dists_parent_dir/dists/$STABLE") return 0;;
145 # "$dists_parent_dir/dists/$STABLE/main") return 0;;
146 # "$dists_parent_dir/dists/$STABLE/main/i18n") return 0;;
147 # "$dists_parent_dir/dists/$STABLE/contrib") return 0;;
148 # "$dists_parent_dir/dists/$STABLE/contrib/i18n") return 0;;
149 # "$dists_parent_dir/dists/$STABLE/non-free") return 0;;
150 # "$dists_parent_dir/dists/$STABLE/non-free/i18n") return 0;;
151 "$dists_parent_dir/dists/$TESTING") return 0;;
152 "$dists_parent_dir/dists/$TESTING/main") return 0;;
153 "$dists_parent_dir/dists/$TESTING/main/i18n") return 0;;
154 "$dists_parent_dir/dists/$TESTING/contrib") return 0;;
155 "$dists_parent_dir/dists/$TESTING/contrib/i18n") return 0;;
156 "$dists_parent_dir/dists/$TESTING/non-free") return 0;;
157 "$dists_parent_dir/dists/$TESTING/non-free/i18n") return 0;;
158 "$dists_parent_dir/dists/$UNSTABLE") return 0;;
159 "$dists_parent_dir/dists/$UNSTABLE/main") return 0;;
160 "$dists_parent_dir/dists/$UNSTABLE/main/i18n") return 0;;
161 "$dists_parent_dir/dists/$UNSTABLE/contrib") return 0;;
162 "$dists_parent_dir/dists/$UNSTABLE/contrib/i18n") return 0;;
163 "$dists_parent_dir/dists/$UNSTABLE/non-free") return 0;;
164 "$dists_parent_dir/dists/$UNSTABLE/non-free/i18n") return 0;;
170 has_valid_fields () {
172 hvf_lang=${hvf_file/*-}
175 function print_status () {
176 printf (\"p: %d, m: %d, s: %d, l: %d\n\", package, md5, s_description, l_description)
179 package = 0 # Indicates if a Package field was found
180 md5 = 0 # Indicates if a Description-md5 field was found
181 s_description = 0 # Indicates if a short description was found
182 l_description = 0 # Indicates if a long description was found
184 failures = 0 # Number of failures (debug only)
185 failed = 0 # Failure already reported for the block
190 if ( (0 != package) \
192 || (0 != s_description) \
193 || (0 != l_description)) {
194 printf (\"Package field unexpected in $hvf_file (line %d)\n\", NR)
197 if ($DEBUG) { failures++ } else { exit 1 }
205 /^Description-md5: / {
207 if ( (1 != package) \
209 || (0 != s_description) \
210 || (0 != l_description)) {
211 printf (\"Description-md5 field unexpected in $hvf_file (line %d)\n\", NR)
214 if ($DEBUG) { failures++ } else { exit 1 }
222 /^Description-$hvf_lang: / {
224 if ( (1 != package) \
226 || (0 != s_description) \
227 || (0 != l_description)) {
228 printf (\"Description-$hvf_lang field unexpected in $hvf_file (line %d)\n\", NR)
231 if ($DEBUG) { failures++ } else { exit 1 }
241 if ( (1 != package) \
243 || (1 != s_description)) {
244 printf (\"Long description unexpected in $hvf_file (line %d)\n\", NR)
247 if ($DEBUG) { failures++ } else { exit 1 }
249 l_description = 1 # There can be any number of long description
250 # lines. Do not count.
258 if ( (1 != package) \
260 || (1 != s_description) \
261 || (1 != l_description)) {
262 printf (\"End of block unexpected in $hvf_file (line %d)\n\", NR)
265 if ($DEBUG) { failures++ } else { exit 1 }
270 package = 0; md5 = 0; s_description = 0; l_description = 0
277 # Anything else: fail
279 printf (\"Unexpected line '\$0' in $hvf_file (line %d)\n\", NR)
282 if ($DEBUG) { failures++ } else { exit 1 }
287 # They must be all set to 0 or all set to 1
288 if ( ( (0 == package) \
290 || (0 == s_description) \
291 || (0 == l_description)) \
292 && ( (0 != package) \
294 || (0 != s_description) \
295 || (0 != l_description))) {
296 printf (\"End of file unexpected in $hvf_file (line %d)\n\", NR)
306 " "$hvf_file" || return 1
311 # $SPECIAL_FILES must exist
312 for sf in $SPECIAL_FILES; do
313 if [ ! -f "$dists_parent_dir/$sf" ]; then
314 echo "Special file ($sf) doesn't exist"
319 # Comparing CHECKSUMS
320 # We don't use -c because a file could exist in the directory tree and not in
321 # the CHECKSUMS, so we sort the existing CHECKSUMS and we create a new one
322 # already sorted, if cmp fails then files are different and we don't want to
324 cd "$dists_parent_dir"
325 find dists -type f -print0 |xargs --null sha256sum > "$TMP_WORK_DIR/$CHECKSUMS.new"
326 sort "$CHECKSUMS" > "$TMP_WORK_DIR/$CHECKSUMS.sorted"
327 sort "$TMP_WORK_DIR/$CHECKSUMS.new" > "$TMP_WORK_DIR/$CHECKSUMS.new.sorted"
328 if ! cmp --quiet "$TMP_WORK_DIR/$CHECKSUMS.sorted" "$TMP_WORK_DIR/$CHECKSUMS.new.sorted"; then
329 echo "Failed to compare the $CHECKSUMS, they are not identical!" >&2
330 diff -au "$TMP_WORK_DIR/$CHECKSUMS.sorted" "$TMP_WORK_DIR/$CHECKSUMS.new.sorted" >&2
335 # Get the list of valid packages (sorted, uniq)
336 for t in "$TESTING" "$UNSTABLE"; do
337 if [ ! -f "$PACKAGES_LISTS_DIR/$t" ]; then
338 echo "Missing $PACKAGES_LISTS_DIR/$t" >&2
341 cut -d' ' -f 1 "$PACKAGES_LISTS_DIR/$t" | sort -u > "$TMP_WORK_DIR/$t.pkgs"
344 /usr/bin/find "$dists_parent_dir" |
347 if ! is_dirname_okay "$f"; then
348 echo "Wrong directory name: $f" >&2
351 elif [ -f "$f" ]; then
352 # If $f is in $SPECIAL_FILES, we skip to the next loop because
353 # we won't check it for format, fields and encoding.
354 for sf in $SPECIAL_FILES; do
355 if [ "$f" = "$dists_parent_dir/$sf" ]; then
360 if ! is_filename_okay "$f"; then
361 echo "Wrong file: $f" >&2
365 # Check that all entries contains the right fields
366 if ! has_valid_fields "$f"; then
367 echo "File $f has an invalid format" >&2
371 # Check that every packages in Translation-$lang exists
372 TPKGS=$(basename "$f").pkgs
373 grep "^Package: " "$f" | cut -d' ' -f 2 | sort -u > "$TMP_WORK_DIR/$TPKGS"
375 */$TESTING/*) t="$TESTING";;
376 */$UNSTABLE/*) t="$UNSTABLE";;
378 if diff "$TMP_WORK_DIR/$t.pkgs" "$TMP_WORK_DIR/$TPKGS" | grep -q "^>"; then
379 diff -au "$TMP_WORK_DIR/$t.pkgs" "$TMP_WORK_DIR/$TPKGS" |grep "^+"
380 echo "$f contains packages which are not in $t" >&2
385 iconv -f utf-8 -t utf-8 < "$f" > /dev/null 2>&1 || {
386 echo "$f is not an UTF-8 file" >&2
390 # We do not check if the md5 in Translation-$lang are
393 if [ "$DRY_RUN" = "0" ]; then
394 # Now generate the compressed files
398 echo "Neither a file or directory: $f" >&2
402 # The while will just fail if an internal check "exit 1", but the script
403 # is not exited. "|| false" makes the script fail (and exit) in that case.
405 echo "$dists_parent_dir structure validated successfully ($(date +%c))"
407 # If we reach this point, everything went fine.
409 rm -rf "$TMP_WORK_DIR"