Apply Perltidy to translation-tool.pl (#6326)

This commit is contained in:
Basil Crow 2022-03-05 07:34:35 -08:00 committed by GitHub
parent ad72f4d9e3
commit f3b8ad3d5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 209 additions and 74 deletions

104
.perltidyrc Normal file
View File

@ -0,0 +1,104 @@
#################################################
# Categories outlined in this config
# are based on the categories in the
# perltidy man page:
# http://perltidy.sourceforge.net/perltidy.html
#################################################
###################################
# I/O Control
###################################
# Send all errors to standard output rather than a file.
--standard-error-output
# All non-critical warnings will be reported as errors.
--warning-output
###################################
# Basic Options
###################################
# Maximum number of characters per line.
--maximum-line-length=80
###################################
# Code Indentation Control
###################################
# The number of spaces to indent a line when a new block starts.
--indent-columns=4
# If a line continues, it should be indented 2 spaces.
--continuation-indentation=4
# If a comment is longer than the maximum line length, break it up for
# readability.
--outdent-long-comments
# If a quoted string is longer than the maximum line length, do not break it up
# for readability.
--no-outdent-long-quotes
###################################
# Whitespace Control
###################################
# Stack opening braces in order to avoid having a brace sitting by itself on
# a line.
--stack-opening-tokens
# Stack closing tokens in order to avoid having a brace sitting by itself on
# a line.
--stack-closing-tokens
# Spaces between parentheses e.g. if ((my $len_tab = length($tabstr)) > 0) {
--paren-tightness=2
# Spaces between brackets e.g. $width = $col[$j + $k] - $col[$j];
--square-bracket-tightness=2
# Spaces between braces in expression e.g. $obj->{$parsed_sql->{'table'}[0]};
--brace-tightness=2
# Spaces between braches with blocks of code
# e.g. %bf = map { $_ => -M $_ } grep { /\.deb$/ } dirents '.';
--block-brace-tightness=0
# Do not add spaces between semicolons within for loops.
--nospace-for-semicolon
###################################
# Comment Controls
###################################
# Indent comments to be at the same level as the code.
--indent-block-comments
###################################
# Line Break Control
###################################
# The else is on the same line as the brace.
--cuddled-else
# Create a break after -> and period if a break is required.
--want-break-after='-> .'
###################################
# Blank Line Control
###################################
# Do not force blank lines before full line comments.
--noblanks-before-comments
# Do not force blank lines before blocks starting with for, foreach, while,
# until, and if,unless.
--noblanks-before-blocks
###################################
# Vertical Alignment
###################################
# Turn off vertical alignment.
-novalign

View File

@ -1,7 +1,8 @@
#!/usr/bin/perl -w #!/usr/bin/perl -w
# The MIT License # The MIT License
# #
# Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number of other of contributors # Copyright (c) 2004-, Kohsuke Kawaguchi, Sun Microsystems, Inc., and a number
# of other of contributors
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy # Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal # of this software and associated documentation files (the "Software"), to deal
@ -27,19 +28,22 @@
# Perl script to generate missing translation keys and missing properties files, # Perl script to generate missing translation keys and missing properties files,
# to remove unused keys, and to convert utf8 properties files to iso or ascii. # to remove unused keys, and to convert utf8 properties files to iso or ascii.
# #
# 1.- It recursively looks for files in a folder, and analyzes them to extract the # 1.- It recursively looks for files in a folder, and analyzes them to extract
# keys being used in the application. # the keys being used in the application.
# 2.- If --add=true, it generates the appropriate file for the desired language and adds # 2.- If --add=true, it generates the appropriate file for the desired language
# these keys to it, adding the english text as a reference. # and adds these keys to it, adding the english text as a reference. If the
# If the properties file already exists the script update it with the new keys. # properties file already exists the script update it with the new keys.
# 3.- When --remove=true and there are unused keys in our file, the script removes them. # 3.- When --remove=true and there are unused keys in our file, the script
# 4.- If an editor is passed as argument, the script edits each modified file after adding new keys. # removes them.
# 5.- Finally, when --toiso=true or --toascii=true, the script is able to convert utf-8 # 4.- If an editor is passed as argument, the script edits each modified file
# properties files to iso or unicode hex representation is ascii. # after adding new keys.
# 5.- Finally, when --toiso=true or --toascii=true, the script is able to
# convert utf-8 properties files to iso or unicode hex representation is
# ascii.
# #
# Note, while the migration to Jenkins this file will report the keys which should point # Note, while the migration to Jenkins this file will report the keys which
# to Jenkins instead of the old name. # should point to Jenkins instead of the old name.
use warnings; use warnings;
use strict; use strict;
@ -47,18 +51,26 @@ use File::Basename;
use File::Find; use File::Find;
use File::Path; use File::Path;
my ($lang, $editor, $dir, $toiso, $toascii, $add, $remove, $reuse, $counter, $target) = (undef, undef, "./", undef, undef, undef, undef, undef, undef, "./"); my (
my ($tfiles, $tkeys, $tmissing, $tunused, $tempty, $tsame, $tnojenkins, $countervalue) = (0, 0, 0, 0, 0, 0, 0, 1); $lang, $editor, $dir, $toiso, $toascii,
$add, $remove, $reuse, $counter, $target
) = (undef, undef, "./", undef, undef, undef, undef, undef, undef, "./");
my ($tfiles, $tkeys, $tmissing, $tunused, $tempty, $tsame, $tnojenkins,
$countervalue)
= (0, 0, 0, 0, 0, 0, 0, 1);
## read arguments ## read arguments
foreach (@ARGV) { foreach (@ARGV) {
if (/^--lang=(.*)$/) { if (/^--lang=(.*)$/) {
$lang = $1; $lang = $1;
} elsif (/^--editor=(.*)$/) { } elsif (/^--editor=(.*)$/) {
$editor = $1; $add = 1; $editor = $1;
$add = 1;
} elsif (/^--toiso$/ || /^--toiso=true$/) { } elsif (/^--toiso$/ || /^--toiso=true$/) {
$toiso = 1; $toascii = 0; $toiso = 1;
$toascii = 0;
} elsif (/^--toascii$/ || /^--toascii=true$/) { } elsif (/^--toascii$/ || /^--toascii=true$/) {
$toascii = 1; $toiso = 0; $toascii = 1;
$toiso = 0;
} elsif (/^--add$/ || /^--add=true$/) { } elsif (/^--add$/ || /^--add=true$/) {
$add = 1; $add = 1;
} elsif (/^--remove$/ || /^--remove=true$/) { } elsif (/^--remove$/ || /^--remove=true$/) {
@ -70,7 +82,7 @@ foreach (@ARGV) {
} elsif (/^--target=(.*)$/) { } elsif (/^--target=(.*)$/) {
$target = $1; $target = $1;
} else { } else {
$dir=$_; $dir = $_;
} }
} }
@ -83,14 +95,15 @@ if (!$lang || $lang eq "en") {
print STDERR "Finding files ...\n"; print STDERR "Finding files ...\n";
## look for Message.properties and *.jelly files in the provided folder ## look for Message.properties and *.jelly files in the provided folder
my @files = findTranslatableFiles($dir); my @files = findTranslatableFiles($dir);
print STDERR "Found ".(scalar keys @files)." files\n"; print STDERR "Found " . (scalar keys @files) . " files\n";
## load a cache with keys already translated to utilize in the case the same key is used ## load a cache with keys already translated to utilize in the case the same key
## is used
my %cache = loadAllTranslatedKeys($reuse, $lang) if ($reuse && -e $reuse); my %cache = loadAllTranslatedKeys($reuse, $lang) if ($reuse && -e $reuse);
## process each file ## process each file
foreach (@files) { foreach (@files) {
$tfiles ++; $tfiles++;
processFile($_); processFile($_);
} }
@ -105,16 +118,22 @@ my $psame = 0;
my $pnojenkins = 0; my $pnojenkins = 0;
if ($tkeys != 0) { if ($tkeys != 0) {
$pdone = $tdone/$tkeys*100; $pdone = $tdone / $tkeys * 100;
$pmissing = $tmissing/$tkeys*100; $pmissing = $tmissing / $tkeys * 100;
$punused = $tunused/$tkeys*100; $punused = $tunused / $tkeys * 100;
$pempty = $tempty/$tkeys*100; $pempty = $tempty / $tkeys * 100;
$psame = $tsame/$tkeys*100; $psame = $tsame / $tkeys * 100;
$pnojenkins = $tnojenkins/$tkeys*100; $pnojenkins = $tnojenkins / $tkeys * 100;
} }
my @formatParameters = ($tfiles, $tkeys, $tdone, $pdone, $tmissing, $pmissing, $tunused, $punused, $tempty, $pempty, $tsame, $psame, $tnojenkins, $pnojenkins); my @formatParameters = (
printf "\nTOTAL: Files: %d Keys: %d Done: %d(%.2f%%)\n Missing: %d(%.2f%%) Orphan: %d(%.2f%%) Empty: %d(%.2f%%) Same: %d(%.2f%%) NoJenkins: %d(%.2f%%)\n\n", (@formatParameters); $tfiles, $tkeys, $tdone, $pdone, $tmissing,
$pmissing, $tunused, $punused, $tempty, $pempty,
$tsame, $psame, $tnojenkins, $pnojenkins
);
printf
"\nTOTAL: Files: %d Keys: %d Done: %d(%.2f%%)\n Missing: %d(%.2f%%) Orphan: %d(%.2f%%) Empty: %d(%.2f%%) Same: %d(%.2f%%) NoJenkins: %d(%.2f%%)\n\n",
(@formatParameters);
## end ## end
exit(); exit();
@ -129,33 +148,35 @@ sub processFile {
$ofile =~ s/(\.jelly)|(\.properties)/_$lang.properties/; $ofile =~ s/(\.jelly)|(\.properties)/_$lang.properties/;
$efile =~ s/(\.jelly)/.properties/; $efile =~ s/(\.jelly)/.properties/;
# keys -> Hash of keys used in jelly or Message.properties files # keys -> Hash of keys used in jelly or Message.properties files
# ekeys -> Hash of key/values in English # ekeys -> Hash of key/values in English
# okeys -> Hash of key/values in the desired language which are already present in the file # okeys -> Hash of key/values in the desired language which are already
# present in the file
my (%keys, %okeys, %ekeys); my (%keys, %okeys, %ekeys);
# Read .jelly or Message.properties files, and fill a hash with the keys found # Read .jelly or Message.properties files, and fill a hash with the keys
# found
if ($file =~ m/.jelly$/) { if ($file =~ m/.jelly$/) {
%keys = loadJellyFile($file); %keys = loadJellyFile($file);
%ekeys = loadPropertiesFile($efile); %ekeys = loadPropertiesFile($efile);
} else { } else {
%keys = %ekeys = loadPropertiesFile($file) %keys = %ekeys = loadPropertiesFile($file);
} }
# load keys already present in the desired locale # load keys already present in the desired locale
%okeys = loadPropertiesFile($ofile); %okeys = loadPropertiesFile($ofile);
# calculate missing keys in the file # calculate missing keys in the file
my $missing = ""; my $missing = "";
foreach (keys %keys) { foreach (keys %keys) {
$tkeys ++; $tkeys++;
if (!defined($okeys{$_})) { if (!defined($okeys{$_})) {
$_ .= "=" . $cache{$_} if (defined($cache{$_})); $_ .= "=" . $cache{$_} if (defined($cache{$_}));
$missing .= ($add ? " Adding " : " Missing") . " -> $_\n"; $missing .= ($add ? " Adding " : " Missing") . " -> $_\n";
$tmissing ++; $tmissing++;
} elsif ($okeys{$_} eq ''){ } elsif ($okeys{$_} eq '') {
$missing .= " Empty -> $_\n"; $missing .= " Empty -> $_\n";
$tempty ++; $tempty++;
} }
} }
@ -164,7 +185,7 @@ sub processFile {
foreach (keys %okeys) { foreach (keys %okeys) {
if (!defined $keys{$_}) { if (!defined $keys{$_}) {
$unused .= " Unused -> $_\n"; $unused .= " Unused -> $_\n";
$tunused ++; $tunused++;
} }
} }
@ -172,37 +193,38 @@ sub processFile {
my $same = ""; my $same = "";
foreach (keys %okeys) { foreach (keys %okeys) {
if ($okeys{$_} && $ekeys{$_} && $okeys{$_} eq $ekeys{$_}) { if ($okeys{$_} && $ekeys{$_} && $okeys{$_} eq $ekeys{$_}) {
$same .= " Same -> $_\n" ; $same .= " Same -> $_\n";
$tsame ++; $tsame++;
} }
} }
my $nj = ""; my $nj = "";
foreach (keys %okeys) { foreach (keys %okeys) {
if ($okeys{$_} && $okeys{$_} =~ /Hudson/ ) { if ($okeys{$_} && $okeys{$_} =~ /Hudson/) {
$nj .= " Non Jenkins -> $_ -> $okeys{$_}\n" ; $nj .= " Non Jenkins -> $_ -> $okeys{$_}\n";
$tnojenkins ++; $tnojenkins++;
} }
} }
# Show Alerts # Show Alerts
print "\nFile: $ofile\n$missing$unused$same$nj" if ($missing ne "" || $unused ne '' || $same ne '' || $nj ne ''); print "\nFile: $ofile\n$missing$unused$same$nj"
if ($missing ne "" || $unused ne '' || $same ne '' || $nj ne '');
# write new keys in our file adding the English translation as a reference # write new keys in our file adding the English translation as a reference
if ($add && $missing ne "") { if ($add && $missing ne "") {
printLicense($ofile) unless(-f $ofile); printLicense($ofile) unless (-f $ofile);
open(F, ">>$ofile"); open(F, ">>$ofile");
foreach (keys %keys) { foreach (keys %keys) {
if (!$okeys{$_}) { if (!$okeys{$_}) {
if (!defined($okeys{$_})) { if (!defined($okeys{$_})) {
print F "$_="; print F "$_=";
if (defined($cache{$_})) { if (defined($cache{$_})) {
print F $cache{$_}."\n"; print F $cache{$_} . "\n";
} else { } else {
if ($counter) { if ($counter) {
# add unique value for each added translation # add unique value for each added translation
print F "---TranslateMe ".$countervalue."--- ".($ekeys{$_} ? $ekeys{$_} : $_)."\n"; print F "---TranslateMe " . $countervalue . "--- " .
($ekeys{$_} ? $ekeys{$_} : $_) . "\n";
} else { } else {
print F "\n"; print F "\n";
} }
@ -214,15 +236,17 @@ sub processFile {
close(F); close(F);
} }
# open the editor if the user has specified it and there are changes to manage # open the editor if the user has specified it and there are changes to
system("$editor $ofile") if ($editor && $add && ($missing ne "" || $same ne "" || $nj ne '')); # manage
system("$editor $ofile")
if ($editor && $add && ($missing ne "" || $same ne "" || $nj ne ''));
# write new keys in our file adding the English translation as a reference # write new keys in our file adding the English translation as a reference
removeUnusedKeys($ofile, %keys) if ($remove && $unused ne ""); removeUnusedKeys($ofile, %keys) if ($remove && $unused ne "");
# convert the language file to ISO or ASCII which are # convert the language file to ISO or ASCII which are
# the charsets which Jenkins supports right now # the charsets which Jenkins supports right now
convert($ofile, $toiso, $toascii) if ( -f $ofile ); convert($ofile, $toiso, $toascii) if (-f $ofile);
} }
# Create a hash with all keys which exist and have an unique value # Create a hash with all keys which exist and have an unique value
@ -233,7 +257,7 @@ sub loadAllTranslatedKeys {
s/(\.jelly)|(\.properties)/_$lang.properties/; s/(\.jelly)|(\.properties)/_$lang.properties/;
next unless (-f $_); next unless (-f $_);
my (%h, $k, $v) = loadPropertiesFile($_); my (%h, $k, $v) = loadPropertiesFile($_);
while (($k,$v) = each(%h)) { while (($k, $v) = each(%h)) {
$ret{$k} = "" if (defined($ret{$k}) && $v ne $ret{$k}); $ret{$k} = "" if (defined($ret{$k}) && $v ne $ret{$k});
$ret{$k} = $v unless defined($ret{$k}); $ret{$k} = $v unless defined($ret{$k});
} }
@ -246,10 +270,15 @@ sub findTranslatableFiles {
my $dir = shift; my $dir = shift;
die "Folder doesn't exist: $dir\n" unless (-e $dir); die "Folder doesn't exist: $dir\n" unless (-e $dir);
my @ret; my @ret;
find(sub { find(
sub {
my $file = $File::Find::name; my $file = $File::Find::name;
push(@ret, $file) if ($file !~ m#(/src/test/)|(/target/)|(\.svn)# && $file =~ /(Messages.properties)$|(.*\.jelly)$/); push(@ret, $file)
}, $dir); if ($file !~ m#(/src/test/)|(/target/)|(\.svn)#
&& $file =~ /(Messages.properties)$|(.*\.jelly)$/);
},
$dir
);
return @ret; return @ret;
} }
@ -258,10 +287,12 @@ sub loadJellyFile {
my $file = shift; my $file = shift;
my %ret; my %ret;
open(F, $file) || die $! . " " . $file; open(F, $file) || die $! . " " . $file;
while(<F>){ while (<F>) {
next if (! /\$\{.*?\%([^\(]+?).*\}/); next if (!/\$\{.*?\%([^\(]+?).*\}/);
my $line = $_; my $line = $_;
while ($line =~ /^.*?\$\{\%([^\(\}]+)(.*)$/ || $line=~ /^.*?\$\{.*?['"]\%([^\(\}\"\']+)(.*)$/ ) { while ($line =~ /^.*?\$\{\%([^\(\}]+)(.*)$/
|| $line =~ /^.*?\$\{.*?['"]\%([^\(\}\"\']+)(.*)$/)
{
$line = $2; $line = $2;
my $word = $1; my $word = $1;
$word =~ s/\(.+$//g; $word =~ s/\(.+$//g;
@ -271,7 +302,7 @@ sub loadJellyFile {
$word =~ s/\&lt;/</g; $word =~ s/\&lt;/</g;
$word =~ s/\&amp;/&/g; $word =~ s/\&amp;/&/g;
$word =~ s/([#:=])/\\$1/g; $word =~ s/([#:=])/\\$1/g;
$ret{$word}=1; $ret{$word} = 1;
} }
} }
close(F); close(F);
@ -289,7 +320,7 @@ sub loadPropertiesFile {
$ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/); $ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/);
if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) { if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) {
($key, $val) = (trim($1), trim($2)); ($key, $val) = (trim($1), trim($2));
$ret{$key}=$val; $ret{$key} = $val;
} }
$cont = (/\\\s*$/) ? 1 : 0; $cont = (/\\\s*$/) ? 1 : 0;
} }
@ -306,7 +337,7 @@ sub removeUnusedKeys {
my $back = $ofile . "~~"; my $back = $ofile . "~~";
if (rename($ofile, $back) && open(FI, $back) && open(FO, ">$ofile")) { if (rename($ofile, $back) && open(FI, $back) && open(FO, ">$ofile")) {
my $cont = 0; my $cont = 0;
while(<FI>){ while (<FI>) {
if (!$cont) { if (!$cont) {
if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) { if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) {
if (!$keys{$1}) { if (!$keys{$1}) {
@ -316,7 +347,7 @@ sub removeUnusedKeys {
} }
print FO $_; print FO $_;
} elsif ($cont && !/\\\s*$/) { } elsif ($cont && !/\\\s*$/) {
$cont = 0 ; $cont = 0;
} }
} }
close(FI); close(FI);
@ -329,10 +360,11 @@ sub removeUnusedKeys {
sub convert { sub convert {
my ($ofile, $toiso, $toascii) = @_; my ($ofile, $toiso, $toascii) = @_;
if (isUtf8($ofile) && ($toiso || $toascii)) { if (isUtf8($ofile) && ($toiso || $toascii)) {
print "\nConverting file $ofile to " . ($toiso ? "ISO-8859" : "ASCII") . "\n"; print "\nConverting file $ofile to " .
($toiso ? "ISO-8859" : "ASCII") . "\n";
my $back = $ofile . "~~"; my $back = $ofile . "~~";
if (rename($ofile, $back) && open(FI, $back) && open(FO, ">$ofile")) { if (rename($ofile, $back) && open(FI, $back) && open(FO, ">$ofile")) {
while(<FI>) { while (<FI>) {
if ($toiso) { if ($toiso) {
s/([\xC2\xC3])([\x80-\xBF])/chr(ord($1)<<6&0xC0|ord($2)&0x3F)/eg; s/([\xC2\xC3])([\x80-\xBF])/chr(ord($1)<<6&0xC0|ord($2)&0x3F)/eg;
} else { } else {
@ -357,7 +389,7 @@ sub convert {
sub isUtf8 { sub isUtf8 {
my $file = shift; my $file = shift;
if (open(F, $file)) { if (open(F, $file)) {
while(<F>) { while (<F>) {
if (/([\xC2\xC3])([\x80-\xBF])/) { if (/([\xC2\xC3])([\x80-\xBF])/) {
close(F); close(F);
return 1; return 1;
@ -371,13 +403,14 @@ sub isUtf8 {
# print MIT license in new files # print MIT license in new files
# Note: the license is read from the head of this file # Note: the license is read from the head of this file
my $license; my $license;
sub printLicense { sub printLicense {
my $file = shift; my $file = shift;
if (!$license && open(F, $0)) { if (!$license && open(F, $0)) {
$license = ""; $license = "";
my $on = 0; my $on = 0;
while(<F>) { while (<F>) {
$on=1 if (!$on && /The MIT/); $on = 1 if (!$on && /The MIT/);
last if ($on && (/^$/ || /^[^#]/)); last if ($on && (/^$/ || /^[^#]/));
$license .= $_ if ($on); $license .= $_ if ($on);
} }
@ -395,8 +428,7 @@ sub printLicense {
} }
# trim function to remove whitespace from the start and end of the string # trim function to remove whitespace from the start and end of the string
sub trim($) sub trim($) {
{
my $string = shift; my $string = shift;
$string =~ s/^\s+//; $string =~ s/^\s+//;
$string =~ s/\s+$//; $string =~ s/\s+$//;
@ -436,4 +468,3 @@ Usage: $0 --lang=xx [options] [dir]
"; ";
exit(); exit();
} }