summaryrefslogtreecommitdiff
path: root/examples/rbsolv
blob: 87f0d16089afa4c05c523e127a05cdf27a620e7d (plain)
1
2
3
4
5
6
7
8
9
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
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
344
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
#!/usr/bin/ruby

require 'solv'
require 'rubygems'
require 'inifile'
require 'tempfile'

class Repo_generic
  def initialize(name, type, attribs = {})
    @name = name
    @type = type
    @attribs = attribs.dup
    @incomplete = false
  end

  def enabled?
    return @attribs['enabled'].to_i != 0
  end

  def autorefresh?
    return @attribs['autorefresh'].to_i != 0
  end

  def id
    return @handle ? @handle.id : 0
  end

  def calc_cookie_fp(f)
    chksum = Solv::Chksum.new(Solv::REPOKEY_TYPE_SHA256)
    chksum.add("1.1")
    chksum.add_fp(f)
    return chksum.raw
  end

  def calc_cookie_file(filename)
    chksum = Solv::Chksum.new(Solv::REPOKEY_TYPE_SHA256)
    chksum.add("1.1")
    chksum.add_stat(filename)
    return chksum.raw
  end

  def calc_cookie_ext(f, cookie)
    chksum = Solv::Chksum.new(Solv::REPOKEY_TYPE_SHA256)
    chksum.add("1.1")
    chksum.add(cookie)
    chksum.add_fstat(f.fileno)
    return chksum.raw()
  end

  def cachepath(ext = nil)
    path = @name.sub(/^\./, '_')
    path += ext ? "_#{ext}.solvx" : '.solv'
    return '/var/cache/solv/' + path.gsub(/\//, '_')
  end

  def load(pool)
    @handle = pool.add_repo(@name)
    @handle.appdata = self
    @handle.priority = 99 - @attribs['priority'].to_i if @attribs['priority']
    dorefresh = autorefresh?
    if dorefresh
      begin
	s = File.stat(cachepath)
	dorefresh = false if s && (@attribs['metadata_expire'].to_i == -1 || Time.now - s.mtime < @attribs['metadata_expire'].to_i)
      rescue SystemCallError
      end
    end
    @cookie = nil
    @extcookie = nil
    if !dorefresh && usecachedrepo(nil)
      puts "repo: '#{@name}' cached"
      return true
    end
    return false
  end

  def load_ext(repodata)
    return false
  end

  def download(file, uncompress, chksum, markincomplete = false)
    url = @attribs['baseurl']
    if !url
      puts "%{@name}: no baseurl"
      return nil
    end
    url = url.sub(/\/$/, '') + "/#{file}"
    f =  Tempfile.new('rbsolv')
    f.unlink
    st = system('curl', '-f', '-s', '-L', '-o', "/dev/fd/" + f.fileno.to_s, '--', url)
    return nil if f.stat.size == 0 && (st || !chksum)
    if !st
	puts "#{file}: download error #{$? >> 8}"
	@incomplete = true if markincomplete
	return nil
    end
    if chksum
      fchksum = Solv::Chksum.new(chksum.type)
      fchksum.add_fd(f.fileno)
      if !fchksum == chksum
	puts "#{file}: checksum error"
	@incomplete = true if markincomplete
	return nil
      end
    end
    rf = nil
    if uncompress
      rf = Solv::xfopen_fd(file, f.fileno)
    else
      rf = Solv::xfopen_fd('', f.fileno)
    end
    f.close
    return rf
  end

  def usecachedrepo(ext, mark = false)
    cookie = ext ? @extcookie : @cookie
    begin
      repopath = cachepath(ext)
      f = File.new(repopath, "r")
      f.sysseek(-32, IO::SEEK_END)
      fcookie = f.sysread(32)
      return false if fcookie.length != 32
      return false if cookie && fcookie != cookie
      if !ext && @type != 'system'
	f.sysseek(-32 * 2, IO::SEEK_END)
	fextcookie = f.sysread(32)
	return false if fextcookie.length != 32
      end
      f.sysseek(0, IO::SEEK_SET)
      nf = Solv::xfopen_fd('', f.fileno)
      f.close
      flags = ext ? Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES : 0
      flags |= Solv::Repo::REPO_LOCALPOOL if ext && ext != 'DL'
      if ! @handle.add_solv(nf, flags)
	nf.close
	return false
      end
      nf.close()
      @cookie = fcookie unless ext
      @extcookie = fextcookie if !ext && @type != 'system'
      now = Time.now
      begin
	File::utime(now, now, repopath) if mark
      rescue SystemCallError
      end
      return true
    rescue SystemCallError
      return false
    end
    return true
  end

  def writecachedrepo(ext, repodata = nil)
    return if @incomplete
    begin
      Dir::mkdir("/var/cache/solv", 0755) unless FileTest.directory?("/var/cache/solv")
      f =  Tempfile.new('.newsolv-', '/var/cache/solv')
      f.chmod(0444)
      sf = Solv::xfopen_fd('', f.fileno)
      if !repodata
	@handle.write(sf)
      elsif ext
	repodata.write(sf)
      else
	@handle.write_first_repodata(sf)
      end
      sf.close
      f.sysseek(0, IO::SEEK_END)
      if @type != 'system' && !ext
	@extcookie = calc_cookie_ext(f, @cookie) unless @extcookie
	f.syswrite(@extcookie)
      end
      f.syswrite(ext ? @extcookie : @cookie)
      f.close
      if @handle.iscontiguous?
	sf = Solv::xfopen(f.path)
	if sf
	  if !ext
	    @handle.empty()
	    abort("internal error, cannot reload solv file") unless @handle.add_solv(sf, repodata ? 0 : Solv::Repo::SOLV_ADD_NO_STUBS)
	  else
	    repodata.extend_to_repo()
	    flags = Solv::Repo::REPO_EXTEND_SOLVABLES
	    flags |= Solv::Repo::REPO_LOCALPOOL if ext != 'DL'
	    repodata.add_solv(sf, flags)
	  end
	  sf.close
	end
      end
      File.rename(f.path, cachepath(ext))
      f.unlink
      return true
    rescue SystemCallError
      return false
    end
  end

  def updateaddedprovides(addedprovides)
    return if @incomplete
    return unless @handle && !@handle.isempty?
    repodata = @handle.first_repodata()
    return unless repodata
    oldaddedprovides = repodata.lookup_idarray(Solv::SOLVID_META, Solv::REPOSITORY_ADDEDFILEPROVIDES)
    return if (oldaddedprovides | addedprovides) == oldaddedprovides
    for id in addedprovides
      repodata.add_idarray(Solv::SOLVID_META, Solv::REPOSITORY_ADDEDFILEPROVIDES, id)
    end
    repodata.internalize()
    writecachedrepo(nil, repodata)
  end

  def packagespath()
    return ''
  end

  @@langtags = {
    Solv::SOLVABLE_SUMMARY     => Solv::REPOKEY_TYPE_STR,
    Solv::SOLVABLE_DESCRIPTION => Solv::REPOKEY_TYPE_STR,
    Solv::SOLVABLE_EULA        => Solv::REPOKEY_TYPE_STR,
    Solv::SOLVABLE_MESSAGEINS  => Solv::REPOKEY_TYPE_STR,
    Solv::SOLVABLE_MESSAGEDEL  => Solv::REPOKEY_TYPE_STR,
    Solv::SOLVABLE_CATEGORY    => Solv::REPOKEY_TYPE_ID,
  }

  def add_ext_keys(ext, repodata, h)
    if ext == 'DL'
      repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOSITORY_DELTAINFO)
      repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_FLEXARRAY)
    elsif ext == 'DU'
      repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_DISKUSAGE)
      repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRNUMNUMARRAY)
    elsif ext == 'FL'
      repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_FILELIST)
      repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRSTRARRAY)
    else
      @@langtags.sort.each do |langid, langtype|
	repodata.add_idarray(h, Solv::REPOSITORY_KEYS, @handle.pool.id2langid(langid, ext, true))
	repodata.add_idarray(h, Solv::REPOSITORY_KEYS, langtype)
      end
    end
  end
