Module: Redhat

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

Instance Method Summary collapse

Instance Method Details

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



10
11
12
13
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
# File 'lib/prm/rpm.rb', line 10

def build_rpm_repo(path,arch,release,gpg,silent)
    arch.peach do |a|
        release.peach do |r|
            full_path = "#{path}/#{r}/#{a}/"
            repo_path = "#{full_path}/repodata/"

            if !File.exists?(full_path)
                FileUtils.mkdir_p(full_path)
            end

            if !File.exists?(repo_path)
                FileUtils.mkdir(repo_path)
            end

            pkgnum = 0
            hpkgnum = Hash.new
            Dir.glob("#{full_path}/*.rpm").each do |file|
                pkgnum = pkgnum + 1
                hpkgnum.store(file, pkgnum)
            end

            primary_xml = Array.new
            filelists_xml = Array.new
            other_xml = Array.new
            package_count = 0

            Dir.glob("#{full_path}/*.rpm").peach do |file|
                package_count = package_count + 1
                time = Time.now
                sha256sum = Digest::SHA256.file(file).hexdigest
                rpm = RPM::File.new(file)
                filesize = File.size?(file)
                pkgmeta = Hash[*rpm.header.tags.collect { |t| [t.tag, t.value] }.inject([]) { |m,v| m + v }]
                start_header = rpm.lead.length + rpm.signature.length
                end_header = start_header + rpm.header.length
                pkgnum = hpkgnum[file]

                primary_xml << create_primary_xml(file, time, sha256sum, rpm, filesize, pkgmeta, start_header, end_header, pkgnum)
                other_xml << create_other_xml(file, time, sha256sum, rpm, filesize, pkgmeta, start_header, end_header, pkgnum)
                filelists_xml << create_filelists_xml(file, time, sha256sum, rpm, filesize, pkgmeta, start_header, end_header, pkgnum)
            end

            erb_files = %w{
                primary
                other
                filelists
            }

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

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

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

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

            Dir.glob("#{repo_path}/*.gz") { |f|
                FileUtils.rm(f)
            }

            erb_files.each { |file|
                xml_data_hash[file]["osize"] = File.size?("#{repo_path}/#{file}.xml")
                xml_data_hash[file]["xml"] = Digest::SHA256.file("#{repo_path}/#{file}.xml").hexdigest

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

                xml_data_hash[file]["size"] = File.size?("#{repo_path}/#{file}.xml.gz")
                xml_data_hash[file]["gz"] = Digest::SHA256.file("#{repo_path}/#{file}.xml.gz").hexdigest

                FileUtils.rm("#{repo_path}/#{file}.xml")
                FileUtils.mv("#{repo_path}/#{file}.xml.gz", "#{repo_path}/#{xml_data_hash[file]['gz']}-#{file}.xml.gz")
            }

            repomd_xml = Array.new
            timestamp = Time.now.to_i

            repomd_xml << create_repomd_xml(xml_data_hash,timestamp)
            erb_two = ERB.new(File.open("#{template_dir}/repomd.xml.erb", nil, "-") { |file|
                file.read
            }).result(binding)

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

            FileUtils.mv("#{repo_path}/repomd.xml.tmp", "#{repo_path}/repomd.xml")
            puts "Built Yum repository for #{r}\n"
        end
    end
end

#create_filelists_xml(file, time, sha256sum, rpm, filesize, pkgmeta, start_header, end_header, pkgnum) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/prm/rpm.rb', line 146

