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 / build-deps.py < prev    next >
Encoding:
Python Source  |  2006-05-08  |  2.0 KB  |  73 lines

  1. #!/usr/bin/python
  2. # this is a example how to access the build dependencies of a package
  3.  
  4. import apt_pkg
  5. import sys
  6. import sets    # only needed for python2.3, python2.4 supports this naively 
  7.  
  8. def get_source_pkg(pkg, records, depcache):
  9.     """ get the source package name of a given package """
  10.     version = depcache.GetCandidateVer(pkg)
  11.     if not version:
  12.         return None
  13.     file, index = version.FileList.pop(0)
  14.     records.Lookup((file, index))
  15.     if records.SourcePkg != "":
  16.         srcpkg = records.SourcePkg
  17.     else:
  18.         srcpkg = pkg.Name
  19.     return srcpkg
  20.  
  21. # main
  22. apt_pkg.init()
  23. cache = apt_pkg.GetCache()
  24. depcache = apt_pkg.GetDepCache(cache)
  25. depcache.Init()
  26. records = apt_pkg.GetPkgRecords(cache)
  27. srcrecords = apt_pkg.GetPkgSrcRecords()
  28.  
  29. # base package that we use for build-depends calculation
  30. if len(sys.argv) < 2:
  31.     print "need a package name as argument"
  32.     sys.exit(1)
  33. try:
  34.     base = cache[sys.argv[1]]
  35. except KeyError:
  36.     print "No package %s found" % sys.argv[1]
  37.     sys.exit(1)
  38. all_build_depends = sets.Set()
  39.  
  40. # get the build depdends for the package itself
  41. srcpkg_name = get_source_pkg(base, records, depcache)
  42. print "srcpkg_name: %s " % srcpkg_name
  43. if not srcpkg_name:
  44.     print "Can't find source package for '%s'" % pkg.Name
  45. srcrec = srcrecords.Lookup(srcpkg_name)
  46. if srcrec:
  47.     print "Files:"
  48.     print srcrecords.Files
  49.     bd = srcrecords.BuildDepends
  50.     print "build-depends of the package: %s " % bd
  51.         for b in bd:
  52.             all_build_depends.add(b[0])
  53.  
  54. # calculate the build depends for all dependencies
  55. depends = depcache.GetCandidateVer(base).DependsList
  56. for dep in depends["Depends"]: # FIXME: do we need to consider PreDepends?
  57.     pkg = dep[0].TargetPkg
  58.     srcpkg_name = get_source_pkg(pkg, records, depcache)
  59.     if not srcpkg_name:
  60.         print "Can't find source package for '%s'" % pkg.Name
  61.         continue
  62.     srcrec = srcrecords.Lookup(srcpkg_name)
  63.     if srcrec:
  64.         #print srcrecords.Package
  65.         #print srcrecords.Binaries
  66.         bd = srcrecords.BuildDepends
  67.         #print "%s: %s " % (srcpkg_name, bd)
  68.         for b in bd:
  69.             all_build_depends.add(b[0])
  70.             
  71.  
  72. print "\n".join(all_build_depends)
  73.