end

class Repo_rpmmd < Repo_generic

  def find(what)
    di = @handle.Dataiterator_meta(Solv::REPOSITORY_REPOMD_TYPE, what, Solv::Dataiterator::SEARCH_STRING)
    di.prepend_keyname(Solv::REPOSITORY_REPOMD)
    for d in di
      dp = d.parentpos()
      filename = dp.lookup_str(Solv::REPOSITORY_REPOMD_LOCATION)
      next unless filename
      checksum = dp.lookup_checksum(Solv::REPOSITORY_REPOMD_CHECKSUM)
      if !checksum
	puts "no #{filename} checksum!"
	return nil, nil
      end
      return filename, checksum
    end
    return nil, nil
  end

  def load(pool)
    return true if super(pool)
    print "rpmmd repo '#{@name}: "
    f = download("repodata/repomd.xml", false, nil, nil)
    if !f
      puts "no repomd.xml file, skipped"
      @handle.free(true)
      @handle = nil
      return false
    end
    @cookie = calc_cookie_fp(f)
    if usecachedrepo(nil, true)
      puts "cached"
      f.close
      return true
    end
    @handle.add_repomdxml(f, 0)
    f.close
    puts "fetching"
    filename, filechksum = find('primary')
    if filename
      f = download(filename, true, filechksum, true)
      if f
	@handle.add_rpmmd(f, nil, 0)
	f.close
      end
      return false if @incomplete
    end
    filename, filechksum = find('updateinfo')
    if filename
      f = download(filename, true, filechksum, true)
      if f
	@handle.add_updateinfoxml(f, 0)
	f.close
      end
    end
    add_exts()
    writecachedrepo(nil)
    @handle.create_stubs()
    return true
  end

  def add_ext(repodata, what, ext)
    filename, filechksum = find(what)
    filename, filechksum = find('prestodelta') if !filename && what == 'deltainfo'
    return unless filename
    h = repodata.new_handle()
    repodata.set_poolstr(h, Solv::REPOSITORY_REPOMD_TYPE, what)
    repodata.set_str(h, Solv::REPOSITORY_REPOMD_LOCATION, filename)
    repodata.set_checksum(h, Solv::REPOSITORY_REPOMD_CHECKSUM, filechksum)
    add_ext_keys(ext, repodata, h)
    repodata.add_flexarray(Solv::SOLVID_META, Solv::REPOSITORY_EXTERNAL, h)
  end

  def add_exts
    repodata = @handle.add_repodata(0)
    repodata.extend_to_repo()
    add_ext(repodata, 'deltainfo', 'DL')
    add_ext(repodata, 'filelists', 'FL')
    repodata.internalize()
  end

  def load_ext(repodata)
    repomdtype = repodata.lookup_str(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_TYPE)
    if repomdtype == 'filelists'
      ext = 'FL'
    elsif repomdtype == 'deltainfo'
      ext = 'DL'
    else
      return false
    end
    print "[#{@name}:#{ext}: "
    STDOUT.flush
    if usecachedrepo(ext)
      puts "cached]\n"
      return true
    end
    puts "fetching]\n"
    filename = repodata.lookup_str(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_LOCATION)
    filechksum = repodata.lookup_checksum(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_CHECKSUM)
    f = download(filename, true, filechksum)
    return false unless f
    if ext == 'FL'
      @handle.add_rpmmd(f, 'FL', Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES|Solv::Repo::REPO_LOCALPOOL)
    elsif ext == 'DL'
      @handle.add_deltainfoxml(f, Solv::Repo::REPO_USE_LOADING)
    end
    f.close
    writecachedrepo(ext, repodata)
    return true
  end

