switch_to_git_translations.pl 7.76 KB
Newer Older
1 2 3 4
#!/usr/bin/perl

# This script walks the webwml tree to look for translated files. It
# looks for the wml::debian::translation-check header to see if a file
5
# is a translation of an original, then checks for the revision
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# status of the master document.
#
# Part of the effort to switch from CVS to Git
#
# Originally written 2018 by Steve McIntyre <93sam@debian.org>
# © Copyright 2018 Software in the public interest, Inc.
# This program is released under the GNU General Public License, v2.

use strict;
use warnings;

use Getopt::Long;
use Data::Dumper;
use File::Spec::Functions;
use File::Find;
use lib ($0 =~ m|(.*)/|, $1 or ".") ."/Perl";
use Webwml::TransCheck;
23
use Local::VCS;
24 25 26 27 28

my $help = 0;
my $verbose = 0;
my $dry_run = 0;
my $revs_file = "";
29
my $db_file = "";
30
my %rev_map;
31
my $VCS = Local::VCS->new();
32 33 34 35 36 37 38 39 40

sub usage {
        print <<'EOT';
Usage: switch_to_git_translations.pl [options]
Options:
  --help         display this message
  --verbose      run verbosely
  --dry-run      do not modify translation-check headers
  --revisions=REVISIONS  location of the cvs2git revisions map file
41 42
  --db=DB        location of a single translated_txt.db to update, *instead* of
	         scanning for translation-check headers
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 166 167 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 196 197 198 199 200 201 202 203

Find all wml/src/etc. files under the current directory, updating revisions for
translations.
EOT
        exit(0);
}

# log very verbose messages
sub vvlog {
    if ($verbose >= 2) {
	print STDOUT $_[0] . "\n";
    }
}

# log verbose messages
sub vlog {
    if ($verbose >= 1) {
	print STDOUT $_[0] . "\n";
    }
}

# Parse the revisions file for use, building a hash of the git and cvs versions for each file
sub parse_revisions
{
    my $revs_file = shift;
    open(IN, "<", "$revs_file") or die "Can't open revisions file \$revs_file\" for reading: $!\n";
    while (my $line = <IN>) {
	chomp $line;
	my ($file, $cvs_ver, $commit_hash);
	if ($line =~ m,^(\S+) ([.\d]+) ([[:xdigit:]]+)$,)
	{
	    $file = $1;
	    $cvs_ver = $2;
	    $commit_hash = $3;
	    $rev_map{"$file"}{"$cvs_ver"}{"commit_hash"} = $commit_hash;
	} else {
	    die "Failed to parse revisions file at line $.\n";
	}
	vvlog("Found file $file with CVS version $cvs_ver in commit hash $commit_hash");
    }
    close IN;
    vlog("Parsed revisions file \"$revs_file\", found revisions for " . scalar(keys %rev_map) . " files");
}

# return a list of filenames with the given extension
sub find_files_ext
{
    my $dir = shift or die('Internal error: No dir specified');
    my $ext = shift or die('Internal error: No ext specified');

    my @files;
    find( sub { if (-f and m/\.$ext$/) { my $filename = $File::Find::name; $filename =~ s,\.\/,,; push @files, $filename }}, $dir );
    return @files;
}

# Update the translation-check metadata header in a wml file
sub update_file_metadata
{
    my $file = shift;
    my $revision = shift;
    my $hash = shift;
    my $text = "";

    open (IN, "< $file") or die "Can't open $file for reading: $!\n";
    while (<IN>) {
	if (m/^#use wml::debian::translation-check/) {
	    s/(translation="?)($revision)("?)/$1$hash$3/;
	}
	$text .= $_;
    }
    close(IN);
    open(OUT, "> $file") or die "Can't open $file for writing: $!\n";
    print OUT $text;
    close OUT;
}

