zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

copyright-fixer.rb (3923B)


      1 require "open3"
      2 
      3 base = ARGV[0]
      4 Dir.chdir base
      5 all_files = Dir.glob('**/*').select {|f| File.file?(f)}
      6 src_files = all_files.select {|f| f.match(/\.(cpp|H|h|C)$/)}
      7 zyn_files = src_files.select {|f| f.match(/^src/)}
      8 
      9 def add_license(fname)
     10     puts "Please Enter a description for #{fname}:"
     11     short = fname.match(/.*\/(.*)/)[1]
     12 
     13     desc = $stdin.gets.strip
     14     str  = %{/*
     15   ZynAddSubFX - a software synthesizer
     16 
     17   #{short} - #{desc}
     18   Copyright (C) 2016 Mark McCurry
     19 
     20   This program is free software; you can redistribute it and/or
     21   modify it under the terms of the GNU General Public License
     22   as published by the Free Software Foundation; either version 2
     23   of the License, or (at your option) any later version.
     24 */}
     25 
     26     Open3.popen3("ed -p: #{fname}") do |stdin, stdout, stderr|
     27         stdin.puts "H"
     28         stdin.puts "0a"
     29         stdin.puts str
     30         stdin.puts "."
     31         stdin.puts "w"
     32         stdin.puts "q"
     33         puts stdout.gets
     34     end
     35 end
     36 
     37 def update_license(fname, first, last)
     38     str = %{  This program is free software; you can redistribute it and/or
     39   modify it under the terms of the GNU General Public License
     40   as published by the Free Software Foundation; either version 2
     41   of the License, or (at your option) any later version.
     42 */}
     43     Open3.popen3("ed -p: #{fname}") do |stdin, stdout, stderr|
     44         stdin.puts "H"
     45         stdin.puts "#{first},#{last}c"
     46         stdin.puts str
     47         stdin.puts "."
     48         stdin.puts "w"
     49         stdin.puts "q"
     50         puts stdout.gets
     51     end
     52 end
     53 
     54 tmp = 5000
     55 zyn_files.sort.each do |fname|
     56     if tmp > 0
     57         tmp -= 1
     58     else
     59         exit
     60     end
     61 
     62     if(fname.match(/jack_osc/) || fname.match(/NSM/))
     63         next
     64     end
     65 
     66     f = File.open(fname, "r")
     67     line = 0
     68     puts
     69     puts "Info: Processing '#{fname}'"
     70 
     71     #Check for Copyright Header
     72     first = f.gets
     73     line += 1
     74 
     75     if(!first.match /\/\*/)
     76         puts "Error: No Copyright Header Found"
     77         f.close
     78         add_license(fname)
     79         next
     80     end
     81 
     82     #Program Title
     83     ln = f.gets
     84     line += 1
     85 
     86     if(!ln.match /ZynAddSubFX - a software synthesizer/)
     87         puts "Warning: Unexpected Program Name"
     88     end
     89 
     90     #Empty
     91     ln = f.gets
     92     line += 1
     93 
     94     if(!ln.match /^$/)
     95         puts "Warning: Broken Formatting"
     96     end
     97 
     98     #File Description
     99     ln = f.gets
    100     line += 1
    101     if(!ln.match /(.*) - (.*)/)
    102         puts "Warning: Invalid Description"
    103     end
    104 
    105     #Copyright Section
    106     copying = true
    107     while copying
    108         ln = f.gets
    109     line += 1
    110         if(ln.match(/^          .*/))
    111             next
    112         end
    113         if(ln.match(/^$/)|| ln.match(/Author/))
    114             break
    115         end
    116         if(!(ln.match(/Copyright \(C\) ([0-9]*)-([0-9]*) (.*)/) ||
    117              ln.match(/Copyright \(C\) ([0-9]*) (.*)/)))
    118             puts "Warning: Invalid Copyright Field"
    119             puts "         <#{ln}>"
    120         end
    121     end
    122     
    123 
    124     #Out-Of-Date Author Section
    125     if(ln.match /Author/)
    126         while true
    127             ln = f.gets
    128             line += 1
    129             if(ln.match(/^$/))
    130                 break
    131             end
    132         end
    133     end
    134 
    135     #Completely Standard Copyright Stuff
    136     ln = f.gets
    137     line += 1
    138     if(ln.match /This program is free software/)
    139         puts "Info: GPL Found..."
    140         initial_line = line
    141         while(!ln.match(/\*\//))
    142             ln = f.gets
    143             line += 1
    144             if(ln.downcase.match(/version 2/))
    145                 puts ln
    146             end
    147         end
    148         final_line = line
    149         f.close
    150         update_license(fname, initial_line, final_line)
    151         puts "Info: GPL Lines #{initial_line} #{final_line} total #{final_line-initial_line}"
    152         next
    153     else
    154         puts "Error: Invalid Copyright Header"
    155         puts "       Line = <#{ln}>"
    156         puts `head #{fname}`
    157     end
    158 end
    159 #zyn_files = src_files.select
    160 #puts Dir.glob('**/*').select { |f| !!(File.file?(f) && f.match(/\.(cpp|H|h|C)$/))}