end

class Repo_susetags < Repo_generic

  def find(what)
    di = @handle.Dataiterator_meta(Solv::SUSETAGS_FILE_NAME, what, Solv::Dataiterator::SEARCH_STRING)
    di.prepend_keyname(Solv::SUSETAGS_FILE)
    for d in di
      dp = d.parentpos()
      checksum = dp.lookup_checksum(Solv::SUSETAGS_FILE_CHECKSUM)
      return what, checksum
    end
    return nil, nil
  end

  def load(pool)
    return true if super(pool)
    print "susetags repo '#{@name}: "
    f = download("content", false, nil, nil)
    if !f
      puts "no content file, skipped"
      @handle.free(true)
      @handle = nil
      return false
    end
    @cookie = calc_cookie_fp(f)
    if usecachedrepo(nil, true)
      puts "cached"
      f.close
      return true
    end
    @handle.add_content(f, 0)
    f.close
    puts "fetching"
    defvendorid = @handle.meta.lookup_id(Solv::SUSETAGS_DEFAULTVENDOR)
    descrdir = @handle.meta.lookup_str(Solv::SUSETAGS_DESCRDIR)
    descrdir = "suse/setup/descr" unless descrdir
    (filename, filechksum) = find('packages.gz')
    (filename, filechksum) = find('packages') unless filename
    if filename
      f = download("#{descrdir}/#{filename}", true, filechksum, true)
      if f
	@handle.add_susetags(f, defvendorid, nil, Solv::Repo::REPO_NO_INTERNALIZE|Solv::Repo::SUSETAGS_RECORD_SHARES)
	f.close
	(filename, filechksum) = find('packages.en.gz')
	(filename, filechksum) = find('packages.en') unless filename
	if filename
	  f = download("#{descrdir}/#{filename}", true, filechksum, true)
	  if f
	    @handle.add_susetags(f, defvendorid, nil, Solv::Repo::REPO_NO_INTERNALIZE|Solv::Repo::REPO_REUSE_REPODATA|Solv::Repo::REPO_EXTEND_SOLVABLES)
	    f.close
	  end
	end
	@handle.internalize()
      end
    end
    add_exts()
    writecachedrepo(nil)
    @handle.create_stubs()
    return true
  end

  def add_ext(repodata, what, ext)
    (filename, filechksum) = find(what)
    h = repodata.new_handle()
    repodata.set_str(h, Solv::SUSETAGS_FILE_NAME, filename)
    repodata.set_checksum(h, Solv::SUSETAGS_FILE_CHECKSUM, filechksum)
    add_ext_keys(ext, repodata, h)
    repodata.add_flexarray(Solv::SOLVID_META, Solv::REPOSITORY_EXTERNAL, h)
  end

  def add_exts
    repodata = @handle.add_repodata(0)
    di = @handle.Dataiterator_meta(Solv::SUSETAGS_FILE_NAME, nil, 0)
    di.prepend_keyname(Solv::SUSETAGS_FILE)
    for d in di
      filename = d.str
      next unless filename && filename =~ /^packages\.(..)(?:\..*)$/
      next if $1 == 'en' || $1 == 'gz'
      add_ext(repodata, filename, $1)
    end
    repodata.internalize()
  end

  def load_ext(repodata)
    filename = repodata.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME)
    ext = filename[9,2]
    print "[#{@name}:#{ext}: "
    STDOUT.flush
    if usecachedrepo(ext)
      puts "cached]\n"
      return true
    end
    puts "fetching]\n"
    defvendorid = @handle.meta.lookup_id(Solv::SUSETAGS_DEFAULTVENDOR)
    descrdir = @handle.meta.lookup_str(Solv::SUSETAGS_DESCRDIR)
    descrdir = "suse/setup/descr" unless descrdir
    filechksum = repodata.lookup_checksum(Solv::SOLVID_META, Solv::SUSETAGS_FILE_CHECKSUM)
    f = download("#{descrdir}/#{filename}", true, filechksum)
    return false unless f
    flags = Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES
    flags |= Solv::Repo::REPO_LOCALPOOL if ext != 'DL'
    @handle.add_susetags(f, defvendorid, ext, flags)
    f.close
    writecachedrepo(ext, repodata)
    return true
  end

  def packagespath()
    datadir = @handle.meta.lookup_str(Solv::SUSETAGS_DATADIR)
    datadir = "suse" unless datadir
    return datadir + '/'
  end