# Parse a file, and see if there's a translation-check header. If so,
# use the rev_map data to switch the translation information from the
# cvs version to the git hash *if available*. If it's not available,
# report an error.
sub parse_file
{
    my $file = shift;
    my $info = 0; # Do we have any translation header info at all?
    my $tc = Webwml::TransCheck->new("$file") or die "Failed transcheck: $!\n";
    vlog("Looking at wml file $file");
    my $target_lang = "english";
    my $maint = $tc->maintainer();
    if (defined($maint)) {
	vvlog("  Maintainer: $maint");
	$info += 1;
    }
    my $revision = $tc->revision();
    if (defined($revision)) {
	vvlog("  Revision: $revision");
	$info += 1;
    }
    my $original = $tc->original();
    if (defined($original)) {
	vvlog("  Original: $original");
	$info += 1;
	$target_lang = $original;
    }
    my $mindelta = $tc->mindelta();
    if (defined($mindelta)) {
	vvlog("  Mindelta: $mindelta");
	$info += 1;
    }
    my $maxdelta = $tc->maxdelta();
    if (defined($maxdelta)) {
	vvlog("  Maxdelta: $maxdelta");
	$info += 1;
    }
    if ($info > 0) {
	my $targetfile = $file;
	$targetfile =~ s,^[^/]+,$target_lang,;
	vvlog("  Depends on $targetfile");
	if (defined($revision)) {
	    # Do we have a cvs->git map for that file and revision?
	    my $hash = $rev_map{"$targetfile"}{"$revision"}{"commit_hash"};
#	    my $file_hash = $rev_map{"$targetfile"}{"$revision"}{"file_hash"};
	    if (defined $hash) {
#		if (!defined $file_hash) {
#		    $file_hash = `git ls-tree -r $hash $targetfile`;
#		    if ($file_hash =~ m/^\s*\d+\s*blob\s+([[:xdigit:]]+)\s+\S+$/) {
#			$file_hash = $1;
#		    }
#		    # Cache the result
#		    $rev_map{"$targetfile"}{"$revision"}{"file_hash"} = $file_hash;
#		}
		vlog("  Depends on $targetfile with cvs rev $revision, commit hash $hash");
	    } else {
		vlog("  Looking up $targetfile with cvs rev $revision, no mapping found");
		return 1;
	    }
	    if (!$dry_run) {
		vlog ("  Updating the file data");

		update_file_metadata($file, $revision, $hash);
	    }
	} else {
	    vlog("  But no revision data!");
	    return 1;
	}
    }
}

#    open(IN, "<", "$file") or die "Can't open file \$wml_file\" for reading: $!#\n";
#    while (my $line = <IN>) {
#	chomp $line;
#	if ($line =~ m/^#use wml::debian::translation-check/) {
#	    my $original="english"; # default
#	}
#    }
#}

# "main"

if (not GetOptions ("help"      => \$help,
		    "verbose=i" => \$verbose,
		    "dry-run"   => \$dry_run,
204
		    "db=s"      => \$db_file,
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		    "revisions=s" => \$revs_file))
{
        warn "Try `$0 --help' for more information.\n";
        exit(1);
}

if ($help) {
    usage();
}

if (! -f $revs_file) {
    die "Can't open revisions file, abort!\n";
}
parse_revisions($revs_file);

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
if (length($db_file) < 2) {
    # Mode 1 - scan for files which need translation-check header
    # updates
    my @wmlfiles = sort(find_files_ext(".", 'wml'));
    my @incfiles = sort(find_files_ext(".", 'inc'));
    my @pofiles = sort(find_files_ext(".", 'po'));
    my @srcfiles = sort(find_files_ext(".", 'src'));
    my @files;
    push @files, @wmlfiles;
    push @files, @incfiles;
    push @files, @pofiles;
    push @files, @srcfiles;
    vlog("Found " . scalar(@files) . " files to work on\n");
    for my $file (@files) {
	parse_file($file);
    }
} else {
    my %translated_files;
    # Mode 2 - update a single translated_txt.db file
    if (! -f $db_file) {
	die "Can't find $db_file, abort!\n";
    }
    open IN, "< $db_file" or die "Couldn't open $db_file: $!";
    my $language;
    $db_file =~ m,^([^/]*)/, and $language = $1;
    vlog("working on $db_file, language $language\n");
    while (my $line = <IN>)
    {
	if (($line =~ m%^([A-Za-z0-9\/\.\-\_]*)[\ \t]*([0-9]+\.[0-9]+)$%)
	    and (-f "$language/$1"))
	{
	    $translated_files{$1} = $2;
	}
    }
    close IN;
    my $num_trans = scalar(keys %translated_files);
    vlog("Parsed $db_file, found revisions for $num_trans translations\n");
    vlog("Caching repo for performance...");
    $VCS->cache_repo();
    vlog(" ...done");
    if (!$dry_run) {
	vlog ("  Rewriting $db_file with git revisions");
	open OUT, "> $db_file" or die "Couldn't write $db_file: $!";
	for my $file (sort keys %translated_files) {
264
	    my $targetfile = "english/$file";
265 266 267 268 269 270 271 272 273 274 275 276
	    my $revision = $translated_files{$file};
	    my $hash = $rev_map{"$targetfile"}{"$revision"}{"commit_hash"};
	    if (!defined $hash) {
		print "Can't find a hash for $targetfile revision $revision\n";
		$hash = $VCS->get_newest_revision($targetfile);
		print "Most recent revision is $hash, using that instead\n";
	    } else {
		printf OUT "%-64s  %s\n", $file, $hash;
	    }
	}
	close OUT;
    }
277
}