find_long_lines.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 (note: standard is 132, most compiler allow for
  27. # more, Cray limit is 256)
  28. flist_with_longlines=[]
  29. f90.each do |fname|
  30. lines=File.open(fname).each_with_index.select {|li,ind| li.length>256}
  31. if lines.any?
  32. fobj=Hash.new()
  33. fobj['name']=fname
  34. fobj['ind']=lines.map(&:last).map{|v| v+1}
  35. fobj['longuest']=lines.map{|p| p[0].length}.max
  36. begin
  37. fobj['isComment']=lines.select{|l,i| l=~/^\s*!/}.map(&:last).map{|v| v+1}
  38. fobj['isOMP']=lines.select{|l,i| l=~/^\s*!\$OMP/i}.map(&:last).map{|v| v+1}
  39. rescue Exception
  40. STDERR.puts "Problem with file: #{fname} (bad encoding?)"
  41. STDERR.puts "All lon lines considered as code."
  42. fobj['isComment']=[]
  43. fobj['isOMP']=[]
  44. end
  45. fobj['isComment']-=fobj['isOMP']
  46. fobj['isCode']=fobj['ind']-fobj['isComment']-fobj['isOMP']
  47. flist_with_longlines << fobj
  48. end
  49. end
  50. # -- Verbose
  51. total=flist_with_longlines.map {|f| f['ind'].length}.inject(:+)
  52. totalf=flist_with_longlines.map {|f| f['isCode'].length}.inject(:+)
  53. totalc=flist_with_longlines.map {|f| f['isComment'].length}.inject(:+)
  54. totalo=flist_with_longlines.map {|f| f['isOMP'].length}.inject(:+)
  55. longuest=flist_with_longlines.map {|f| f['longuest']}.max
  56. puts "There are #{flist_with_longlines.length} files out of #{f90.length} with too long lines,"
  57. puts "for a total of #{total} too long lines (Code: #{totalf}; Comment: #{totalc}; OpenMP: #{totalo})."
  58. puts "The longest line is #{longuest} characters long."
  59. print "Do you want the list of files AND all lines numbers [Y/n]? "
  60. res=$stdin.gets.chomp
  61. if res=='' or res.upcase=='Y'
  62. flist_with_longlines.each do |f|
  63. puts
  64. puts f['name'].sub(root,'.') +':',"--long code--",f['isCode'],"--long comment--",f['isComment']
  65. puts "--long OpenMP--",f['isOMP']
  66. end
  67. else
  68. print "Do you want the ONLY the list of files (no lines numbers) [Y/n]? "
  69. res2=$stdin.gets.chomp
  70. if res2=='' or res2.upcase=='Y'
  71. flist_with_longlines.each do |f|
  72. nlc=f['isCode'].length
  73. if nlc>0
  74. puts f['name'].sub(root,'.') +" (#{nlc})"
  75. end
  76. end
  77. end
  78. end