/
relicense.pl
executable file
·104 lines (86 loc) · 2.53 KB
/
relicense.pl
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
#!/usr/bin/perl
# __BEGIN_LICENSE__
# Copyright (C) 2008-2010 United States Government as represented by
# the Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# __END_LICENSE__
use strict;
use warnings;
use File::Slurp;
use File::Basename;
my %comment = (
".ac" => "dnl",
".am" => "#",
".cc" => "//",
".cpp" => "//",
".cxx" => "//",
".oldtest" => "//",
".hpp" => "//",
".cg" => "//",
".glsl" => "//",
".h" => "//",
".hh" => "//",
".i" => "//",
".m4" => "dnl",
".mak" => "#",
".pl" => "#",
".py" => "#",
".sh" => "#",
".js" => "//",
".tcc" => "//",
".rst" => "|",
".java" => "//",
".aidl" => "//",
".xml" => "<!--,-->",
);
# Read the license text from __DATA__ by default
my $f = \*DATA;
$f = $ARGV[0] if @ARGV > 0;
my @license = read_file($f);
my $shebang = '';
@license = map { chop; $_; } @license;
# process each line given on stdin
foreach my $filename (<>) {
chomp $filename;
# get the extension, and skip it if we don't know about it
my (undef, undef, $ext) = fileparse($filename, qr/\.[^.]*/);
unless (exists $comment{$ext}) {
warn "Skipped $filename\n";
next;
}
my $file = read_file($filename);
next if ($file =~ /^\s*$/);
next if ($file =~ /__NO_RELICENSE__/);
$shebang = '';
# Protect a shebang line or xml declaration
if ($file =~ s/^(#!.*\n)//) {
if (defined($1)) {
$shebang = $1;
}
} elsif ($file =~ s/^(<\?xml.*\n)//) {
if (defined($1)) {
$shebang = $1;
}
}
# Remove a license header if it exists
$file =~ s/^[^\n]*__BEGIN_LICENSE__.*?__END_LICENSE__[^\n]*$//ms;
# Remove all blank files from the top of the file
while ($file =~ s/^\s*\n//) {};
# prepend the license text, prepending the comment string to each line.
# Also, separate the license header from content by two blank lines
my ($chead, $ctail);
if ($comment{$ext} =~ /,/) {
($chead, $ctail) = split(/,/, $comment{$ext});
$ctail = " " . $ctail;
} else {
($chead, $ctail) = ($comment{$ext}, "");
}
$file = $shebang . $chead . join($ctail . "\n" . $chead, @license) . $ctail . "\n\n" . $file;
write_file($filename, $file);
}
__DATA__
__BEGIN_LICENSE__
Copyright (C) 2008-2010 United States Government as represented by
the Administrator of the National Aeronautics and Space Administration.
All Rights Reserved.
__END_LICENSE__