end

class Repo_unknown < Repo_generic
  def load(pool)
    puts "unsupported repo '#{@name}: skipped"
    return false
  end
end

class Repo_system < Repo_generic
  def load(pool)
    @handle = pool.add_repo(@name)
    @handle.appdata = self
    pool.installed = @handle
    print "rpm database: "
    @cookie = calc_cookie_file("/var/lib/rpm/Packages")
    if usecachedrepo(nil)
      puts "cached"
      return true
    end
    puts "reading"
    if @handle.respond_to? :add_products
      @handle.add_products("/etc/products.d", Solv::Repo::REPO_NO_INTERNALIZE)
    end
    f = Solv::xfopen(cachepath())
    @handle.add_rpmdb_reffp(f, Solv::Repo::REPO_REUSE_REPODATA)
    f.close if f
    writecachedrepo(nil)
    return true
  end
end

args = ARGV
cmd = args.shift

cmdabbrev = { 'ls' => 'list', 'in' => 'install', 'rm' => 'erase',
              've' => 'verify', 'se' => 'search' }
cmd = cmdabbrev[cmd] if cmdabbrev.has_key?(cmd)

cmdactionmap = { 
  'install' => Solv::Job::SOLVER_INSTALL,
  'erase'   => Solv::Job::SOLVER_ERASE,
  'up'      => Solv::Job::SOLVER_UPDATE,
  'dup'     => Solv::Job::SOLVER_DISTUPGRADE,
  'verify'  => Solv::Job::SOLVER_VERIFY,
  'list'    => 0,  
  'info'    => 0,
}

