3 # $Id: ddtp_i18n_check.sh 1670 2009-03-31 20:57:49Z nekral-guest $
5 # Copyright (C) 2008, 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 SHA256SUMS="SHA256SUMS"
93 # These special files must exist on the top of dists_parent_dir
94 SPECIAL_FILES="$SHA256SUMS $TIMESTAMP $TIMESTAMP.gpg"
96 # Temporary working directory. We need a full path to reduce the
97 # complexity of checking SHA256SUMS 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
110 trap trap_exit EXIT HUP INT QUIT TERM
112 is_filename_okay () {
115 # Check that the file in on an "i18n" directory
116 # This ensures that the Translation-$lang files are not e.g. in
117 # dists/etch/ or dists/etch/main/
118 ifo_d=$(basename $(dirname "$ifo_file"))
119 if [ "x$ifo_d" = "xi18n" ]; then
121 # Check that the file is named Translation-$lang
122 ifo_f=$(basename "$ifo_file")
124 Translation-[a-z][a-z][a-z]_[A-Z][A-Z]) return 0;;
125 Translation-[a-z][a-z]_[A-Z][A-Z]) return 0;;
126 Translation-[a-z][a-z][a-z]) return 0;;
127 Translation-[a-z][a-z]) return 0;;
134 # Check a directory name against a directory whitelist
139 "$dists_parent_dir") return 0;;
140 "$dists_parent_dir/dists") return 0;;
141 # TODO/FIXME: It is undecided how to update at stable/point-releases, so we
142 # don't allow files to $STABLE.
143 # "$dists_parent_dir/dists/$STABLE") return 0;;
144 # "$dists_parent_dir/dists/$STABLE/main") return 0;;
145 # "$dists_parent_dir/dists/$STABLE/main/i18n") return 0;;
146 # "$dists_parent_dir/dists/$STABLE/contrib") return 0;;
147 # "$dists_parent_dir/dists/$STABLE/contrib/i18n") return 0;;
148 # "$dists_parent_dir/dists/$STABLE/non-free") return 0;;
149 # "$dists_parent_dir/dists/$STABLE/non-free/i18n") return 0;;
150 "$dists_parent_dir/dists/$TESTING") return 0;;
151 "$dists_parent_dir/dists/$TESTING/main") return 0;;
152 "$dists_parent_dir/dists/$TESTING/main/i18n") return 0;;
153 "$dists_parent_dir/dists/$TESTING/contrib") return 0;;
154 "$dists_parent_dir/dists/$TESTING/contrib/i18n") return 0;;
155 "$dists_parent_dir/dists/$TESTING/non-free") return 0;;
156 "$dists_parent_dir/dists/$TESTING/non-free/i18n") return 0;;
157 "$dists_parent_dir/dists/$UNSTABLE") return 0;;
158 "$dists_parent_dir/dists/$UNSTABLE/main") return 0;;
159 "$dists_parent_dir/dists/$UNSTABLE/main/i18n") return 0;;
160 "$dists_parent_dir/dists/$UNSTABLE/contrib") return 0;;
161 "$dists_parent_dir/dists/$UNSTABLE/contrib/i18n") return 0;;
162 "$dists_parent_dir/dists/$UNSTABLE/non-free") return 0;;
163 "$dists_parent_dir/dists/$UNSTABLE/non-free/i18n") return 0;;
169 has_valid_fields () {
171 hvf_lang=${hvf_file/*-}
174 function print_status () {
175 printf (\"p: %d, m: %d, s: %d, l: %d\n\", package, md5, s_description, l_description)
178 package = 0 # Indicates if a Package field was found
179 md5 = 0 # Indicates if a Description-md5 field was found
180 s_description = 0 # Indicates if a short description was found
181 l_description = 0 # Indicates if a long description was found
183 failures = 0 # Number of failures (debug only)
184 failed = 0 # Failure already reported for the block
189 if ( (0 != package) \
191 || (0 != s_description) \
192 || (0 != l_description)) {
193 printf (\"Package field unexpected in $hvf_file (line %d)\n\", NR)
196 if ($DEBUG) { failures++ } else { exit 1 }
204 /^Description-md5: / {
206 if ( (1 != package) \
208 || (0 != s_description) \
209 || (0 != l_description)) {
210 printf (\"Description-md5 field unexpected in $hvf_file (line %d)\n\", NR)
213 if ($DEBUG) { failures++ } else { exit 1 }
221 /^Description-$hvf_lang: / {
223 if ( (1 != package) \
225 || (0 != s_description) \
226 || (0 != l_description)) {
227 printf (\"Description-$hvf_lang field unexpected in $hvf_file (line %d)\n\", NR)
230 if ($DEBUG) { failures++ } else { exit 1 }
240 if ( (1 != package) \
242 || (1 != s_description)) {
243 printf (\"Long description unexpected in $hvf_file (line %d)\n\", NR)
246 if ($DEBUG) { failures++ } else { exit 1 }
248 l_description = 1 # There can be any number of long description
249 # lines. Do not count.
257 if ( (1 != package) \
259 || (1 != s_description) \
260 || (1 != l_description)) {
261 printf (\"End of block unexpected in $hvf_file (line %d)\n\", NR)
264 if ($DEBUG) { failures++ } else { exit 1 }
269 package = 0; md5 = 0; s_description = 0; l_description = 0
276 # Anything else: fail
278 printf (\"Unexpected line '\$0' in $hvf_file (line %d)\n\", NR)
281 if ($DEBUG) { failures++ } else { exit 1 }
286 # They must be all set to 0 or all set to 1
287 if ( ( (0 == package) \
289 || (0 == s_description) \
290 || (0 == l_description)) \
291 && ( (0 != package) \
293 || (0 != s_description) \
294 || (0 != l_description))) {
295 printf (\"End of file unexpected in $hvf_file (line %d)\n\", NR)
305 " "$hvf_file" || return 1
310 # $SPECIAL_FILES must exist
311 for sf in $SPECIAL_FILES; do
312 if [ ! -f "$dists_parent_dir/$sf" ]; then
313 echo "Special file ($sf) doesn't exist"
318 # Comparing SHA256SUMS
319 # We don use -c because a file could exist in the directory tree and not in
320 # the SHA256SUMS, so we sort the existing SHA256SUMS and we create a new one
321 # already sorted, if cmp fails then files are different and we don't want to
323 cd "$dists_parent_dir"
324 find dists -type f -print0 |xargs --null sha256sum > "$TMP_WORK_DIR/$SHA256SUMS.new"
325 sort "$SHA256SUMS" > "$TMP_WORK_DIR/$SHA256SUMS.sorted"
326 sort "$TMP_WORK_DIR/$SHA256SUMS.new" > "$TMP_WORK_DIR/$SHA256SUMS.new.sorted"
327 if ! cmp --quiet "$TMP_WORK_DIR/$SHA256SUMS.sorted" "$TMP_WORK_DIR/$SHA256SUMS.new.sorted"; then
328 echo "Failed to compare the SHA256SUMS, they are not identical!" >&2
329 diff -au "$TMP_WORK_DIR/$SHA256SUMS.sorted" "$TMP_WORK_DIR/$SHA256SUMS.new.sorted" >&2
334 # Get the list of valid packages (sorted, uniq)
335 for t in "$TESTING" "$UNSTABLE"; do
336 if [ ! -f "$PACKAGES_LISTS_DIR/$t" ]; then
337 echo "Missing $PACKAGES_LISTS_DIR/$t" >&2
340 cut -d' ' -f 1 "$PACKAGES_LISTS_DIR/$t" | sort -u > "$TMP_WORK_DIR/$t.pkgs"
343 /usr/bin/find "$dists_parent_dir" |
346 if ! is_dirname_okay "$f"; then
347 echo "Wrong directory name: $f" >&2
350 elif [ -f "$f" ]; then
351 # If $f is in $SPECIAL_FILES, we skip to the next loop because
352 # we won't check it for format, fields and encoding.
353 for sf in $SPECIAL_FILES; do
354 if [ "$f" = "$dists_parent_dir/$sf" ]; then
359 if ! is_filename_okay "$f"; then
360 echo "Wrong file: $f" >&2
364 # Check that all entries contains the right fields
365 if ! has_valid_fields "$f"; then
366 echo "File $f has an invalid format" >&2
370 # Check that every packages in Translation-$lang exists
371 TPKGS=$(basename "$f").pkgs
372 grep "^Package: " "$f" | cut -d' ' -f 2 | sort -u > "$TMP_WORK_DIR/$TPKGS"
374 */$TESTING/*) t="$TESTING";;
375 */$UNSTABLE/*) t="$UNSTABLE";;
377 if diff "$TMP_WORK_DIR/$t.pkgs" "$TMP_WORK_DIR/$TPKGS" | grep -q "^>"; then
378 diff -au "$TMP_WORK_DIR/$t.pkgs" "$TMP_WORK_DIR/$TPKGS" |grep "^+"
379 echo "$f contains packages which are not in $t" >&2
384 iconv -f utf-8 -t utf-8 < "$f" > /dev/null 2>&1 || {
385 echo "$f is not an UTF-8 file" >&2
389 # We do not check if the md5 in Translation-$lang are
392 if [ "$DRY_RUN" = "0" ]; then
393 # Now generate the compressed files
397 echo "Neither a file or directory: $f" >&2
401 # The while will just fail if an internal check "exit 1", but the script
402 # is not exited. "|| false" makes the script fail (and exit) in that case.
404 echo "$dists_parent_dir structure validated successfully ($(date +%c))"
406 # If we reach this point, everything went fine.
408 rm -rf "$TMP_WORK_DIR"