Bump to libzypp-bindings 0.7.4
[platform/upstream/libzypp-bindings.git] / examples / python / SimpleWalkthrough.py
1 #! /usr/bin/python
2 import zypp
3 # ========================================================================================
4
5 def poolInstall( Z, capstr ):
6   print "Request: install %s" % capstr
7   Z.resolver().addRequire( zypp.Capability( capstr ) )
8
9 def poolRemove( Z, capstr ):
10   print "Request: delete  %s" % capstr
11   Z.resolver().addConflict( zypp.Capability( capstr ) )
12
13 def poolPrintTransaction( Z ):
14   todo = Z.pool().getTransaction()
15   for item in todo._toDelete:
16     print '-- %s | %s-%s | %s' % (item.repoInfo().alias(), item.name(), item.edition(), item.status() )
17   for item in todo._toInstall:
18     print '++ %s | %s-%s | %s' % (item.repoInfo().alias(), item.name(), item.edition(), item.status() )
19
20 def poolResolve( Z ):
21   print "Resolve pool:"
22   while not Z.resolver().resolvePool():
23     # Print _all_ problems and possible solutions:
24     problems = Z.resolver().problems()
25     pn = 0
26     for problem in problems:
27       pn += 1
28       print "Problem %d:" % pn
29       print "=============================="
30       print problem.description()
31       if problem.details():
32         print problem.details()
33       print "------------------------------"
34       sn = 0
35       for solution in problem.solutions():
36         sn += 1
37         print "Solution %d.%d:" % ( pn, sn )
38         print solution.description()
39         if solution.details():
40           print solution.details()
41       print "=============================="
42       print
43
44     # Faked user interaction: stupidly pick all 1st solutions (don't do this in real life!)
45     #
46     # In real life you probably pick just a single solution
47     # and re-solve immedaitely, because one solution may solve
48     # multiple ploblems - or create new ones.
49     #
50     pickedSolutions =  zypp.ProblemSolutionList()
51     pn = 0
52     for problem in problems:
53       pn += 1
54       sn = 0
55       for solution in problem.solutions():
56         sn += 1
57         print "Stupidly pick solution %d.%d" % ( pn, sn )
58         pickedSolutions.push_back( solution )
59         break
60     # Apply picked solutions:
61     Z.resolver().applySolutions( pickedSolutions )
62
63     #
64     print "Example stops here instead of starting a new iteration..."
65     print
66     raise BaseException("Solver Error")
67
68   poolPrintTransaction( Z )
69   print "[done]"
70
71 def poolUpdate( Z ):
72   # In contrary to
73   print "Update pool:"
74   Z.resolver().doUpdate()
75   poolPrintTransaction( Z )
76   print "[done]"
77
78 # ========================================================================================
79 Z = zypp.ZYppFactory_instance().getZYpp()
80
81 # Load system rooted at "/"...
82 #
83 Z.initializeTarget( zypp.Pathname("/") )
84 Z.target().load();
85
86 # Load all enabled repositories...
87 #
88 repoManager = zypp.RepoManager()
89 for repo in repoManager.knownRepositories():
90   if not repo.enabled():
91     continue
92   if not repoManager.isCached( repo ):
93     repoManager.buildCache( repo )
94   repoManager.loadFromCache( repo );
95
96 # Now all installed and available items are in the pool:
97 #
98 print "Known items: %d" % ( Z.pool().size() )
99 if True:
100     # Iterate the pool to query items. PoolItems are not just packages
101     # but also patterns, patches, products, ...
102     # PoolItem provides the common attributes and status. For specific
103     # attibutes cast the item inot the specific kind.
104     print "Printing just the Products..."
105     for item in Z.pool():
106         if not zypp.isKindProduct( item ):
107           continue
108
109         if item.status().isInstalled():
110           t = "i"
111         else:
112           t = "*"
113         print "%s %s:%s-%s.%s\t(%s)" % ( t,
114                                         item.kind(),
115                                         item.name(),
116                                         item.edition(),
117                                         item.arch(),
118                                         item.repoInfo().alias() )
119
120         # How to access e.g. product specific attributes:
121         if zypp.isKindProduct( item ):
122           prod = zypp.asKindProduct( item )
123           print "  %s (%s)" % ( prod.shortName(), prod.flavor() )
124     print
125
126 # Building and resolving a transaction:
127 #
128 doUpdate = False
129 if doUpdate:
130   # Simply try to update all installed packages:
131   poolUpdate( Z )
132 else:
133   # Add jobs to the pools resolver
134   # and finally resolve the jobs.
135   poolInstall( Z, "libzypp = 13.9.0-13.1" )
136   poolInstall( Z, "pattern:unknown" )
137   poolRemove( Z, "xteddy < 1.0" )
138   poolResolve( Z )
139
140 # finally install (here dryRun)
141 #
142 policy = zypp.ZYppCommitPolicy()
143 policy.syncPoolAfterCommit( False )
144 policy.dryRun( True )
145
146 result = Z.commit( policy )
147 print result