make/scripts/normalizer.pl

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

mercurial