make/scripts/normalizer.pl

Mon, 14 Sep 2020 16:42:03 +0100

author
andrew
date
Mon, 14 Sep 2020 16:42:03 +0100
changeset 2554
7f60c2d9823e
parent 0
75a576e87639
permissions
-rw-r--r--

Added tag jdk8u272-b08 for changeset 34c6baf21464

aoqi@0 1 #!/usr/bin/perl
aoqi@0 2
aoqi@0 3 #
aoqi@0 4 # Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 6 #
aoqi@0 7 # This code is free software; you can redistribute it and/or modify it
aoqi@0 8 # under the terms of the GNU General Public License version 2 only, as
aoqi@0 9 # published by the Free Software Foundation.
aoqi@0 10 #
aoqi@0 11 # This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 # version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 # accompanied this code).
aoqi@0 16 #
aoqi@0 17 # You should have received a copy of the GNU General Public License version
aoqi@0 18 # 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 #
aoqi@0 21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 # or visit www.oracle.com if you need additional information or have any
aoqi@0 23 # questions.
aoqi@0 24 #
aoqi@0 25
aoqi@0 26 #
aoqi@0 27 # Parses java files:
aoqi@0 28 # 1. Removes from the end of lines spaces and TABs
aoqi@0 29 # 2. Replaces TABs by spaces
aoqi@0 30 # 3. Replaces all NewLine separators by Unix NewLine separators
aoqi@0 31 # 4. Makes one and only one empty line at the end of each file
aoqi@0 32
aoqi@0 33 if ($#ARGV < 0) {
aoqi@0 34 &usage;
aoqi@0 35
aoqi@0 36 die;
aoqi@0 37 }
aoqi@0 38
aoqi@0 39 use Cwd 'abs_path';
aoqi@0 40
aoqi@0 41 my @extensions = ("java");
aoqi@0 42
aoqi@0 43 # Read options
aoqi@0 44 my $dirpos = 0;
aoqi@0 45
aoqi@0 46 while ($dirpos < $#ARGV) {
aoqi@0 47 if ($ARGV[$dirpos] eq "-e") {
aoqi@0 48 @extensions = split(/,/, $ARGV[$dirpos + 1]);
aoqi@0 49 } else {
aoqi@0 50 last;
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 $dirpos += 2;
aoqi@0 54 }
aoqi@0 55
aoqi@0 56 if ($dirpos > $#ARGV) {
aoqi@0 57 &usage;
aoqi@0 58
aoqi@0 59 die;
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 use Cwd;
aoqi@0 63 my $currdir = getcwd;
aoqi@0 64
aoqi@0 65 my $allfiles = 0;
aoqi@0 66
aoqi@0 67 my $filecount = 0;
aoqi@0 68
aoqi@0 69 my @tabvalues;
aoqi@0 70
aoqi@0 71 # Init tabvalues
aoqi@0 72 push (@tabvalues, " ");
aoqi@0 73
aoqi@0 74 for (my $i = 1; $i < 8; $i++) {
aoqi@0 75 push(@tabvalues, $tabvalues[$i - 1] . " ");
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 open(FILELIST, ">$currdir/filelist") or die "Failed while open $currdir/filelist: $!\n";
aoqi@0 79
aoqi@0 80 while ($dirpos <= $#ARGV) {
aoqi@0 81 use File::Find;
aoqi@0 82
aoqi@0 83 find(\&parse_file, abs_path($ARGV[$dirpos]));
aoqi@0 84
aoqi@0 85 $dirpos += 1;
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 close(FILELIST);
aoqi@0 89
aoqi@0 90 use Cwd 'chdir';
aoqi@0 91 chdir $currdir;
aoqi@0 92
aoqi@0 93 print "Checked $allfiles file(s)\n";
aoqi@0 94 print "Modified $filecount file(s)\n";
aoqi@0 95 print "See results in the file $currdir/filelist\n";
aoqi@0 96
aoqi@0 97 sub parse_file {
aoqi@0 98 my $filename = $File::Find::name;
aoqi@0 99
aoqi@0 100 # Skip directories
aoqi@0 101 return if -d;
aoqi@0 102
aoqi@0 103 # Skip SCCS files
aoqi@0 104 return if ($filename =~ /\/SCCS\//);
aoqi@0 105
aoqi@0 106 # Skip files with invalid extensions
aoqi@0 107 my $accepted = 0;
aoqi@0 108 foreach my $ext (@extensions) {
aoqi@0 109 if ($_ =~ /\.$ext$/i) {
aoqi@0 110 $accepted = 1;
aoqi@0 111
aoqi@0 112 last;
aoqi@0 113 }
aoqi@0 114 }
aoqi@0 115 return if ($accepted == 0);
aoqi@0 116
aoqi@0 117 use File::Basename;
aoqi@0 118 my $dirname = dirname($filename);
aoqi@0 119
aoqi@0 120 use Cwd 'chdir';
aoqi@0 121 chdir $dirname;
aoqi@0 122
aoqi@0 123 open(FILE, $filename) or die "Failed while open $filename: $!\n";
aoqi@0 124
aoqi@0 125 # Read file
aoqi@0 126 my @content;
aoqi@0 127 my $line;
aoqi@0 128 my $emptylinescount = 0;
aoqi@0 129 my $modified = 0;
aoqi@0 130
aoqi@0 131 while ($line = <FILE>) {
aoqi@0 132 my $originalline = $line;
aoqi@0 133
aoqi@0 134 # Process line
aoqi@0 135
aoqi@0 136 # Remove from the end of the line spaces and return character
aoqi@0 137 while ($line =~ /\s$/) {
aoqi@0 138 chop($line);
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 # Replace TABs
aoqi@0 142 for (my $i = 0; $i < length($line); $i++) {
aoqi@0 143 if (substr($line, $i, 1) =~ /\t/) {
aoqi@0 144 $line = substr($line, 0, $i) . $tabvalues[7 - ($i % 8)] . substr($line, $i + 1);
aoqi@0 145 }
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 if (length($line) == 0) {
aoqi@0 149 $emptylinescount++;
aoqi@0 150 } else {
aoqi@0 151 while ($emptylinescount > 0) {
aoqi@0 152 push(@content, "");
aoqi@0 153
aoqi@0 154 $emptylinescount--;
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 push(@content, $line);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 if ($originalline ne ($line . "\n")) {
aoqi@0 161 $modified = 1;
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 $allfiles++;
aoqi@0 167
aoqi@0 168 if ($emptylinescount > 0) {
aoqi@0 169 $modified = 1;
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 close(FILE);
aoqi@0 173
aoqi@0 174 if ($modified != 0) {
aoqi@0 175 # Write file
aoqi@0 176 open(FILE, ">$filename") or die "Failed while open $filename: $!\n";
aoqi@0 177
aoqi@0 178 for (my $i = 0; $i <= $#content; $i++) {
aoqi@0 179 print FILE "$content[$i]\n";
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 close(FILE);
aoqi@0 183
aoqi@0 184 # Print name from current dir
aoqi@0 185 if (index($filename, $currdir) == 0) {
aoqi@0 186 print FILELIST substr($filename, length($currdir) + 1);
aoqi@0 187 } else {
aoqi@0 188 print FILELIST $filename;
aoqi@0 189 }
aoqi@0 190 print FILELIST "\n";
aoqi@0 191
aoqi@0 192 $filecount++;
aoqi@0 193
aoqi@0 194 print "$filename: modified\n";
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 sub usage {
aoqi@0 199 print "Usage:\n";
aoqi@0 200 print " normalizer.pl [-options] <dir> [dir2 dir3 ...]\n";
aoqi@0 201 print " Available options:\n";
aoqi@0 202 print " -e comma separated files extensions. By default accepts only java files\n";
aoqi@0 203 print "\n";
aoqi@0 204 print "Examples:\n";
aoqi@0 205 print " normalizer.pl -e c,cpp,h,hpp .\n";
aoqi@0 206 }
aoqi@0 207
aoqi@0 208

mercurial