home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-apt / examples / acquire.py next >
Encoding:
Python Source  |  2009-03-30  |  2.0 KB  |  78 lines

  1. import apt
  2. import apt_pkg
  3. import os
  4. import sys
  5. import tempfile
  6.  
  7.  
  8. def get_file(fetcher, uri, destFile):
  9.     # get the file
  10.     af = apt_pkg.GetPkgAcqFile(fetcher, uri=uri, descr="sample descr",
  11.                                destFile=destFile)
  12.     res = fetcher.Run()
  13.     if res != fetcher.ResultContinue:
  14.         return False
  15.     return True
  16.  
  17. apt_pkg.init()
  18.  
  19. #apt_pkg.Config.Set("Debug::pkgDPkgPM","1");
  20. #apt_pkg.Config.Set("Debug::pkgPackageManager","1");
  21. #apt_pkg.Config.Set("Debug::pkgDPkgProgressReporting","1");
  22.  
  23. cache = apt_pkg.GetCache()
  24. depcache = apt_pkg.GetDepCache(cache)
  25.  
  26. recs = apt_pkg.GetPkgRecords(cache)
  27. list = apt_pkg.GetPkgSourceList()
  28. list.ReadMainList()
  29.  
  30. # show the amount fetch needed for a dist-upgrade
  31. depcache.Upgrade(True)
  32. progress = apt.progress.TextFetchProgress()
  33. fetcher = apt_pkg.GetAcquire(progress)
  34. pm = apt_pkg.GetPackageManager(depcache)
  35. pm.GetArchives(fetcher, list, recs)
  36. print "%s (%s)" % (apt_pkg.SizeToStr(fetcher.FetchNeeded), fetcher.FetchNeeded)
  37. actiongroup = apt_pkg.GetPkgActionGroup(depcache)
  38. for pkg in cache.Packages:
  39.     depcache.MarkKeep(pkg)
  40.  
  41. try:
  42.     os.mkdir("/tmp/pyapt-test")
  43.     os.mkdir("/tmp/pyapt-test/partial")
  44. except OSError:
  45.     pass
  46. apt_pkg.Config.Set("Dir::Cache::archives", "/tmp/pyapt-test")
  47.  
  48. pkg = cache["3ddesktop"]
  49. depcache.MarkInstall(pkg)
  50.  
  51. progress = apt.progress.TextFetchProgress()
  52. fetcher = apt_pkg.GetAcquire(progress)
  53. #fetcher = apt_pkg.GetAcquire()
  54. pm = apt_pkg.GetPackageManager(depcache)
  55.  
  56. print pm
  57. print fetcher
  58.  
  59. get_file(fetcher, "ftp://ftp.debian.org/debian/dists/README", "/tmp/lala")
  60.  
  61. pm.GetArchives(fetcher, list, recs)
  62.  
  63. for item in fetcher.Items:
  64.     print item
  65.     if item.Status == item.StatError:
  66.         print "Some error ocured: '%s'" % item.ErrorText
  67.     if item.Complete == False:
  68.         print "No error, still nothing downloaded (%s)" % item.ErrorText
  69.     print
  70.  
  71.  
  72. res = fetcher.Run()
  73. print "fetcher.Run() returned: %s" % res
  74.  
  75. print "now runing pm.DoInstall()"
  76. res = pm.DoInstall(1)
  77. print "pm.DoInstall() returned: %s"% res
  78.