hlship 2005/01/05 09:10:05
Added: support copyright-java.txt update-copyrights.rb
Removed: support update-copyrights.py copyright-header.txt
Log:
Add new Ruby script for updating copyright comment blocks on files.
Revision Changes Path
1.1 jakarta-hivemind/support/copyright-java.txt
Index: copyright-java.txt
===================================================================
// Copyright {YEAR} The Apache Software Foundation
//
// 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.
1.1 jakarta-hivemind/support/update-copyrights.rb
Index: update-copyrights.rb
===================================================================
#!/usr/bin/ruby -w
#
# Executable Ruby script to walk one or more directories worth of
# source files and add or update the copyright comment block on each file.
#
# File types are identified by extension, different file types
# vary in the format of the copyright comment block and its exact position.
#
# The template for each block will contain the string {YEAR}. This is
replaced
# by the current year. However, if a current comment block exists,
# then the year is updated to include the current year, if necessary.
require 'find'
# Directory containing this script, used to locate templates (which are stored
# relative to the script itself.
SCRIPT_DIR = File.split(__FILE__)[0]
YEAR = Time.now.year.to_s
def read_template(file)
result = []
File.open(SCRIPT_DIR + "/" + file) do |file|
file.each { |line| result << line }
end
return result
end
# Writes out the content (array of strings) to the file.
# Actually, writes to a temporary file, then deletes the original
# file and renames the new file to it.
def write_file(path, content)
puts "Writing #{path} ..."
temp = path + "~"
File.open(temp, "w") do |file|
content.each { |line| file << line }
end
File.delete(path)
File.rename(temp, path)
end
# Scans the content (which should be the complete file)
# for the copyright year. Returns the year, which
# may be as single year ("2004") or a sequence of
# years ("2004, 2005, 2007"). Returns YEAR if
# no copyright year was found in the content.
def scan_for_year(content, comment_prefix)
content.each do |line|
if ! line.strip.empty? then
return YEAR if line[0, comment_prefix.length] != comment_prefix
if line =~ /Copyright ((\d+)(\s*,\s*\d+)*)/
then
found_year = $1
return "#{found_year}, #{YEAR}" if found_year.index(YEAR) == nil
return found_year
end
end
end
# Degenerate case -- a file that contains just comments? Shouldn't happen
# but just in case.
return YEAR
end
# Converts a line from the template into an active template line.
# Reponsible for converting {YEAR} into a year string within the template
line.
def convert_template_line(line, year)
return line.sub(/\{YEAR\}/, year)
end
class JavaFilter
def initialize
@template = read_template("copyright-java.txt")
end
def process(line)
return line if @line >= @template.length
end
def update(path)
content = nil
dirty = false
File.open(path) { |file| content = file.readlines }
year = scan_for_year(content, "//")
copyright_comment = @template.collect { |line|
convert_template_line(line, year) }
0.upto(@template.length() - 1) do |line|
dirty ||= content[line] != copyright_comment[line]
end
# TODO: What if the new comment is *shorter* than the old comment?
# Need to find and trim those line.
return false if !dirty
# Strip out all leading blank lines and comments
while ! content.empty?
line = content[0]
if (line[0, 2] == "//" || line.strip.empty?)
content.delete_at(0)
else
break
end
end
# content[0] should now be the package statement (or, if in the default
package,
# an import, class, interface, etc.
content.insert(0, *copyright_comment)
# Write the new content to the file
write_file(path, content)
return true
end
end
# Extracts the extension from a path
def extension(path)
dot = path.rindex(".")
if dot == nil then
return nil
else
return path[dot+1, path.length - dot]
end
end
files = []
filters =
{
"java" => JavaFilter.new
}
Find.find(*ARGV) do |f|
if f =~ /[CVS|SVN]/ then
Find.prune
return
end
# puts "Searching #{f} ..." if File.directory?(f)
# return unless File.file?(f)
if filters.has_key?(extension(f)) then
files << f
end
end
update_count = 0
files.each do |path|
filter = filters[extension(path)]
update_count +=1 if filter.update(path)
end
puts "Updated #{update_count} files (of #{files.length} files found)."
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]