1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#!/usr/bin/perl
use strict;
my $APACHE = <<"EOF";
Copyright 2005-2016 ECMWF.
This software is licensed under the terms of the Apache Licence Version 2.0
which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
EOF
my $LGPL = <<"EOF";
Copyright 2005-2007 ECMWF
Licensed under the GNU Lesser General Public License which
incorporates the terms and conditions of version 3 of the GNU
General Public License.
See LICENSE and gpl-3.0.txt for details.
EOF
my $GPL = <<"EOF";
Copyright 2005-2007 ECMWF
Licensed under the GNU General Public License version 3.
See LICENSE for details.
EOF
my $LICENSE = $APACHE;
my %COMMENTS = (
java => { start => "/**\n" , end => "*/\n\n" , comment => "* " },
xml => { start => "<!--\n", end => "-->\n\n" , after => "\<\?xml[^<]*\>" },
# xsd => { start => "<!--\n", end => "-->\n\n" },
# jsp => { start => "<!--\n", end => "-->\n\n" },
sh => { comment => "# ", end => "\n", after => "^#!/.*\n" },
pl => { comment => "# ", end => "\n", after => "^#!/.*\n" },
js => { start => "/**\n" , end => "*/\n\n" , comment => "* " },
c => { start => "/**\n" , end => "*/\n\n" , comment => "* " },
h => { start => "/**\n" , end => "*/\n\n" , comment => "* " },
l => { start => "/**\n" , end => "*/\n\n" , comment => "* " },
y => { start => "/**\n" , end => "*/\n\n" , comment => "* " },
css => { start => "/**\n" , end => "*/\n\n" , comment => "* " },
sql => { comment => "-- ", end => "\n" },
properties => { comment => "# ", end => "\n" },
def => { comment => "# ", end => "\n" },
F => { comment => "C ", end => "C\n\n " },
f => { comment => "C ", end => "C\n\n" },
);
foreach my $file ( @ARGV )
{
my $doit=1;
$file =~ /\.(\w+)/;
my $ext = $1;
my $c = $COMMENTS{$ext};
unless($c)
{
print "$file: unsupported extension. File ignored\n";
next;
}
open(IN,"<$file") or die "$file: $!";
my @text = <IN>;
close(IN);
if(join("",@text) =~ /Licensed under the/gs)
{
print "$file: License already stated. File ignored\n";
next;
}
open(OUT,">$file.tmp") or die "$file.tmp: $!";
if($c->{after})
{
my @x;
my $re = $c->{after};
loop: while(@text)
{
if($text[0] =~ m/$re/)
{
print OUT @x, shift @text;
@x = ();
last loop;
}
push @x,shift @text;
}
@text = (@x,@text);
}
print OUT $c->{start};
foreach my $line ( split("\n",$LICENSE) )
{
print OUT $c->{comment}, $line,"\n";
}
print OUT $c->{end};
print OUT @text;
close(OUT) or die "$file: $!";
if($doit) {
use File::Copy qw(cp);
use File::Compare qw(compare_text compare);
if(compare_text("$file.tmp",$file)) {
print "UPDATING file $file\n";
system("p4 edit $file") unless(-w $file);
cp($file,"$file.old") or die "cp($file,$file.old): $!";
cp("$file.tmp",$file) or die "cp($file.tmp,$file): $!";
}
} else {
print "IGNORING file $file\n";
}
unlink("$file.tmp");
}
|