ricardo-larosa
9/14/2016 - 10:32 AM

pathfinder.rb

require 'find'

def parse_config config_file

raise Errno::EACCES, "#{config_file} Does not exists." unless File.exist?(config_file) 

 params = Hash.new

 File.open(config_file, "r") do |file|
   file.each_line do |line|
    line.scan(/^(\w+)\s*=\s*"(.*)";/){ |key,value| params[key] = value}
    line.scan(/^\s*(\w+)\s*=\s*([a-z0-9]+);/){ |key,value| params[key] = value}
   end
 end
return params

end

def pathfind root, type

raise Errno::EACCES, "#{root} does not exists." unless Dir.exist?(root)

paths = Hash.new
count = 0

Find.find(root) do |f|
    count += 1
    #puts f
    if File.basename(f) == 'parms.cfg'
      #puts "found!"
      name = File.dirname(f)+"/"+File.basename(f)
      list = parse_config name
      url = list['extHostName']
      paths[url] = File.dirname(f)
      Find.prune
    elsif File.path(f) == root || File.basename(f) =~ /#{type}/i
      next
    else
      Find.prune
    end
end
return paths, count
end

p pathfind '/home/ricardo/tmp','lms'
#p parse_config './parms.cfg'