Class: Rpm

Inherits:
CliFuncs show all
Defined in:
lib/cli_funcs_rpm.rb

Defined Under Namespace

Classes: RpmDependencies, RpmProvides

Instance Attribute Summary collapse

Attributes inherited from CliFuncs

#base_dir, #data_dir, #ignore_bad_exit, #output

Instance Method Summary collapse

Methods inherited from CliFuncs

#clear_values, #flag_add, #flags_run, #run_and_capture, #set_dirs

Constructor Details

#initializeRpm

Returns a new instance of Rpm.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cli_funcs_rpm.rb', line 11

def initialize
  super
  @utility ||= CliUtils.new("rpm").utility_path
  @packages_dir ||= SYSTEM_CONFIG["packages_dir"]
  @default_dist ||= SYSTEM_CONFIG["default_dist"]
  @rpm_file ||= ""
  @fpp = ""
  @extract_dir = ""
  @final_args = Array.new
  @control = Hash.new
  @output_filter_control = Array.new
  @dependencies = Hash.new
  @provides = Hash.new
  @filelist = Array.new
  @scripts = Hash.new
end

Instance Attribute Details

#controlObject (readonly)

Returns the value of attribute control.



9
10
11
# File 'lib/cli_funcs_rpm.rb', line 9

def control
  @control
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



9
10
11
# File 'lib/cli_funcs_rpm.rb', line 9

def dependencies
  @dependencies
end

#extract_dirObject

Returns the value of attribute extract_dir.



8
9
10
# File 'lib/cli_funcs_rpm.rb', line 8

def extract_dir
  @extract_dir
end

#filelistObject (readonly)

Returns the value of attribute filelist.



9
10
11
# File 'lib/cli_funcs_rpm.rb', line 9

def filelist
  @filelist
end

#flags_allObject

Returns the value of attribute flags_all.



8
9
10
# File 'lib/cli_funcs_rpm.rb', line 8

def flags_all
  @flags_all
end

#fppObject (readonly)

Returns the value of attribute fpp.



9
10
11
# File 'lib/cli_funcs_rpm.rb', line 9

def fpp
  @fpp
end

#providesObject (readonly)

Returns the value of attribute provides.



9
10
11
# File 'lib/cli_funcs_rpm.rb', line 9

def provides
  @provides
end

#rpm_fileObject

Returns the value of attribute rpm_file.



8
9
10
# File 'lib/cli_funcs_rpm.rb', line 8

def rpm_file
  @rpm_file
end

#scriptsObject (readonly)

Returns the value of attribute scripts.



9
10
11
# File 'lib/cli_funcs_rpm.rb', line 9

def scripts
  @scripts
end

Instance Method Details

#cmd_runObject



306
307
308
309
310
# File 'lib/cli_funcs_rpm.rb', line 306

def cmd_run
  u = CliUtils.new(@utility)
  puts [u.utility_path, flags_run, @rpm_file, @final_args].flatten.inspect if DEBUG
  [u.utility_path, flags_run, @rpm_file, @final_args].flatten
end

#do_extractObject



48
49
50
51
52
# File 'lib/cli_funcs_rpm.rb', line 48

def do_extract
  prepare_package_dir
  extract_package
  write_info
end

#extract_packageObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cli_funcs_rpm.rb', line 54

def extract_package
  # We have to copy the package to the files directory before we can run the rpm2cpio command
  begin
    FileUtils.cp(@rpm_file, "#{@fpp}/files")
  rescue Exception => e
    puts "Tried to copy #{@rpm_file} to #{@fpp}/files during Rpm.extract_package, received exception: #{e}"
  end

  # This should fill up @output with the archive content
  rpm2cpio

  # Move the rpm to the redhat directory for safe keeping
  begin
    FileUtils.mv("#{@fpp}/files/#{@rpm_file.split("/")[-1]}","#{@fpp}/redhat")
  rescue Exception => e
    puts "Tried to move #{@fpp}/files/#{@rpm_file.split("/")[-1]} to #{@fpp}/redhat during Rpm.extract_package, received exception: #{e}"
  end

end

#flags_query_package_controlObject



312
313
314
# File 'lib/cli_funcs_rpm.rb', line 312

def flags_query_package_control
  flag_add("-qpi")
end

#flags_query_package_dependenciesObject



316
317
318
# File 'lib/cli_funcs_rpm.rb', line 316

def flags_query_package_dependencies
  flag_add("-qpR")
end

#flags_query_package_filelistObject



325
326
327
328
# File 'lib/cli_funcs_rpm.rb', line 325

def flags_query_package_filelist
  flag_add("-qp")
  flag_add("--list")
end

#flags_query_package_providesObject



320
321
322
323
# File 'lib/cli_funcs_rpm.rb', line 320

