Simplify msftidy with Find.find and add fixed()

Also, enforce binary encoding like the other Metasploit tools.

This opens the door to fixing files that have things that could be fixed
programmatically.

    [SeeRM #8497]
bug/bundler_fix
Tod Beardsley 2013-10-16 10:40:42 -05:00
parent 41ab4739e3
commit 3fc1a75a6b
No known key found for this signature in database
GPG Key ID: 1EFFB682ADB9F193
1 changed files with 29 additions and 36 deletions

View File

@ -1,10 +1,13 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# -*- coding: binary -*-
# #
# Check (recursively) for style compliance violations and other # Check (recursively) for style compliance violations and other
# tree inconsistencies. # tree inconsistencies.
# #
# by jduck and friends # by jduck and friends
# #
require 'fileutils'
require 'find'
CHECK_OLD_RUBIES = !!ENV['MSF_CHECK_OLD_RUBIES'] CHECK_OLD_RUBIES = !!ENV['MSF_CHECK_OLD_RUBIES']
@ -22,6 +25,10 @@ class String
"\e[1;33;40m#{self}\e[0m" "\e[1;33;40m#{self}\e[0m"
end end
def green
"\e[1;32;40m#{self}\e[0m"
end
def ascii_only? def ascii_only?
self =~ Regexp.new('[\x00-\x08\x0b\x0c\x0e-\x19\x7f-\xff]', nil, 'n') ? false : true self =~ Regexp.new('[\x00-\x08\x0b\x0c\x0e-\x19\x7f-\xff]', nil, 'n') ? false : true
end end
@ -31,9 +38,12 @@ class Msftidy
LONG_LINE_LENGTH = 200 # From 100 to 200 which is stupidly long LONG_LINE_LENGTH = 200 # From 100 to 200 which is stupidly long
attr_reader :full_filepath, :source, :name
def initialize(source_file) def initialize(source_file)
@full_filepath = source_file
@source = load_file(source_file) @source = load_file(source_file)
@name = source_file @name = File.basename(source_file)
end end
public public
@ -56,6 +66,11 @@ class Msftidy
puts "#{@name}#{line_msg} - [#{'ERROR'.red}] #{txt}" puts "#{@name}#{line_msg} - [#{'ERROR'.red}] #{txt}"
end end
def fixed(txt, line=0)
line_msg = (line>0) ? ":#{line.to_s}" : ''
puts "#{@name}#{line_msg} - [#{'FIXED'.green}] #{txt}"
end
## ##
# #
@ -234,12 +249,12 @@ class Msftidy
end end
end end
def test_old_rubies(f_rel) def test_old_rubies
return true unless CHECK_OLD_RUBIES return true unless CHECK_OLD_RUBIES
return true unless Object.const_defined? :RVM return true unless Object.const_defined? :RVM
puts "Checking syntax for #{f_rel}." puts "Checking syntax for #{@name}."
rubies ||= RVM.list_strings rubies ||= RVM.list_strings
res = %x{rvm all do ruby -c #{f_rel}}.split("\n").select {|msg| msg =~ /Syntax OK/} res = %x{rvm all do ruby -c #{@full_filepath}}.split("\n").select {|msg| msg =~ /Syntax OK/}
error("Fails alternate Ruby version check") if rubies.size != res.size error("Fails alternate Ruby version check") if rubies.size != res.size
end end
@ -411,14 +426,14 @@ class Msftidy
end end
end end
def run_checks(f_rel) def run_checks(full_filepath)
tidy = Msftidy.new(f_rel) tidy = Msftidy.new(full_filepath)
tidy.check_ref_identifiers tidy.check_ref_identifiers
tidy.check_old_keywords tidy.check_old_keywords
tidy.check_verbose_option tidy.check_verbose_option
tidy.check_badchars tidy.check_badchars
tidy.check_extname tidy.check_extname
tidy.test_old_rubies(f_rel) tidy.test_old_rubies
tidy.check_ranking tidy.check_ranking
tidy.check_disclosure_date tidy.check_disclosure_date
tidy.check_title_casing tidy.check_title_casing
@ -441,33 +456,11 @@ if dirs.length < 1
exit(1) exit(1)
end end
dirs.each { |dir| dirs.each do |dir|
f = nil Find.find(dir) do |full_filepath|
old_dir = nil next if full_filepath =~ /\.git[\x5c\x2f]/
next unless File.file? full_filepath
if dir next unless full_filepath =~ /\.rb$/
if File.file?(dir) run_checks(full_filepath)
# whoa, a single file!
f = File.basename(dir)
dir = File.dirname(dir)
end end
end
old_dir = Dir.getwd
Dir.chdir(dir)
dparts = dir.split('/')
else
dparts = []
end
# Only one file?
if f
run_checks(f)
else
# Do a recursive check of the specified directory
Dir.glob('**/*.rb') { |f|
run_checks(f)
}
end
Dir.chdir(old_dir)
}