Re: Apache license headers

2007-04-01 Thread robert burrell donkin
On Fri, 2007-03-30 at 10:21 +0200, Henning Schmiedehausen wrote:
 Very nice! I love the compactness and readability of Ruby (no joke!).
 
 If you are interested in a more overengineered solution to that problem, 
 there is CodeWrestler at 
 http://henning.schmiedehausen.org/eyewiki/Wiki.jsp?page=CodeWrestler 
 Especially the license.ReLicense and license.CheckLicense modules.
 
 I use this tool on the projects that I work on and e.g. the last 
 Velocity Release got its license headers 'codewrestled'... :-)

snip

seems like there are number of people from different projects all
working in this area :-)

 Matthieu Riou schrieb:
  Hi,
  
  I've just written a small Ruby script to check whether all your files 
  have the Apache license headers and optionally add them where they're 
  missing. 

this is the area RAT started out in

the real problem isn't ensure that every file has the current license
header but that each file has the appropriate license header. this
turned out to be quite a complex little  problem but i think i
understand it now.

i haven't really found the time to push RAT forward this year in the way
that i would have hope to. if anyone wants to combine their efforts in
this area or would be interested in analysis of the problem and possible
solutions, that'd be great.

- robert


signature.asc
Description: This is a digitally signed message part


Re: Apache license headers

2007-03-30 Thread Henning Schmiedehausen

Very nice! I love the compactness and readability of Ruby (no joke!).

If you are interested in a more overengineered solution to that problem, 
there is CodeWrestler at 
http://henning.schmiedehausen.org/eyewiki/Wiki.jsp?page=CodeWrestler 
Especially the license.ReLicense and license.CheckLicense modules.


I use this tool on the projects that I work on and e.g. the last 
Velocity Release got its license headers 'codewrestled'... :-)


Best regards
Henning



Matthieu Riou schrieb:

Hi,

I've just written a small Ruby script to check whether all your files 
have the Apache license headers and optionally add them where they're 
missing. It's as simple as:


   ruby check_license_headers.rb # list files with no header
   ruby check_license_headers.rb add  # add headers automatically

It scans for 'java', 'xml', 'bpel', 'wsdl', 'c' and 'cpp' files but it's 
rather easy to add more files type. Might not be bullet proof but it 
worked well enough for me. Thought I'd just post it here in case someone 
else finds it useful.


Cheers,
Matthieu

==

module FileBrowser
  def browse(root)
queue = Array.new.push(root)
while !queue.empty?
  filename = queue.pop
  if File.file?(filename)
yield(filename)
  else
Dir.new(filename).each do |child|
  unless ['..', '.','.svn'].include? child
queue.push(filename + / + child)
  end
end
  end
end
  end
end

class HeadersCheck
  EXT = ['java', 'xml', 'bpel', 'wsdl', 'c', 'cpp']

  include FileBrowser

  def check_files(dir, dry_run)
count = 0
browse(dir) do |filename|
  if /\.#{EXT.join('$|\.')}$/ =~ filename
match = nil
f = File.new (filename)
# Checking for the Apache header in the 4 first lines
4.times do
  match ||= (/Licensed to the Apache Software Foundation/ =~ 
f.readline) rescue nil

#puts(File #{filename} too short to check.)
end
f.close
unless match
  if dry_run
puts Missing header in #{filename}
  else
add_header(filename)
  end
  count += 1
end
  end
end
if dry_run
  puts #{count} files don't have an Apache license header.
else
  puts #{count} files have been changed to include the Apache 
license header.

end
  end

  def add_header(filename)
# Extracting file extension
ext = /\.([^\.]*)$/.match(filename[1..-1])[1]
header = HEADERS[ext]
content = File.new(filename, 'r').read
if content[0..4] == '?xml'
  # If the file has a xml header, the license needs to be appended after
  content = content[0..content.index(\n)] + header + 
content[(content.index (\n) + 1)..-1]

else
  content = header + content
end
File.new(filename, 'w').write(content)
  end

end

JAVA_HEADER = JAVA
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 
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.
 */
JAVA

XML_HEADER = XML
!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you 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.
  --
XML

HEADERS = {
  'java' = JAVA_HEADER,
  'c' = JAVA_HEADER,
  'cpp' = JAVA_HEADER,
  'xml' = XML_HEADER,
  'bpel' = XML_HEADER,
  'wsdl' = XML_HEADER
}

if ['-h', '--help', 'help'].include? ARGV[0]
  puts Scans the current directory for files with missing Apache 
  puts license headers.
  putsruby check_license_headers.rb

Re: Apache license headers

2007-03-30 Thread Vadim Gritsenko

Matthieu Riou wrote:

Hi,

I've just written a small Ruby script to check whether all your files 
have the Apache license headers and optionally add them where they're 
missing. It's as simple as:


   ruby check_license_headers.rb # list files with no header
   ruby check_license_headers.rb add  # add headers automatically

It scans for 'java', 'xml', 'bpel', 'wsdl', 'c' and 'cpp' files but it's 
rather easy to add more files type. Might not be bullet proof but it 
worked well enough for me. Thought I'd just post it here in case someone 
else finds it useful.


It is indeed easier to read than perl ;-)

In case you need more features, in /committers/relicense/src/perl you can find 
scripts which can update from old style license header to new one, and have more 
configuration options.