repos = []
reposdirs = []
if FileTest.directory?('/etc/zypp/repos.d')
  reposdirs = [ '/etc/zypp/repos.d' ]
else
  reposdirs = [ '/etc/yum/repos.d' ]
end
for reposdir in reposdirs do
  next unless FileTest.directory?(reposdir)
  for reponame in Dir["#{reposdir}/*.repo"].sort do
    cfg = IniFile.load(reponame)
    cfg.each_section do |ali|
      repoattr = { 'alias' => ali, 'enabled' => 0, 'priority' => 99, 'autorefresh' => 1, 'type' => 'rpm-md', 'metadata_expire' => 900}
      repoattr.update(cfg[ali])
      if repoattr['type'] == 'rpm-md'
	repo = Repo_rpmmd.new(ali, 'repomd', repoattr)
      elsif repoattr['type'] == 'yast2'
	repo = Repo_susetags.new(ali, 'susetags', repoattr)
      else
	repo = Repo_unknown.new(ali, 'unknown', repoattr)
      end
      repos.push(repo)
    end
  end
end

pool = Solv::Pool.new()
pool.setarch()

pool.set_loadcallback { |repodata|
  repo = repodata.repo.appdata
  repo ? repo.load_ext(repodata) : false
}

sysrepo = Repo_system.new('@System', 'system')
sysrepo.load(pool)
for repo in repos
  repo.load(pool) if repo.enabled?
end

if cmd == 'search'
  pool.createwhatprovides()
  sel = pool.Selection
  for di in pool.Dataiterator(Solv::SOLVABLE_NAME, args[0], Solv::Dataiterator::SEARCH_SUBSTRING | Solv::Dataiterator::SEARCH_NOCASE)
    sel.add_raw(Solv::Job::SOLVER_SOLVABLE, di.solvid)
  end
  for s in sel.solvables
    puts "- #{s.str} [#{s.repo.name}]: #{s.lookup_str(Solv::SOLVABLE_SUMMARY)}"
  end
  exit
end