def flags_query_package_provides
  flag_add("-qp")
  flag_add("--provides")
end

#flags_query_package_scriptsObject



330
331
332
333
# File 'lib/cli_funcs_rpm.rb', line 330

def flags_query_package_scripts
  flag_add("-qp")
  flag_add("--scripts")
end

#get_dependencyObject



122
123
# File 'lib/cli_funcs_rpm.rb', line 122

def get_dependency
end

#output_process_controlObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/cli_funcs_rpm.rb', line 239

def output_process_control
  @output.each do |line|
    @output_filter_control.each do |pattern|
      if line =~ /#{pattern}/
        if(($1 and $2) and ($3 and $4))
          @control[$1.strip] = $2.strip
          @control[$3.strip] = $4.strip
        elsif($1 and $2)
          @control[$1.strip] = $2.strip
        end
      end
    end
  end
end

#output_process_dependenciesObject



254
255
256
257
258
259
260
261
262
263
# File 'lib/cli_funcs_rpm.rb', line 254

def output_process_dependencies
  @output.each do |line|
    pkg,ver = line.strip.chomp.split(/\s/, 2)
    if(ver == nil)
      @dependencies[pkg] = nil
    else
      @dependencies[pkg] = ver
    end
  end
end

#output_process_filelistObject



265
266
267
268
269
# File 'lib/cli_funcs_rpm.rb', line 265

def output_process_filelist
  @output.each do |line|
    @filelist.push(line.chomp)
  end
end

#output_process_providesObject



271
272
273
274
275
276
277
278
279
280
# File 'lib/cli_funcs_rpm.rb', line 271

def output_process_provides
  @output.each do |line|
    pkg,ver = line.strip.chomp.split(" = ", 2)
    if(ver == nil)
      @provides[pkg] = nil
    else
      @provides[pkg] = ver
    end
  end
end

#output_process_scriptsObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/cli_funcs_rpm.rb', line 221

def output_process_scripts
  script = ""

  @output.each do |line|
    next if line.length == 0
    if line =~ /^(\w+)\sscriptlet\s/
      script = $1
      puts "SCRIPT: #{script}" if DEBUG
      next
    end
    if(@scripts[script] == nil)
      @scripts[script] = line
    else
      @scripts[script] << line
    end
  end
end

#prepare_package_dirObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cli_funcs_rpm.rb', line 158

def prepare_package_dir
  group = @control["Group"].gsub(" ","_")
  name = @control["Name"]
  version = "#{@control["Version"]}-#{@control["Release"]}"
  arch = @rpm_file.split(".")[-2]
  @fpp = "#{@packages_dir}/#{@default_dist}/#{group}/#{name}.#{arch}/#{version}"
  begin
    FileUtils.mkdir_p "#{@fpp}/files"
  rescue Exception => e
    puts "Tried to create #{@fpp}/files during 'Rpm.prepare_package_dir', received exception: #{e}"
  end
  begin
    FileUtils.mkdir_p "#{@fpp}/redhat"
  rescue Exception => e
    puts "Tried to create #{@fpp}/redhat during 'Rpm.prepare_package_dir', received exception: #{e}"
  end
end

#rpmObject



34
35
36
# File 'lib/cli_funcs_rpm.rb', line 34

def rpm
  run_and_capture(cmd_run)
end

#rpm2cpioObject



38
39
40
41
42
43
44
45
46
# File 'lib/cli_funcs_rpm.rb', line 38

def rpm2cpio
  r = Rpm2Cpio.new
  r.rpm_file = @rpm_file
  r.rpm2cpio
  c = Cpio.new
  c.cpio_data = r.output
  c.extract_dir = "#{@fpp}/files"
  c.cpio
end

#set_controlObject



213
214
215
216
217
218
219
# File 'lib/cli_funcs_rpm.rb', line 213

def set_control
  clear_values
  set_output_filter_control
  flags_query_package_control
  rpm
  output_process_control
end

#set_dependenciesObject



206
207
208
209
210
211
# File 'lib/cli_funcs_rpm.rb', line 206

def set_dependencies
  clear_values
  flags_query_package_dependencies
  rpm
  output_process_dependencies
end

#set_filelistObject



199
200
201
202
203
204
# File 'lib/cli_funcs_rpm.rb', line 199

def set_filelist
  clear_values
  flags_query_package_filelist
  rpm
  output_process_filelist
end

#set_infoObject



176
177
178
179
180
181
182
183
# File 'lib/cli_funcs_rpm.rb', line 176

def set_info
  initialize
  set_control
  set_dependencies
  set_provides
  set_filelist
  set_scripts
end

#set_output_filter_controlObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/cli_funcs_rpm.rb', line 282

