diff options
Diffstat (limited to 'doc/examples')
| -rw-r--r-- | doc/examples/acquire.py | 92 | ||||
| -rw-r--r-- | doc/examples/action.py | 3 | ||||
| -rw-r--r-- | doc/examples/all_deps.py | 34 | ||||
| -rwxr-xr-x | doc/examples/build-deps.py | 2 | ||||
| -rwxr-xr-x | doc/examples/deb_inspect.py | 12 | ||||
| -rwxr-xr-x | doc/examples/dependant-pkgs.py | 36 | ||||
| -rwxr-xr-x | doc/examples/gui-inst.py | 57 | ||||
| -rw-r--r-- | doc/examples/indexfile.py | 21 | ||||
| -rw-r--r-- | doc/examples/inst.py | 70 | ||||
| -rwxr-xr-x | doc/examples/print_uris.py | 22 | ||||
| -rw-r--r-- | doc/examples/progress.py | 17 | ||||
| -rw-r--r-- | doc/examples/sources.py | 6 |
12 files changed, 302 insertions, 70 deletions
diff --git a/doc/examples/acquire.py b/doc/examples/acquire.py new file mode 100644 index 00000000..a0790c98 --- /dev/null +++ b/doc/examples/acquire.py @@ -0,0 +1,92 @@ +import apt +import apt_pkg +import os +import sys +import tempfile + +def get_file(fetcher, uri, destFile): + cwd = os.getcwd() + # create a temp dir + dir = tempfile.mkdtemp() + os.chdir(dir) + # get the file + af = apt_pkg.GetPkgAcqFile(fetcher, + uri=uri, + descr="sample descr") + res = fetcher.Run() + if res != fetcher.ResultContinue: + os.rmdir(dir) + os.chdir(cwd) + return False + filename = os.path.basename(uri) + os.rename(dir+"/"+filename,destFile) + # cleanup + os.rmdir(dir) + os.chdir(cwd) + return True + +apt_pkg.init() + +#apt_pkg.Config.Set("Debug::pkgDPkgPM","1"); +#apt_pkg.Config.Set("Debug::pkgPackageManager","1"); +#apt_pkg.Config.Set("Debug::pkgDPkgProgressReporting","1"); + +cache = apt_pkg.GetCache() +depcache = apt_pkg.GetDepCache(cache) + +recs = apt_pkg.GetPkgRecords(cache) +list = apt_pkg.GetPkgSourceList() +list.ReadMainList() + +# show the amount fetch needed for a dist-upgrade +depcache.Upgrade(True) +progress = apt.progress.TextFetchProgress() +fetcher = apt_pkg.GetAcquire(progress) +pm = apt_pkg.GetPackageManager(depcache) +pm.GetArchives(fetcher,list,recs) +print "%s (%s)" % (apt_pkg.SizeToStr(fetcher.FetchNeeded), fetcher.FetchNeeded) + +for pkg in cache.Packages: + depcache.MarkKeep(pkg) + +try: + os.mkdir("/tmp/pyapt-test") + os.mkdir("/tmp/pyapt-test/partial") +except OSError: + pass +apt_pkg.Config.Set("Dir::Cache::archives","/tmp/pyapt-test") + +pkg = cache["3ddesktop"] +depcache.MarkInstall(pkg) + +progress = apt.progress.TextFetchProgress() +fetcher = apt_pkg.GetAcquire(progress) +#fetcher = apt_pkg.GetAcquire() +pm = apt_pkg.GetPackageManager(depcache) + +print pm +print fetcher + +get_file(fetcher, "ftp://ftp.debian.org/debian/dists/README", "/tmp/lala") +sys.exit(1) + +pm.GetArchives(fetcher,list,recs) + +for item in fetcher.Items: + print item + if item.Status == item.StatError: + print "Some error ocured: '%s'" % item.ErrorText + if item.Complete == False: + print "No error, still nothing downloaded (%s)" % item.ErrorText + print + + +res = fetcher.Run() +print "fetcher.Run() returned: %s" % res + +print "now runing pm.DoInstall()" +res = pm.DoInstall(1) +print "pm.DoInstall() returned: %s"% res + + + diff --git a/doc/examples/action.py b/doc/examples/action.py index a794317a..adf26539 100644 --- a/doc/examples/action.py +++ b/doc/examples/action.py @@ -30,9 +30,6 @@ sys.exit(0) - - - iter = cache["base-config"] print "example package iter: %s" % iter diff --git a/doc/examples/all_deps.py b/doc/examples/all_deps.py new file mode 100644 index 00000000..f4f1741c --- /dev/null +++ b/doc/examples/all_deps.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python
+
+import sys
+import apt
+
+
+def dependencies(cache, pkg, deps, key="Depends"):
+ #print "pkg: %s (%s)" % (pkg.name, deps)
+ candver = cache._depcache.GetCandidateVer(pkg._pkg)
+ if candver == None:
+ return deps
+ dependslist = candver.DependsList
+ if dependslist.has_key(key):
+ for depVerList in dependslist[key]:
+ for dep in depVerList:
+ if cache.has_key(dep.TargetPkg.Name):
+ if pkg.name != dep.TargetPkg.Name and not dep.TargetPkg.Name in deps:
+ deps.add(dep.TargetPkg.Name)
+ dependencies(cache, cache[dep.TargetPkg.Name], deps, key)
+ return deps
+
+
+pkgname = sys.argv[1]
+c = apt.Cache()
+pkg = c[pkgname]
+
+deps = set()
+
+deps = dependencies(c,pkg, deps, "Depends")
+print " ".join(deps)
+
+preDeps = set()
+preDeps = dependencies(c,pkg, preDeps, "PreDepends")
+print " ".join(preDeps)
diff --git a/doc/examples/build-deps.py b/doc/examples/build-deps.py index b580f5de..65e35f3d 100755 --- a/doc/examples/build-deps.py +++ b/doc/examples/build-deps.py @@ -24,7 +24,7 @@ cache = apt_pkg.GetCache() depcache = apt_pkg.GetDepCache(cache) depcache.Init() records = apt_pkg.GetPkgRecords(cache) -srcrecords = apt_pkg.GetPkgSrcRecords(cache) +srcrecords = apt_pkg.GetPkgSrcRecords() # base package that we use for build-depends calculation if len(sys.argv) < 2: diff --git a/doc/examples/deb_inspect.py b/doc/examples/deb_inspect.py index 4173c196..0befd2bb 100755 --- a/doc/examples/deb_inspect.py +++ b/doc/examples/deb_inspect.py @@ -4,6 +4,7 @@ import apt_pkg import apt_inst import sys +import os.path def Callback(What,Name,Link,Mode,UID,GID,Size,MTime,Major,Minor): """ callback for debExtract """ @@ -35,5 +36,12 @@ if __name__ == "__main__": print apt_pkg.ParseDepends(depends) - - + print "extracting archive" + dir = "/tmp/deb" + os.mkdir(dir) + apt_inst.debExtractArchive(open(file),dir) + def visit(arg, dirname, names): + print "%s/" % dirname + for file in names: + print "\t%s" % file + os.path.walk(dir, visit, None) diff --git a/doc/examples/dependant-pkgs.py b/doc/examples/dependant-pkgs.py new file mode 100755 index 00000000..f36936a8 --- /dev/null +++ b/doc/examples/dependant-pkgs.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +import apt +import sys + +pkgs = set() +cache = apt.Cache() +for pkg in cache: + candver = cache._depcache.GetCandidateVer(pkg._pkg) + if candver == None: + continue + dependslist = candver.DependsList + for dep in dependslist.keys(): + # get the list of each dependency object + for depVerList in dependslist[dep]: + for z in depVerList: + # get all TargetVersions of + # the dependency object + for tpkg in z.AllTargets(): + if sys.argv[1] == tpkg.ParentPkg.Name: + pkgs.add(pkg.name) + +main = set() +universe = set() +for pkg in pkgs: + if "universe" in cache[pkg].section: + universe.add(cache[pkg].sourcePackageName) + else: + main.add(cache[pkg].sourcePackageName) + +print "main:" +print "\n".join(main) +print + +print "universe:" +print "\n".join(universe) diff --git a/doc/examples/gui-inst.py b/doc/examples/gui-inst.py index 38417e10..deb325fe 100755 --- a/doc/examples/gui-inst.py +++ b/doc/examples/gui-inst.py @@ -2,6 +2,7 @@ # example how to install in a custom terminal widget # see also gnome bug: #169201 +import apt import apt_pkg import sys, os, fcntl import copy @@ -13,6 +14,7 @@ pygtk.require('2.0') import gtk import vte import time +import posix from apt.progress import OpProgress, FetchProgress, InstallProgress @@ -36,10 +38,12 @@ class GuiFetchProgress(gtk.Window, FetchProgress): def stop(self): self.hide() def pulse(self): - self.label.set_text("Speed: %s/s" % apt_pkg.SizeToStr(self.currentCPS)) + FetchProgress.pulse(self) + self.label.set_text("Speed: %s/s" % apt_pkg.SizeToStr(self.currentCPS)) #self.progressbar.set_fraction(self.currentBytes/self.totalBytes) while gtk.events_pending(): gtk.main_iteration() + return True class TermInstallProgress(InstallProgress, gtk.Window): def __init__(self): @@ -50,11 +54,16 @@ class TermInstallProgress(InstallProgress, gtk.Window): self.add(box) self.term = vte.Terminal() self.term.show() + # check for the child + self.reaper = vte.reaper_get() + self.reaper.connect("child-exited",self.child_exited) + self.finished = False + box.pack_start(self.term) self.progressbar = gtk.ProgressBar() self.progressbar.show() box.pack_start(self.progressbar) - + (read, write) = os.pipe() self.writefd=write self.status = os.fdopen(read, "r") @@ -62,7 +71,12 @@ class TermInstallProgress(InstallProgress, gtk.Window): print "read-fd: %s" % self.status.fileno() print "write-fd: %s" % self.writefd self.read = "" - + + def child_exited(self,term, pid, status): + print "child_exited: %s %s %s %s" % (self,term,pid,status) + self.apt_status = posix.WEXITSTATUS(status) + self.finished = True + def startUpdate(self): print "start" self.show() @@ -84,29 +98,26 @@ class TermInstallProgress(InstallProgress, gtk.Window): self.read = "" while gtk.events_pending(): gtk.main_iteration() + def finishUpdate(self): sys.stdin.readline() - def fork(self): + def run(self, pm): print "fork" env = ["VTE_PTY_KEEP_FD=%s"%self.writefd] print env pid = self.term.forkpty(envv=env) + if pid == 0: + res = pm.DoInstall(self.writefd) + print res + sys.exit(res) print "After fork: %s " % pid - return pid - - -# init -apt_pkg.init() - -progress = OpProgress() -cache = apt_pkg.GetCache(progress) -print "Available packages: %s " % cache.PackageCount + while not self.finished: + self.updateInterface() + return self.apt_status +cache = apt.Cache() +print "Available packages: %s " % cache._cache.PackageCount -# get depcache -depcache = apt_pkg.GetDepCache(cache) -depcache.ReadPinFile() -depcache.Init(progress) # update the cache fprogress = GuiFetchProgress() @@ -125,15 +136,15 @@ while gtk.events_pending(): gtk.main_iteration() -iter = cache["3dchess"] -print "\n%s"%iter +pkg = cache["3dchess"] +print "\n%s"%pkg.name # install or remove, the importend thing is to keep us busy :) -if iter.CurrentVer == None: - depcache.MarkInstall(iter) +if pkg.isInstalled: + pkg.markDelete() else: - depcache.MarkDelete(iter) -depcache.Commit(fprogress, iprogress) + pkg.markInstall() +cache.commit(fprogress, iprogress) print "Exiting" sys.exit(0) diff --git a/doc/examples/indexfile.py b/doc/examples/indexfile.py new file mode 100644 index 00000000..d383fd61 --- /dev/null +++ b/doc/examples/indexfile.py @@ -0,0 +1,21 @@ + +import apt_pkg + +apt_pkg.init() + +sources = apt_pkg.GetPkgSourceList() +sources.ReadMainList() + +cache = apt_pkg.GetCache() +depcache = apt_pkg.GetDepCache(cache) +pkg = cache["libimlib2"] +cand = depcache.GetCandidateVer(pkg) +for (f,i) in cand.FileList: + index = sources.FindIndex(f) + print index + if index: + print index.Size + print index.IsTrusted + print index.Exists + print index.HasPackages + print index.ArchiveURI("some/path") diff --git a/doc/examples/inst.py b/doc/examples/inst.py index fe5ec8e3..0e91c28f 100644 --- a/doc/examples/inst.py +++ b/doc/examples/inst.py @@ -2,60 +2,48 @@ # example how to deal with the depcache import apt -import apt_pkg import sys, os import copy +import time -from progress import TextFetchProgress, TextInstallProgress -from apt.progress import OpTextProgress +from apt.progress import InstallProgress class TextInstallProgress(InstallProgress): def __init__(self): - InstallProgress.__init__(self) - self.status = None - def StartUpdate(self): - print "StartUpdate: %s" % self.statusfd - self.status = os.fdopen(self.statusfd, "r") - print self.status - def UpdateInterface(self): - if self.status != None: - s = self.status.readline() - if s: - print s - def FinishUpdate(self): - self.status.close() - -# init -apt_pkg.init() - -progress = OpTextProgress() -cache = apt_pkg.GetCache(progress) -print "Available packages: %s " % cache.PackageCount - -# get depcache -depcache = apt_pkg.GetDepCache(cache) -depcache.ReadPinFile() -depcache.Init(progress) - -# do something -fprogress = TextFetchProgress() + apt.progress.InstallProgress.__init__(self) + self.last = 0.0 + def updateInterface(self): + InstallProgress.updateInterface(self) + if self.last >= self.percent: + return + sys.stdout.write("\r[%s] %s\n" %(self.percent, self.status)) + sys.stdout.flush() + self.last = self.percent + def conffile(self,current,new): + print "conffile prompt: %s %s" % (current,new) + def error(self, errorstr): + print "got dpkg error: '%s'" % errorstr + +cache = apt.Cache(apt.progress.OpTextProgress()) + +fprogress = apt.progress.TextFetchProgress() iprogress = TextInstallProgress() -# can be used to set a custom fork method (like vte.Terminal.forkpty) -#iprogress.fork = os.fork - -iter = cache["base-config"] -print "\n%s"%iter +pkg = cache["test-package"] +pkg.markUpgrade() +cache.commit(fprogress,iprogress) +sys.exit(1) # install or remove, the importend thing is to keep us busy :) -if iter.CurrentVer == None: - depcache.MarkInstall(iter) +if pkg.isInstalled: + print "Going to delete %s" % pkg.name + pkg.markDelete() else: - depcache.MarkDelete(iter) -res = depcache.Commit(fprogress, iprogress) + print "Going to install %s" % pkg.name + pkg.markInstall() +res = cache.commit(fprogress, iprogress) print res -print "Exiting" sys.exit(0) diff --git a/doc/examples/print_uris.py b/doc/examples/print_uris.py new file mode 100755 index 00000000..c8a64223 --- /dev/null +++ b/doc/examples/print_uris.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# +# a example that prints the URIs of all upgradable packages +# + +import apt +import apt_pkg + + +cache = apt.Cache() +upgradable = filter(lambda p: p.isUpgradable, cache) + + +for pkg in upgradable: + pkg._lookupRecord(True) + path = apt_pkg.ParseSection(pkg._records.Record)["Filename"] + cand = pkg._depcache.GetCandidateVer(pkg._pkg) + for (packagefile,i) in cand.FileList: + indexfile = cache._list.FindIndex(packagefile) + if indexfile: + uri = indexfile.ArchiveURI(path) + print uri diff --git a/doc/examples/progress.py b/doc/examples/progress.py index d820fcb2..2723c382 100644 --- a/doc/examples/progress.py +++ b/doc/examples/progress.py @@ -44,12 +44,16 @@ class TextFetchProgress(apt.FetchProgress): class TextInstallProgress(apt.InstallProgress): def __init__(self): + apt.InstallProgress.__init__(self) pass def startUpdate(self): print "StartUpdate" def finishUpdate(self): print "FinishUpdate" + def statusChange(self, pkg, percent, status): + print "[%s] %s: %s" % (percent, pkg, status) def updateInterface(self): + apt.InstallProgress.updateInterface(self) # usefull to e.g. redraw a GUI time.sleep(0.1) @@ -70,3 +74,16 @@ class TextCdromProgress(apt.CdromProgress): print "Please insert cdrom and press <ENTER>" answer = sys.stdin.readline() return True + + +if __name__ == "__main__": + c = apt.Cache() + pkg = c["3dchess"] + if pkg.isInstalled: + pkg.markDelete() + else: + pkg.markInstall() + + res = c.commit(TextFetchProgress(), TextInstallProgress()) + + print res diff --git a/doc/examples/sources.py b/doc/examples/sources.py index 79514621..c12c6f15 100644 --- a/doc/examples/sources.py +++ b/doc/examples/sources.py @@ -4,6 +4,12 @@ import apt_pkg apt_pkg.init() +#cache = apt_pkg.GetCache() +#sources = apt_pkg.GetPkgSrcRecords(cache) + sources = apt_pkg.GetPkgSrcRecords() +sources.Restart() while sources.Lookup('hello'): print sources.Package, sources.Version, sources.Maintainer, sources.Section, `sources.Binaries` + print sources.Files + print sources.Index.ArchiveURI("") |
