require 'nokogiri' require 'digest/sha1' require 'zlib' module RightPublish class ZyppRepo < YumRepo REPO_KEY = :zypp_repo private def regen_metadata(repo_path) super(repo_path) do_in_subdir(repo_path) do SuseMetadata.new.add end end class Package attr_reader :checksum attr_reader :name attr_reader :arch attr_reader :ver attr_reader :rel def initialize(rpmfile) @checksum = Digest::SHA1.hexdigest(File.new(rpmfile).read) rpm = File.basename(rpmfile, ".rpm") rpm, _, @arch = rpm.rpartition(".") rpm, _,@rel = rpm.rpartition("-") @name, _,@ver = rpm.rpartition("-") end end # http://en.opensuse.org/openSUSE:Standards_Rpm_Metadata#SUSE_primary_data_.28susedata.xml.29.2C_extensions_to_primary.xml class SuseMetadata def initialize @packages = Dir["*.rpm"].map { |rpmfile| Package.new(rpmfile) } end def href "repodata/susedata.xml.gz" end def suse_data @suse_data ||= Nokogiri::XML::Builder.new do |xml| xml.susedata("packages" => @packages.length) do |susedata| @packages.each do |package| susedata.package('pkgid' => package.checksum, 'name' => package.name) do |pkg| pkg.version('ver' => package.ver, 'rel' => package.rel, 'arch' => package.arch, 'epoch' => 0) pkg.keyword("support_l3") end end end end.to_xml end def create susedata_file = File.join("repodata", "susedata.xml.gz") Zlib::GzipWriter.open(susedata_file) { |file| file.write(suse_data) } @checksum = Digest::SHA256.hexdigest(File.open(susedata_file).read) @open_checksum = Digest::SHA256.hexdigest(suse_data) end def update_repomd repomd = File.join("repodata", "repomd.xml") doc = Nokogiri::XML(File.open(repomd)) Nokogiri::XML::Builder.with(doc.at_css("repomd")) do |xml| xml.data('type' => 'susedata') do |data| data.timestamp(File.ctime(href).to_i) data.checksum({ 'type' => 'sha256' }, @checksum) data.location('href' => href) data.send(:"open-checksum", { 'type' => 'sha256' }, @open_checksum) end end File.open(repomd, "w") { |f| doc.write_xml_to f } end def add create update_repomd end end end RightPublish::RepoManager.register_repo(ZyppRepo) end