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
# 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
# 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,
# 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
# keys being used in the application.
# 2.- If --add=true, it generates the appropriate file for the desired language and adds
# these keys to it, adding the english text as a reference.
# If the 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.
# 4.- If an editor is passed as argument, the script edits each modified file 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.
# 1.- It recursively looks for files in a folder, and analyzes them to extract
# the keys being used in the application.
# 2.- If --add=true, it generates the appropriate file for the desired language
# and adds these keys to it, adding the english text as a reference. If the
# 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.
# 4.- If an editor is passed as argument, the script edits each modified file
# 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
# to Jenkins instead of the old name.
# Note, while the migration to Jenkins this file will report the keys which
# should point to Jenkins instead of the old name.
use warnings;
use strict;
@ -47,18 +51,26 @@ use File::Basename;
use File::Find;
use File::Path;
my ($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);
my (
$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
foreach (@ARGV) {
if (/^--lang=(.*)$/) {
$lang = $1;
} elsif (/^--editor=(.*)$/) {
$editor = $1; $add = 1;
$editor = $1;
$add = 1;
} elsif (/^--toiso$/ || /^--toiso=true$/) {
$toiso = 1; $toascii = 0;
$toiso = 1;
$toascii = 0;
} elsif (/^--toascii$/ || /^--toascii=true$/) {
$toascii = 1; $toiso = 0;
$toascii = 1;
$toiso = 0;
} elsif (/^--add$/ || /^--add=true$/) {
$add = 1;
} elsif (/^--remove$/ || /^--remove=true$/) {
@ -70,7 +82,7 @@ foreach (@ARGV) {
} elsif (/^--target=(.*)$/) {
$target = $1;
} else {
$dir=$_;
$dir = $_;
}
}
@ -83,14 +95,15 @@ if (!$lang || $lang eq "en") {
print STDERR "Finding files ...\n";
## look for Message.properties and *.jelly files in the provided folder
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);
## process each file
foreach (@files) {
$tfiles ++;
$tfiles++;
processFile($_);
}
@ -105,16 +118,22 @@ my $psame = 0;
my $pnojenkins = 0;
if ($tkeys != 0) {
$pdone = $tdone/$tkeys*100;
$pmissing = $tmissing/$tkeys*100;
$punused = $tunused/$tkeys*100;
$pempty = $tempty/$tkeys*100;
$psame = $tsame/$tkeys*100;
$pnojenkins = $tnojenkins/$tkeys*100;
$pdone = $tdone / $tkeys * 100;
$pmissing = $tmissing / $tkeys * 100;
$punused = $tunused / $tkeys * 100;
$pempty = $tempty / $tkeys * 100;
$psame = $tsame / $tkeys * 100;
$pnojenkins = $tnojenkins / $tkeys * 100;
}
my @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);
my @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
exit();
@ -129,33 +148,35 @@ sub processFile {
$ofile =~ s/(\.jelly)|(\.properties)/_$lang.properties/;
$efile =~ s/(\.jelly)/.properties/;
# keys -> Hash of keys used in jelly or Message.properties files
# ekeys -> Hash of key/values in English
# okeys -> Hash of key/values in the desired language which are already present in the file
# keys -> Hash of keys used in jelly or Message.properties files
# ekeys -> Hash of key/values in English
# okeys -> Hash of key/values in the desired language which are already
# present in the file
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$/) {
%keys = loadJellyFile($file);
%keys = loadJellyFile($file);
%ekeys = loadPropertiesFile($efile);
} else {
%keys = %ekeys = loadPropertiesFile($file)
%keys = %ekeys = loadPropertiesFile($file);
}
# load keys already present in the desired locale
%okeys = loadPropertiesFile($ofile);
%okeys = loadPropertiesFile($ofile);
# calculate missing keys in the file
my $missing = "";
foreach (keys %keys) {
$tkeys ++;
$tkeys++;
if (!defined($okeys{$_})) {
$_ .= "=" . $cache{$_} if (defined($cache{$_}));
$_ .= "=" . $cache{$_} if (defined($cache{$_}));
$missing .= ($add ? " Adding " : " Missing") . " -> $_\n";
$tmissing ++;
} elsif ($okeys{$_} eq ''){
$tmissing++;
} elsif ($okeys{$_} eq '') {
$missing .= " Empty -> $_\n";
$tempty ++;
$tempty++;
}
}
@ -164,7 +185,7 @@ sub processFile {
foreach (keys %okeys) {
if (!defined $keys{$_}) {
$unused .= " Unused -> $_\n";
$tunused ++;
$tunused++;
}
}
@ -172,37 +193,38 @@ sub processFile {
my $same = "";
foreach (keys %okeys) {
if ($okeys{$_} && $ekeys{$_} && $okeys{$_} eq $ekeys{$_}) {
$same .= " Same -> $_\n" ;
$tsame ++;
$same .= " Same -> $_\n";
$tsame++;
}
}
my $nj = "";
foreach (keys %okeys) {
if ($okeys{$_} && $okeys{$_} =~ /Hudson/ ) {
$nj .= " Non Jenkins -> $_ -> $okeys{$_}\n" ;
$tnojenkins ++;
if ($okeys{$_} && $okeys{$_} =~ /Hudson/) {
$nj .= " Non Jenkins -> $_ -> $okeys{$_}\n";
$tnojenkins++;
}
}
# 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
if ($add && $missing ne "") {
printLicense($ofile) unless(-f $ofile);
printLicense($ofile) unless (-f $ofile);
open(F, ">>$ofile");
foreach (keys %keys) {
if (!$okeys{$_}) {
if (!defined($okeys{$_})) {
print F "$_=";
if (defined($cache{$_})) {
print F $cache{$_}."\n";
print F $cache{$_} . "\n";
} else {
if ($counter) {
# add unique value for each added translation
print F "---TranslateMe ".$countervalue."--- ".($ekeys{$_} ? $ekeys{$_} : $_)."\n";
print F "---TranslateMe " . $countervalue . "--- " .
($ekeys{$_} ? $ekeys{$_} : $_) . "\n";
} else {
print F "\n";
}
@ -214,15 +236,17 @@ sub processFile {
close(F);
}
# open the editor if the user has specified it and there are changes to manage
system("$editor $ofile") if ($editor && $add && ($missing ne "" || $same ne "" || $nj ne ''));
# open the editor if the user has specified it and there are changes to
# 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
removeUnusedKeys($ofile, %keys) if ($remove && $unused ne "");
# convert the language file to ISO or ASCII which are
# 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
@ -233,7 +257,7 @@ sub loadAllTranslatedKeys {
s/(\.jelly)|(\.properties)/_$lang.properties/;
next unless (-f $_);
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} = $v unless defined($ret{$k});
}
@ -246,10 +270,15 @@ sub findTranslatableFiles {
my $dir = shift;
die "Folder doesn't exist: $dir\n" unless (-e $dir);
my @ret;
find(sub {
find(
sub {
my $file = $File::Find::name;
push(@ret, $file) if ($file !~ m#(/src/test/)|(/target/)|(\.svn)# && $file =~ /(Messages.properties)$|(.*\.jelly)$/);
}, $dir);
push(@ret, $file)
if ($file !~ m#(/src/test/)|(/target/)|(\.svn)#
&& $file =~ /(Messages.properties)$|(.*\.jelly)$/);
},
$dir
);
return @ret;
}
@ -258,10 +287,12 @@ sub loadJellyFile {
my $file = shift;
my %ret;
open(F, $file) || die $! . " " . $file;
while(<F>){
next if (! /\$\{.*?\%([^\(]+?).*\}/);
while (<F>) {
next if (!/\$\{.*?\%([^\(]+?).*\}/);
my $line = $_;
while ($line =~ /^.*?\$\{\%([^\(\}]+)(.*)$/ || $line=~ /^.*?\$\{.*?['"]\%([^\(\}\"\']+)(.*)$/ ) {
while ($line =~ /^.*?\$\{\%([^\(\}]+)(.*)$/
|| $line =~ /^.*?\$\{.*?['"]\%([^\(\}\"\']+)(.*)$/)
{
$line = $2;
my $word = $1;
$word =~ s/\(.+$//g;
@ -271,7 +302,7 @@ sub loadJellyFile {
$word =~ s/\&lt;/</g;
$word =~ s/\&amp;/&/g;
$word =~ s/([#:=])/\\$1/g;
$ret{$word}=1;
$ret{$word} = 1;
}
}
close(F);
@ -289,7 +320,7 @@ sub loadPropertiesFile {
$ret{$key} .= "\n$1" if ($cont && /\s*(.*)[\\\s]*$/);
if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) {
($key, $val) = (trim($1), trim($2));
$ret{$key}=$val;
$ret{$key} = $val;
}
$cont = (/\\\s*$/) ? 1 : 0;
}
@ -306,7 +337,7 @@ sub removeUnusedKeys {
my $back = $ofile . "~~";
if (rename($ofile, $back) && open(FI, $back) && open(FO, ">$ofile")) {
my $cont = 0;
while(<FI>){
while (<FI>) {
if (!$cont) {
if (/^([^#\s].*?[^\\])=(.*)[\s\\]*$/) {
if (!$keys{$1}) {
@ -316,7 +347,7 @@ sub removeUnusedKeys {
}
print FO $_;
} elsif ($cont && !/\\\s*$/) {
$cont = 0 ;
$cont = 0;
}
}
close(FI);
@ -329,10 +360,11 @@ sub removeUnusedKeys {
sub convert {
my ($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 . "~~";
if (rename($ofile, $back) && open(FI, $back) && open(FO, ">$ofile")) {
while(<FI>) {
while (<FI>) {
if ($toiso) {
s/([\xC2\xC3])([\x80-\xBF])/chr(ord($1)<<6&0xC0|ord($2)&0x3F)/eg;
} else {
@ -357,7 +389,7 @@ sub convert {
sub isUtf8 {
my $file = shift;
if (open(F, $file)) {
while(<F>) {
while (<F>) {
if (/([\xC2\xC3])([\x80-\xBF])/) {
close(F);
return 1;
@ -371,13 +403,14 @@ sub isUtf8 {
# print MIT license in new files
# Note: the license is read from the head of this file
my $license;
sub printLicense {
my $file = shift;
if (!$license && open(F, $0)) {
$license = "";
my $on = 0;
while(<F>) {
$on=1 if (!$on && /The MIT/);
while (<F>) {
$on = 1 if (!$on && /The MIT/);
last if ($on && (/^$/ || /^[^#]/));
$license .= $_ if ($on);
}
@ -395,8 +428,7 @@ sub printLicense {
}
# trim function to remove whitespace from the start and end of the string
sub trim($)
{
sub trim($) {
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
@ -436,4 +468,3 @@ Usage: $0 --lang=xx [options] [dir]
";
exit();
}