home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-apt / examples / acquire.py next >
Encoding:
Python Source  |  2006-03-02  |  2.2 KB  |  93 lines

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