Vadim

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Apache license headers

2007-03-30 Thread Matthieu Riou

Thanks! I remembered seeing this script somewhere but I wanted to have a
small Ruby script to be easily callable from Rake. Plus writing it was
somewhat quicker than looking for it and it's sometimes fun to reinvent the
wheel ;)

On 3/30/07, Vadim Gritsenko [EMAIL PROTECTED] wrote:


Matthieu Riou wrote:
 Hi,

 I've just written a small Ruby script to check whether all your files
 have the Apache license headers and optionally add them where they're
 missing. It's as simple as:

ruby check_license_headers.rb # list files with no header
ruby check_license_headers.rb add  # add headers automatically

 It scans for 'java', 'xml', 'bpel', 'wsdl', 'c' and 'cpp' files but it's
 rather easy to add more files type. Might not be bullet proof but it
 worked well enough for me. Thought I'd just post it here in case someone
 else finds it useful.

It is indeed easier to read than perl ;-)

In case you need more features, in /committers/relicense/src/perl you can
find
scripts which can update from old style license header to new one, and
have more
configuration options.

Vadim

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Apache license headers

2007-03-30 Thread Vadim Gritsenko

Matthieu Riou wrote:
Thanks! I remembered seeing this script somewhere but I wanted to have a 
small Ruby script to be easily callable from Rake. Plus writing it was 
somewhat quicker than looking for it and it's sometimes fun to reinvent 
the wheel ;)


since no /ruby exists at that location, i suggest you to break new ground add 
your script there :)


Vadim

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Apache license headers

2007-03-30 Thread Matthieu Riou

Okay, if nobody shouts against it I'll just go for it.

Thanks,
Matthieu

On 3/30/07, Vadim Gritsenko [EMAIL PROTECTED] wrote:


Matthieu Riou wrote:
 Thanks! I remembered seeing this script somewhere but I wanted to have a
 small Ruby script to be easily callable from Rake. Plus writing it was
 somewhat quicker than looking for it and it's sometimes fun to reinvent
 the wheel ;)

since no /ruby exists at that location, i suggest you to break new ground
add
your script there :)

Vadim

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Apache license headers

2007-03-29 Thread Matthieu Riou

Hi,

I've just written a small Ruby script to check whether all your files have
the Apache license headers and optionally add them where they're missing.
It's as simple as:

  ruby check_license_headers.rb # list files with no header
  ruby check_license_headers.rb add  # add headers automatically

It scans for 'java', 'xml', 'bpel', 'wsdl', 'c' and 'cpp' files but it's
rather easy to add more files type. Might not be bullet proof but it worked
well enough for me. Thought I'd just post it here in case someone else finds
it useful.

Cheers,
Matthieu

==

module FileBrowser
 def browse(root)
   queue = Array.new.push(root)
   while !queue.empty?
 filename = queue.pop
 if File.file?(filename)
   yield(filename)
 else
   Dir.new(filename).each do |child|
 unless ['..', '.','.svn'].include? child
   queue.push(filename + / + child)
 end
   end
 end
   end
 end
end

class HeadersCheck
 EXT = ['java', 'xml', 'bpel', 'wsdl', 'c', 'cpp']

 include FileBrowser

 def check_files(dir, dry_run)
   count = 0
   browse(dir) do |filename|
 if /\.#{EXT.join('$|\.')}$/ =~ filename
   match = nil
   f = File.new(filename)
   # Checking for the Apache header in the 4 first lines
   4.times do
 match ||= (/Licensed to the Apache Software Foundation/ =~
f.readline) rescue nil
   #puts(File #{filename} too short to check.)
   end
   f.close
   unless match
 if dry_run
   puts Missing header in #{filename}
 else
   add_header(filename)
 end
 count += 1
   end
 end
   end
   if dry_run
 puts #{count} files don't have an Apache license header.
   else
 puts #{count} files have been changed to include the Apache license
header.
   end
 end

 def add_header(filename)
   # Extracting file extension
   ext = /\.([^\.]*)$/.match(filename[1..-1])[1]
   header = HEADERS[ext]
   content = File.new(filename, 'r').read
   if content[0..4] == '?xml'
 # If the file has a xml header, the license needs to be appended after
 content = content[0..content.index(\n)] + header + content[(
content.index(\n) + 1)..-1]
   else
 content = header + content
   end
   File.new(filename, 'w').write(content)
 end

end

JAVA_HEADER = JAVA
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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.
*/
JAVA

XML_HEADER = XML
!--
 ~ Licensed to the Apache Software Foundation (ASF) under one
 ~ or more contributor license agreements.  See the NOTICE file
 ~ distributed with this work for additional information
 ~ regarding copyright ownership.  The ASF licenses this file
 ~ to you 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.
 --
XML

HEADERS = {
 'java' = JAVA_HEADER,
 'c' = JAVA_HEADER,
 'cpp' = JAVA_HEADER,
 'xml' = XML_HEADER,
 'bpel' = XML_HEADER,
 'wsdl' = XML_HEADER
}

if ['-h', '--help', 'help'].include? ARGV[0]
 puts Scans the current directory for files with missing Apache 
 puts license headers.
 putsruby check_license_headers.rb  # list files
 putsruby check_license_headers.rb add  # add headers automatically
else
 HeadersCheck.new.check_files('.', ARGV[0] != 'add')
end