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"
75 return load_if_changed()
78 def load_ext(repodata)
86 def download(file, uncompress, chksum, markincomplete = false)
87 url = @attribs['baseurl']
89 puts "%{@name}: no baseurl"
92 url = url.sub(/\/$/, '') + "/#{file}"
93 f = Tempfile.new('rbsolv')
95 st = system('curl', '-f', '-s', '-L', '-o', "/dev/fd/" + f.fileno.to_s, '--', url)
96 return nil if f.stat.size == 0 && (st || !chksum)
98 puts "#{file}: download error #{$? >> 8}"
99 @incomplete = true if markincomplete
103 fchksum = Solv::Chksum.new(chksum.type)
104 fchksum.add_fd(f.fileno)
105 if !fchksum == chksum
106 puts "#{file}: checksum error"
107 @incomplete = true if markincomplete
112 return Solv::xfopen_dup(file, f.fileno)
114 return Solv::xfopen_dup('', f.fileno)
118 def usecachedrepo(ext, mark = false)
119 cookie = ext ? @extcookie : @cookie
121 repopath = cachepath(ext)
122 f = File.new(repopath, "r")
123 f.sysseek(-32, IO::SEEK_END)
124 fcookie = f.sysread(32)
125 return false if fcookie.length != 32
126 return false if cookie && fcookie != cookie
127 if !ext && @type != 'system'
128 f.sysseek(-32 * 2, IO::SEEK_END)
129 fextcookie = f.sysread(32)
130 return false if fextcookie.length != 32
132 f.sysseek(0, IO::SEEK_SET)
133 f = Solv::xfopen_dup('', 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(f, 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)
157 Dir::mkdir("/var/cache/solv", 0755) unless FileTest.directory?("/var/cache/solv")
158 f = Tempfile.new('.newsolv-', '/var/cache/solv')
160 sf = Solv::xfopen_dup('', f.fileno)
166 @handle.write_first_repodata(sf)
169 f.sysseek(0, IO::SEEK_END)
170 if @type != 'system' && !ext
171 @extcookie = calc_cookie_ext(f, @cookie) unless @extcookie
172 f.syswrite(@extcookie)
174 f.syswrite(ext ? @extcookie : @cookie)
176 if @handle.iscontiguous?
177 sf = Solv::xfopen(f.path)
181 abort("internal error, cannot reload solv file") unless @handle.add_solv(sf, Solv::Repo::SOLV_ADD_NO_STUBS)
183 info.extend_to_repo()
184 info.add_solv(sf, Solv::Repo::REPO_EXTEND_SOLVABLES)
189 File.rename(f.path, cachepath(ext))
192 rescue SystemCallError
197 def updateaddedprovides(addedprovides)
198 return if @incomplete
199 return unless @handle && !@handle.isempty?
200 repodata = @handle.first_repodata()
201 return unless repodata
202 oldaddedprovides = repodata.lookup_idarray(Solv::SOLVID_META, Solv::REPOSITORY_ADDEDFILEPROVIDES)
203 return if (oldaddedprovides | addedprovides) == oldaddedprovides
204 for id in addedprovides
205 repodata.add_idarray(Solv::SOLVID_META, Solv::REPOSITORY_ADDEDFILEPROVIDES, id)
207 repodata.internalize()
208 writecachedrepo(nil, repodata)
216 class Repo_rpmmd < Repo_generic
219 di = @handle.Dataiterator(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_TYPE, what, Solv::Dataiterator::SEARCH_STRING)
220 di.prepend_keyname(Solv::REPOSITORY_REPOMD)
223 filename = dp.lookup_str(Solv::REPOSITORY_REPOMD_LOCATION)
225 checksum = dp.lookup_checksum(Solv::REPOSITORY_REPOMD_CHECKSUM)
227 puts "no #{filename} checksum!"
230 return filename, checksum
236 print "rpmmd repo '#{@name}: "
237 f = download("repodata/repomd.xml", false, nil, nil)
239 puts "no repomd.xml file, skipped"
244 @cookie = calc_cookie_fp(f)
245 if usecachedrepo(nil, true)
250 @handle.add_repomdxml(f, 0)
253 filename, filechksum = find('primary')
255 f = download(filename, true, filechksum, true)
257 @handle.add_rpmmd(f, nil, 0)
260 return false if @incomplete
262 filename, filechksum = find('updateinfo')
264 f = download(filename, true, filechksum, true)
266 @handle.add_updateinfoxml(f, 0)
271 writecachedrepo(nil) unless @incomplete
272 @handle.create_stubs()
276 def add_ext(repodata, what, ext)
277 filename, filechksum = find(what)
278 filename, filechksum = find('prestodelta') if !filename && what == 'deltainfo'
279 return unless filename
280 h = repodata.new_handle()
281 repodata.set_poolstr(h, Solv::REPOSITORY_REPOMD_TYPE, what)
282 repodata.set_str(h, Solv::REPOSITORY_REPOMD_LOCATION, filename)
283 repodata.set_checksum(h, Solv::REPOSITORY_REPOMD_CHECKSUM, filechksum)
285 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOSITORY_DELTAINFO)
286 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_FLEXARRAY)
288 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_FILELIST)
289 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRSTRARRAY)
291 repodata.add_flexarray(Solv::SOLVID_META, Solv::REPOSITORY_EXTERNAL, h)
295 repodata = @handle.add_repodata(0)
296 add_ext(repodata, 'deltainfo', 'DL')
297 add_ext(repodata, 'filelists', 'FL')
298 repodata.internalize()
301 def load_ext(repodata)
302 repomdtype = repodata.lookup_str(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_TYPE)
303 if repomdtype == 'filelists'
305 elsif repomdtype == 'deltainfo'
310 print "[#{@name}:#{ext}: "
312 if usecachedrepo(ext)
317 filename = repodata.lookup_str(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_LOCATION)
318 filechksum = repodata.lookup_checksum(Solv::SOLVID_META, Solv::REPOSITORY_REPOMD_CHECKSUM)
319 f = download(filename, true, filechksum)
320 return false unless f
322 @handle.add_rpmmd(f, 'FL', Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES)
324 @handle.add_deltainfoxml(f, Solv::Repo::REPO_USE_LOADING)
327 writecachedrepo(ext, repodata)
333 class Repo_susetags < Repo_generic
336 di = @handle.Dataiterator(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME, what, Solv::Dataiterator::SEARCH_STRING)
337 di.prepend_keyname(Solv::SUSETAGS_FILE)
340 checksum = dp.lookup_checksum(Solv::SUSETAGS_FILE_CHECKSUM)
341 return what, checksum
347 print "susetags repo '#{@name}: "
348 f = download("content", false, nil, nil)
350 puts "no content file, skipped"
355 @cookie = calc_cookie_fp(f)
356 if usecachedrepo(nil, true)
361 @handle.add_content(f, 0)
364 defvendorid = @handle.lookup_id(Solv::SOLVID_META, Solv::SUSETAGS_DEFAULTVENDOR)
365 descrdir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DESCRDIR)
366 descrdir = "suse/setup/descr" unless descrdir
367 (filename, filechksum) = find('packages.gz')
368 (filename, filechksum) = find('packages') unless filename
370 f = download("#{descrdir}/#{filename}", true, filechksum, true)
372 @handle.add_susetags(f, defvendorid, nil, Solv::Repo::REPO_NO_INTERNALIZE|Solv::Repo::SUSETAGS_RECORD_SHARES)
374 (filename, filechksum) = find('packages.en.gz')
375 (filename, filechksum) = find('packages.en') unless filename
377 f = download("#{descrdir}/#{filename}", true, filechksum, true)
379 @handle.add_susetags(f, defvendorid, nil, Solv::Repo::REPO_NO_INTERNALIZE|Solv::Repo::REPO_REUSE_REPODATA|Solv::Repo::REPO_EXTEND_SOLVABLES)
383 @handle.internalize()
387 writecachedrepo(nil) unless @incomplete
388 @handle.create_stubs()
393 Solv::SOLVABLE_SUMMARY => Solv::REPOKEY_TYPE_STR,
394 Solv::SOLVABLE_DESCRIPTION => Solv::REPOKEY_TYPE_STR,
395 Solv::SOLVABLE_EULA => Solv::REPOKEY_TYPE_STR,
396 Solv::SOLVABLE_MESSAGEINS => Solv::REPOKEY_TYPE_STR,
397 Solv::SOLVABLE_MESSAGEDEL => Solv::REPOKEY_TYPE_STR,
398 Solv::SOLVABLE_CATEGORY => Solv::REPOKEY_TYPE_ID,
401 def add_ext(repodata, what, ext)
402 (filename, filechksum) = find(what)
403 h = repodata.new_handle()
404 repodata.set_str(h, Solv::SUSETAGS_FILE_NAME, filename)
405 repodata.set_checksum(h, Solv::SUSETAGS_FILE_CHECKSUM, filechksum)
407 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOSITORY_DELTAINFO)
408 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_FLEXARRAY)
410 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_DISKUSAGE)
411 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRNUMNUMARRAY)
413 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::SOLVABLE_FILELIST)
414 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, Solv::REPOKEY_TYPE_DIRSTRARRAY)
416 @@langtags.sort.each do |langid, langtype|
417 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, @handle.pool.id2langid(langid, ext, true))
418 repodata.add_idarray(h, Solv::REPOSITORY_KEYS, langtype)
421 repodata.add_flexarray(Solv::SOLVID_META, Solv::REPOSITORY_EXTERNAL, h)
425 repodata = @handle.add_repodata(0)
426 di = @handle.Dataiterator(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME, nil, 0)
427 di.prepend_keyname(Solv::SUSETAGS_FILE)
430 next unless filename && filename =~ /^packages\.(..)(?:\..*)$/
431 next if $1 == 'en' || $1 == 'gz'
432 add_ext(repodata, filename, $1)
434 repodata.internalize()
437 def load_ext(repodata)
438 filename = repodata.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_FILE_NAME)
440 print "[#{@name}:#{ext}: "
442 if usecachedrepo(ext)
447 defvendorid = @handle.lookup_id(Solv::SOLVID_META, Solv::SUSETAGS_DEFAULTVENDOR)
448 descrdir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DESCRDIR)
449 descrdir = "suse/setup/descr" unless descrdir
450 filechksum = repodata.lookup_checksum(Solv::SOLVID_META, Solv::SUSETAGS_FILE_CHECKSUM)
451 f = download("#{descrdir}/#{filename}", true, filechksum)
452 return false unless f
453 @handle.add_susetags(f, defvendorid, ext, Solv::Repo::REPO_USE_LOADING|Solv::Repo::REPO_EXTEND_SOLVABLES)
455 writecachedrepo(ext, repodata)
460 datadir = @handle.lookup_str(Solv::SOLVID_META, Solv::SUSETAGS_DATADIR)
461 datadir = "suse" unless datadir
466 class Repo_unknown < Repo_generic
468 puts "unsupported repo '#{@name}: skipped"
473 class Repo_system < Repo_generic
475 @handle = pool.add_repo(@name)
476 @handle.appdata = self
477 pool.installed = @handle
478 print "rpm database: "
479 @cookie = calc_cookie_file("/var/lib/rpm/Packages")
480 if usecachedrepo(nil)
485 if @handle.respond_to? :add_products
486 @handle.add_products("/etc/products.d", Solv::Repo::REPO_NO_INTERNALIZE)
488 @handle.add_rpmdb(nil, Solv::Repo::REPO_REUSE_REPODATA)
496 cmd = 'list' if cmd == 'li'
497 cmd = 'install' if cmd == 'in'
498 cmd = 'erase' if cmd == 'rm'
499 cmd = 'verify' if cmd == 've'
500 cmd = 'search' if cmd == 'se'
504 if FileTest.directory?('/etc/zypp/repos.d')
505 reposdirs = [ '/etc/zypp/repos.d' ]
507 reposdirs = [ '/etc/yum/repos.d' ]
509 for reposdir in reposdirs do
510 next unless FileTest.directory?(reposdir)
511 for reponame in Dir["#{reposdir}/*.repo"].sort do
512 cfg = IniFile.new(reponame)
513 cfg.each_section do |ali|
514 repoattr = { 'alias' => ali, 'enabled' => 0, 'priority' => 99, 'autorefresh' => 1, 'type' => 'rpm-md', 'metadata_expire' => 900}
515 repoattr.update(cfg[ali])
516 if repoattr['type'] == 'rpm-md'
517 repo = Repo_rpmmd.new(ali, 'repomd', repoattr)
518 elsif repoattr['type'] == 'yast2'
519 repo = Repo_susetags.new(ali, 'susetags', repoattr)
521 repo = Repo_unknown.new(ali, 'unknown', repoattr)
528 pool = Solv::Pool.new()
529 # require 'sys/uname' ; sysarch = Sys::Uname.machine
530 sysarch = `uname -p`.strip
531 pool.setarch(sysarch)
533 pool.set_loadcallback { |repodata|
534 repo = repodata.repo.appdata
535 repo ? repo.load_ext(repodata) : false
538 sysrepo = Repo_system.new('@System', 'system')
541 repo.load(pool) if repo.enabled?
546 for di in pool.Dataiterator(0, Solv::SOLVABLE_NAME, args[0], Solv::Dataiterator::SEARCH_SUBSTRING | Solv::Dataiterator::SEARCH_NOCASE)
547 matches[di.solvid] = true
549 for solvid in matches.keys.sort
550 s = pool.solvables[solvid]
551 puts "- #{s.str} [#{s.repo.name}]"
556 addedprovides = pool.addfileprovides_queue()
557 if !addedprovides.empty?
558 sysrepo.updateaddedprovides(addedprovides)
560 repo.updateaddedprovides(addedprovides)
563 pool.createwhatprovides()
567 flags = Solv::Selection::SELECTION_NAME | Solv::Selection::SELECTION_PROVIDES|Solv::Selection::SELECTION_GLOB
569 flags |= Solv::Selection::SELECTION_FILELIST
570 flags |= Solv::Selection::SELECTION_INSTALLED_ONLY if cmd == 'erase'
572 sel = pool.select(arg, flags)
574 sel = pool.select(arg, flags | Solv::Selection::SELECTION_NOCASE)
575 puts "[ignoring case for '#{arg}']" unless sel.isempty?
577 puts "[using file list match for '#{arg}']" if sel.flags & Solv::Selection::SELECTION_FILELIST != 0
578 puts "[using capability match for '#{arg}']" if sel.flags & Solv::Selection::SELECTION_PROVIDES != 0
582 if jobs.empty? && (cmd == 'up' || cmd == 'dup' || cmd == 'verify')
583 sel = pool.Selection()
584 sel.addsimple(Solv::Job::SOLVER_SOLVABLE_ALL, 0)
588 if cmd == 'list' || cmd == 'info'
589 abort("no package matched.") if jobs.empty?
591 for s in job.solvables()
593 puts "Name: #{s.str}"
594 puts "Repo: #{s.repo.name}"
595 puts "Summary: #{s.lookup_str(Solv::SOLVABLE_SUMMARY)}"
596 str = s.lookup_str(Solv::SOLVABLE_URL)
597 puts "Url: #{str}" if str
598 str = s.lookup_str(Solv::SOLVABLE_LICENSE)
599 puts "License: #{str}" if str
600 puts "Description:\n#{s.lookup_str(Solv::SOLVABLE_DESCRIPTION)}"
603 puts " - #{s.str} [#{s.repo.name}]"
604 puts " #{s.lookup_str(Solv::SOLVABLE_SUMMARY)}"
611 if cmd == 'install' || cmd == 'erase' || cmd == 'up' || cmd == 'dup' || cmd == 'verify'
612 abort("no package matched.") if jobs.empty?
615 if job.how == Solv::Job::SOLVER_SOLVABLE_ALL || job.solvables.any? {|s| s.isinstalled?}
616 job.how |= Solv::Job::SOLVER_UPDATE
618 job.how |= Solv::Job::SOLVER_INSTALL
620 elsif cmd == 'install'
621 job.how |= Solv::Job::SOLVER_INSTALL
623 job.how |= Solv::Job::SOLVER_ERASE
625 job.how |= Solv::Job::SOLVER_DISTUPGRADE
626 elsif cmd == 'verify'
627 job.how |= Solv::Job::SOLVER_VERIFY
632 #pool.set_debuglevel(1)
635 solver.set_flag(Solv::Solver::SOLVER_FLAG_SPLITPROVIDES, 1)
636 solver.set_flag(Solv::Solver::SOLVER_FLAG_ALLOW_UNINSTALL, 1) if cmd == 'erase'
637 problems = solver.solve(jobs)
638 break if problems.empty?
639 for problem in problems
640 puts "Problem #{problem.id}:"
641 puts problem.findproblemrule.info.problemstr
642 solutions = problem.solutions
643 for solution in solutions
644 puts " Solution #{solution.id}:"
645 elements = solution.elements(true)
646 for element in elements
647 puts " - #{element.str}"
653 print "Please choose a solution: "
655 sol = STDIN.gets.strip
656 break if sol == 's' || sol == 'q'
657 break if sol =~ /^\d+$/ && sol.to_i >= 1 && sol.to_i <= solutions.length
661 solution = solutions[sol.to_i - 1]
662 for element in solution.elements
663 newjob = element.Job()
664 if element.type == Solv::Solver::SOLVER_SOLUTION_JOB
665 jobs[element.jobidx] = newjob
667 jobs.push(newjob) if newjob && !jobs.include?(newjob)
672 trans = solver.transaction
675 puts "Nothing to do."
678 puts "\nTransaction summary:\n"
679 for cl in trans.classify()
680 if cl.type == Solv::Transaction::SOLVER_TRANSACTION_ERASE
681 puts "#{cl.count} erased packages:"
682 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_INSTALL
683 puts "#{cl.count} installed packages:"
684 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_REINSTALLED
685 puts "#{cl.count} reinstalled packages:"
686 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED
687 puts "#{cl.count} downgraded packages:"
688 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_CHANGED
689 puts "#{cl.count} changed packages:"
690 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_UPGRADED
691 puts "#{cl.count} upgraded packages:"
692 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_VENDORCHANGE
693 puts "#{cl.count} vendor changes from '#{pool.id2str(cl.fromid)}' to '#{pool.id2str(cl.toid)}':"
694 elsif cl.type == Solv::Transaction::SOLVER_TRANSACTION_ARCHCHANGE
695 puts "#{cl.count} arch changes from '#{pool.id2str(cl.fromid)}' to '#{pool.id2str(cl.toid)}':"
699 for p in cl.solvables
700 if cl.type == Solv::Transaction::SOLVER_TRANSACTION_UPGRADED || cl.type == Solv::Transaction::SOLVER_TRANSACTION_DOWNGRADED
701 puts " - #{p.str} -> #{trans.othersolvable(p).str}"
708 puts "install size change: #{trans.calc_installsizechange()} K\n\n"
710 print("OK to continue (y/n)? ")
712 yn = STDIN.gets.strip
716 newpkgs = trans.newpackages()
721 downloadsize += p.lookup_num(Solv::SOLVABLE_DOWNLOADSIZE)
723 puts "Downloading #{newpkgs.length} packages, #{downloadsize} K"
725 repo = p.repo.appdata
726 location, medianr = p.lookup_location()
728 location = repo.packagespath + location
729 chksum = p.lookup_checksum(Solv::SOLVABLE_CHECKSUM)
730 f = repo.download(location, false, chksum)
731 abort("\n#{@name}: #{location} not found in repository\n") unless f
738 puts "Committing transaction:"
742 steptype = trans.steptype(p, Solv::Transaction::SOLVER_TRANSACTION_RPM_ONLY)
743 if steptype == Solv::Transaction::SOLVER_TRANSACTION_ERASE
744 puts "erase #{p.str}"
745 next unless p.lookup_num(Solv::RPM_RPMDBID)
746 evr = p.evr.sub(/^[0-9]+:/, '')
747 system('rpm', '-e', '--nodeps', '--nodigest', '--nosignature', "#{p.name}-#{evr}.#{p.arch}") || abort("rpm failed: #{$? >> 8}")
748 elsif (steptype == Solv::Transaction::SOLVER_TRANSACTION_INSTALL || steptype == Solv::Transaction::SOLVER_TRANSACTION_MULTIINSTALL)
749 puts "install #{p.str}"
750 f = newpkgsfp.delete(p.id)
752 mode = steptype == Solv::Transaction::SOLVER_TRANSACTION_INSTALL ? '-U' : '-i'
753 system('rpm', mode, '--force', '--nodeps', '--nodigest', '--nosignature', "/dev/fd/#{Solv::xfileno(f).to_s}") || abort("rpm failed: #{$? >> 8}")