diff options
| author | Michael Vogt <michael.vogt@ubuntu.com> | 2005-05-09 12:59:08 +0000 |
|---|---|---|
| committer | Michael Vogt <michael.vogt@ubuntu.com> | 2005-05-09 12:59:08 +0000 |
| commit | ae3f10fba648644a568dab3e21480580eb21e8a1 (patch) | |
| tree | 4e7f64dc172c4813f926d8607672e28ce07f04bb | |
| parent | 5e4d32fbdcb6ab0bfe5326e09fea1c9ecd7ef98d (diff) | |
| download | python-apt-ae3f10fba648644a568dab3e21480580eb21e8a1.tar.gz | |
* more work on the native python apt interface
| -rw-r--r-- | apt/cache.py | 60 | ||||
| -rw-r--r-- | apt/package.py | 154 | ||||
| -rw-r--r-- | apt/progress.py | 49 | ||||
| -rwxr-xr-x | debian/rules | 2 | ||||
| -rw-r--r-- | doc/examples/depcache.py | 24 | ||||
| -rw-r--r-- | doc/examples/progress.py | 12 | ||||
| -rw-r--r-- | python/depcache.cc | 2 | ||||
| -rw-r--r-- | setup.py | 3 |
8 files changed, 279 insertions, 27 deletions
diff --git a/apt/cache.py b/apt/cache.py new file mode 100644 index 00000000..e575745f --- /dev/null +++ b/apt/cache.py @@ -0,0 +1,60 @@ +import apt_pkg +from apt import Package +from UserDict import UserDict + +class Filter(object): + def apply(self, pkg): + pass + + + +class Cache(object): + def __init__(self): + #dict.__init__(self) + self.open() + + def open(self) + self._cache = apt_pkg.GetCache() + self._depcache = apt_pkg.GetDepCache(self._cache) + self._records = apt_pkg.GetPkgRecords(self._cache) + self._depcache.Init() + self._dict = {} + + # build the packages dict + for pkg in self._cache.Packages: + self._dict[pkg.Name] = Package(self._cache, self._depcache, self._records, pkg) + + def __getitem__(self, key): + return self._dict[key] + + def has_key(self, key): + try: + self._dict[key] + except KeyError: + return False + return True + + def __len__(self): + return len(self._dict) + + def keys(self): + return self._dict.keys() + + def Upgrade(self, DistUpgrade=False): + self._depcache.Upgrade(DistUpgrade) + + def Commit(self, fprogress, iprogress): + self._depcache.Commit(fprogress, iprogress) + + def + +if __name__ == "__main__": + print "Cache self test" + apt_pkg.init() + c = Cache() + print c.has_key("aptitudex") + p = c["aptitude"] + print p.Name() + + for pkg in c.keys(): + x= c[pkg].Name() diff --git a/apt/package.py b/apt/package.py new file mode 100644 index 00000000..138a520f --- /dev/null +++ b/apt/package.py @@ -0,0 +1,154 @@ +import apt_pkg, string + +class Package(object): + + def __init__(self, cache, depcache, records, pkgiter): + """ Init the Package object """ + self._cache = cache + self._depcache = depcache + self._records = records + self._pkg = pkgiter + 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 + file, index = ver.FileList.pop(0) + self._records.Lookup((file,index)) + + + # basic information + def Name(self): + """ return the name of the package """ + return self._pkg.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 + + def InstalledVersion(self): + """ return the installed version as string """ + ver = self._pkg.CurrentVer + if ver != None: + return ver.VerStr + else: + return None + + def CandidateVersion(self): + """ return the candidate version as string """ + ver = self._depcache.GetCandidateVer(self._pkg) + if ver != None: + return ver.VerStr + else: + return None + + 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 + + def Section(self): + """ return the section of the package""" + return self._pkg.Section + + def Priority(self, UseCandidate=True): + """ return the priority """ + if UseCandidate: + ver = self._depcache.GetCandidateVer(self._pkg) + else: + ver = self._pkg.CurrentVer + if ver: + return ver.PriorityStr + else: + return None + + def Summary(self): + """ return the short description (one-line summary) """ + self._LookupRecord() + return self._records.ShortDesc + + def Description(self, format=False): + """ return the long description """ + self._LookupRecord() + if format: + desc = "" + for line in string.split(self._records.LongDesc, "\n"): + tmp = string.strip(line) + if tmp == ".": + desc += "\n" + else: + desc += tmp + "\n" + return desc + else: + return self._records.LongDesc + + # depcache state + def MarkedInstall(self): + return self._depcache.MarkedInstall(self._pkg) + def MarkedUpgrade(self): + return self._depcache.MarkedUpgrade(self._pkg) + def MarkedDelete(self): + return self._depcache.MarkedKeep(self._pkg) + def MarkedKeep(self): + return self._depcache.MarkedKeep(self._pkg) + def IsUpgradable(self): + return self._depcache.IsUpgradable(self._pkg) + + # depcache action + def MarkKeep(self): + return self._depcache.MarkKeep(self._pkg) + def MarkDelete(self): + return self._depcache.MarkDelete(self._pkg) + def MarkInstall(self): + return self._depcache.MarkInstall(self._pkg) + + # size + def PackageSize(self, UseCandidate=True): + if UseCandidate: + ver = self._depcache.GetCandidateVer(self._pkg) + else: + ver = self._pkg.GetCurrentVer + return ver.Size + + def InstalledSize(self, UseCandidate=True): + if UseCandidate: + ver = self._depcache.GetCandidateVer(self._pkg) + else: + ver = self._pkg.GetCurrentVer + return ver.InstalledSize + + def Commit(self, fprogress, iprogress): + 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, iter) + print "Name: %s " % pkg.Name() + print "Installed: %s " % pkg.InstalledVersion() + print "Candidate: %s " % pkg.CandidateVersion() + print "SourcePkg: %s " % pkg.SourcePackageName() + print "Section: %s " % pkg.Section() + print "Priority (Candidate): %s " % pkg.Priority() + print "Priority (Installed): %s " % pkg.Priority(False) + print "Summary: %s" % pkg.Summary() + print "Description:\n%s" % pkg.Description() + print "Description (formated) :\n%s" % pkg.Description(True) + print "InstalledSize: %s " % pkg.InstalledSize() + print "PackageSize: %s " % pkg.PackageSize() diff --git a/apt/progress.py b/apt/progress.py new file mode 100644 index 00000000..ae5706af --- /dev/null +++ b/apt/progress.py @@ -0,0 +1,49 @@ + +class OpProgress: + def __init__(self): + pass + def Update(self, percent): + pass + def Done(self): + pass + +class FetchProgress: + def __init__(self): + pass + + def Start(self): + pass + + def Stop(self): + pass + + def UpdateStatus(self, uri, descr, shortDescr, status): + pass + + def Pulse(self): + pass + + def MediaChange(self, medium, drive): + pass + +class InstallProgress: + def __init__(self): + pass + def StartUpdate(self): + pass + def FinishUpdate(self): + pass + def UpdateInterface(self): + pass + + +class CdromProgress: + def __init__(self): + pass + def Update(self, text, step): + """ update is called regularly so that the gui can be redrawn """ + pass + def AskCdromName(self): + pass + def ChangeCdrom(self): + pass diff --git a/debian/rules b/debian/rules index 0ab6fa27..cba35c97 100755 --- a/debian/rules +++ b/debian/rules @@ -22,7 +22,7 @@ build-stamp: dh_testdir for PY in $(PYTHON); do \ - /usr/bin/$$PY setup.py build; \ + CC="g++" /usr/bin/$$PY setup.py build; \ done touch build-stamp diff --git a/doc/examples/depcache.py b/doc/examples/depcache.py index 50dd7703..556c954b 100644 --- a/doc/examples/depcache.py +++ b/doc/examples/depcache.py @@ -1,29 +1,17 @@ #!/usr/bin/python # example how to deal with the depcache +import apt import apt_pkg import sys import copy +from progress import TextProgress -class MyProgress: - def __init__(self): - self.last = 0.0 - - def Update(self, percent): - if (self.last + 1.0) <= percent: - print "Progress: %s " % (percent) - self.last = percent - if percent >= 100: - self.last = 0.0 - - def Done(self): - self.last = 0.0 - print "Done!" # init apt_pkg.init() -progress = MyProgress() +progress = TextProgress() cache = apt_pkg.GetCache(progress) print "Available packages: %s " % cache.PackageCount @@ -32,10 +20,10 @@ print "example package iter: %s" % iter # get depcache print "\n\n depcache" -depcache = apt_pkg.GetDepCache(cache, progress) +depcache = apt_pkg.GetDepCache(cache) depcache.ReadPinFile() # init is needed after the creation/pin file reading -depcache.Init() +depcache.Init(progress) print "got a depcache: %s " % depcache print "Marked for install: %s " % depcache.InstCount @@ -78,7 +66,7 @@ print "Marking %s for keep" % iter.Name depcache.MarkKeep(iter) print "Install: %s " % depcache.InstCount -iter = cache["3dwm-server"] +iter = cache["synaptic"] print "\nMarking '%s' for install" % iter.Name depcache.MarkInstall(iter) print "Install: %s " % depcache.InstCount diff --git a/doc/examples/progress.py b/doc/examples/progress.py index 67d39c9c..d96b8916 100644 --- a/doc/examples/progress.py +++ b/doc/examples/progress.py @@ -1,11 +1,11 @@ -import apt_pkg +import apt import sys import time import string -class OpProgress: +class TextProgress(apt.OpProgress): def __init__(self): - self.last = 0.0 + self.last=0.0 def Update(self, percent): if (self.last + 1.0) <= percent: @@ -19,7 +19,7 @@ class OpProgress: print "\rDone " -class FetchProgress: +class TextFetchProgress(apt.FetchProgress): def __init__(self): pass @@ -40,7 +40,7 @@ class FetchProgress: #return False -class InstallProgress: +class TextInstallProgress(apt.InstallProgress): def __init__(self): pass def StartUpdate(self): @@ -52,7 +52,7 @@ class InstallProgress: time.sleep(0.1) -class CdromProgress: +class TextCdromProgress(apt.CdromProgress): def __init__(self): pass # update is called regularly so that the gui can be redrawn diff --git a/python/depcache.cc b/python/depcache.cc index d2ec4a8d..218fd0e9 100644 --- a/python/depcache.cc +++ b/python/depcache.cc @@ -493,7 +493,7 @@ PyTypeObject PkgDepCacheType = PyObject *GetDepCache(PyObject *Self,PyObject *Args) { PyObject *Owner; - if (PyArg_ParseTuple(Args,"O!",&PkgCacheType,&Owner) == 0) + if (PyArg_ParseTuple(Args,"O!|O",&PkgCacheType,&Owner) == 0) return 0; PyObject *DepCachePyObj; @@ -5,6 +5,7 @@ from distutils.core import setup, Extension from distutils.sysconfig import parse_makefile import string, glob + # The apt_pkg module files = string.split(parse_makefile("python/makefile")["APT_PKG_SRC"]); for i in range(0,len(files)): @@ -20,7 +21,7 @@ apt_inst = Extension("apt_inst", files, libraries=["apt-pkg","apt-inst"]); setup(name="python-apt", - version="0.5.37", + version="0.6.12", description="Python bindings for APT", author="APT Development Team", author_email="deity@lists.debian.org", |
