Module: Redhat

Included in:
PRM::Repo
Defined in:
lib/prm/repo.rb

Instance Method Summary collapse

Instance Method Details

#build_rpm_repo(path, arch, release, gpg, silent) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/prm/repo.rb', line 14

def build_rpm_repo(path,arch,release,gpg,silent)
  release.each { |r|
    arch.each { |a|
      orgpath = path
      path = path + "/repodata/"
      FileUtils.mkpath(path)
      timestamp = Time.now.to_i
      package_hash = Hash.new

      template_dir = File.join(File.dirname(__FILE__), "..", "..", "templates")

      erb_files = %w{
        filelists
        other
        primary
      }

      Dir.glob("#{orgpath}/*.rpm").peach do |rpm|
        sha256sum = ''
        sha256_path = "#{orgpath}/sha256_results"
        unless File.directory?(sha256_path)
          FileUtils.mkdir(sha256_path)
        end
        trpm = rpm.split('/').last
        if File.exists?("#{sha256_path}/#{trpm}")
          file = File.open("#{sha256_path}/#{trpm}", 'r')
          sha256sum = file.read
          file.close
        else
          sha256sum = Digest::SHA256.file(rpm).hexdigest
          File.open("#{sha256_path}/#{trpm}", 'w') { |file| file.write(sha256sum) }
        end

        nrpm = RPM::File.new(rpm)
        sheader = nrpm.lead.length + nrpm.signature.length
        eheader = sheader + nrpm.header.length
        
        info = Hash[*nrpm.header.tags.collect { |t| [t.tag, t.value] }.inject([]) { |m,v| m + v }]
        package_hash[trpm] = {
            "sha256"       => sha256sum,
            "name"         => info[:name],
            "arch"         => info[:arch],
            "version"      => info[:version],
            "release"      => info[:release],
            "start_header" => sheader,
            "end_header"   => eheader,
            "files"        => nrpm.files,
            "license"      => info[:license],
            "vendor"       => info[:vendor],
            "buildtime"    => info[:buildtime],
            "buildhost"    => info[:buildhost],
            "requires"     => nrpm.requires,
            "conflicts"    => nrpm.conflicts,
            "provides"     => nrpm.provides,
            "epoch"        => info[:epochnum],
            "sourcerpm"    => info[:sourcerpm],
            "buildtime"    => info[:buildtime],
            "installtime"  => info[:installtime],
            "url"          => info[:url],
            "summary"      => info[:summary],
            "description"  => info[:description],
            "packager"     => info[:packager],
            "size"         => info[:size],
            "longsize"     => info[:longsize],
            "filesizes"    => info[:filesizes]
        }
        
      end

      xml_hash = Hash.new()
      xml_hash = {
        "filelists" => {
          "xml"   => "",
          "gz"    => "",
          "size"  => "",
          "osize" => "",
        },
        "other"     => {
          "xml"   => "",
          "gz"    => "",
          "size"  => "",
          "osize" => "",
        },
        "primary"   => {
          "xml"   => "",
          "gz"    => "",
          "size"  => "",
          "osize" => "",
        }
      }

      erb_files.each { |f|
        erb = ERB.new(File.open("#{template_dir}/#{f}.xml.erb") { |file|
          file.read
        }).result(binding)

        release_file = File.new("#{path}/#{f}.xml.tmp","wb")
        release_file.puts erb
        release_file.close 

        Zlib::GzipWriter.open("#{path}/#{f}.xml.tmp.gz") do |gz|
          ff = File.new("#{path}/#{f}.xml.tmp", "r")
          ff.each do |line|
            gz.write(line)
          end
        end
      }

      erb_files.each { |f|  
        xml_hash[f]["gz"] = Digest::SHA256.file("#{path}/#{f}.xml.tmp.gz").hexdigest
        xml_hash[f]["xml"] = Digest::SHA256.file("#{path}/#{f}.xml.tmp").hexdigest
        xml_hash[f]["size"] = File.size?("#{path}/#{f}.xml.tmp.gz")
        xml_hash[f]["osize"] = File.size?("#{path}/#{f}.xml.tmp")
      }

      erb_two = ERB.new(File.open("#{template_dir}/repomd.xml.erb") { |file|
        file.read
      }).result(binding)

      r_file = File.new("#{path}/repomd.xml.tmp","wb")
      r_file.puts erb_two
      r_file.close

      Dir.glob("#{path}/*.gz") do |f|
        if f =~ /gz$/
          unless f =~ /tmp\.gz$/
            FileUtils.rm(f)
          end
        end
      end

      erb_files.each { |f| 
        FileUtils.move("#{path}/#{f}.xml.tmp.gz", "#{path}/" + xml_hash[f]["gz"] + "-#{f}.xml.gz")
        FileUtils.rm("#{path}/#{f}.xml.tmp")
      }
      FileUtils.move("#{path}/repomd.xml.tmp", "#{path}/repomd.xml")
    }
  }
end