abort("unknown command '#{cmd}'\n") unless cmdactionmap.has_key?(cmd)

addedprovides = pool.addfileprovides_queue()
if !addedprovides.empty?
  sysrepo.updateaddedprovides(addedprovides)
  for repo in repos
    repo.updateaddedprovides(addedprovides)
  end
end
pool.createwhatprovides()

jobs = []
for arg in args
  flags = Solv::Selection::SELECTION_NAME | Solv::Selection::SELECTION_PROVIDES | Solv::Selection::SELECTION_GLOB
  flags |= Solv::Selection::SELECTION_CANON | Solv::Selection::SELECTION_DOTARCH | Solv::Selection::SELECTION_REL
  if arg =~ /^\//
    flags |= Solv::Selection::SELECTION_FILELIST
    flags |= Solv::Selection::SELECTION_INSTALLED_ONLY if cmd == 'erase'
  end
  sel = pool.select(arg, flags)
  if sel.isempty?
    sel = pool.select(arg, flags |  Solv::Selection::SELECTION_NOCASE)
    puts "[ignoring case for '#{arg}']" unless sel.isempty?
  end
  puts "[using file list match for '#{arg}']" if sel.flags & Solv::Selection::SELECTION_FILELIST != 0
  puts "[using capability match for '#{arg}']" if sel.flags & Solv::Selection::SELECTION_PROVIDES != 0
  jobs += sel.jobs(cmdactionmap[cmd])
end

if jobs.empty? && (cmd == 'up' || cmd == 'dup' || cmd == 'verify')
  sel = pool.Selection_all()
  jobs += sel.jobs(cmdactionmap[cmd])
end

abort("no package matched.") if jobs.empty?

if cmd == 'list' || cmd == 'info'
  for job in jobs
    for s in job.solvables()
      if cmd == 'info'
	puts "Name:        #{s.str}"
	puts "Repo:        #{s.repo.name}"
	puts "Summary:     #{s.lookup_str(Solv::SOLVABLE_SUMMARY)}"
	str = s.lookup_str(Solv::SOLVABLE_URL)
	puts "Url:         #{str}" if str
	str = s.lookup_str(Solv::SOLVABLE_LICENSE)
	puts "License:     #{str}" if str
	puts "Description:\n#{s.lookup_str(Solv::SOLVABLE_DESCRIPTION)}"
	puts
      else
	puts "  - #{s.str} [#{s.repo.name}]"
	puts "    #{s.lookup_str(Solv::SOLVABLE_SUMMARY)}"
      end
    end
  end
  exit
end

for job in jobs
  job.how ^= Solv::Job::SOLVER_UPDATE ^ Solv::Job::SOLVER_INSTALL if cmd == 'up' and job.isemptyupdate?
end

solver = pool.Solver
solver.set_flag(Solv::Solver::SOLVER_FLAG_SPLITPROVIDES, 1)
solver.set_flag(Solv::Solver::SOLVER_FLAG_ALLOW_UNINSTALL, 1) if cmd == 'erase'
#pool.set_debuglevel(1)

while true
  problems = solver.solve(jobs)
  break if problems.empty?
  for problem in problems
    puts "Problem #{problem.id}/#{problems.count}:"
    puts problem
    solutions = problem.solutions
    for solution in solutions
      puts "  Solution #{solution.id}:"
      elements = solution.elements(true)
      for element in elements
	puts "  - #{element.str}"
      end
      puts
    end
    sol = nil
    while true
      print "Please choose a solution: "
      STDOUT.flush
      sol = STDIN.gets.strip
      break if sol == 's' || sol == 'q'
      break if sol =~ /^\d+$/ && sol.to_i >= 1 && sol.to_i <= solutions.length
    end
    next if sol == 's'
    abort if sol == 'q'
    solution = solutions[sol.to_i - 1]
    for element in solution.elements
      newjob = element.Job()
      if element.type == Solv::Solver::SOLVER_SOLUTION_JOB
	jobs[element.jobidx] = newjob
      else
	jobs.push(newjob) if newjob && !jobs.include?(newjob)
      end
    end
  end
