9 def initialize(name, type, attribs = {})
12 @attribs = attribs.dup
17 return @attribs['enabled'].to_i != 0
21 return @attribs['autorefresh'].to_i != 0
25 return @handle ? @handle.id : 0
29 chksum = Solv::Chksum.new(Solv::REPOKEY_TYPE_SHA256)
35 def calc_cookie_file(filename)
36 chksum = Solv::Chksum.new(Solv::REPOKEY_TYPE_SHA256)
38 chksum.add_stat(filename)
42 def calc_cookie_ext(f, cookie)
43 chksum = Solv::Chksum.new(Solv::REPOKEY_TYPE_SHA256)
46 chksum.add_fstat(f.fileno)
47 extcookie = chksum.raw()
48 extcookie[0] = 1 if extcookie[0] == 0
52 def cachepath(ext = nil)
53 path = @name.sub(/^\./, '_')
54 path += ext ? "_#{ext}.solvx" : '.solv'
55 return '/var/cache/solv/' + path.gsub(/\//, '_')
59 @handle = pool.add_repo(@name)
60 @handle.appdata = self
61 @handle.priority = 99 - @attribs['priority'].to_i if @attribs['priority']
62 dorefresh = autorefresh?
65 s = File.stat(cachepath)
66 dorefresh = false if s && (@attribs['metadata_expire'].to_i == -1 || Time.now - s.mtime < @attribs['metadata_expire'].to_i)
67 rescue SystemCallError
71 if !dorefresh && usecachedrepo(nil)
72 puts "repo: '#{@name}' cached"
78 def load_ext(repodata)
82 def download(file, uncompress, chksum, markincomplete = false)
83 url = @attribs['baseurl']
85 puts "%{@name}: no baseurl"
88 url = url.sub(/\/$/, '') + "/#{file}"
89 f = Tempfile.new('rbsolv')
91 st = system('curl', '-f', '-s', '-L', '-o', "/dev/fd/" + f.fileno.to_s, '--', url)
92 return nil if f.stat.size == 0 && (st || !chksum)
94 puts "#{file}: download error #{$? >> 8}"
95 @incomplete = true if markincomplete
99 fchksum = Solv::Chksum.new(chksum.type)
100 fchksum.add_fd(f.fileno)
101 if !fchksum == chksum
102 puts "#{file}: checksum error"
103 @incomplete = true if markincomplete
109 rf = Solv::xfopen_fd(file, f.fileno)
111 rf = Solv::xfopen_fd('', f.fileno)
117 def usecachedrepo(ext, mark = false)
118 cookie = ext ? @extcookie : @cookie
120 repopath = cachepath(ext)
121 f = File.new(repopath, "r")
122 f.sysseek(-32, IO::SEEK_END)
123 fcookie = f.sysread(32)
124 return false if fcookie.length != 32
125 return false if cookie && fcookie != cookie
126 if !ext && @type != 'system'
127 f.sysseek(-32 * 2, IO::SEEK_END)
128 fextcookie = f.sysread(32)
129 return false if fextcookie.length != 32
131 f.sysseek(0, IO::SEEK_SET)
132 nf = Solv::xfopen_fd('', f.fileno)
134 flags = ext ? Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES : 0
135 flags |= Solv::Repo::REPO_LOCALPOOL if ext && ext != 'DL'
136 if ! @handle.add_solv(nf, flags)
141 @cookie = fcookie unless ext
142 @extcookie = fextcookie if !ext && @type != 'system'
145 File::utime(now, now, repopath) if mark
146 rescue SystemCallError
149 rescue SystemCallError
155 def writecachedrepo(ext, info = nil)
156 return if @incomplete
158 Dir::mkdir("/var/cache/solv", 0755) unless FileTest.directory?("/var/cache/solv")
159 f = Tempfile.new('.newsolv-', '/var/cache/solv')
161 sf = Solv::xfopen_fd('', f.fileno)
167 @handle.write_first_repodata(sf)
170 f.sysseek(0, IO::SEEK_END)
171 if @type != 'system' && !ext
172 @extcookie = calc_cookie_ext(f, @cookie) unless @extcookie
173 f.syswrite(@extcookie)
175 f.syswrite(ext ? @extcookie : @cookie)
177 if @handle.iscontiguous?
178 sf = Solv::xfopen(f.path)
182 abort("internal error, cannot reload solv file") unless @handle.add_solv(sf, Solv::Repo::SOLV_ADD_NO_STUBS)
184 info.extend_to_repo()
185 flags = Solv::Repo::REPO_EXTEND_SOLVABLES
186 flags |= Solv::Repo::REPO_LOCALPOOL if ext != 'DL'
187 info.add_solv(sf, flags)
192 File.rename(f.path, cachepath(ext))
195 rescue SystemCallError
200 def updateaddedprovides(addedprovides)
201 return if @incomplete
202 return unless @handle && !@handle.isempty?
203 repodata = @handle.first_repodata()
204 return unless repodata
205 oldaddedprovides = repodata.lookup_idarray(Solv::SOLVID_META, Solv::REPOSITORY_ADDEDFILEPROVIDES)
206 return if (oldaddedprovides | addedprovides) == oldaddedprovides
207 for id in addedprovides
208 repodata.add_idarray(Solv::SOLVID_META, Solv::REPOSITORY_ADDEDFILEPROVIDES, id)
210 repodata.internalize()
211 writecachedrepo(nil, repodata)
219 class Repo_rpmmd < Repo_generic
222 di = @handle.Dataiterator(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_TYPE, what, Solv::Dataiterator::SEARCH_STRING)
223 di.prepend_keyname(Solv::REPOSITORY_REPOMD)
226 filename = dp.lookup_str(Solv::REPOSITORY_REPOMD_LOCATION)
228 checksum = dp.lookup_checksum(Solv::REPOSITORY_REPOMD_CHECKSUM)
230 puts "no #{filename} checksum!"
233 return filename, checksum
239 return true if super(pool)
240 print "rpmmd repo '#{@name}: "
241 f = download("repodata/repomd.xml", false, nil, nil)
243 puts "no repomd.xml file, skipped"
248 @cookie = calc_cookie_fp(f)
249 if usecachedrepo(nil, true)
254 @handle.add_repomdxml(f, 0)
257 filename, filechksum = find('primary')
259 f = download(filename, true, filechksum, true)
261 @handle.add_rpmmd(f, nil, 0)
264 return false if @incomplete
266 filename, filechksum = find('updateinfo')
268 f = download(filename, true, filechksum, true)
270 @handle.add_updateinfoxml(f, 0)
276 @handle.create_stubs()
280 def add_ext(repodata, what, ext)
281 filename, filechksum = find(what)
282 filename, filechksum = find('prestodelta') if !filename && what == 'deltainfo'
283 return unless filename
284 h = repodata.new_handle()
285 repodata.set_poolstr(h, Solv::REPOSITORY_REPOMD_TYPE, what)
286 repodata.set_str(h, Solv::REPOSITORY_REPOMD_LOCATION, filename)
287 repodata.set_checksum(h, Solv::REPOSITORY_REPOMD_CHECKSUM, filechksum)
289 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOSITORY_DELTAINFO)
290 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_FLEXARRAY)
292 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_FILELIST)
293 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRSTRARRAY)
295 repodata.add_flexarray(Solv::SOLVID_META, Solv::REPOSITORY_EXTERNAL, h)
299 repodata = @handle.add_repodata(0)
300 add_ext(repodata, 'deltainfo', 'DL')
301 add_ext(repodata, 'filelists', 'FL')
302 repodata.internalize()
305 def load_ext(repodata)
306 repomdtype = repodata.lookup_str(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_TYPE)
307 if repomdtype == 'filelists'
309 elsif repomdtype == 'deltainfo'
314 print "[#{@name}:#{ext}: "
316 if usecachedrepo(ext)
321 filename = repodata.lookup_str(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_LOCATION)
322 filechksum = repodata.lookup_checksum(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_CHECKSUM)
323 f = download(filename, true, filechksum)
324 return false unless f
326 @handle.add_rpmmd(f, 'FL', Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES|Solv::Repo::REPO_LOCALPOOL)
328 @handle.add_deltainfoxml(f, Solv::Repo::REPO_USE_LOADING)
331 writecachedrepo(ext, repodata)
337 class Repo_susetags < Repo_generic
340 di = @handle.Dataiterator(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME, what, Solv::Dataiterator::SEARCH_STRING)
341 di.prepend_keyname(Solv::SUSETAGS_FILE)
344 checksum = dp.lookup_checksum(Solv::SUSETAGS_FILE_CHECKSUM)
345 return what, checksum
351 return true if super(pool)
352 print "susetags repo '#{@name}: "
353 f = download("content", false, nil, nil)
355 puts "no content file, skipped"
360 @cookie = calc_cookie_fp(f)
361 if usecachedrepo(nil, true)
366 @handle.add_content(f, 0)
369 defvendorid = @handle.lookup_id(Solv::SOLVID_META, Solv::SUSETAGS_DEFAULTVENDOR)
370 descrdir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DESCRDIR)
371 descrdir = "suse/setup/descr" unless descrdir
372 (filename, filechksum) = find('packages.gz')
373 (filename, filechksum) = find('packages') unless filename
375 f = download("#{descrdir}/#{filename}", true, filechksum, true)
377 @handle.add_susetags(f, defvendorid, nil, Solv::Repo::REPO_NO_INTERNALIZE|Solv::Repo::SUSETAGS_RECORD_SHARES)
379 (filename, filechksum) = find('packages.en.gz')
380 (filename, filechksum) = find('packages.en') unless filename
382 f = download("#{descrdir}/#{filename}", true, filechksum, true)
384 @handle.add_susetags(f, defvendorid, nil, Solv::Repo::REPO_NO_INTERNALIZE|Solv::Repo::REPO_REUSE_REPODATA|Solv::Repo::REPO_EXTEND_SOLVABLES)
388 @handle.internalize()
393 @handle.create_stubs()
398 Solv::SOLVABLE_SUMMARY => Solv::REPOKEY_TYPE_STR,
399 Solv::SOLVABLE_DESCRIPTION => Solv::REPOKEY_TYPE_STR,
400 Solv::SOLVABLE_EULA => Solv::REPOKEY_TYPE_STR,
401 Solv::SOLVABLE_MESSAGEINS => Solv::REPOKEY_TYPE_STR,
402 Solv::SOLVABLE_MESSAGEDEL => Solv::REPOKEY_TYPE_STR,
403 Solv::SOLVABLE_CATEGORY => Solv::REPOKEY_TYPE_ID,
406 def add_ext(repodata, what, ext)
407 (filename, filechksum) = find(what)
408 h = repodata.new_handle()
409 repodata.set_str(h, Solv::SUSETAGS_FILE_NAME, filename)
410 repodata.set_checksum(h, Solv::SUSETAGS_FILE_CHECKSUM, filechksum)
412 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOSITORY_DELTAINFO)
413 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_FLEXARRAY)
415 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_DISKUSAGE)
416 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRNUMNUMARRAY)
418 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_FILELIST)
419 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRSTRARRAY)
421 @@langtags.sort.each do |langid, langtype|
422 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, @handle.pool.id2langid(langid, ext, true))
423 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, langtype)
426 repodata.add_flexarray(Solv::SOLVID_META, Solv::REPOSITORY_EXTERNAL, h)
430 repodata = @handle.add_repodata(0)
431 di = @handle.Dataiterator(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME, nil, 0)
432 di.prepend_keyname(Solv::SUSETAGS_FILE)
435 next unless filename && filename =~ /^packages\.(..)(?:\..*)$/
436 next if $1 == 'en' || $1 == 'gz'
437 add_ext(repodata, filename, $1)
439 repodata.internalize()
442 def load_ext(repodata)
443 filename = repodata.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME)
445 print "[#{@name}:#{ext}: "
447 if usecachedrepo(ext)
452 defvendorid = @handle.lookup_id(Solv::SOLVID_META, Solv::SUSETAGS_DEFAULTVENDOR)
453 descrdir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DESCRDIR)
454 descrdir = "suse/setup/descr" unless descrdir
455 filechksum = repodata.lookup_checksum(Solv::SOLVID_META, Solv::SUSETAGS_FILE_CHECKSUM)
456 f = download("#{descrdir}/#{filename}", true, filechksum)
457 return false unless f
458 flags = Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES
459 flags |= Solv::Repo::REPO_LOCALPOOL if ext != 'DL'
460 @handle.add_susetags(f, defvendorid, ext, flags)
462 writecachedrepo(ext, repodata)
467 datadir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DATADIR)
468 datadir = "suse" unless datadir
473 class Repo_unknown < Repo_generic
475 puts "unsupported repo '#{@name}: skipped"
480 class Repo_system < Repo_generic
482 @handle = pool.add_repo(@name)
483 @handle.appdata = self
484 pool.installed = @handle
485 print "rpm database: "
486 @cookie = calc_cookie_file("/var/lib/rpm/Packages")
487 if usecachedrepo(nil)
492 if @handle.respond_to? :add_products
493 @handle.add_products("/etc/products.d", Solv::Repo::REPO_NO_INTERNALIZE)
495 @handle.add_rpmdb(nil, Solv::Repo::REPO_REUSE_REPODATA)
503 cmd = 'list' if cmd == 'li'
504 cmd = 'install' if cmd == 'in'
505 cmd = 'erase' if cmd == 'rm'
506 cmd = 'verify' if cmd == 've'
507 cmd = 'search' if cmd == 'se'
511 if FileTest.directory?('/etc/zypp/repos.d')
512 reposdirs = [ '/etc/zypp/repos.d' ]
514 reposdirs = [ '/etc/yum/repos.d' ]
516 for reposdir in reposdirs do
517 next unless FileTest.directory?(reposdir)
518 for reponame in Dir["#{reposdir}/*.repo"].sort do
519 cfg = IniFile.new(reponame)
520 cfg.each_section do |ali|
521 repoattr = { 'alias' => ali, 'enabled' => 0, 'priority' => 99, 'autorefresh' => 1, 'type' => 'rpm-md', 'metadata_expire' => 900}
522 repoattr.update(cfg[ali])
523 if repoattr['type'] == 'rpm-md'
524 repo = Repo_rpmmd.new(ali, 'repomd', repoattr)
525 elsif repoattr['type'] == 'yast2'
526 repo = Repo_susetags.new(ali, 'susetags', repoattr)
528 repo = Repo_unknown.new(ali, 'unknown', repoattr)
535 pool = Solv::Pool.new()
538 pool.set_loadcallback { |repodata|
539 repo = repodata.repo.appdata
540 repo ? repo.load_ext(repodata) : false
543 sysrepo = Repo_system.new('@System', 'system')
546 repo.load(pool) if repo.enabled?
551 for di in pool.Dataiterator(0, Solv::SOLVABLE_NAME, args[0], Solv::Dataiterator::SEARCH_SUBSTRING | Solv::Dataiterator::SEARCH_NOCASE)
552 matches[di.solvid] = true
554 for solvid in matches.keys.sort
555 s = pool.solvables[solvid]
556 puts "- #{s.str} [#{s.repo.name}]"
561 addedprovides = pool.addfileprovides_queue()
562 if !addedprovides.empty?
563 sysrepo.updateaddedprovides(addedprovides)
565 repo.updateaddedprovides(addedprovides)
568 pool.createwhatprovides()
572 flags = Solv::Selection::SELECTION_NAME | Solv::Selection::SELECTION_PROVIDES|Solv::Selection::SELECTION_GLOB
574 flags |= Solv::Selection::SELECTION_FILELIST
575 flags |= Solv::Selection::SELECTION_INSTALLED_ONLY if cmd == 'erase'
577 sel = pool.select(arg, flags)
579 sel = pool.select(arg, flags | Solv::Selection::SELECTION_NOCASE)
580 puts "[ignoring case for '#{arg}']" unless sel.isempty?
582 puts "[using file list match for '#{arg}']" if sel.flags & Solv::Selection::SELECTION_FILELIST != 0
583 puts "[using capability match for '#{arg}']" if sel.flags & Solv::Selection::SELECTION_PROVIDES != 0
587 if jobs.empty? && (cmd == 'up' || cmd == 'dup' || cmd == 'verify')
588 sel = pool.Selection()
589 sel.addsimple(Solv::Job::SOLVER_SOLVABLE_ALL, 0)
593 if cmd == 'list' || cmd == 'info'
594 abort("no package matched.") if jobs.empty?
596 for s in job.solvables()
598 puts "Name: #{s.str}"
599 puts "Repo: #{s.repo.name}"
600 puts "Summary: #{s.lookup_str(Solv::SOLVABLE_SUMMARY)}"
601 str = s.lookup_str(Solv::SOLVABLE_URL)
602 puts "Url: #{str}" if str
603 str = s.lookup_str(Solv::SOLVABLE_LICENSE)
604 puts "License: #{str}" if str
605 puts "Description:\n#{s.lookup_str(Solv::SOLVABLE_DESCRIPTION)}"
608 puts " - #{s.str} [#{s.repo.name}]"
609 puts " #{s.lookup_str(Solv::SOLVABLE_SUMMARY)}"
616 if cmd == 'install' || cmd == 'erase' || cmd == 'up' || cmd == 'dup' || cmd == 'verify'
617 abort("no package matched.") if jobs.empty?
620 job.how |= Solv::Job::SOLVER_UPDATE
621 job.how ^= Solv::Job::SOLVER_UPDATE ^ Solv::Job::SOLVER_INSTALL if job.isemptyupdate?
622 elsif cmd == 'install'
623 job.how |= Solv::Job::SOLVER_INSTALL
625 job.how |= Solv::Job::SOLVER_ERASE
627 job.how |= Solv::Job::SOLVER_DISTUPGRADE
628 elsif cmd == 'verify'
629 job.how |= Solv::Job::SOLVER_VERIFY
634 #pool.set_debuglevel(1)
637 solver.set_flag(Solv::Solver::SOLVER_FLAG_SPLITPROVIDES, 1)
638 solver.set_flag(Solv::Solver::SOLVER_FLAG_ALLOW_UNINSTALL, 1) if cmd == 'erase'
639 problems = solver.solve(jobs)
640 break if problems.empty?
641 for problem in problems
642 puts "Problem #{problem.id}:"
643 puts problem.findproblemrule.info.problemstr
644 solutions = problem.solutions
645 for solution in solutions
646 puts " Solution #{solution.id}:"
647 elements = solution.elements(true)
648 for element in elements
649 puts " - #{element.str}"
655 print "Please choose a solution: "
657 sol = STDIN.gets.strip
658 break if sol == 's' || sol == 'q'
659 break if sol =~ /^\d+$/ && sol.to_i >= 1 && sol.to_i <= solutions.length
663 solution = solutions[sol.to_i - 1]
664 for element in solution.elements
665 newjob = element.Job()
666 if element.type == Solv::Solver::SOLVER_SOLUTION_JOB
667 jobs[element.jobidx] = newjob
669 jobs.push(newjob) if newjob && !jobs.include?(newjob)
674 trans = solver.transaction
677 puts "Nothing to do."
680 puts "\nTransaction summary:\n"
681 for cl in trans.classify()
682 if cl.type == Solv::Transaction::SOLVER_TRANSACTION_ERASE
683 puts "#{cl.count} erased packages:"
684 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_INSTALL
685 puts "#{cl.count} installed packages:"
686 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_REINSTALLED
687 puts "#{cl.count} reinstalled packages:"
688 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED
689 puts "#{cl.count} downgraded packages:"
690 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_CHANGED
691 puts "#{cl.count} changed packages:"
692 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_UPGRADED
693 puts "#{cl.count} upgraded packages:"
694 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_VENDORCHANGE
695 puts "#{cl.count} vendor changes from '#{cl.fromdep}' to '#{cl.todep}':"
696 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_ARCHCHANGE
697 puts "#{cl.count} arch changes from '#{cl.fromdep}' to '#{cl.todep}':"
701 for p in cl.solvables
702 if cl.type == Solv::Transaction::SOLVER_TRANSACTION_UPGRADED || cl.type == Solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED
703 puts " - #{p.str} -> #{trans.othersolvable(p).str}"
710 puts "install size change: #{trans.calc_installsizechange()} K\n\n"
712 print("OK to continue (y/n)? ")
714 yn = STDIN.gets.strip
718 newpkgs = trans.newpackages()
723 downloadsize += p.lookup_num(Solv::SOLVABLE_DOWNLOADSIZE)
725 puts "Downloading #{newpkgs.length} packages, #{downloadsize} K"
727 repo = p.repo.appdata
728 location, medianr = p.lookup_location()
730 location = repo.packagespath + location
731 chksum = p.lookup_checksum(Solv::SOLVABLE_CHECKSUM)
732 f = repo.download(location, false, chksum)
733 abort("\n#{@name}: #{location} not found in repository\n") unless f
740 puts "Committing transaction:"
744 steptype = trans.steptype(p, Solv::Transaction::SOLVER_TRANSACTION_RPM_ONLY)
745 if steptype == Solv::Transaction::SOLVER_TRANSACTION_ERASE
746 puts "erase #{p.str}"
747 next unless p.lookup_num(Solv::RPM_RPMDBID)
748 evr = p.evr.sub(/^[0-9]+:/, '')
749 system('rpm', '-e', '--nodeps', '--nodigest', '--nosignature', "#{p.name}-#{evr}.#{p.arch}") || abort("rpm failed: #{$? >> 8}")
750 elsif (steptype == Solv::Transaction::SOLVER_TRANSACTION_INSTALL || steptype == Solv::Transaction::SOLVER_TRANSACTION_MULTIINSTALL)
751 puts "install #{p.str}"
752 f = newpkgsfp.delete(p.id)
754 mode = steptype == Solv::Transaction::SOLVER_TRANSACTION_INSTALL ? '-U' : '-i'
755 system('rpm', mode, '--force', '--nodeps', '--nodigest', '--nosignature', "/dev/fd/#{f.fileno().to_s}") || abort("rpm failed: #{$? >> 8}")