home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / GDebi / DebPackage.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  9.9 KB  |  368 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import apt_inst
  5. import apt_pkg
  6. import apt
  7. import sys
  8. import os
  9. from gettext import gettext as _
  10.  
  11. class DebPackage(object):
  12.     debug = 0
  13.     
  14.     def __init__(self, cache, file):
  15.         cache.clear()
  16.         self._cache = cache
  17.         self.file = file
  18.         self._needPkgs = None
  19.         control = apt_inst.debExtractControl(open(file))
  20.         self._sections = apt_pkg.ParseSection(control)
  21.         self.pkgName = self._sections['Package']
  22.  
  23.     
  24.     def _isOrGroupSatisfied(self, or_group):
  25.         """ this function gets a 'or_group' and analyzes if
  26.             at least one dependency of this group is already satisfied """
  27.         self._dbg(2, '_checkOrGroup(): %s ' % or_group)
  28.         for dep in or_group:
  29.             depname = dep[0]
  30.             ver = dep[1]
  31.             oper = dep[2]
  32.             if not self._cache.has_key(depname):
  33.                 if self._cache.isVirtualPkg(depname):
  34.                     for pkg in self._cache.getProvidersForVirtual(depname):
  35.                         if pkg.isInstalled:
  36.                             return True
  37.                             continue
  38.                     
  39.                 continue
  40.             
  41.             inst = self._cache[depname]
  42.             instver = inst.installedVersion
  43.             if instver != None and apt_pkg.CheckDep(instver, oper, ver) == True:
  44.                 return True
  45.                 continue
  46.         
  47.         return False
  48.  
  49.     
  50.     def _satisfyOrGroup(self, or_group):
  51.         ''' try to satisfy the or_group '''
  52.         or_found = False
  53.         virtual_pkg = None
  54.         for dep in or_group:
  55.             depname = dep[0]
  56.             ver = dep[1]
  57.             oper = dep[2]
  58.             if not self._cache.has_key(depname):
  59.                 if self._cache.isVirtualPkg(depname):
  60.                     virtual_pkg = depname
  61.                     continue
  62.                 continue
  63.             
  64.             cand = self._cache[depname]
  65.             candver = self._cache._depcache.GetCandidateVer(cand._pkg)
  66.             if not candver:
  67.                 continue
  68.             
  69.             if not apt_pkg.CheckDep(candver.VerStr, oper, ver):
  70.                 continue
  71.             
  72.             self._dbg(2, 'Need to get: %s' % depname)
  73.             self._needPkgs.append(depname)
  74.             return True
  75.         
  76.         or_str = ''
  77.         for dep in or_group:
  78.             or_str += dep[0]
  79.             if dep != or_group[len(or_group) - 1]:
  80.                 or_str += '|'
  81.                 continue
  82.         
  83.         self._failureString += _('Dependency is not satisfiable: %s\n' % or_str)
  84.         return False
  85.  
  86.     
  87.     def _checkSinglePkgConflict(self, pkgname, ver, oper):
  88.         ''' returns true if a pkg conflicts with a real installed/marked
  89.             pkg '''
  90.         pkgver = None
  91.         cand = self._cache[pkgname]
  92.         if cand.isInstalled:
  93.             pkgver = cand.installedVersion
  94.         elif cand.markedInstall:
  95.             pkgver = cand.candidateVersion
  96.         
  97.         if pkgver and apt_pkg.CheckDep(pkgver, oper, ver):
  98.             self._failureString += _("Conflicts with the installed package '%s'" % cand.name)
  99.             return True
  100.         
  101.         return False
  102.  
  103.     
  104.     def _checkConflictsOrGroup(self, or_group):
  105.         ''' check the or-group for conflicts with installed pkgs '''
  106.         self._dbg(2, '_checkConflictsOrGroup(): %s ' % or_group)
  107.         or_found = False
  108.         virtual_pkg = None
  109.         for dep in or_group:
  110.             depname = dep[0]
  111.             ver = dep[1]
  112.             oper = dep[2]
  113.             if not self._cache.has_key(depname):
  114.                 if self._cache.isVirtualPkg(depname):
  115.                     for pkg in self._cache.getProvidersForVirtual(depname):
  116.                         if self._checkSinglePkgConflict(pkg.name, ver, oper):
  117.                             return True
  118.                             continue
  119.                     
  120.                 continue
  121.             
  122.             if self._checkSinglePkgConflict(depname, ver, oper):
  123.                 return True
  124.                 continue
  125.         
  126.         return False
  127.  
  128.     
  129.     def checkConflicts(self):
  130.         ''' check if the pkg conflicts with a existing or to be installed
  131.             package. Return True if the pkg is ok '''
  132.         key = 'Conflicts'
  133.         if self._sections.has_key(key):
  134.             conflicts = apt_pkg.ParseDepends(self._sections[key])
  135.             for or_group in conflicts:
  136.                 if self._checkConflictsOrGroup(or_group):
  137.                     return False
  138.                     continue
  139.             
  140.         
  141.         return True
  142.  
  143.     (NO_VERSION, VERSION_OUTDATED, VERSION_SAME, VERSION_IS_NEWER) = range(4)
  144.     
  145.     def compareToVersionInCache(self, useInstalled = True):
  146.         ''' checks if the pkg is already installed or availabe in the cache
  147.             and if so in what version, returns if the version of the deb
  148.             is not available,older,same,newer
  149.         '''
  150.         self._dbg(3, 'compareToVersionInCache')
  151.         pkgname = self._sections['Package']
  152.         debver = self._sections['Version']
  153.         self._dbg(1, 'debver: %s' % debver)
  154.         if self._cache.has_key(pkgname):
  155.             if useInstalled:
  156.                 cachever = self._cache[pkgname].installedVersion
  157.             else:
  158.                 cachever = self._cache[pkgname].candidateVersion
  159.             if cachever != None:
  160.                 cmp = apt_pkg.VersionCompare(cachever, debver)
  161.                 self._dbg(1, 'CompareVersion(debver,instver): %s' % cmp)
  162.                 if cmp == 0:
  163.                     return self.VERSION_SAME
  164.                 elif cmp < 0:
  165.                     return self.VERSION_IS_NEWER
  166.                 elif cmp > 0:
  167.                     return self.VERSION_OUTDATED
  168.                 
  169.             
  170.         
  171.         return self.NO_VERSION
  172.  
  173.     
  174.     def checkDeb(self):
  175.         self._dbg(3, 'checkDepends')
  176.         self._needPkgs = []
  177.         depends = []
  178.         arch = self._sections['Architecture']
  179.         if arch != 'all' and arch != apt_pkg.CPU:
  180.             self._dbg(1, 'ERROR: Wrong architecture dude!')
  181.             self._failureString = _("Wrong architecture '%s'" % arch)
  182.             return False
  183.         
  184.         res = self.compareToVersionInCache()
  185.         if res == self.VERSION_OUTDATED:
  186.             self._failureString = _('A later version is already installed')
  187.             return False
  188.         
  189.         self._failureString = ''
  190.         if not self.checkConflicts():
  191.             return False
  192.         
  193.         for key in [
  194.             'Depends',
  195.             'PreDepends']:
  196.             if self._sections.has_key(key):
  197.                 depends.extend(apt_pkg.ParseDepends(self._sections[key]))
  198.                 continue
  199.         
  200.         for or_group in depends:
  201.             if not self._isOrGroupSatisfied(or_group):
  202.                 if not self._satisfyOrGroup(or_group):
  203.                     return False
  204.                 
  205.             
  206.             for pkg in self._needPkgs:
  207.                 
  208.                 try:
  209.                     self._cache[pkg].markInstall()
  210.                 continue
  211.                 except SystemError:
  212.                     self._failureString = _("Cannot install '%s'" % pkg)
  213.                     self._cache.clear()
  214.                     return False
  215.                     continue
  216.                 
  217.  
  218.             
  219.         
  220.         if not self.checkConflicts():
  221.             return False
  222.         
  223.         if self._cache._depcache.BrokenCount > 0:
  224.             self._failureString = _('Failed to satisfy all dependencies (broken cache)')
  225.             self._cache.clear()
  226.             return False
  227.         
  228.         return True
  229.  
  230.     
  231.     def missingDeps(self):
  232.         self._dbg(1, 'Installing: %s' % self._needPkgs)
  233.         if self._needPkgs == None:
  234.             self.checkDeb()
  235.         
  236.         return self._needPkgs
  237.  
  238.     missingDeps = property(missingDeps)
  239.     
  240.     def requiredChanges(self):
  241.         install = []
  242.         remove = []
  243.         unauthenticated = []
  244.         for pkg in self._cache:
  245.             if pkg.markedInstall or pkg.markedUpgrade:
  246.                 install.append(pkg.name)
  247.                 authenticated = False
  248.                 for origin in pkg.candidateOrigin:
  249.                     authenticated |= origin.trusted
  250.                 
  251.                 if not authenticated:
  252.                     unauthenticated.append(pkg.name)
  253.                 
  254.             
  255.             if pkg.markedDelete:
  256.                 remove.append(pkg.name)
  257.                 continue
  258.         
  259.         return (install, remove, unauthenticated)
  260.  
  261.     requiredChanges = property(requiredChanges)
  262.     
  263.     def filelist(self):
  264.         ''' return the list of files in the deb '''
  265.         files = []
  266.         
  267.         def extract_cb(What, Name, Link, Mode, UID, GID, Size, MTime, Major, Minor):
  268.             files.append(Name)
  269.  
  270.         apt_inst.debExtract(open(self.file), extract_cb, 'data.tar.gz')
  271.         return files
  272.  
  273.     filelist = property(filelist)
  274.     
  275.     def __getitem__(self, item):
  276.         if not self._sections.has_key(item):
  277.             return _('%s is not available' % item)
  278.         
  279.         return self._sections[item]
  280.  
  281.     
  282.     def _dbg(self, level, msg):
  283.         '''Write debugging output to sys.stderr.
  284.         '''
  285.         if level <= self.debug:
  286.             print >>sys.stderr, msg
  287.         
  288.  
  289.  
  290.  
  291. class MyCache(apt.Cache):
  292.     ''' helper to provide some additonal functions '''
  293.     
  294.     def clear(self):
  295.         ''' unmark all pkgs '''
  296.         self._depcache.Init()
  297.  
  298.     
  299.     def isVirtualPkg(self, pkgname):
  300.         ''' this function returns true if pkgname is a virtual
  301.             pkg '''
  302.         
  303.         try:
  304.             virtual_pkg = self._cache[pkgname]
  305.         except KeyError:
  306.             return False
  307.  
  308.         if len(virtual_pkg.VersionList) == 0:
  309.             return True
  310.         
  311.         return False
  312.  
  313.     
  314.     def downloadable(self, pkg, useCandidate = True):
  315.         ''' check if the given pkg can be downloaded '''
  316.         if useCandidate:
  317.             ver = self._depcache.GetCandidateVer(pkg._pkg)
  318.         else:
  319.             ver = pkg._pkg.CurrentVer
  320.         if ver == None:
  321.             return False
  322.         
  323.         return ver.Downloadable
  324.  
  325.     
  326.     def getProvidersForVirtual(self, virtual_pkg):
  327.         providers = []
  328.         
  329.         try:
  330.             vp = self._cache[virtual_pkg]
  331.             if len(vp.VersionList) != 0:
  332.                 return providers
  333.         except IndexError:
  334.             return providers
  335.  
  336.         for pkg in self:
  337.             v = self._depcache.GetCandidateVer(pkg._pkg)
  338.             if v == None:
  339.                 continue
  340.             
  341.             for p in v.ProvidesList:
  342.                 if virtual_pkg == p[0]:
  343.                     providers.append(pkg)
  344.                     continue
  345.             
  346.         
  347.         return providers
  348.  
  349.  
  350. if __name__ == '__main__':
  351.     cache = MyCache()
  352.     vp = 'www-browser'
  353.     print '%s virtual: %s' % (vp, cache.isVirtualPkg(vp))
  354.     providers = cache.getProvidersForVirtual(vp)
  355.     print 'Providers for %s :' % vp
  356.     for pkg in providers:
  357.         print ' %s' % pkg.name
  358.     
  359.     d = DebPackage(cache, sys.argv[1])
  360.     print 'Deb: %s' % d.pkgName
  361.     if not d.checkDeb():
  362.         print "can't be satified"
  363.         print d._failureString
  364.     
  365.     print 'missing deps: %s' % d.missingDeps
  366.     print d.requiredChanges
  367.  
  368.