end

trans = solver.transaction
solver = nil
if trans.isempty?
  puts "Nothing to do."
  exit
end

puts "\nTransaction summary:\n"
for cl in trans.classify(Solv::Transaction::SOLVER_TRANSACTION_SHOW_OBSOLETES | Solv::Transaction::SOLVER_TRANSACTION_OBSOLETE_IS_UPGRADE)
  if cl.type == Solv::Transaction::SOLVER_TRANSACTION_ERASE
    puts "#{cl.count} erased packages:"
  elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_INSTALL
    puts "#{cl.count} installed packages:"
  elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_REINSTALLED
    puts "#{cl.count} reinstalled packages:"
  elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED
    puts "#{cl.count} downgraded packages:"
  elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_CHANGED
    puts "#{cl.count} changed packages:"
  elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_UPGRADED
    puts "#{cl.count} upgraded packages:"
  elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_VENDORCHANGE
    puts "#{cl.count} vendor changes from '#{cl.fromstr}' to '#{cl.tostr}':"
  elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_ARCHCHANGE
    puts "#{cl.count} arch changes from '#{cl.fromstr}' to '#{cl.tostr}':"
  else
    next
  end
  for p in cl.solvables
    if cl.type == Solv::Transaction::SOLVER_TRANSACTION_UPGRADED || cl.type == Solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED
      puts "  - #{p.str} -> #{trans.othersolvable(p).str}"
    else
      puts "  - #{p.str}"
    end
  end
  puts
end
puts "install size change: #{trans.calc_installsizechange()} K\n\n"

while true
  print("OK to continue (y/n)? ")
  STDOUT.flush
  yn = STDIN.gets.strip
  break if yn == 'y'
  abort if yn == 'n' || yn == 'q'
end

newpkgs = trans.newsolvables()
newpkgsfp = {}
if !newpkgs.empty?
  downloadsize = 0
  for p in newpkgs
    downloadsize += p.lookup_num(Solv::SOLVABLE_DOWNLOADSIZE)
  end
  puts "Downloading #{newpkgs.length} packages, #{downloadsize / 1024} K"
  for p in newpkgs
    repo = p.repo.appdata
    location, medianr = p.lookup_location()
    next unless location
    location = repo.packagespath + location
    chksum = p.lookup_checksum(Solv::SOLVABLE_CHECKSUM)
    f = repo.download(location, false, chksum)
    abort("\n#{@name}: #{location} not found in repository\n") unless f
    newpkgsfp[p.id] = f
    print "."
    STDOUT.flush()
  end
  puts
end

puts "Committing transaction:"
puts
trans.order()
for p in trans.steps
  steptype = trans.steptype(p, Solv::Transaction::SOLVER_TRANSACTION_RPM_ONLY)
  if steptype == Solv::Transaction::SOLVER_TRANSACTION_ERASE
    puts "erase #{p.str}"
    next unless p.lookup_num(Solv::RPM_RPMDBID)
    evr = p.evr.sub(/^[0-9]+:/, '')
    system('rpm', '-e', '--nodeps', '--nodigest', '--nosignature', "#{p.name}-#{evr}.#{p.arch}") || abort("rpm failed: #{$? >> 8}") 
  elsif (steptype == Solv::Transaction::SOLVER_TRANSACTION_INSTALL || steptype == Solv::Transaction::SOLVER_TRANSACTION_MULTIINSTALL)
    puts "install #{p.str}"
    f = newpkgsfp.delete(p.id)
    next unless f
    mode = steptype == Solv::Transaction::SOLVER_TRANSACTION_INSTALL ? '-U' : '-i'
    f.cloexec(0)
    system('rpm', mode, '--force', '--nodeps', '--nodigest', '--nosignature', "/dev/fd/#{f.fileno().to_s}") || abort("rpm failed: #{$? >> 8}")
    f.close
  end
end