Skip to content
Snippets Groups Projects
CopyRight 3.64 KiB
Newer Older
mh17's avatar
mh17 committed
#!/usr/bin/perl
# update copyright messgage in source file, sandwiched by CVS operations
# *Please* do this with no files active from cvs and a clean build expected
# mh17 03 Mar 2010
# Copyright(c) 2010 Genome Research Limited
# test data in CopyRight.txt, copy to a header first the run over it (the other file)
# to use on ZMap try:
#
#     find ~/zmap/ZMap/src -name "*.c" -exec ./CopyRight \{\} \;
#     find ~/zmap/ZMap/src -name "*.h" -exec ./CopyRight \{\} \;
#

use warnings;

my ($file, $date_found, $date_changed,$file_date,$new_date);
my ($ofile);
my ($year_now);
my $CVS = 1;      # use the CVS
my $many = 0;     # change more than one: good for testing

sub change_date
{
      for(@_)     # process our one argument directly
      {
                  # our copyright?  let's enforce nice capitalised words
            if(/Copyright/ && /Genome/)
            {
                  if(m/(\d+)[- ]+(\d+)/)
                  {
                        $date_found = 1;
                        $file_date = $1. '-' .$2;

mh17's avatar
mh17 committed
                        if($1 != 2006 || $2 != $year_now)
mh17's avatar
mh17 committed
                        {
mh17's avatar
mh17 committed
                              $new_date = "2006-$year_now";
                              s/(\d+)[- ]+(\d+)/$new_date/;
mh17's avatar
mh17 committed
                              $date_changed = 1;
mh17's avatar
mh17 committed
                              print "Date changed from $file_date to $new_date\n";
mh17's avatar
mh17 committed
                        }
                  }
                  elsif(m/(\d+)/)
                  {
                        $date_found = 1;
                        $file_date = $1;
mh17's avatar
mh17 committed
                        $new_date = "2006-$year_now";
                        s/(\d+)/$new_date/;
                        $date_changed = 1;
mh17's avatar
mh17 committed
                        print "Date changed from $file_date to $new_date\n";
                  }
            }
      }
}

mh17's avatar
mh17 committed

mh17's avatar
mh17 committed
for($i = $#ARGV;$i > 0;$i--)
mh17's avatar
mh17 committed
{
mh17's avatar
mh17 committed
      $file = shift @ARGV;
      if($file eq "-many")
      {
            $many = 1;
      }
      elsif($file eq "-no-CVS")
      {
            $CVS = 0;
      }
mh17's avatar
mh17 committed
}
mh17's avatar
mh17 committed
$file = shift @ARGV;    # we take one file arg only
if(!$file) {      die("No file!\n"); }

mh17's avatar
mh17 committed
print "$file: ";

@time = localtime;
mh17's avatar
mh17 committed

mh17's avatar
mh17 committed
$new_date = $year_now = $time[5] + 1900;
$file_date = "??";
$date_found = 0;
$date_changed = 0;
$lineno = 0;

if($CVS)
{
      @args = ("cvs", "edit", $file);
mh17's avatar
mh17 committed
      # test if writeable: not sure if CVS return values are good
      system(@args);

      if(!(-w $file))
      {
            die("Cannot checkout $file from CVS - not writeable\n");
      }
mh17's avatar
mh17 committed
}

open(IFILE,$file) or die("cannot open $file\n");

$ofile = $file . ".tmp";         # make tmp file in same directory
$year_now = 2010;
open(OFILE,"> ".$ofile) or die("cannot open $ofile\n");

while($line = <IFILE>)
{
      $lineno++;
      if($lineno < 50 && (!$date_found || $many))
      {
mh17's avatar
mh17 committed
            #$line =
mh17's avatar
mh17 committed
            change_date($line);
      }
      print OFILE $line;
}

close $ofile;

if(!$date_found)
{
      print "No copyright date found\n";
}

if($date_changed)
{
      unlink $file or die ("Can't remove old file\n");
      rename($ofile,$file) or die("Can't update file\n");

      if($CVS)
      {
            @args = ("cvs", "commit", "-m", "CopyRight date set", $file);
mh17's avatar
mh17 committed
            system(@args);
            if(-w $file)
            {
                  die("Copyright date set but cannot commit $file to CVS: still writeable\n");
            }
      unlink $ofile;

mh17's avatar
mh17 committed
      if($date_found)
      {
            print "Copyright date was $file_date: not changed\n";
      }
      if($CVS)
      {
            @args = ("cvs", "unedit", $file);
mh17's avatar
mh17 committed
            system(@args);
            if(-w $file)
            {
                  die("Can't CVS unedit $file: still writeable\n");
mh17's avatar
mh17 committed
            }