check_desc_trans.pl 7.03 KB
Newer Older
1
#!/usr/bin/perl
2

3 4 5 6 7 8 9 10
# Check translation status for mailing list descriptions. Since these files
# aren't WML files, the translation data is stored in a separate file in
# each directory, listing the names of the files and the corresponding
# English version.
#
# Since I couldn't figure out how to add this to the regular check_trans.pl
# script, this is a separate script.
#
11 12 13 14 15 16 17
# To use this script, create a file called translation-check in each
# directory under <language>/MailingLists/desc/. In it you list the name of
# the translated file and the version of the English original, separated by
# whitespace. Then run this script, and it will tell you about which files
# are missing, which files are outdated, and if there are files translating
# files that are no longer in the English directory.
#
18 19
# The script needs to be run from the top level webwml directory.
#
20 21 22 23
# The language to check can be specified on the command line, in 
# language.conf, or in the DWWW_LANG environment variable.
#
# Originally written 2002-10-05 by Peter Karlsson <peterk@debian.org>
Bas Zoetekouw's avatar
Bas Zoetekouw committed
24
# © Copyright 2002-2007 Software in the public interest, Inc.
25
# Complete rewrite 2008 by Bas Zoetekouw <bas@debian.org>
Bas Zoetekouw's avatar
Bas Zoetekouw committed
26
# © Copyright 2008 by Bas Zoetekouw <bas@debian.org>
27 28 29 30 31
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
32 33 34

# $Id$

35 36 37 38 39 40 41 42
use FindBin;
use lib "$FindBin::Bin/Perl";

use File::Basename;
use File::Spec::Functions;
use File::Find::Rule;
use Term::ANSIColor;

43 44
use Local::VCS  ':all';
use Local::Util 'uniq';
45 46 47 48

use strict;
use warnings;

49 50 51 52 53 54 55
# see if -c was specified 
if ( $ARGV[0] eq '-c' )
{
	shift @ARGV;
	$ENV{'ANSI_COLORS_DISABLED'} = '1';
}

56 57 58 59 60 61 62
# Get language configuration
my $language;
if ( $ARGV[0] )
{
	$language = $ARGV[0];
}
elsif (exists $ENV{DWWW_LANG}) 
63 64 65
{
	$language = $ENV{DWWW_LANG};
} 
66
elsif ( open( my $conf, '<', 'language.conf' ) )
67
{
68
	while ( my $line  = <$conf> )
69
	{
70 71 72 73
		next if $line =~ /^#/;
		chomp $line ;
		$language = $line;
		last;
74
	}
75 76

	close $conf;
77
}
78

79 80
die "Language not defined in DWWW_LANG or language.conf\n"
	unless defined $language;
81 82
die "Language `$language' doesn't exist\n"
	unless -d $language;
83

84
# Counters
85 86 87 88 89 90 91 92 93 94
my $old = 0;
my $uptodate = 0;
my $unknown = 0;
my $needtranslation = 0;

# directories
my $directory = catdir( 'MailingLists' , 'desc' );
my $srcdir    = catdir( 'english', $directory );
my $destdir   = catdir( $language, $directory );

95 96 97 98
my $VCS = Local::VCS->new();

# read VCS info about files in source dir
my %revision_info = $VCS->path_info( $srcdir, 'recursive' => 1 );
99 100 101 102 103 104

# read the translation-check files in dest dir
my %transcheck = read_transcheck( $destdir );

# check all files
my ($nr_uptodate,$nr_old,$nr_needtrans,$nr_obsolete,$nr_error) = 
105
	check_all( $VCS, $language, $directory, \%transcheck, \%revision_info );
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

# print results
print "\nResults:\n";
printf "  %3i are up to date.\n",        $nr_uptodate;
printf "  %3i need to be updated.\n",    $nr_old;
printf "  %3i need to be translated.\n", $nr_needtrans;
printf "  %3i are obsolete.\n",          $nr_obsolete;
printf "  %3i are broken.\n",            $nr_error;

exit 0;


#============================================================


