start ruby bindings and example solver
[platform/upstream/libsolv.git] / examples / rbsolv
1 #!/usr/bin/ruby
2
3 #  bool: method?
4 #  inplace mod: method!
5 #  set method: method=
6
7 # map  => collect
8 # grep => find_all
9
10 require 'solv'
11
12 def depglob(pool, name, globname, globdep)
13   id = pool.str2id(name, 0)
14   if id != 0
15     match = false
16     providers = pool.providers(id)
17     if globname && providers.find {|s| s.nameid == id }
18       return [ pool.Job(Solv::Job::SOLVER_SOLVABLE_NAME, id) ]
19     end
20     if !providers.empty?
21       puts "[using capability match for '#{name}']" if globname && globdep
22       return [ pool.Job(Solv::Job::SOLVER_SOLVABLE_PROVIDES, id) ]
23     end
24   end
25   return [] unless name =~ /[\[*?]/;
26   if globname
27     idmatches = {}
28     for d in pool.Dataiterator(0, Solv::SOLVABLE_NAME, name, Solv::Dataiterator::SEARCH_GLOB)
29       s = d.solvable
30       idmatches[s.nameid] = 1 if s.installable?
31     end
32     if !idmatches.empty?
33       return idmatches.keys.sort.collect { |id| pool.Job(Solv::Job::SOLVER_SOLVABLE_NAME, id) }
34     end
35   end
36   if globdep
37     idmatches = pool.matchprovidingids(name, Solv::Dataiterator::SEARCH_GLOB);
38     if !idmatches.empty?
39       puts "[using capability match for '#{name}']"
40       return idmatches.sort.collect { |id| pool.Job(Solv::Job::SOLVER_SOLVABLE_PROVIDES, id) }
41     end
42   end
43   return []
44 end
45
46
47 pool = Solv::Pool.new()
48 repo = pool.add_repo("TEST")
49 pool.installed = repo
50 repo.add_solv("/var/cache/solv/@System.solv", 0);
51
52 pool.addfileprovides
53 pool.createwhatprovides
54 jobs = depglob(pool, ARGV[0], true, true)
55 for job in jobs
56   job.how |= Solv::Job::SOLVER_ERASE
57 end
58 solver = pool.Solver
59 problems = solver.solve(jobs)
60 for problem in problems
61   puts "Problem #{problem.id}:"
62   puts problem.findproblemrule.info.problemstr
63   solutions = problem.solutions
64   for solution in solutions
65     puts "  Solution #{solution.id}:"
66     elements = solution.elements
67     for element in elements:
68       puts "  - type #{element.type}"
69     end
70   end
71 end