Hi,

I tried to write a little script that checks for the copyright notice and updates it if necessary. THere are some comments on what should be done (SVN date comparing and stuff.

hope this helps,
kjs
#
# Start of a script to recursively process all files in a directory to change 
the copyright notice.
#
use File::Find;
use strict;
use warnings;

sub process_file {
                my $oldValue = 2006; #the minimal value for a year to be 
replaced
                my $newValue = 2007; #the new year!
    my $startYear = 2000; # Parrot start year
    my $shouldChange = 0;  # flag only set when file was changed in a year 
later than it's copyright notice year.
    
    my $file = $_;
    
    # only handle files, not directories
    return unless -f $file;
    
    # only handle text files
    return unless -T $file;
    
    # only handle file if it is writable
    return unless -w $file;
    
    # open file for reading and writing
    open(FILE, "+< $file") or die "can't open file $File::Find::name: $!";
        
    # get last date the file was changed according to svn:
    #
    # TODO:
    # Get SVN info on the file, get its "Last Changed Date" year ("LCD")
    # Then, compare this year with the file's current copyright date.
    # If LCD > CURRENT, adjust the file.
    # 
    # can't get it to work right now, on windows. probably the "/" vs "\" thing.
    print "Getting SVN info from $File::Find::name...\n";
    my $str = "svn info $File::Find::name"; 
    print "Executing:\n$str";
    # Maybe use<allison> 
    # kjs: may make sense to just use 'capture_output' from 
lib/Parrot/Configure/Step.pm
    # (instead of system)
    system($str);
    #
    #
    # set a flag if necessary "$shouldChange"
    #
    
    # buffer to store file contents
    my $filebuffer = '';   
    
    # read file line by line
    while(<FILE>) {
        
        # flag indicating whether the file has been changed yet
        my $changed = 0;
        
        # store the year to be replaced in a temp, it'll be decremented if it's 
not found 
        # so, if 2006 is not found, it'll look for 2005, 2004 etc.      
        my $old = $oldValue;
        
        # look in the file as long as it's not changed, and the year to be 
changed $startYear
        while($changed == 0 && $old >= $startYear) {
                
                # check for copyright lines containing only 1 year
                if ($_ =~ m/.*[cC]opyright.*\ $old.*/ ) { # check for match
                        print "Changing: '$_' \ninto: \n";
                        
                        # change a notice like '2006' into '2006-2007'
                        $_ =~ s/$old/$old\-$newValue/;
                        
                        print $_;
                        print "\n";
                        
                        # add line to file buffer
                        $filebuffer .= $_;              
                        
                        # set changed flag
                        $changed = 1;
                }    
                # check for copyright lines like X-Y                    
                elsif ($_ =~ m/.*[cC]opyright.*\-$old.*/ ) {
                        print "Changing: '$_' \ninto: \n";
                        
                        # make sure only the 'Y', not 'X', in 'X-Y' is changed
                        $_ =~ s/\-$old/\-$newValue/;
                        
                        print $_;
                        print "\n";
                        $filebuffer .= $_;
                        $changed = 1;                           
                }
                # try a lower value of $oldvalue, so an earlier year            
        
                $old--;                                                 
        }
        
        # if the line was not changed, it was not a copyright notice.
        # add it to the buffer.
        if($changed == 0) {
                $filebuffer .= $_;
        }
    }
        
        # end of file reached, now write the buffer back to file again.
        # this code is taken from the Perl Cookbook.
        seek(FILE, 0, 0)               or die "can't seek to start of $file: 
$!";
                print FILE $filebuffer         or die "can't print to $file: 
$!";        
                truncate(FILE, tell(FILE))     or die "can't truncate $file: 
$!";        
                close(FILE)                    or die "can't close $file: $!";  
         
    
    print "Handled file $file\n";
    
    
}

if (@ARGV < 1) {
        print <<'USAGE' 

Usage: perl changeCopyright <directory>

        Updates the copyright notice to 2007 for all files in <directory>.
        
USAGE

}
else {
        my @DIRLIST = ();
        push @DIRLIST, $ARGV[0];
        find(\&process_file, @DIRLIST);
}


# TODO: compile a list of files that has been changed by this script. 

Reply via email to