summaryrefslogtreecommitdiff
path: root/examples/python/SimpleWalkthrough.py
blob: e14cc87319e28ec92eb35c385549a1af5fe07c67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /usr/bin/python
import zypp
# ========================================================================================

def poolInstall( Z, capstr ):
  print("Request: install %s" % capstr)
  Z.resolver().addRequire( zypp.Capability( capstr ) )

def poolRemove( Z, capstr ):
  print("Request: delete  %s" % capstr)
  Z.resolver().addConflict( zypp.Capability( capstr ) )

def poolPrintTransaction( Z ):
  todo = Z.pool().getTransaction()
  for item in todo._toDelete:
    print('-- %s | %s-%s | %s' % (item.repoInfo().alias(), item.name(), item.edition(), item.status() ))
  for item in todo._toInstall:
    print('++ %s | %s-%s | %s' % (item.repoInfo().alias(), item.name(), item.edition(), item.status() ))

def poolResolve( Z ):
  print("Resolve pool:")
  while not Z.resolver().resolvePool():
    # Print _all_ problems and possible solutions:
    problems = Z.resolver().problems()
    pn = 0
    for problem in problems:
      pn += 1
      print("Problem %d:" % pn)
      print("==============================")
      print(problem.description())
      if problem.details():
	print(problem.details())
      print("------------------------------")
      sn = 0
      for solution in problem.solutions():
	sn += 1
	print("Solution %d.%d:" % ( pn, sn ))
	print(solution.description())
	if solution.details():
	  print(solution.details())
      print("==============================")
      print()

    # Faked user interaction: stupidly pick all 1st solutions (don't do this in real life!)
    #
    # In real life you probably pick just a single solution
    # and re-solve immedaitely, because one solution may solve
    # multiple ploblems - or create new ones.
    #
    pickedSolutions =  zypp.ProblemSolutionList()
    pn = 0
    for problem in problems:
      pn += 1
      sn = 0
      for solution in problem.solutions():
	sn += 1
	print("Stupidly pick solution %d.%d" % ( pn, sn ))
	pickedSolutions.push_back( solution )
	break
    # Apply picked solutions:
    Z.resolver().applySolutions( pickedSolutions )

    #
    print("Example stops here instead of starting a new iteration...")
    print()
    raise BaseException("Solver Error")

  poolPrintTransaction( Z )
  print("[done]")

def poolUpdate( Z ):
  # In contrary to
  print("Update pool:")
  Z.resolver().doUpdate()
  poolPrintTransaction( Z )
  print("[done]")

# ========================================================================================
Z = zypp.ZYppFactory_instance().getZYpp()

# Load system rooted at "/"...
#
Z.initializeTarget( zypp.Pathname("/") )
Z.target().load();

# Load all enabled repositories...
#
repoManager = zypp.RepoManager()
for repo in repoManager.knownRepositories():
  if not repo.enabled():
    continue
  if not repoManager.isCached( repo ):
    repoManager.buildCache( repo )
  repoManager.loadFromCache( repo );

# Now all installed and available items are in the pool:
#
print("Known items: %d" % ( Z.pool().size() ))
if True:
    # Iterate the pool to query items. PoolItems are not just packages
    # but also patterns, patches, products, ...
    # PoolItem provides the common attributes and status. For specific
    # attibutes cast the item inot the specific kind.
    print("Printing just the Products...")
    for item in Z.pool():
	if not zypp.isKindProduct( item ):
	  continue

	if item.status().isInstalled():
	  t = "i"
	else:
	  t = "*"
	print("%s %s:%s-%s.%s\t(%s)" % ( t,
					item.kind(),
					item.name(),
					item.edition(),
					item.arch(),
					item.repoInfo().alias() ))

	# How to access e.g. product specific attributes:
	if zypp.isKindProduct( item ):
	  prod = zypp.asKindProduct( item )
	  print("  %s (%s)" % ( prod.shortName(), prod.flavor() ))
    print()

# Building and resolving a transaction:
#
doUpdate = False
if doUpdate:
  # Simply try to update all installed packages:
  poolUpdate( Z )
else:
  # Add jobs to the pools resolver
  # and finally resolve the jobs.
  poolInstall( Z, "libzypp = 13.9.0-13.1" )
  poolInstall( Z, "pattern:unknown" )
  poolRemove( Z, "xteddy < 1.0" )
  poolResolve( Z )

# finally install (here dryRun)
#
policy = zypp.ZYppCommitPolicy()
policy.syncPoolAfterCommit( False )
policy.dryRun( True )

result = Z.commit( policy )
print(result)