def set_output_filter_control
  # Name / Relocations
  @output_filter_control.push("^(Name)\s+: (.*)\s+(Relocations): (.*)$")
  # Version / Vendor
  @output_filter_control.push("^(Version)\s+: (.*)\s+(Vendor): (.*)$")
  # Release / Build Date
  @output_filter_control.push("^(Release)\s+: (.*)\s+(Build Date): (.*)$")
  # Install Date / Build Host
  @output_filter_control.push("^(Install Date): (.*)\s+(Build Host): (.*)$")
  # Group  / Source RPM
  @output_filter_control.push("^(Group)\s+: (.*)\s+(Source RPM): (.*)$")
  # Size / License
  @output_filter_control.push("^(Size)\s+: (.*)\s+(License): (.*)$")
  # Signature
  @output_filter_control.push("^(Signature)\s+: (.*)$")
  # Packager
  @output_filter_control.push("^(Packager)\s+: (.*)$")
  # URL
  @output_filter_control.push("^(URL)\s+: (.*)$")
  # Summary
  @output_filter_control.push("^(Summary)\s+: (.*)$")
  # We are not capturing 'Description' right now since there's no need
end

#set_providesObject



192
193
194
195
196
197
# File 'lib/cli_funcs_rpm.rb', line 192

def set_provides
  clear_values
  flags_query_package_provides
  rpm
  output_process_provides
end

#set_scriptsObject



185
186
187
188
189
190
# File 'lib/cli_funcs_rpm.rb', line 185

def set_scripts
  clear_values
  flags_query_package_scripts
  rpm
  output_process_scripts
end

#write_controlObject



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cli_funcs_rpm.rb', line 137

def write_control
  begin
    f = open("#{@fpp}/redhat/info", 'w+')
    @control.each_pair do |k,v|
      f.puts "#{k}: #{v}"
    end
    f.close
  rescue Exception => e
    puts "Tried to open file #{@fpp}/redhat/#{k} for writing during 'Rpm.write_control', received exception: #{e}"
  end
end

#write_dependencies_to_dbObject



108
109
110
111
112
113
# File 'lib/cli_funcs_rpm.rb', line 108

def write_dependencies_to_db
  arch = @rpm_file.split(".")[-2]
  @dependencies.each_pair do |k,v|
    RpmDependencies.create(:dependency => k, :version => v, :rpm => @rpm_file.split("/")[-1], :arch => arch)
  end
end

#write_dependencies_to_fileObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cli_funcs_rpm.rb', line 96

def write_dependencies_to_file
  begin
    f = open("#{@fpp}/redhat/dependencies", 'w+')
    @dependencies.each_pair do |k,v|
      f.puts "#{k} #{v}"
    end
    f.close
  rescue Exception => e
    puts "Tried to open file #{@fpp}/redhat/dependencies for writing during 'Rpm.write_dependencies', received exception: #{e}"
  end
end

#write_filelistObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cli_funcs_rpm.rb', line 84

def write_filelist
  begin
    f = open("#{@fpp}/redhat/filelist", 'w+')
    @filelist.each do |file|
      f.puts file
    end
    f.close
  rescue Exception => e
    puts "Tried to open file #{@fpp}/redhat/filelist for writing during 'Rpm.write_filelist', received exception: #{e}"
  end
end

#write_infoObject



74
75
76
77
78
79
80
81
82
# File 'lib/cli_funcs_rpm.rb', line 74

def write_info
  write_control
  write_scripts unless @write_scripts == nil
  write_filelist
  write_provides_to_file
  write_provides_to_db
  write_dependencies_to_file
  write_dependencies_to_db
end

#write_provides_to_dbObject



115
116
117
118
119
120
# File 'lib/cli_funcs_rpm.rb', line 115

def write_provides_to_db
  arch = @rpm_file.split(".")[-2]
  @provides.each_pair do |k,v|
    RpmProvides.create(:provides => k, :providedby => "#{@control['Name']}.#{arch}", :version => v, :rpm => @rpm_file.split("/")[-1], :arch => arch)
  end
end

#write_provides_to_fileObject



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cli_funcs_rpm.rb', line 125

def write_provides_to_file
  begin
    f = open("#{@fpp}/redhat/provides", 'w+')
    @provides.each_pair do |k,v|
      f.puts "#{k} #{v}"
    end
    f.close
  rescue Exception => e
    puts "Tried to open file #{@fpp}/redhat/provides for writing during 'Rpm.write_provides', received exception: #{e}"
  end
end

#write_scriptsObject



149
150
151
152
153
154
155
156
# File 'lib/cli_funcs_rpm.rb', line 149

def write_scripts
  @scripts.each_pair do |k,v|
    #puts "KEY: #{k} VAL: #{v}"
    f = open("#{@fpp}/redhat/#{k}", 'w+')
    f.print v
    f.close
  end
end