class based

This commit is contained in:
James Bowes 2012-07-26 14:21:16 -03:00
parent 4e0f638cd2
commit 606b0ea5e6

View file

@ -12,6 +12,39 @@ end
# usage: ./content_from_pem.rb 5286016419950084643.pem # usage: ./content_from_pem.rb 5286016419950084643.pem
class Node
attr_accessor :path, :children
def initialize(path)
@path = path
@children = []
end
def has_key?(key)
@children.each do |child|
if child.path == key
return true
end
end
return false
end
def get_child(name)
@children.each do |child|
if child.path == name
return child
end
end
return nil
end
def to_json(*a)
{
@path => @children
}.to_json(*a)
end
end
def akamai_hex_to_content_set(akamai_hex) def akamai_hex_to_content_set(akamai_hex)
gzipped_hex = akamai_hex.gsub(":","").chomp("00") gzipped_hex = akamai_hex.gsub(":","").chomp("00")
gzipped_data = [gzipped_hex].pack("H*") gzipped_data = [gzipped_hex].pack("H*")
@ -25,28 +58,28 @@ def akamai_hex_to_content_set(akamai_hex)
return content_sets return content_sets
end end
def mk_hash(sgmts, hash = nil) def mk_hash(sgmts, parent)
hash = {} unless hash.kind_of? Hash
segment = sgmts.shift segment = sgmts.shift
return hash if segment.nil? return parent if segment.nil?
unless hash.has_key?(segment) unless parent.has_key?(segment)
hash[segment] = mk_hash(sgmts, {}) parent.children << mk_hash(sgmts, Node.new(segment))
else else
hash[segment].update(mk_hash(sgmts, hash[segment])) mk_hash(sgmts, parent.get_child(segment))
# else
# hash[segment].update(mk_hash(sgmts, hash[segment]))
end end
return hash return parent
end end
def compress_prefix(hash) def compress_prefix(parent)
hash.keys.each do |key| parent.children.each do |child|
hash[key] = compress_prefix(hash[key]) compress_prefix(child)
if hash[key].length == 1
new_key = key + "/" + hash[key].keys[0]
hash[new_key] = hash[key].values[0]
hash.delete(key)
end
end end
return hash if parent.children.length == 1
parent.path += "/" + parent.children[0].path
parent.children = parent.children[0].children
end
return parent
end end
def binary_write(file, hash) def binary_write(file, hash)
@ -81,17 +114,17 @@ if $0 == __FILE__
file.write(sets) file.write(sets)
end end
File.open(json_name, "w+") do |file| File.open(json_name, "w+") do |file|
h = {} parent = Node.new("")
sets.each do |set| sets.each do |set|
line = set.start_with?("/") ? set[1..-1] : set line = set.start_with?("/") ? set[1..-1] : set
# => ["content", "beta", "rhel", "server", "6", "$releasever", "$basearch", "scalablefilesystem", "debug"] # => ["content", "beta", "rhel", "server", "6", "$releasever", "$basearch", "scalablefilesystem", "debug"]
chunks = line.split("/") chunks = line.split("/")
h = mk_hash(chunks, h) parent = mk_hash(chunks, parent)
end end
h = compress_prefix(h) h = compress_prefix(parent)
binary_write(file, h) # binary_write(file, parent)
# file.write(h.to_json) file.write(parent.to_json)
end end
puts "Wrote:\n [%d] %s\n [%d] %s" % [File.size(txt_name), txt_name, File.size(json_name), json_name] puts "Wrote:\n [%d] %s\n [%d] %s" % [File.size(txt_name), txt_name, File.size(json_name), json_name]