Class: Ports::PortsDB

Inherits:
Object
  • Object
show all
Defined in:
lib/port_upgrade.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, outdated = nil) ⇒ PortsDB

Returns a new instance of PortsDB.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/port_upgrade.rb', line 207

def initialize(path=nil,outdated=nil)
  @db = SQLite3::Database.new(':memory:')#('port_tree.db')
  @pt = PortTree.new(self,path)
  @installed = @pt.installed
  set_outdated(outdated)
  @to_remove = nil
  config_file = locate_config_file
  unless config_file.nil?
    begin
      @config = YAML::load(File.open(config_file))
      @config = {} if @config == false
    rescue Errno::ENOENT
      $stderr.puts("No configuration loaded.")
    rescue ArgumentError
      $stderr.puts("Badly formed config file.")
      exit(-1)
    end
  else
    $stderr.puts("No configuration loaded.")
  end
end

Instance Method Details

#closeObject



249
250
251
# File 'lib/port_upgrade.rb', line 249

def close
  @db.close
end

#dbObject



245
246
247
# File 'lib/port_upgrade.rb', line 245

def db
  @db
end

#dump_treeObject



257
258
259
# File 'lib/port_upgrade.rb', line 257

def dump_tree
  @installed.dump_tree
end

#get_after_install(portname) ⇒ Object



420
421
422
# File 'lib/port_upgrade.rb', line 420

def get_after_install(portname)
  get_port_action(portname,:after_install)
end

#get_after_uninstall(portname) ⇒ Object



412
413
414
# File 'lib/port_upgrade.rb', line 412

def get_after_uninstall(portname)
  get_port_action(portname,:after_uninstall)
end

#get_before_install(portname) ⇒ Object



416
417
418
# File 'lib/port_upgrade.rb', line 416

def get_before_install(portname)
  get_port_action(portname,:before_install)
end

#get_before_uninstall(portname) ⇒ Object



408
409
410
# File 'lib/port_upgrade.rb', line 408

def get_before_uninstall(portname)
  get_port_action(portname,:before_uninstall)
end

#get_final_install(portname) ⇒ Object



424
425
426
# File 'lib/port_upgrade.rb', line 424

def get_final_install(portname)
  get_port_action(portname,:final_install)
end

#get_force(portname) ⇒ Object



399
400
401
402
403
404
405
406
# File 'lib/port_upgrade.rb', line 399

def get_force(portname)
  force = get_port_action(portname,:force_install)
  if force
    return "-f"
  else
    return ""
  end
end

#get_leavesObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/port_upgrade.rb', line 269

def get_leaves
  $stderr.print "get_leaves " if $DEBUG
  rs = @db.query('select port from remports')
  ports = rs.to_a.flatten.sort.uniq
  rs.close
  $stderr.print "ports: #{ports.size} " if $DEBUG
  rs = @db.query('select dep from remports')
  deps = rs.to_a.flatten.sort.uniq
  rs.close
  $stderr.print "deps: #{deps.size} " if $DEBUG
  diff = (ports - deps).sort
  $stderr.puts "diff: #{diff.size}" if $DEBUG
  diff.each{|p| @db.execute("delete from remports where port = ?",p)}
  diff
end

#installedObject



241
242
243
# File 'lib/port_upgrade.rb', line 241

def installed
  @installed
end

#locate_config_fileObject



229
230
231
232
233
234
235
236
237
238
239
# File 'lib/port_upgrade.rb', line 229

def locate_config_file
  to_search = []
  local_dir = File.dirname($0).sub(/bin$/,"")
  local_dir = local_dir == "" ? "." : local_dir
  to_search << File.join(local_dir,"etc",Ports::CONFIG_FILE)
  to_search << File.join(ENV['HOME'],"."+Ports::CONFIG_FILE)
  to_search.each do |path|
    return path if File.readable?(path)
  end
  return nil
end

#outdated(reload = false) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/port_upgrade.rb', line 296