# read in all transcheck files under the specified directory
sub read_transcheck
{
	my $dir = shift  or  die("No directory specified");

126
	# get a list of all translation-check files
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	my @files = File::Find::Rule->file()->name('translation-check')->in($dir);

	my %info;
	foreach my $file (@files)
	{
		my $thisdir = dirname $file;

		# TODO: use a nice File::Spec function for this
		$thisdir =~ s{^$dir/*}{} ;

		open( my $fd, '<', $file ) or die("Can't open `$file': $!\n");
		while ( my $line = <$fd> )
		{
			chomp $line;

			# skip comments and empty lines
			next if $line =~ m{^#};
			next if $line =~ m{^\s+$};

			# read the file name and the revision from the file
			my ($listfile,$revision) = split( /\s+/, $line, 2 );
			warn "Couldn't parse line $. of $file\n" unless $revision;

			# prepend the directory name, if needed
			my $thefile = $thisdir ? catfile( $thisdir, $listfile ) : $listfile;

			# save the data 
			$info{ $thefile } = $revision;
		}
		close( $fd );
	}

	return %info;

}


# check all translations
sub check_all
166
{
167
	my $VCS     = shift;
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	my $lang    = shift  or  die("No language specified");
	my $dir     = shift  or  die("No directory specified");
	my $files   = shift  or  die("No transcheck files specified");
	my $revinfo = shift  or  die("No revision info specified");

	die("Language `$lang' doesn't exists\n") unless -d $lang;

	my $source      = catdir( 'english', $dir );
	my $destination = catdir( $lang,     $dir );

	# create a list of all files (note that the filenames are relative to the
	# english and translated mailinglist directories)
	my @allfiles = sort {$a cmp $b} uniq( keys %$files, keys %$revinfo );

	# counters
	my ($nr_uptodate,$nr_old,$nr_obsolete,$nr_needtrans,$nr_error) = (0,0,0,0,0);

	foreach my $file ( @allfiles )
	{
		# special case, this doesn't need to be translated
		next if $file eq 'README';

		my $file_english = catfile( 'english', $dir, $file );
		my $file_transl  = catfile( $lang,     $dir, $file );

		# check if the info from vcs and from the fs are consistent
		if ( -e $file_english  and  not exists $revinfo->{$file} )
		{
196
			warn "$file_english: english version found, but no revision info available!\n";
197 198 199 200 201
			next;
		}
		# check if the info from translation-check and from the fs are consistent
		if ( -e $file_transl  and  not exists $files->{$file} )
		{
202
			warn "$file_transl: $lang version found, but not found in a translation-check file!\n";
203 204 205 206 207 208 209
			next;
		}

		# now check for out-of-dateness and other things
		if ( -e $file_english and -e $file_transl )
		{
			# needs update
210
			if ( $VCS->cmp_rev( $file_english, $files->{$file}, $revinfo->{$file}->{'cmt_rev'} ) == -1 )
211 212
			{
				$nr_old++;
213
				print color('blue'), $file_transl, color('reset');
214 215 216 217
				printf ": needs to be updated from revision %s to revison %s\n", 
					$files->{$file}, $revinfo->{$file}->{'cmt_rev'};
			}
			# translated file is too new
218
			elsif ( $VCS->cmp_rev( $file_english, $files->{$file}, $revinfo->{$file}->{'cmt_rev'} ) == -1 )
219 220
			{
				$nr_error++;
221
				print color('blue'), $file_transl, color('reset');
222 223 224 225 226 227 228 229 230 231 232 233 234
				printf ": %s revision %s is larger than english revision %s\n",
					$lang, $files->{$file}, $revinfo->{$file}->{'cmt_rev'};
			}
			# up to date!
			else
			{
				$nr_uptodate++;
			}
		}
		# file not translated yet
		elsif ( -e $file_english )
		{
			$nr_needtrans++;
235
			print color('blue'), $file_transl, color('reset');
236 237 238 239 240 241
			printf ": need to translate revision %s\n", $revinfo->{$file}->{'cmt_rev'};
		}
		# translation exists, but original has been removed
		elsif ( -e $file_transl )
		{
			$nr_obsolete++;
242
			print color('blue'), $file_transl, color('reset');
243 244 245 246 247 248 249 250 251 252 253 254 255
			print ": no english file found!\n";
		}
		# weirdness
		else
		{
			$nr_error++;
			print color('blue'), $file, color('reset');
			print ": Woopsie, neither english nor $lang file found\n";
			next;
		}
	}

	return ($nr_uptodate,$nr_old,$nr_needtrans,$nr_obsolete,$nr_error);
256
}
257

258

259
__END__