Module: Registration::RegistrationCodesLoader

Includes:
Yast::I18n, Yast::Transfer::FileFromUrl
Included in:
Storage::RegCodes
Defined in:
src/lib/registration/registration_codes_loader.rb

Overview

Aids Registration::Storage::RegCodes in loading the codes from external storage so that the user does not have to type them each time when doing multiple installations. FATE#316796

Constant Summary collapse

REGCODES_NAME_HANDLERS =
{
  "regcodes.xml" => :reg_codes_from_xml,
  "regcodes.txt" => :reg_codes_from_txt
}.freeze

Instance Method Summary collapse

Instance Method Details

#parse_xml(xml_hash) ⇒ Hash{String => String}?


77
78
79
80
81
82
83
84
# File 'src/lib/registration/registration_codes_loader.rb', line 77

def parse_xml(xml_hash)
  suse_register = xml_hash.fetch("suse_register", {})
  addons = suse_register.fetch("addons", [])
  pairs = addons.map do |a|
    [a.fetch("name", ""), a.fetch("reg_code", "")]
  end
  Hash[pairs]
end

#reg_codes_from_txt(filename) ⇒ Hash{String => String}?

The format is: lines with a key and a value, separated by white space


90
91
92
93
94
95
96
97
98
# File 'src/lib/registration/registration_codes_loader.rb', line 90

def reg_codes_from_txt(filename)
  return nil unless File.readable?(filename) && File.file?(filename)
  text = File.read(filename)
  # TODO: report parse errors in log
  pairs = text.each_line.map do |l|
    l.chomp.split(/\s+/, 2)
  end
  Hash[pairs.reject(&:empty?)]
end

#reg_codes_from_usb_stickHash{String => String}?


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'src/lib/registration/registration_codes_loader.rb', line 43

def reg_codes_from_usb_stick
  with_tempfile("regcodes-") do |path|
    REGCODES_NAME_HANDLERS.each do |name, handler|
      next unless get_file_from_url(scheme: "usb", host: "",
                                    urlpath: "/#{name}",
                                    localfile: path,
                                    urltok: {}, destdir: "")
      codes = send(handler, path)
      return codes if codes
    end
  end
  nil
end

#reg_codes_from_xml(filename) ⇒ Hash{String => String}?


69
70
71
72
73
# File 'src/lib/registration/registration_codes_loader.rb', line 69

def reg_codes_from_xml(filename)
  return nil unless File.readable?(filename) && File.file?(filename)
  xml_hash = Yast::XML.XMLToYCPFile(filename)
  parse_xml(xml_hash)
end

#with_tempfile(pattern) {|actual| ... } ⇒ Object

Yield Parameters:

  • actual

    file name


59
60
61
62
63
64
65
# File 'src/lib/registration/registration_codes_loader.rb', line 59

def with_tempfile(pattern, &block)
  tempfile = Tempfile.new(pattern)
  block.call(tempfile.path)
ensure
  tempfile.close
  tempfile.unlink
end