def outdated(reload = false)
  return @outdated unless @outdated.nil? or reload == true
  @outdated = []
  @installed.each do |port|
    d = File.join(@pt.receipt_path,port)
    Dir.entries(d)[2..-1].each do |version|
      d2 = File.join(d,version,'receipt.bz2')
      reader = BZ2::Reader.new(File.new(d2))
      lines = reader.readlines
      cats = []
      lines.collect do |line|
        md = /categories (\{([^}]*)\}|([^ ]*))/.match(line)
        unless md.nil?
          cats << (md[2].nil? ? md[1] : md[2].split.first)
        end
      end
      portfile_path = File.join(MACPORTS_DB,cats.flatten,port,'Portfile')
      unless File.exist?(portfile_path)
        $stderr.puts "Searching for #{port}'s Portfile"
        Dir.entries(MACPORTS_DB).each do |d|
          if File.directory?(File.join(MACPORTS_DB,d)) && d != '.' && d != '..'
            testpath = File.join(MACPORTS_DB,d,port,'Portfile')
            if File.exist?(testpath)
               portfile_path = testpath
               break
             end
          end
        end
      end
      if File.exist?(portfile_path)
        curver = Portfile.new(portfile_path).version
        #puts "%-32s%s < %s" %[port,version.split('+').first,curver] if Ports::Utilities.cmp_vers(version.split('+').first,curver) < 0
        cmp = Ports::Utilities.cmp_vers(version.split('+').first,curver)
        $stderr.puts("#{port}: #{version.split('+').first} <=> #{curver}       #{cmp}") if $DEBUG
        if cmp.nil?
          $stderr.puts "Unable to compare versions: #{[port]}"
        else
           if cmp < 0
             @outdated << port
           end
         end
      else
        $stderr.puts "Unable to process Portfile (File Not Found) for #{port}"
      end
    end
  end
  @outdated.uniq
end

#port_treeObject



253
254
255
# File 'lib/port_upgrade.rb', line 253

def port_tree
  @pt
end

#set_outdated(out) ⇒ Object



285
286
287
288
289
290
291
292
293
294
# File 'lib/port_upgrade.rb', line 285

def set_outdated(out)
  begin
    out.each{|o| Port.new(o)} unless out.nil?
  rescue RuntimeError => ex
    $stderr.puts ex
    exit(-1)
  end
  @outdated = out
  @to_remove = nil
end

#to_removeObject



261
262
263
264
265
266
267
# File 'lib/port_upgrade.rb', line 261

def to_remove
  return @to_remove unless @to_remove.nil?
  @pt.setup_remports(outdated)
  @db.query("select distinct port from remports order by port") do |rs|
    @to_remove = rs.to_a
  end
end

#upgrade(path = 'port_upgrade.sh') ⇒ Object



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/port_upgrade.rb', line 345

def upgrade(path='port_upgrade.sh')
  final = []
  uninstall_data = []
  install_data = []
  @pt.setup_remports(outdated) if @to_remove.nil?
  remports = []
  remvariants = Hash.new {|h,k| h[k] = Array.new}
  stmt = @db.prepare("select count(*) from remports")
  dotsh = File.new(path,'w')
  dotsh.chmod(0700)
  $stderr.puts "port_upgrade.sh open for write" if $DEBUG
  while stmt.execute.to_a.first[0].to_i > 0
      temp = get_leaves
      break if temp.size == 0
      temp.each do |o|
        @db.query("select port,version,variant from ports where port = ?",o) do |rs|
          installed = rs.to_a
          installed.each do |port|
            bu = get_before_uninstall(port[0])
            uninstall_data << bu unless bu.nil?
            uninstall_data << "port uninstall #{port[0]} @#{port[1]}#{port[2]} || exit -1"
            au = get_after_uninstall(port[0])
            uninstall_data << au unless au.nil?
            remports.push(port[0])
            remvariants[port[0]].push(port[2]) if remvariants[port[0]] and remvariants[port[0]].index(port[2]).nil?
          end
        end
      end
  end
  remports.uniq!
  while remports.size > 0
    port = remports.pop
    if remvariants[port].uniq.size > 1
      $stderr.puts "Found multiple variants for #{port}."
      variantindex = choose_variant(port,remvariants[port])
    else
      variantindex = 0
    end
    bi = get_before_install(port)
    uninstall_data << bi unless bi.nil?
    install_data << "port #{get_force(port)} -x install #{port} #{remvariants[port][variantindex]} || exit -1"
    ai = get_after_install(port)
    fi = get_final_install(port)
    final << fi unless fi.nil?
    install_data << ai unless ai.nil?
  end
  stmt.close
  final_actions = final
  template = ERB.new(File.read(SH_ERB_PATH))
  dotsh.puts template.result(binding)
  dotsh.close
  true
end