class RankMirror::Config

Public Class Methods

new(options) click to toggle source
# File lib/rankmirror/config.rb, line 8
def initialize(options)
        @options = options
        @name = @options.os
        @file = @name + ".mirrorlist"
        @systempath = File.expand_path(File.join(File.dirname(__FILE__),"mirrorlists"))
        @localpath = ENV['HOME'] + "/.rankmirror/"
end

Public Instance Methods

parse(config,args) click to toggle source
# File lib/rankmirror/config.rb, line 47
def parse(config,args)
        f = open(config)
        mirrors = f.readlines.map!{|l|
                unless l.start_with?("#")
                        elements = l.strip.split("\t")
                        mirror = OpenStruct.new
                        args.each_with_index {|arg,index| mirror[arg] = elements[index] }
                        mirror
                end
        }.compact
        f.close
        return mirrors
end
path() click to toggle source
# File lib/rankmirror/config.rb, line 16
def path
        if File.exist?(systemconfig) && File.readable?(systemconfig)
                if File.exist?(localconfig)
                        m = merge(systemconfig,localconfig)
                        write(m,localconfig,@options.keys)
                        localconfig
                else
                        systemconfig
                end
        else
                if File.exist?(localconfig)
                        localconfig
                else
                        nil
                end
        end
end
save(array,header) click to toggle source
# File lib/rankmirror/config.rb, line 34
def save(array,header)
        FileUtils.mkdir_p @localpath unless File.directory?(@localpath)
        mirrors_array = array.map! do |uri|
                mirror = RankMirror::Status.new(uri,@name).get
                mirror.name = URI.parse(uri).host if mirror.name.nil?
                mirror.continent = "world" if ["opensuse","packman"].include?(@name)
                mirror.country = "world"
                mirror.http = uri
                mirror
        end
        write(mirrors_array,localconfig,header)
end

Private Instance Methods

localconfig() click to toggle source
# File lib/rankmirror/config.rb, line 63
def localconfig
        File.join(@localpath,@file)
end
merge(systemconfig,localconfig) click to toggle source
# File lib/rankmirror/config.rb, line 71
def merge(systemconfig,localconfig)
        system_mirrors = parse(systemconfig,@options.keys)
        local_mirrors = parse(localconfig,@options.keys)
        system_mirrors.each do |system_mirror|
                http_match = false
                local_mirrors.each do |local_mirror|
                        if system_mirror.http == local_mirror.http
                                http_match = true
                                local_mirror.continent = system_mirror.continent if ["opensuse","packman"].include?(@name)
                                local_mirror.country = system_mirror.country
                        end
                end
                if http_match == false
                        local_mirrors << system_mirror
                end
        end
        return local_mirrors
end
process_header(header) click to toggle source
# File lib/rankmirror/config.rb, line 107
def process_header(header)
        str = "#"
        header.each do |title|
                if title == header[header.size - 1]
                        str << title + "\n"
                else
                        str << title + "\t"
                end
        end
        return str
end
systemconfig() click to toggle source
# File lib/rankmirror/config.rb, line 67
def systemconfig
        File.join(@systempath,@file)
end
write(mirrors_array,file,header) click to toggle source
# File lib/rankmirror/config.rb, line 90
def write(mirrors_array,file,header)
        f = open(file,'w')
        f.write process_header(header)
        mirrors_array.each do |m|
                str = String.new
                header.each do |item|
                        if header.index(item) == header.size - 1
                                str << m[item].to_s + "\n"
                        else
                                str << m[item].to_s + "\t"
                        end
                end
                f.write str
        end
        f.close
end