-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicense_headers.rb
204 lines (172 loc) · 4.38 KB
/
license_headers.rb
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env ruby
#L
# Copyright Northwestern University.
#
# Distributed under the OSI-approved BSD 3-Clause License.
# See http://ncip.github.com/psc/LICENSE.txt for details.
#L
######
# Run this script to add/update NCIP's mandatory license headers or to verify
# the presence of the headers in all applicable files.
#
# Usage:
# $ cd {PSC_WORKING_ROOT}
# $ ruby license_headers.rb [update|check]
#
# The `update` subcommand applies the current license to all applicable files.
#
# The `check` subcommand verifies that all applicable files have the license. If
# any are missing the license, it exits with a status of 1 and prints a
# descriptive message to standard out.
###### UPDATE STRATEGIES
class LicenseUpdater
LICENSE_TOKEN = /\{LICENSE\}/
LICENSE_HEADER_TEXT = <<-TEXT
Copyright Northwestern University.
Distributed under the OSI-approved BSD 3-Clause License.
See http://ncip.github.com/psc/LICENSE.txt for details.
TEXT
attr_reader :filename, :contents
def initialize(filename)
@filename = filename
@contents = File.read(filename)
end
def needs_update?
new_contents != contents
end
def update
File.open(filename, 'w') { |f| f.write(new_contents) } if needs_update?
end
def new_contents
if contents =~ license_pattern
contents.sub(license_pattern, license_comment)
else
with_new_license
end
end
def with_new_license
insert_point = license_insert_points.map { |re| contents.match(re) }.compact.first
fail "No insert point matches for #{filename} from #{license_insert_points.inspect}" unless insert_point
i = insert_point.end(0)
contents.dup.insert(i, "#{"\n" if i != 0}#{license_comment}#{"\n" if contents.slice(i, 1) != "\n"}")
end
def license_pattern
@license_pattern ||= begin
re_string = license_header_lines.collect { |line|
if line =~ LICENSE_TOKEN
line_prefix = line.sub(/\s*#{LICENSE_TOKEN.source}\s*/, '')
"(#{line_prefix}.*?\n)+?"
else
Regexp.escape(line) + "\n"
end
}.join('')
Regexp.new(re_string, Regexp::MULTILINE)
end
end
def license_comment
license_header_lines.collect { |line|
if line =~ LICENSE_TOKEN
LICENSE_HEADER_TEXT.split("\n").collect { |l|
line.sub(LICENSE_TOKEN, l).gsub(/\s*$/, '')
}.join("\n") + "\n"
else
"#{line}\n"
end
}.join('')
end
end
class JavaLicenseUpdater < LicenseUpdater
def license_insert_points
[ /\A/ ]
end
def license_header_lines
[
"/*L",
" * {LICENSE}",
" */"
]
end
end
class RubyLicenseUpdater < LicenseUpdater
def license_insert_points
[
%r{#!.*?\n},
/\A/
]
end
def license_header_lines
[
"#L",
"# {LICENSE}",
"#L"
]
end
end
class XmlLicenseUpdater < LicenseUpdater
def license_insert_points
[
%r{<\?xml.*?\?>\s*?\n},
/\A/
]
end
def license_header_lines
[
"<!--L",
" {LICENSE}",
"L-->"
]
end
end
###### MAIN EXECUTION
class LicenseCLI
attr_reader :command
def initialize(args)
@command = args.pop
end
def run
if command && respond_to?(command)
send(command)
elsif command
fail "Unknown subcommand #{command.inspect}"
else
fail "Please specify update or check."
end
end
def update
outdated.each_with_index do |u, i|
$stderr.print "\rUpdating #{i + 1} / #{outdated.size}"
u.update
end
$stderr.puts "\rUpdated #{outdated.size} license header#{'s' unless outdated.size == 1}."
exit(0)
end
def check
if outdated.empty?
$stderr.puts "All license headers up to date."
exit(0)
else
$stderr.puts "#{outdated.size} files have missing or outdated license header#{'s' unless outdated.size == 1}:"
outdated.collect(&:filename).sort.each do |fn|
puts " #{fn}"
end
exit(1)
end
end
def outdated
@outdated ||= updaters.select { |u| u.needs_update? }
end
def updaters
@updaters ||= {
"**/*.java" => JavaLicenseUpdater,
"**/*.rake" => RubyLicenseUpdater,
"**/*.rb" => RubyLicenseUpdater,
"buildfile" => RubyLicenseUpdater,
"**/src/**/*.xml" => XmlLicenseUpdater
}.collect { |pattern, updater_class|
Dir[pattern].collect { |filename|
updater_class.new(filename)
}
}.flatten
end
end
LicenseCLI.new(ARGV).run