Wed, 07 Mar 2012 13:45:18 -0800
Merge
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/make/scripts/normalizer.pl Wed Mar 07 13:45:18 2012 -0800 1.3 @@ -0,0 +1,208 @@ 1.4 +#!/usr/bin/perl 1.5 + 1.6 +# 1.7 +# Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. 1.8 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.9 +# 1.10 +# This code is free software; you can redistribute it and/or modify it 1.11 +# under the terms of the GNU General Public License version 2 only, as 1.12 +# published by the Free Software Foundation. 1.13 +# 1.14 +# This code is distributed in the hope that it will be useful, but WITHOUT 1.15 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 +# version 2 for more details (a copy is included in the LICENSE file that 1.18 +# accompanied this code). 1.19 +# 1.20 +# You should have received a copy of the GNU General Public License version 1.21 +# 2 along with this work; if not, write to the Free Software Foundation, 1.22 +# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 +# 1.24 +# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 +# or visit www.oracle.com if you need additional information or have any 1.26 +# questions. 1.27 +# 1.28 + 1.29 +# 1.30 +# Parses java files: 1.31 +# 1. Removes from the end of lines spaces and TABs 1.32 +# 2. Replaces TABs by spaces 1.33 +# 3. Replaces all NewLine separators by Unix NewLine separators 1.34 +# 4. Makes one and only one empty line at the end of each file 1.35 + 1.36 +if ($#ARGV < 0) { 1.37 + &usage; 1.38 + 1.39 + die; 1.40 +} 1.41 + 1.42 +use Cwd 'abs_path'; 1.43 + 1.44 +my @extensions = ("java"); 1.45 + 1.46 +# Read options 1.47 +my $dirpos = 0; 1.48 + 1.49 +while ($dirpos < $#ARGV) { 1.50 + if ($ARGV[$dirpos] eq "-e") { 1.51 + @extensions = split(/,/, $ARGV[$dirpos + 1]); 1.52 + } else { 1.53 + last; 1.54 + } 1.55 + 1.56 + $dirpos += 2; 1.57 +} 1.58 + 1.59 +if ($dirpos > $#ARGV) { 1.60 + &usage; 1.61 + 1.62 + die; 1.63 +} 1.64 + 1.65 +use Cwd; 1.66 +my $currdir = getcwd; 1.67 + 1.68 +my $allfiles = 0; 1.69 + 1.70 +my $filecount = 0; 1.71 + 1.72 +my @tabvalues; 1.73 + 1.74 +# Init tabvalues 1.75 +push (@tabvalues, " "); 1.76 + 1.77 +for (my $i = 1; $i < 8; $i++) { 1.78 + push(@tabvalues, $tabvalues[$i - 1] . " "); 1.79 +} 1.80 + 1.81 +open(FILELIST, ">$currdir/filelist") or die "Failed while open $currdir/filelist: $!\n"; 1.82 + 1.83 +while ($dirpos <= $#ARGV) { 1.84 + use File::Find; 1.85 + 1.86 + find(\&parse_file, abs_path($ARGV[$dirpos])); 1.87 + 1.88 + $dirpos += 1; 1.89 +} 1.90 + 1.91 +close(FILELIST); 1.92 + 1.93 +use Cwd 'chdir'; 1.94 +chdir $currdir; 1.95 + 1.96 +print "Checked $allfiles file(s)\n"; 1.97 +print "Modified $filecount file(s)\n"; 1.98 +print "See results in the file $currdir/filelist\n"; 1.99 + 1.100 +sub parse_file { 1.101 + my $filename = $File::Find::name; 1.102 + 1.103 + # Skip directories 1.104 + return if -d; 1.105 + 1.106 + # Skip SCCS files 1.107 + return if ($filename =~ /\/SCCS\//); 1.108 + 1.109 + # Skip files with invalid extensions 1.110 + my $accepted = 0; 1.111 + foreach my $ext (@extensions) { 1.112 + if ($_ =~ /\.$ext$/i) { 1.113 + $accepted = 1; 1.114 + 1.115 + last; 1.116 + } 1.117 + } 1.118 + return if ($accepted == 0); 1.119 + 1.120 + use File::Basename; 1.121 + my $dirname = dirname($filename); 1.122 + 1.123 + use Cwd 'chdir'; 1.124 + chdir $dirname; 1.125 + 1.126 + open(FILE, $filename) or die "Failed while open $filename: $!\n"; 1.127 + 1.128 + # Read file 1.129 + my @content; 1.130 + my $line; 1.131 + my $emptylinescount = 0; 1.132 + my $modified = 0; 1.133 + 1.134 + while ($line = <FILE>) { 1.135 + my $originalline = $line; 1.136 + 1.137 + # Process line 1.138 + 1.139 + # Remove from the end of the line spaces and return character 1.140 + while ($line =~ /\s$/) { 1.141 + chop($line); 1.142 + } 1.143 + 1.144 + # Replace TABs 1.145 + for (my $i = 0; $i < length($line); $i++) { 1.146 + if (substr($line, $i, 1) =~ /\t/) { 1.147 + $line = substr($line, 0, $i) . $tabvalues[7 - ($i % 8)] . substr($line, $i + 1); 1.148 + } 1.149 + } 1.150 + 1.151 + if (length($line) == 0) { 1.152 + $emptylinescount++; 1.153 + } else { 1.154 + while ($emptylinescount > 0) { 1.155 + push(@content, ""); 1.156 + 1.157 + $emptylinescount--; 1.158 + } 1.159 + 1.160 + push(@content, $line); 1.161 + } 1.162 + 1.163 + if ($originalline ne ($line . "\n")) { 1.164 + $modified = 1; 1.165 + } 1.166 + 1.167 + } 1.168 + 1.169 + $allfiles++; 1.170 + 1.171 + if ($emptylinescount > 0) { 1.172 + $modified = 1; 1.173 + } 1.174 + 1.175 + close(FILE); 1.176 + 1.177 + if ($modified != 0) { 1.178 + # Write file 1.179 + open(FILE, ">$filename") or die "Failed while open $filename: $!\n"; 1.180 + 1.181 + for (my $i = 0; $i <= $#content; $i++) { 1.182 + print FILE "$content[$i]\n"; 1.183 + } 1.184 + 1.185 + close(FILE); 1.186 + 1.187 + # Print name from current dir 1.188 + if (index($filename, $currdir) == 0) { 1.189 + print FILELIST substr($filename, length($currdir) + 1); 1.190 + } else { 1.191 + print FILELIST $filename; 1.192 + } 1.193 + print FILELIST "\n"; 1.194 + 1.195 + $filecount++; 1.196 + 1.197 + print "$filename: modified\n"; 1.198 + } 1.199 +} 1.200 + 1.201 +sub usage { 1.202 + print "Usage:\n"; 1.203 + print " normalizer.pl [-options] <dir> [dir2 dir3 ...]\n"; 1.204 + print " Available options:\n"; 1.205 + print " -e comma separated files extensions. By default accepts only java files\n"; 1.206 + print "\n"; 1.207 + print "Examples:\n"; 1.208 + print " normalizer.pl -e c,cpp,h,hpp .\n"; 1.209 +} 1.210 + 1.211 +