Skip to content

Instantly share code, notes, and snippets.

@tkfm-yamaguchi
Created August 13, 2013 05:43
Show Gist options
  • Save tkfm-yamaguchi/6218189 to your computer and use it in GitHub Desktop.
Save tkfm-yamaguchi/6218189 to your computer and use it in GitHub Desktop.
require 'yaml'
require 'json'
require 'pathname'
BASEDIR = Pathname(File.expand_path(File.dirname(__FILE__)))
CONFPATH = BASEDIR.join("config.yaml")
result = {}
configs = YAML.load_file(CONFPATH)
configs.each do |conf|
filetype = conf["filetype"]
root_path = Pathname(conf["root_dir"])
path_patterns = conf["path_patterns"]
if root_path.relative?
root_path = CONFPATH.dirname.join(root_path)
end
root_path.find do |filepath|
next unless filepath.file?
path_patterns.each do |regex|
if filepath.to_s =~ %r|#{regex}|
size = filepath.size
data_id = $~[:data_id]
code = $~.names.member?("code") ? $~[:code] : "00"
result[data_id] ||= {}
result[data_id][code] ||= []
result[data_id][code] << {
name: filetype,
path: filepath.to_s,
size: size
}
end
end
end
end
puts result.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment