This repository has been archived by the owner on Nov 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
add-license.pl
157 lines (129 loc) · 4.94 KB
/
add-license.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/perl
# Copyright 2019 Google LLC
#
# 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
#
# https://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.
$GOOGLE_LICENSE_REGEX = qr/\n[#* ]*Copyright 2019 Google LLC/;
$OTHER_LICENSE_REGEX = qr([Cc]opyright);
$LOG_INFO = "[\x1B[1;30mINFO\x1B[0m]";
$LOG_ERROR = "[\x1B[1;31mERROR\x1B[0m]";
$LOG_WARN = "[\x1B[1;33mWARNING\x1B[0m]";
$TEMPLATE_C = <<'END_TEMPLATE_C';
/*
* Copyright 2019 Google LLC
*
* 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
*
* https://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.
*/
END_TEMPLATE_C
$TEMPLATE_SH = <<'END_TEMPLATE_SH';
# Copyright 2019 Google LLC
#
# 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
#
# https://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.
END_TEMPLATE_SH
$TEMPLATE_HTML = <<'END_TEMPLATE_HTML';
<!--
Copyright 2019 Google LLC
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
https://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.
-->
END_TEMPLATE_HTML
if (0 == @ARGV) {
print STDERR "Syntax: add-license.pl [file1 [file2 [file3 ... ] ] ]\n";
exit 1;
}
while ($file = shift) {
my $error = add_license($file);
if ($error) {
print STDERR "$LOG_ERROR $file: $error\n";
}
}
sub add_license {
my $file = shift;
# Find out what license format to use.
my $template = "";
$file =~ /\.cs$/ and $template = $TEMPLATE_C;
$file =~ /\.js\.txt$/ and $template = $TEMPLATE_C;
$file =~ /\.js$/ and $template = $TEMPLATE_C;
$file =~ /\.c$/ and $template = $TEMPLATE_C;
$file =~ /\.cpp$/ and $template = $TEMPLATE_C;
$file =~ /\.h$/ and $template = $TEMPLATE_C;
$file =~ /\.css$/ and $template = $TEMPLATE_C;
$file =~ /\.shader$/ and $template = $TEMPLATE_C;
$file =~ /\.sh$/ and $template = $TEMPLATE_SH;
$file =~ /\.pl$/ and $template = $TEMPLATE_SH;
$file =~ /\.py$/ and $template = $TEMPLATE_SH;
$file =~ /\.html$/ and $template = $TEMPLATE_HTML;
if ($template eq "") {
return "Error: not a recognized file type: $file\n";
}
# Read full contents of file.
open my $fh, $file or return "Failed to read $file";
my $contents = do { local $/; <$fh> };
close $fh;
# Is it a DOS file or a Unix file?
my $line_ending = $contents =~ /\r\n/ ? "\r\n" : "\n";
# Does it already contain the copyright notice? (must be at the start
# of the file).
if (substr($contents, 0, 100) =~ /$GOOGLE_LICENSE_REGEX/s) {
print STDERR "$LOG_INFO $file: already has a Google LLC copyright notice.\n";
return ""; # Not an error
}
# Does the file already have some other copyright?
if ($contents =~ $OTHER_LICENSE_REGEX) {
return "File has a (NON-GOOGLE) copyright notice??";
}
# We must preserve the BOM and the shell/HTML declaration line at the
# beginning of the file when inserting the copyright notice.
my $header = "";
my $body = $contents;
if ($contents =~ /^(\xEF\xBB\xBF)?([#<]![^\n\r]*\r?\n)?(.*)$/s) {
$header = "$1$2";
$body = $3;
}
# Add copyright notice to top of body, keeping line endings.
my $notice = $template;
$notice =~ s/\r//g;
$notice =~ s/\n/$line_ending/g;
$body = "$notice$body";
open OUT, ">$file" or return "Failed to write file.";
print OUT "$header$body";
close OUT;
print STDERR "$LOG_INFO $file: successfully added copyright notice.\n";
return "";
}