Skip to content

Commit

Permalink
Port r7242: Add check option. #3068.
Browse files Browse the repository at this point in the history
This option scans all the sources to determine if any of them have a
missing or outdated license header. If any do, it exits with status 1.
Otherwise, it exits with 0.

This is intended for use in the CI build.

git-svn-id: https://ncisvn.nci.nih.gov/svn/psc/trunk@7244 0d517254-b314-0410-acde-c619094fa49f
  • Loading branch information
Rhett Sutphin committed Dec 27, 2012
1 parent 2806880 commit 6ff511f
Showing 1 changed file with 86 additions and 25 deletions.
111 changes: 86 additions & 25 deletions license_headers.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#!/usr/bin/env ruby

######
# Run this script to add/update NCIP's mandatory license headers.
# 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
# $ 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.

class LicenseUpdater
attr_reader :filename, :contents
###### UPDATE STRATEGIES

class LicenseUpdater
LICENSE_TOKEN = /\{LICENSE\}/

LICENSE_HEADER_TEXT = <<-TEXT
Expand All @@ -19,32 +26,36 @@ class LicenseUpdater
See http://ncip.github.com/psc/LICENSE 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
$stderr.print "Processing #{filename}..."
File.open(filename, 'w') { |f| f.write(new_contents) } if needs_update?
end

new_contents =
if contents =~ license_pattern
$stderr.puts "updating existing comment."
contents.sub(license_pattern, license_comment)
else
$stderr.puts "adding new comment."
insert_new_license
end
File.open(filename, 'w') { |f| f.write(new_contents) }
def new_contents
if contents =~ license_pattern
contents.sub(license_pattern, license_comment)
else
with_new_license
end
end

def insert_new_license
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.insert(i, "#{"\n" if i != 0}#{license_comment}#{"\n" if contents.slice(i, 1) != "\n"}")
contents.dup.insert(i, "#{"\n" if i != 0}#{license_comment}#{"\n" if contents.slice(i, 1) != "\n"}")
end

def license_pattern
Expand Down Expand Up @@ -123,14 +134,64 @@ def license_header_lines
end
end

{
"**/*.java" => JavaLicenseUpdater,
"**/*.rake" => RubyLicenseUpdater,
"**/*.rb" => RubyLicenseUpdater,
"buildfile" => RubyLicenseUpdater,
"**/src/**/*.xml" => XmlLicenseUpdater
}.each do |pattern, updater|
Dir[pattern].each do |filename|
updater.new(filename).update
###### 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

0 comments on commit 6ff511f

Please sign in to comment.