find_long_lines.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env ruby
  2. #
  3. # Run like this:
  4. #
  5. # ./find_long_lines.rb [DIR]
  6. #
  7. ###############################################################
  8. # Idea from the command line: #
  9. # #
  10. # > cd wherever-you-want #
  11. # > files=$(find . -iname "*.f90") #
  12. # > ruby -ne 'puts $<.filename, $_ if $_.length > 132' $files #
  13. ###############################################################
  14. # -- where to look. Default to '~/TM5'.
  15. if ARGV.size==1
  16. root=ARGV[0]
  17. else
  18. root=File.join(ENV['HOME'],'TM5')
  19. end
  20. if not File.directory?(root)
  21. puts "Dir #{root} does not exist! Returning..."
  22. exit
  23. end
  24. # -- get list of files
  25. f90=Dir["#{root}/**/*.[fF]90"]
  26. # -- build list of culprit
  27. flist_with_longlines=[]
  28. f90.each do |fname|
  29. lines=File.open(fname).each_with_index.select {|li,ind| li.length>132}
  30. if lines.any?
  31. fobj=Hash.new()
  32. fobj['name']=fname
  33. fobj['ind']=lines.map(&:last).map{|v| v+1}
  34. fobj['longuest']=lines.map{|p| p[0].length}.max
  35. begin
  36. fobj['isComment']=lines.select{|l,i| l=~/^\s*!/}.map(&:last).map{|v| v+1}
  37. fobj['isOMP']=lines.select{|l,i| l=~/^\s*!\$OMP/i}.map(&:last).map{|v| v+1}
  38. rescue Exception
  39. STDERR.puts "Problem with file: #{fname} (bad encoding?)"
  40. STDERR.puts "All lon lines considered as code."
  41. fobj['isComment']=[]
  42. fobj['isOMP']=[]
  43. end
  44. fobj['isComment']-=fobj['isOMP']
  45. fobj['isCode']=fobj['ind']-fobj['isComment']-fobj['isOMP']
  46. flist_with_longlines << fobj
  47. end
  48. end
  49. # -- Verbose
  50. total=flist_with_longlines.map {|f| f['ind'].length}.inject(:+)
  51. totalf=flist_with_longlines.map {|f| f['isCode'].length}.inject(:+)
  52. totalc=flist_with_longlines.map {|f| f['isComment'].length}.inject(:+)
  53. totalo=flist_with_longlines.map {|f| f['isOMP'].length}.inject(:+)
  54. longuest=flist_with_longlines.map {|f| f['longuest']}.max
  55. puts "There are #{flist_with_longlines.length} files out of #{f90.length} with too long lines,"
  56. puts "for a total of #{total} too long lines (Code: #{totalf}; Comment: #{totalc}; OpenMP: #{totalo})."
  57. puts "The longest line is #{longuest} characters long."
  58. print "Do you want the list of files AND all lines numbers [Y/n]? "
  59. res=$stdin.gets.chomp
  60. if res=='' or res.upcase=='Y'
  61. flist_with_longlines.each do |f|
  62. puts
  63. puts f['name'].sub(root,'.') +':',"--long code--",f['isCode'],"--long comment--",f['isComment']
  64. puts "--long OpenMP--",f['isOMP']
  65. end
  66. else
  67. print "Do you want the ONLY the list of files (no lines numbers) [Y/n]? "
  68. res2=$stdin.gets.chomp
  69. if res2=='' or res2.upcase=='Y'
  70. flist_with_longlines.each do |f|
  71. nlc=f['isCode'].length
  72. if nlc>0
  73. puts f['name'].sub(root,'.') +" (#{nlc})"
  74. end
  75. end
  76. end
  77. end