2013-08-07 16:34:43 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# -*- coding: binary -*-
|
|
|
|
|
|
|
|
# Replace leading tabs with 2-width spaces.
|
|
|
|
# I'm sure there's a sed/awk/perl oneliner that's
|
|
|
|
# a million times better but this is more readable for me.
|
2013-09-03 16:54:31 +00:00
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# metasploit-framework$ ./tools/dev/retab.rb [path]
|
|
|
|
#
|
|
|
|
# If local backups are desired, prepend with "MSF_RETAB_BACKUPS" set,
|
|
|
|
# like so:
|
|
|
|
# metasploit-framework$ MSF_RETAB_BACKUPS=1 ./tools/dev/retab.rb [path]
|
2013-08-07 16:34:43 +00:00
|
|
|
|
|
|
|
require 'fileutils'
|
|
|
|
require 'find'
|
|
|
|
|
|
|
|
dir = ARGV[0] || "."
|
2013-09-03 16:54:31 +00:00
|
|
|
keep_backups = !!(ENV['MSF_RETAB_BACKUPS'] || ENV['MSF_RETAB_BACKUP'])
|
|
|
|
puts "Keeping .notab backups" if keep_backups
|
|
|
|
|
2013-08-07 16:34:43 +00:00
|
|
|
raise ArgumentError, "Need a filename or directory" unless (dir and File.readable? dir)
|
|
|
|
|
2013-09-13 15:25:26 +00:00
|
|
|
def is_ruby?(fname)
|
2013-09-13 15:51:50 +00:00
|
|
|
return true if fname =~ /\.rb$/
|
2013-09-13 15:25:26 +00:00
|
|
|
file_util = ""
|
|
|
|
begin
|
|
|
|
file_util = %x{which file}.to_s.chomp
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
end
|
|
|
|
if File.executable? file_util
|
|
|
|
file_fingerprint = %x{#{file_util} #{fname}}
|
|
|
|
!!(file_fingerprint =~ /Ruby script/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-07 16:34:43 +00:00
|
|
|
Find.find(dir) do |infile|
|
|
|
|
next unless File.file? infile
|
2013-09-13 15:25:26 +00:00
|
|
|
next unless is_ruby? infile
|
2013-09-13 15:15:05 +00:00
|
|
|
outfile = infile
|
2013-09-03 16:54:31 +00:00
|
|
|
|
2013-09-13 15:15:05 +00:00
|
|
|
if keep_backups
|
|
|
|
backup = "#{infile}.notab"
|
|
|
|
FileUtils.cp infile, backup
|
2013-08-07 16:34:43 +00:00
|
|
|
end
|
2013-09-13 15:15:05 +00:00
|
|
|
|
|
|
|
data = File.open(infile, "rb") {|f| f.read f.stat.size}
|
|
|
|
fixed = []
|
|
|
|
data.each_line do |line|
|
|
|
|
fixed << line
|
|
|
|
next unless line =~ /^\x09/
|
|
|
|
index = []
|
|
|
|
i = 0
|
|
|
|
line.each_char do |char|
|
|
|
|
break unless char =~ /[\x20\x09]/
|
|
|
|
index << i if char == "\x09"
|
|
|
|
i += 1
|
|
|
|
end
|
|
|
|
index.reverse.each do |idx|
|
|
|
|
line[idx] = " "
|
|
|
|
end
|
|
|
|
fixed[-1] = line
|
2013-08-07 16:34:43 +00:00
|
|
|
end
|
|
|
|
|
2013-09-13 15:15:05 +00:00
|
|
|
fh = File.open(outfile, "wb")
|
|
|
|
fh.write fixed.join
|
|
|
|
fh.close
|
|
|
|
puts "Retabbed #{fh.path}"
|
2013-08-07 16:34:43 +00:00
|
|
|
end
|