tools/fshell_copyright.pl
changeset 0 7f656887cf89
child 8 eb5f524dc31d
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 #!/usr/bin/perl
       
     2 # fshell_copyright.pl
       
     3 # 
       
     4 # Copyright (c) 2010 Accenture. All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of the "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 # 
       
    10 # Initial Contributors:
       
    11 # Accenture - Initial contribution
       
    12 #
       
    13 
       
    14 use strict;
       
    15 use File::Copy;
       
    16 use File::Path;
       
    17 
       
    18 my $debug = 0;
       
    19 
       
    20 my $notice = << 'ENDTEXT';
       
    21 // FILENAME
       
    22 // 
       
    23 // Copyright (c) COPYRIGHTYEAR Accenture. All rights reserved.
       
    24 // This component and the accompanying materials are made available
       
    25 // under the terms of the "Eclipse Public License v1.0"
       
    26 // which accompanies this distribution, and is available
       
    27 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    28 // 
       
    29 // Initial Contributors:
       
    30 // Accenture - Initial contribution
       
    31 //
       
    32 ENDTEXT
       
    33 
       
    34 my @errorfiles = ();
       
    35 
       
    36 sub update_copyright($$)
       
    37   {
       
    38   my ($file, $name) = @_;
       
    39   
       
    40   open FILE, "<$file" or print "ERROR: Cannot open $file: $!\n" and return "Cannot open";
       
    41   if ($debug) { print "Processing $file\n"; }
       
    42   my @lines = <FILE>;
       
    43   close FILE;
       
    44   
       
    45   my $start = 1;
       
    46   my $copyrightyear = "2010";
       
    47   my $updated = 0;
       
    48   my @newlines = ();
       
    49   
       
    50   my $commentstart = "//";
       
    51   if ($name =~ /\.(pl|pm|script|pod|ini|idf|esk|mk|cif)/i || !($name =~ /\./))
       
    52 	{
       
    53 	# Need to use # instead of //
       
    54 	# Second part of conditional is for perl scripts that don't specify any ending
       
    55 	$commentstart = "#";
       
    56 	}
       
    57   elsif ($name =~ /\.(bat)/)
       
    58 	{
       
    59 	$commentstart = '@REM';
       
    60 	}
       
    61   elsif ($name =~ /\.(pkg)/)
       
    62 	{
       
    63 	$commentstart = ";";
       
    64 	}
       
    65 	
       
    66   while (my $line = shift @lines)
       
    67     { 
       
    68 	if ($start == 1)
       
    69 		{
       
    70 		if ($line =~ /^$commentstart Copyright ?\(c\) [a-zA-Z .]*(20..|19..)/)
       
    71 			{
       
    72 			# Some evil things put Copyright Symbian Software Ltd 2009 rather than putting the date first. The [a-zA-Z] bit is to skip that without causing a longest prefix match to pick up the end date instead of the start
       
    73 			$copyrightyear = $1;
       
    74 			
       
    75 			if ($line =~ /Nokia Corporation/)
       
    76 				{
       
    77 				# Leave well alone, not ours to change the headers on
       
    78 				push @newlines, $line;
       
    79 				$start = 0;
       
    80 				}
       
    81 			}
       
    82 		elsif ($line =~ /^#!/)
       
    83 			{
       
    84 			# Leave shebang lines alone!
       
    85 			push @newlines, $line;
       
    86 			}
       
    87 		elsif ($line =~ /^$commentstart/)
       
    88 			{
       
    89 			# Comment we're not interested in
       
    90 			}
       
    91 		else
       
    92 			{
       
    93 			# Reached the end of initial comment
       
    94 			my $filecopyrightnotice = $notice;
       
    95 			if ($copyrightyear ne "2010") { $copyrightyear = "$copyrightyear - 2010"; }
       
    96 			$filecopyrightnotice =~ s/\/\//$commentstart/g; # Change '//' to '#' if needed
       
    97 			$filecopyrightnotice =~ s/http:$commentstart/http:\/\//g; # I don't know why putting a ^ before the above pattern doesn't work, my brain is hurting
       
    98 			$filecopyrightnotice =~ s/COPYRIGHTYEAR/$copyrightyear/;
       
    99 			$filecopyrightnotice =~ s/FILENAME/$name/;
       
   100 
       
   101 			push @newlines, $filecopyrightnotice;
       
   102 			push @newlines, $line;
       
   103 			$start = 0;
       
   104 			}
       
   105 		next;
       
   106 		}
       
   107     push @newlines, $line;
       
   108     }
       
   109 
       
   110   #return if (!$updated);
       
   111 
       
   112  
       
   113   
       
   114   open NEWFILE, ">$file" or die("Cannot overwrite $file: $!\n");
       
   115   print NEWFILE @newlines, @lines;
       
   116   close NEWFILE or die("Failed to update $file: $!\n");
       
   117   print "* updated $file\n";
       
   118   }
       
   119 
       
   120 # Process tree
       
   121 
       
   122 sub scan_directory($)
       
   123   {
       
   124   my ($path) = @_;
       
   125   
       
   126   return if $path =~ /\.hg/ ; # Don't go inside mercurial dir
       
   127 
       
   128   opendir DIR, $path;
       
   129   my @files = grep !/^\.\.?$/, readdir DIR;
       
   130   closedir DIR;
       
   131   
       
   132   foreach my $file (@files)
       
   133     {
       
   134     my $newpath = "$path/$file";
       
   135     
       
   136     if (-d $newpath)
       
   137       {
       
   138       scan_directory($newpath);
       
   139       next;
       
   140       }
       
   141     next if (-B $newpath);  # ignore binary files
       
   142 	next if ($newpath =~ /\.(pod|def|txt|src|index|clw|dsp|dsw|plg|rc|svg|css|esk)$/i); # ignore stuff that we're not copyrighting
       
   143     
       
   144     update_copyright($newpath, $file);
       
   145     }
       
   146   }
       
   147 
       
   148 scan_directory(".");
       
   149 
       
   150 printf "%d problem files\n", scalar @errorfiles;
       
   151 print "\t", join("\n\t", @errorfiles), "\n";
       
   152