def create_filelists_xml(file, time, sha256sum, rpm, filesize, pkgmeta, start_header, end_header, pkgnum)
    init_filelists_data = String.new
    init_filelists_data <<
    %Q(<package pkgid="#{sha256sum}" name="#{pkgmeta[:name]}" arch="#{pkgmeta[:arch]}">
    <version epoch="0" ver="#{pkgmeta[:version]}" rel="#{pkgmeta[:release]}"/>\n\n)

    rpm.files.each do |file|
        init_filelists_data << %Q(        <file>#{file}</file>\n)
    end

    init_filelists_data <<
    %Q(</package>)
    return init_filelists_data
end

#create_other_xml(file, time, sha256sum, rpm, filesize, pkgmeta, start_header, end_header, pkgnum) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/prm/rpm.rb', line 161

def create_other_xml(file, time, sha256sum, rpm, filesize, pkgmeta, start_header, end_header, pkgnum)
    init_other_data = String.new
    init_other_data << 
    %Q(<package pkgid="#{sha256sum}" name="#{pkgmeta[:name]}" arch="#{pkgmeta[:arch]}">
       <version epoch="0" ver="#{pkgmeta[:version]}" rel="#{pkgmeta[:release]}"/>\n)
       init_other_data <<
       %Q(</package>)

       return init_other_data
end

#create_primary_xml(file, time, sha256sum, rpm, filesize, pkgmeta, start_header, end_header, pkgnum) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/prm/rpm.rb', line 172

def create_primary_xml(file, time, sha256sum, rpm, filesize, pkgmeta, start_header, end_header, pkgnum)
    time = time.to_i
    cut_file = File.basename(file)

    init_primary_data = String.new
    init_primary_data = %Q(<package type=\"rpm\">
    <name>#{pkgmeta[:name]}</name>
    <arch>#{pkgmeta[:arch]}</arch>
    <version epoch=\"0\" ver=\"#{pkgmeta[:version]}\" rel=\"#{pkgmeta[:release]}\"/>
    <checksum type=\"sha256\" pkgid=\"YES\">#{sha256sum}</checksum>
    <summary>#{pkgmeta[:summary]}</summary>
    <description>#{pkgmeta[:description]}</description>
    <packager></packager>
    <url>#{pkgmeta[:url]}</url>
    <time file=\"#{time}\" build=\"#{time}\"/>
    <size package=\"#{filesize}\" installed=\"\" archive=\"\"/>
    <location href=\"#{cut_file}\"/>
    <format>
    <rpm:license>#{pkgmeta[:license]}</rpm:license>
    <rpm:vendor/>
    <rpm:group>#{pkgmeta[:group]}</rpm:group>
    <rpm:buildhost>#{pkgmeta[:buildhost]}</rpm:buildhost>
    <rpm:sourcerpm>#{pkgmeta[:sourcerpm]}</rpm:sourcerpm>\n)
#        <rpm:header-range start=\"#{start_header}\" end=\"#{end_header}\"/ >\n)

    provide_primary_data = String.new
    if !rpm.provides.empty?
        provide_primary_data << "<rpm:provides>\n"
        rpm.provides.each do |prov|
            name = prov[1]
            prov[1].nil? ? flag = "" : flag = prov[1]
            prov[2].nil? ? version = "" && release = "" : (version,release = prov[2].split(/-/))
            provide_primary_data << 
            "<rpm:entry name=\"#{name}\" flags=\"#{flag}\" epoch=\"0\" ver=\"#{version}\" rel=\"#{release}\"/>\n"
        end
        provide_primary_data << "</rpm:provides>\n"
    end

    init_primary_data + provide_primary_data

    require_primary_data = String.new
    if !rpm.requires.empty?
        require_primary_data << "<rpm:requires>\n"
        rpm.requires.each do |req|
            next if req[0] =~ /^rpmlib/
                name = req[0]
            req[1].nil? ? flag = "" : flag = req[1]
            req[2].nil? ? version = "" && release = "" : (version,release = req[2].split(/-/))
            require_primary_data << 
            "<rpm:entry name=\"#{name}\" flags=\"#{flag}\" epoch=\"0\" ver=\"#{version}\" rel=\"#{release}\"/>\n"
        end
        require_primary_data << "</rpm:requires>\n"
    end

    init_primary_data + require_primary_data

    conflict_primary_data = String.new
    if !rpm.conflicts.empty?
        conflict_primary_data << "<rpm:conflicts>\n"
        rpm.conflicts.each do |con|
            name = con[0]
            conflict_primary_data <<
            "<rpm:entry name=\"#{name}\">\n"
        end
        conflict_primary_data << "</rpm:conflicts>\n"
    end

    init_primary_data + conflict_primary_data

    files_primary_data = String.new
    rpm.files.each do |file|
        files_primary_data <<
        "<file>#{file}</file>"
    end

    init_primary_data + files_primary_data

    init_primary_data << 
    %Q(        </format>\n) + %Q(</package>)

    return init_primary_data
end

#create_repomd_xml(xml_data_hash, timestamp) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/prm/rpm.rb', line 130

def create_repomd_xml(xml_data_hash,timestamp)
    repomd_meta = String.new
    xml_data_hash.each_pair do |k,v|
        repomd_meta <<
        %Q(<data type="#{k}">
            <checksum type="sha256">#{v["gz"]}</checksum>
            <open-checksum type="sha256">#{v["xml"]}</open-checksum>
            <location href="repodata/#{v["gz"]}-#{k}.xml.gz"/>
            <timestamp>#{timestamp}</timestamp>
            <size>#{v["size"]}</size>
            <open-size>#{v["osize"]}</open-size>
        </data>)
    end
    return repomd_meta
end