Advertisement
Guest User

simplified CPacMan Yum plugin

a guest
Mar 12th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.30 KB | None | 0 0
  1. import os, sys, re
  2. import os.path
  3. try:
  4.     from hashlib import md5
  5. except ImportError:
  6.     from md5 import md5
  7.  
  8. import ConfigParser
  9.  
  10. import rpm
  11.  
  12. from yum.plugins import PluginYumExit, TYPE_CORE
  13. from yum.repos import Repository
  14. from yum.yumRepo import YumRepository # , YumPackageSack
  15. from yum.packageSack import PackageSack
  16. from yum.packages import YumLocalPackage, YumHeaderPackage, YumAvailablePackage
  17. from yum.repoMDObject import RepoMD
  18. import rpmUtils, yum.Errors
  19.  
  20.  
  21. requires_api_version = '2.3'
  22. plugin_type = (TYPE_CORE,)
  23. cpacman_on=True
  24.  
  25. # really weird location for libraries. Oh well...
  26. sys.path.append('/usr/share/yum-cli')
  27. from yumcommands import YumCommand
  28.  
  29. # class CPacManPackage(YumHeaderPackage):
  30. class CPacManPackage(YumLocalPackage):
  31.     def __init__(self,repo,hdr,localpath):
  32.         self.localpath=localpath
  33.         YumHeaderPackage.__init__(self,repo,hdr)
  34.         self.pkgid = self.hdr[rpm.RPMTAG_SHA1HEADER]
  35.         if not self.pkgid:
  36.             self.pkgid = "%s.%s" %(self.hdr['name'], self.hdr['buildtime'])
  37.         self.id=self.pkgid
  38.         self.pkgKey = self.__hash__()
  39.  
  40.     def __getattr__(self, thing):
  41.         #FIXME - if an error - return AttributeError, not KeyError
  42.         # ONLY FIX THIS AFTER THE API BREAK
  43.         if thing.startswith('__') and thing.endswith('__'):
  44.             # If these existed, then we wouldn't get here ...
  45.             # So these are missing.
  46.             raise AttributeError, "%s has no attribute %s" % (self, thing)
  47.         try:
  48.             return self.hdr[thing]
  49.         except KeyError:
  50.             #  Note above, API break to fix this ... this at least is a nicer
  51.             # msg. so we know what we accessed that is bad.
  52.             # print "%s has no attribute %s" % (self, thing)
  53.             # return None
  54.             raise KeyError, "%s (%s) has no attribute %s" % (self, self.repoid, thing)
  55.         except ValueError:
  56.             #  Note above, API break to fix this ... this at least is a nicer
  57.             # msg. so we know what we accessed that is bad.
  58.             raise ValueError, "%s (%s) has no attribute %s" % (self, self.repoid, thing)
  59.  
  60.     def returnChecksums(self):
  61.         return YumAvailablePackage.returnChecksums(self)
  62.  
  63. class CPacManSack(PackageSack):
  64.     def __init__(self):
  65.         PackageSack.__init__(self)
  66.         # looks like this is required property in some cases...
  67.         self._pkgtup2pkgs=[]
  68.         self.added={}
  69.  
  70.     def populate(self, repo, mdtype='metadata', callback=None, cacheonly=0):
  71.         # print "In CPacMan Populate"
  72.         self.added[repo]=[]
  73.         return
  74.  
  75.     def get_added(self):
  76.         return {}
  77.  
  78.     # added=property(get_added)
  79.  
  80.     def searchPrimaryFieldsMultipleStrings(self,fields,searchstrings):
  81.         ##FIXME
  82.         print "-=search=--=search=-"
  83.         return []
  84.  
  85. class CPacManYumRepo(YumRepository):
  86.     """CPacMan repo that translates from collects all of the available packages into
  87.    a neat Yum-like Repo object"""
  88.     def __init__(self,server_name,repo_path,repoid):
  89.         # global server_db, server
  90.         YumRepository.__init__(self,repoid)
  91.         self.cpacman_sack=CPacManSack()
  92.         # we need private copies of ServerDB and Server here as we're going
  93.         # to manipulate them
  94.         self.repo_path=repo_path
  95.         self.pkgdir=repo_path
  96.         self.cost=1500
  97.         self.__init_sack()
  98.         ##FIXME Hacking around EL5 deficiencies
  99.         try:
  100.             self.repoXML=RepoMD(self.id)
  101.         except TypeError:
  102.             # looks like older version of Yum... we should be OK
  103.             pass
  104.     def __init_sack(self):
  105.         # simplifying original call:
  106.         #  pl=self.server.listAvailablePackages(all_versions=0, ignore_policy=0, filter_path=self.repo_filter)
  107.         # to a bare dictionary:
  108.         pl={'a-1.1-1.i386':{'path':self.repo_path, 'package_name':'a-1.1-1.i386.rpm'}}
  109.        
  110.         ts=rpm.TransactionSet()
  111.         # hack to disable DIGEST and SIGNATURE checks
  112.         ts.setVSFlags((rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS))
  113.         for pk in pl.keys():
  114.             pkg=pl[pk]
  115.              
  116.             localpath=os.path.join(pkg['path'],pkg['package_name'])
  117.  
  118.             try:
  119.                 hdr = rpmUtils.miscutils.hdrFromPackage(ts, localpath)
  120.             except rpmUtils.RpmUtilsError:
  121.                 raise yum.Errors.MiscError, \
  122.                     'Could not open local rpm file: %s' % localpath
  123.             ypkg=CPacManPackage(self, hdr, localpath)
  124.             ypkg._populatePrco()
  125.             ypkg.basepath='file://'+pkg['path']
  126.             ypkg.relativepath=pkg['package_name']
  127.             self.cpacman_sack.addPackage(ypkg)
  128.        
  129.     def getPackageSack(self):
  130.         return self.cpacman_sack
  131.  
  132. CPacManRepo=CPacManYumRepo
  133.  
  134.  
  135. def prereposetup_hook(conduit):
  136.     ## Most of action happens here since this is the first stage where command-line arguments are being
  137.     ## processed. Everything prior to this hook has no access to CLI args which we need to identify
  138.     ## the server we're working on, or whether we need to kick-in at all...
  139.    
  140.     global cpacman_on
  141.     # simplifying process here... we actually get
  142.     # sc_path_str via different means...
  143.     sc_path_str="/foo/bar:/baz/moo"
  144.     # ... we have set cpacman_on based on previous conditions by now...
  145.     if cpacman_on:
  146.         repos=conduit.getRepos()
  147.         ## filter out the paths of repos that are already enabled
  148.         ## to avoid double-repo added via cpacman...
  149.         path_filter=[]
  150.         for r in repos.listEnabled():
  151.             try:
  152.                 path_filter.append(r.pkgdir)
  153.                 conduit.info(2, "Adding repo.pkgdir: "+r.pkgdir)
  154.             except AttributeError:
  155.                 # repo doesn't have pkgrid attribute
  156.                 # which is fine (?)...
  157.                 pass
  158.         sc_path=sc_path_str.split(':')
  159.         for p in sc_path:
  160.             if not (p in path_filter):
  161.                 conduit.info(2, "Adding path: "+p)
  162.                 repoid=p.replace('/','_')
  163.                 cpm_repo=CPacManRepo(server_name=conf.servername,repo_path=p,repoid=repoid)
  164.                 cpacman_repos.append(repoid)
  165.                 try:
  166.                     repos.add(cpm_repo)
  167.                 except yum.Errors.DuplicateRepoError:
  168.                     pass
  169.                 repos.enableRepo(repoid)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement