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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
# package.py - apt package abstraction
#
# Copyright (c) 2005 Canonical
#
# Author: Michael Vogt <michael.vogt@ubuntu.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
import apt_pkg, string
import random
class Package(object):
""" This class represents a package in the cache
"""
def __init__(self, cache, depcache, records, pcache, pkgiter):
""" Init the Package object """
self._cache = cache # low level cache
self._depcache = depcache
self._records = records
self._pkg = pkgiter
self._pcache = pcache # python cache in cache.py
pass
# helper
def _lookupRecord(self, UseCandidate=True):
""" internal helper that moves the Records to the right
position, must be called before _records is accessed """
if UseCandidate:
ver = self._depcache.GetCandidateVer(self._pkg)
else:
ver = self._pkg.CurrentVer
# check if we found a version
if ver == None:
print "No version for: %s (Candiate: %s)" % (self._pkg.Name, UseCandidate)
return False
if ver.FileList == None:
print "No FileList for: %s " % self._pkg.Name()
return False
file, index = ver.FileList.pop(0)
self._records.Lookup((file,index))
return True
# basic information (implemented as properties)
# FIXME once python2.3 is dropped we can use @property instead
# of name = property(name)
def name(self):
""" Return the name of the package """
return self._pkg.Name
name = property(name)
def id(self):
""" Return a uniq ID for the pkg, can be used to store
additional information about the pkg """
return self._pkg.ID
id = property(id)
def installedVersion(self):
""" Return the installed version as string """
ver = self._pkg.CurrentVer
if ver != None:
return ver.VerStr
else:
return None
installedVersion = property(installedVersion)
def candidateVersion(self):
""" Return the candidate version as string """
ver = self._depcache.GetCandidateVer(self._pkg)
if ver != None:
return ver.VerStr
else:
return None
candidateVersion = property(candidateVersion)
def sourcePackageName(self):
""" Return the source package name as string """
self._lookupRecord()
src = self._records.SourcePkg
if src != "":
return src
else:
return self._pkg.Name
sourcePackageName = property(sourcePackageName)
def section(self):
""" Return the section of the package"""
return self._pkg.Section
section = property(section)
def priority(self):
""" Return the priority (of the candidate version)"""
ver = self._depcache.GetCandidateVer(self._pkg)
if ver:
return ver.PriorityStr
else:
return None
priority = property(priority)
def installedPriority(self):
""" Return the priority (of the installed version)"""
ver = self._depcache.GetCandidateVer(self._pkg)
if ver:
return ver.PriorityStr
else:
return None
installedPriority = property(installedPriority)
def summary(self):
""" Return the short description (one line summary) """
self._lookupRecord()
return self._records.ShortDesc
summary = property(summary)
def description(self, format=True):
""" Return the formated long description """
self._lookupRecord()
desc = ""
for line in string.split(self._records.LongDesc, "\n"):
tmp = string.strip(line)
if tmp == ".":
desc += "\n"
else:
desc += tmp + "\n"
return desc
description = property(description)
def rawDescription(self):
""" return the long description (raw)"""
self._lookupRecord()
return self._records.LongDesc
rawDescription = property(rawDescription)
# depcache states
def markedInstall(self):
""" Package is marked for install """
return self._depcache.MarkedInstall(self._pkg)
markedInstall = property(markedInstall)
def markedUpgrade(self):
""" Package is marked for upgrade """
return self._depcache.MarkedUpgrade(self._pkg)
markedUpgrade = property(markedUpgrade)
def markedDelete(self):
""" Package is marked for delete """
return self._depcache.MarkedDelete(self._pkg)
markedDelete = property(markedDelete)
def markedKeep(self):
""" Package is marked for keep """
return self._depcache.MarkedKeep(self._pkg)
markedKeep = property(markedKeep)
def markedDowngrade(self):
""" Package is marked for downgrade """
return self._depcache.MarkedDowngrade(self._pkg)
markedDowngrade = property(markedDowngrade)
def markedReinstall(self):
""" Package is marked for reinstall """
return self._depcache.MarkedReinstall(self._pkg)
markedReinstall = property(markedReinstall)
def isInstalled(self):
""" Package is installed """
return (self._pkg.CurrentVer != None)
isInstalled = property(isInstalled)
def isUpgradable(self):
""" Package is upgradable """
return self.isInstalled and self._depcache.IsUpgradable(self._pkg)
isUpgradable = property(isUpgradable)
# size
def packageSize(self):
""" The size of the candidate deb package """
ver = self._depcache.GetCandidateVer(self._pkg)
return ver.Size
packageSize = property(packageSize)
def installedPackageSize(self):
""" The size of the installed deb package """
ver = self._pkg.CurrentVer
return ver.Size
installedPackageSize = property(installedPackageSize)
def candidateInstalledSize(self, UseCandidate=True):
""" The size of the candidate installed package """
ver = self._depcache.GetCandidateVer(self._pkg)
candidateInstalledSize = property(candidateInstalledSize)
def installedSize(self):
""" The size of the currently installed package """
ver = self._pkg.CurrentVer
return ver.InstalledSize
installedSize = property(installedSize)
# depcache actions
def markKeep(self):
""" mark a package for keep """
self._pcache.cachePreChange()
self._depcache.MarkKeep(self._pkg)
self._pcache.cachePostChange()
def markDelete(self, autoFix=True):
""" mark a package for delete. Run the resolver if autoFix is set """
self._pcache.cachePreChange()
self._depcache.MarkDelete(self._pkg)
# try to fix broken stuffsta
if autoFix and self._depcache.BrokenCount > 0:
Fix = apt_pkg.GetPkgProblemResolver(self._depcache)
Fix.Clear(self._pkg)
Fix.Protect(self._pkg)
Fix.Remove(self._pkg)
Fix.InstallProtect()
Fix.Resolve()
self._pcache.cachePostChange()
def markInstall(self, autoFix=True):
""" mark a package for install. Run the resolver if autoFix is set """
self._pcache.cachePreChange()
self._depcache.MarkInstall(self._pkg)
# try to fix broken stuff
if autoFix and self._depcache.BrokenCount > 0:
fixer = apt_pkg.GetPkgProblemResolver(self._depcache)
fixer.Clear(self._pkg)
fixer.Protect(self._pkg)
fixer.Resolve(True)
self._pcache.cachePostChange()
def markUpgrade(self):
""" mark a package for upgrade """
if self.isUpgradable:
self.MarkInstall()
# FIXME: we may want to throw a exception here
sys.stderr.write("MarkUpgrade() called on a non-upgrable pkg")
def commit(self, fprogress, iprogress):
""" commit the changes, need a FetchProgress and InstallProgress
object as argument
"""
self._depcache.Commit(fprogress, iprogress)
# self-test
if __name__ == "__main__":
print "Self-test for the Package modul"
apt_pkg.init()
cache = apt_pkg.GetCache()
depcache = apt_pkg.GetDepCache(cache)
records = apt_pkg.GetPkgRecords(cache)
iter = cache["apt-utils"]
pkg = Package(cache, depcache, records, None, iter)
print "Name: %s " % pkg.name
print "ID: %s " % pkg.id
print "Priority (Candidate): %s " % pkg.priority
print "Priority (Installed): %s " % pkg.installedPriority
print "Installed: %s " % pkg.installedVersion
print "Candidate: %s " % pkg.candidateVersion
print "SourcePkg: %s " % pkg.sourcePackageName
print "Section: %s " % pkg.section
print "Summary: %s" % pkg.summary
print "Description (formated) :\n%s" % pkg.description
print "Description (unformated):\n%s" % pkg.rawDescription
print "InstalledSize: %s " % pkg.installedSize
print "PackageSize: %s " % pkg.packageSize
# now test install/remove
import apt
progress = apt.progress.OpTextProgress()
cache = apt.Cache(progress)
for i in [True, False]:
print "Running install on random upgradable pkgs with AutoFix: %s " % i
for name in cache.keys():
pkg = cache[name]
if pkg.isUpgradable:
if random.randint(0,1) == 1:
pkg.markInstall(i)
print "Broken: %s " % cache._depcache.BrokenCount
print "InstCount: %s " % cache._depcache.InstCount
print
# get a new cache
for i in [True, False]:
print "Randomly remove some packages with AutoFix: %s" % i
cache = apt.Cache(progress)
for name in cache.keys():
if random.randint(0,1) == 1:
try:
cache[name].markDelete(i)
except SystemError:
print "Error trying to remove: %s " % name
print "Broken: %s " % cache._depcache.BrokenCount
print "DelCount: %s " % cache._depcache.DelCount
|