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
108 return Solv::xfopen_dup(file, f.fileno)
110 return Solv::xfopen_dup('', f.fileno)
114 def usecachedrepo(ext, mark = false)
115 cookie = ext ? @extcookie : @cookie
117 repopath = cachepath(ext)
118 f = File.new(repopath, "r")
119 f.sysseek(-32, IO::SEEK_END)
120 fcookie = f.sysread(32)
121 return false if fcookie.length != 32
122 return false if cookie && fcookie != cookie
123 if !ext && @type != 'system'
124 f.sysseek(-32 * 2, IO::SEEK_END)
125 fextcookie = f.sysread(32)
126 return false if fextcookie.length != 32
128 f.sysseek(0, IO::SEEK_SET)
129 f = Solv::xfopen_dup('', f.fileno)
130 flags = ext ? Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES : 0
131 flags |= Solv::Repo::REPO_LOCALPOOL if ext && ext != 'DL'
132 if ! @handle.add_solv(f, flags)
137 @cookie = fcookie unless ext
138 @extcookie = fextcookie if !ext && @type != 'system'
141 File::utime(now, now, repopath) if mark
142 rescue SystemCallError
145 rescue SystemCallError
151 def writecachedrepo(ext, info = nil)
152 return if @incomplete && !ext
154 Dir::mkdir("/var/cache/solv", 0755) unless FileTest.directory?("/var/cache/solv")
155 f = Tempfile.new('.newsolv-', '/var/cache/solv')
157 sf = Solv::xfopen_dup('', f.fileno)
163 @handle.write_first_repodata(sf)
166 f.sysseek(0, IO::SEEK_END)
167 if @type != 'system' && !ext
168 @extcookie = calc_cookie_ext(f, @cookie) unless @extcookie
169 f.syswrite(@extcookie)
171 f.syswrite(ext ? @extcookie : @cookie)
173 if @handle.iscontiguous?
174 sf = Solv::xfopen(f.path)
178 abort("internal error, cannot reload solv file") unless @handle.add_solv(sf, Solv::Repo::SOLV_ADD_NO_STUBS)
180 info.extend_to_repo()
181 info.add_solv(sf, Solv::Repo::REPO_EXTEND_SOLVABLES)
186 File.rename(f.path, cachepath(ext))
189 rescue SystemCallError
194 def updateaddedprovides(addedprovides)
195 return if @incomplete
196 return unless @handle && !@handle.isempty?
197 repodata = @handle.first_repodata()
198 return unless repodata
199 oldaddedprovides = repodata.lookup_idarray(Solv::SOLVID_META, Solv::REPOSITORY_ADDEDFILEPROVIDES)
200 return if (oldaddedprovides | addedprovides) == oldaddedprovides
201 for id in addedprovides
202 repodata.add_idarray(Solv::SOLVID_META, Solv::REPOSITORY_ADDEDFILEPROVIDES, id)
204 repodata.internalize()
205 writecachedrepo(nil, repodata)
213 class Repo_rpmmd < Repo_generic
216 di = @handle.Dataiterator(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_TYPE, what, Solv::Dataiterator::SEARCH_STRING)
217 di.prepend_keyname(Solv::REPOSITORY_REPOMD)
220 filename = dp.lookup_str(Solv::REPOSITORY_REPOMD_LOCATION)
222 checksum = dp.lookup_checksum(Solv::REPOSITORY_REPOMD_CHECKSUM)
224 puts "no #{filename} checksum!"
227 return filename, checksum
233 return true if super(pool)
234 print "rpmmd repo '#{@name}: "
235 f = download("repodata/repomd.xml", false, nil, nil)
237 puts "no repomd.xml file, skipped"
242 @cookie = calc_cookie_fp(f)
243 if usecachedrepo(nil, true)
248 @handle.add_repomdxml(f, 0)
251 filename, filechksum = find('primary')
253 f = download(filename, true, filechksum, true)
255 @handle.add_rpmmd(f, nil, 0)
258 return false if @incomplete
260 filename, filechksum = find('updateinfo')
262 f = download(filename, true, filechksum, true)
264 @handle.add_updateinfoxml(f, 0)
270 @handle.create_stubs()
274 def add_ext(repodata, what, ext)
275 filename, filechksum = find(what)
276 filename, filechksum = find('prestodelta') if !filename && what == 'deltainfo'
277 return unless filename
278 h = repodata.new_handle()
279 repodata.set_poolstr(h, Solv::REPOSITORY_REPOMD_TYPE, what)
280 repodata.set_str(h, Solv::REPOSITORY_REPOMD_LOCATION, filename)
281 repodata.set_checksum(h, Solv::REPOSITORY_REPOMD_CHECKSUM, filechksum)
283 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOSITORY_DELTAINFO)
284 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_FLEXARRAY)
286 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_FILELIST)
287 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRSTRARRAY)
289 repodata.add_flexarray(Solv::SOLVID_META, Solv::REPOSITORY_EXTERNAL, h)
293 repodata = @handle.add_repodata(0)
294 add_ext(repodata, 'deltainfo', 'DL')
295 add_ext(repodata, 'filelists', 'FL')
296 repodata.internalize()
299 def load_ext(repodata)
300 repomdtype = repodata.lookup_str(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_TYPE)
301 if repomdtype == 'filelists'
303 elsif repomdtype == 'deltainfo'
308 print "[#{@name}:#{ext}: "
310 if usecachedrepo(ext)
315 filename = repodata.lookup_str(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_LOCATION)
316 filechksum = repodata.lookup_checksum(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_CHECKSUM)
317 f = download(filename, true, filechksum)
318 return false unless f
320 @handle.add_rpmmd(f, 'FL', Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES)
322 @handle.add_deltainfoxml(f, Solv::Repo::REPO_USE_LOADING)
325 writecachedrepo(ext, repodata)
331 class Repo_susetags < Repo_generic
334 di = @handle.Dataiterator(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME, what, Solv::Dataiterator::SEARCH_STRING)
335 di.prepend_keyname(Solv::SUSETAGS_FILE)
338 checksum = dp.lookup_checksum(Solv::SUSETAGS_FILE_CHECKSUM)
339 return what, checksum
345 return true if super(pool)
346 print "susetags repo '#{@name}: "
347 f = download("content", false, nil, nil)
349 puts "no content file, skipped"
354 @cookie = calc_cookie_fp(f)
355 if usecachedrepo(nil, true)
360 @handle.add_content(f, 0)
363 defvendorid = @handle.lookup_id(Solv::SOLVID_META, Solv::SUSETAGS_DEFAULTVENDOR)
364 descrdir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DESCRDIR)
365 descrdir = "suse/setup/descr" unless descrdir
366 (filename, filechksum) = find('packages.gz')
367 (filename, filechksum) = find('packages') unless filename
369 f = download("#{descrdir}/#{filename}", true, filechksum, true)
371 @handle.add_susetags(f, defvendorid, nil, Solv::Repo::REPO_NO_INTERNALIZE|Solv::Repo::SUSETAGS_RECORD_SHARES)
373 (filename, filechksum) = find('packages.en.gz')
374 (filename, filechksum) = find('packages.en') unless filename
376 f = download("#{descrdir}/#{filename}", true, filechksum, true)
378 @handle.add_susetags(f, defvendorid, nil, Solv::Repo::REPO_NO_INTERNALIZE|Solv::Repo::REPO_REUSE_REPODATA|Solv::Repo::REPO_EXTEND_SOLVABLES)
382 @handle.internalize()
387 @handle.create_stubs()
392 Solv::SOLVABLE_SUMMARY => Solv::REPOKEY_TYPE_STR,
393 Solv::SOLVABLE_DESCRIPTION => Solv::REPOKEY_TYPE_STR,
394 Solv::SOLVABLE_EULA => Solv::REPOKEY_TYPE_STR,
395 Solv::SOLVABLE_MESSAGEINS => Solv::REPOKEY_TYPE_STR,
396 Solv::SOLVABLE_MESSAGEDEL => Solv::REPOKEY_TYPE_STR,
397 Solv::SOLVABLE_CATEGORY => Solv::REPOKEY_TYPE_ID,
400 def add_ext(repodata, what, ext)
401 (filename, filechksum) = find(what)
402 h = repodata.new_handle()
403 repodata.set_str(h, Solv::SUSETAGS_FILE_NAME, filename)
404 repodata.set_checksum(h, Solv::SUSETAGS_FILE_CHECKSUM, filechksum)
406 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOSITORY_DELTAINFO)
407 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_FLEXARRAY)
409 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_DISKUSAGE)
410 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRNUMNUMARRAY)
412 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_FILELIST)
413 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRSTRARRAY)
415 @@langtags.sort.each do |langid, langtype|
416 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, @handle.pool.id2langid(langid, ext, true))
417 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, langtype)
420 repodata.add_flexarray(Solv::SOLVID_META, Solv::REPOSITORY_EXTERNAL, h)
424 repodata = @handle.add_repodata(0)
425 di = @handle.Dataiterator(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME, nil, 0)
426 di.prepend_keyname(Solv::SUSETAGS_FILE)
429 next unless filename && filename =~ /^packages\.(..)(?:\..*)$/
430 next if $1 == 'en' || $1 == 'gz'
431 add_ext(repodata, filename, $1)
433 repodata.internalize()
436 def load_ext(repodata)
437 filename = repodata.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME)
439 print "[#{@name}:#{ext}: "
441 if usecachedrepo(ext)
446 defvendorid = @handle.lookup_id(Solv::SOLVID_META, Solv::SUSETAGS_DEFAULTVENDOR)
447 descrdir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DESCRDIR)
448 descrdir = "suse/setup/descr" unless descrdir
449 filechksum = repodata.lookup_checksum(Solv::SOLVID_META, Solv::SUSETAGS_FILE_CHECKSUM)
450 f = download("#{descrdir}/#{filename}", true, filechksum)
451 return false unless f
452 @handle.add_susetags(f, defvendorid, ext, Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES)
454 writecachedrepo(ext, repodata)
459 datadir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DATADIR)
460 datadir = "suse" unless datadir
465 class Repo_unknown < Repo_generic
467 puts "unsupported repo '#{@name}: skipped"
472 class Repo_system < Repo_generic
474 @handle = pool.add_repo(@name)
475 @handle.appdata = self
476 pool.installed = @handle
477 print "rpm database: "
478 @cookie = calc_cookie_file("/var/lib/rpm/Packages")
479 if usecachedrepo(nil)
484 if @handle.respond_to? :add_products
485 @handle.add_products("/etc/products.d", Solv::Repo::REPO_NO_INTERNALIZE)
487 @handle.add_rpmdb(nil, Solv::Repo::REPO_REUSE_REPODATA)
495 cmd = 'list' if cmd == 'li'
496 cmd = 'install' if cmd == 'in'
497 cmd = 'erase' if cmd == 'rm'
498 cmd = 'verify' if cmd == 've'
499 cmd = 'search' if cmd == 'se'
503 if FileTest.directory?('/etc/zypp/repos.d')
504 reposdirs = [ '/etc/zypp/repos.d' ]
506 reposdirs = [ '/etc/yum/repos.d' ]
508 for reposdir in reposdirs do
509 next unless FileTest.directory?(reposdir)
510 for reponame in Dir["#{reposdir}/*.repo"].sort do
511 cfg = IniFile.new(reponame)
512 cfg.each_section do |ali|
513 repoattr = { 'alias' => ali, 'enabled' => 0, 'priority' => 99, 'autorefresh' => 1, 'type' => 'rpm-md', 'metadata_expire' => 900}
514 repoattr.update(cfg[ali])
515 if repoattr['type'] == 'rpm-md'
516 repo = Repo_rpmmd.new(ali, 'repomd', repoattr)
517 elsif repoattr['type'] == 'yast2'
518 repo = Repo_susetags.new(ali, 'susetags', repoattr)
520 repo = Repo_unknown.new(ali, 'unknown', repoattr)
527 pool = Solv::Pool.new()
528 # require 'sys/uname' ; sysarch = Sys::Uname.machine
529 sysarch = `uname -p`.strip
530 pool.setarch(sysarch)
532 pool.set_loadcallback { |repodata|
533 repo = repodata.repo.appdata
534 repo ? repo.load_ext(repodata) : false
537 sysrepo = Repo_system.new('@System', 'system')
540 repo.load(pool) if repo.enabled?
545 for di in pool.Dataiterator(0, Solv::SOLVABLE_NAME, args[0], Solv::Dataiterator::SEARCH_SUBSTRING | Solv::Dataiterator::SEARCH_NOCASE)
546 matches[di.solvid] = true
548 for solvid in matches.keys.sort
549 s = pool.solvables[solvid]
550 puts "- #{s.str} [#{s.repo.name}]"
555 addedprovides = pool.addfileprovides_queue()
556 if !addedprovides.empty?
557 sysrepo.updateaddedprovides(addedprovides)
559 repo.updateaddedprovides(addedprovides)
562 pool.createwhatprovides()
566 flags = Solv::Selection::SELECTION_NAME | Solv::Selection::SELECTION_PROVIDES|Solv::Selection::SELECTION_GLOB
568 flags |= Solv::Selection::SELECTION_FILELIST
569 flags |= Solv::Selection::SELECTION_INSTALLED_ONLY if cmd == 'erase'
571 sel = pool.select(arg, flags)
573 sel = pool.select(arg, flags | Solv::Selection::SELECTION_NOCASE)
574 puts "[ignoring case for '#{arg}']" unless sel.isempty?
576 puts "[using file list match for '#{arg}']" if sel.flags & Solv::Selection::SELECTION_FILELIST != 0
577 puts "[using capability match for '#{arg}']" if sel.flags & Solv::Selection::SELECTION_PROVIDES != 0
581 if jobs.empty? && (cmd == 'up' || cmd == 'dup' || cmd == 'verify')
582 sel = pool.Selection()
583 sel.addsimple(Solv::Job::SOLVER_SOLVABLE_ALL, 0)
587 if cmd == 'list' || cmd == 'info'
588 abort("no package matched.") if jobs.empty?
590 for s in job.solvables()
592 puts "Name: #{s.str}"
593 puts "Repo: #{s.repo.name}"
594 puts "Summary: #{s.lookup_str(Solv::SOLVABLE_SUMMARY)}"
595 str = s.lookup_str(Solv::SOLVABLE_URL)
596 puts "Url: #{str}" if str
597 str = s.lookup_str(Solv::SOLVABLE_LICENSE)
598 puts "License: #{str}" if str
599 puts "Description:\n#{s.lookup_str(Solv::SOLVABLE_DESCRIPTION)}"
602 puts " - #{s.str} [#{s.repo.name}]"
603 puts " #{s.lookup_str(Solv::SOLVABLE_SUMMARY)}"
610 if cmd == 'install' || cmd == 'erase' || cmd == 'up' || cmd == 'dup' || cmd == 'verify'
611 abort("no package matched.") if jobs.empty?
614 if job.how == Solv::Job::SOLVER_SOLVABLE_ALL || job.solvables.any? {|s| s.isinstalled?}
615 job.how |= Solv::Job::SOLVER_UPDATE
617 job.how |= Solv::Job::SOLVER_INSTALL
619 elsif cmd == 'install'
620 job.how |= Solv::Job::SOLVER_INSTALL
622 job.how |= Solv::Job::SOLVER_ERASE
624 job.how |= Solv::Job::SOLVER_DISTUPGRADE
625 elsif cmd == 'verify'
626 job.how |= Solv::Job::SOLVER_VERIFY
631 #pool.set_debuglevel(1)
634 solver.set_flag(Solv::Solver::SOLVER_FLAG_SPLITPROVIDES, 1)
635 solver.set_flag(Solv::Solver::SOLVER_FLAG_ALLOW_UNINSTALL, 1) if cmd == 'erase'
636 problems = solver.solve(jobs)
637 break if problems.empty?
638 for problem in problems
639 puts "Problem #{problem.id}:"
640 puts problem.findproblemrule.info.problemstr
641 solutions = problem.solutions
642 for solution in solutions
643 puts " Solution #{solution.id}:"
644 elements = solution.elements(true)
645 for element in elements
646 puts " - #{element.str}"
652 print "Please choose a solution: "
654 sol = STDIN.gets.strip
655 break if sol == 's' || sol == 'q'
656 break if sol =~ /^\d+$/ && sol.to_i >= 1 && sol.to_i <= solutions.length
660 solution = solutions[sol.to_i - 1]
661 for element in solution.elements
662 newjob = element.Job()
663 if element.type == Solv::Solver::SOLVER_SOLUTION_JOB
664 jobs[element.jobidx] = newjob
666 jobs.push(newjob) if newjob && !jobs.include?(newjob)
671 trans = solver.transaction
674 puts "Nothing to do."
677 puts "\nTransaction summary:\n"
678 for cl in trans.classify()
679 if cl.type == Solv::Transaction::SOLVER_TRANSACTION_ERASE
680 puts "#{cl.count} erased packages:"
681 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_INSTALL
682 puts "#{cl.count} installed packages:"
683 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_REINSTALLED
684 puts "#{cl.count} reinstalled packages:"
685 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED
686 puts "#{cl.count} downgraded packages:"
687 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_CHANGED
688 puts "#{cl.count} changed packages:"
689 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_UPGRADED
690 puts "#{cl.count} upgraded packages:"
691 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_VENDORCHANGE
692 puts "#{cl.count} vendor changes from '#{pool.id2str(cl.fromid)}' to '#{pool.id2str(cl.toid)}':"
693 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_ARCHCHANGE
694 puts "#{cl.count} arch changes from '#{pool.id2str(cl.fromid)}' to '#{pool.id2str(cl.toid)}':"
698 for p in cl.solvables
699 if cl.type == Solv::Transaction::SOLVER_TRANSACTION_UPGRADED || cl.type == Solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED
700 puts " - #{p.str} -> #{trans.othersolvable(p).str}"
707 puts "install size change: #{trans.calc_installsizechange()} K\n\n"
709 print("OK to continue (y/n)? ")
711 yn = STDIN.gets.strip
715 newpkgs = trans.newpackages()
720 downloadsize += p.lookup_num(Solv::SOLVABLE_DOWNLOADSIZE)
722 puts "Downloading #{newpkgs.length} packages, #{downloadsize} K"
724 repo = p.repo.appdata
725 location, medianr = p.lookup_location()
727 location = repo.packagespath + location
728 chksum = p.lookup_checksum(Solv::SOLVABLE_CHECKSUM)
729 f = repo.download(location, false, chksum)
730 abort("\n#{@name}: #{location} not found in repository\n") unless f
737 puts "Committing transaction:"
741 steptype = trans.steptype(p, Solv::Transaction::SOLVER_TRANSACTION_RPM_ONLY)
742 if steptype == Solv::Transaction::SOLVER_TRANSACTION_ERASE
743 puts "erase #{p.str}"
744 next unless p.lookup_num(Solv::RPM_RPMDBID)
745 evr = p.evr.sub(/^[0-9]+:/, '')
746 system('rpm', '-e', '--nodeps', '--nodigest', '--nosignature', "#{p.name}-#{evr}.#{p.arch}") || abort("rpm failed: #{$? >> 8}")
747 elsif (steptype == Solv::Transaction::SOLVER_TRANSACTION_INSTALL || steptype == Solv::Transaction::SOLVER_TRANSACTION_MULTIINSTALL)
748 puts "install #{p.str}"
749 f = newpkgsfp.delete(p.id)
751 mode = steptype == Solv::Transaction::SOLVER_TRANSACTION_INSTALL ? '-U' : '-i'
752 system('rpm', mode, '--force', '--nodeps', '--nodigest', '--nosignature', "/dev/fd/#{Solv::xfileno(f).to_s}") || abort("rpm failed: #{$? >> 8}")