Script to add missing copyright headers on files.

This commit is contained in:
Brad Fitzpatrick 2011-01-27 22:59:58 -08:00
parent 0baf46c69a
commit 9148279f3d
1 changed files with 68 additions and 0 deletions

68
misc/copyrightifity Executable file
View File

@ -0,0 +1,68 @@
#!/usr/bin/perl
#
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# This script adds copyright headers to files.
use strict;
my $header = do { local $/; <DATA> };
$header =~ s!\s+$!\n!;
my $yyyy = (localtime())[5] + 1900;
$header =~ s/YYYY/$yyyy/ or die;
unless (@ARGV == 1) {
die "Usage: copyrightify <filename>\n";
}
my $file = shift;
open(my $fh, $file) or die "Open $file error: $!\n";
my $source = do { local $/; <$fh> };
close($fh);
if ($source =~ /Copyright \d\d\d\d/) {
print STDERR "# $file - OK\n";
exit;
}
my $newsource = $source;
if ($file =~ /\.go$/) {
$header = "/*\n$header*/\n\n";
$newsource = $header . $source;
} elsif ($file =~ /\.py$/) {
$header = join("", map { "# $_\n" } split(/\n/, $header));
$newsource = $header . $source;
} else {
die "File type not supported.";
}
open(my $fh, ">$file") or die "Open $file error: $!\n";
print $fh $newsource;
close($fh) or die;
__END__
Copyright YYYY Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.