Skip to content

Instantly share code, notes, and snippets.

@droopy4096
Created March 7, 2014 02:09
Show Gist options
  • Save droopy4096/9403724 to your computer and use it in GitHub Desktop.
Save droopy4096/9403724 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import yum
import sys
import os
sys.path.append('/usr/share/yum-cli')
import rpm
from yum.plugins import PluginYumExit, TYPE_CORE
from yum.repos import Repository
from yum.yumRepo import YumRepository # , YumPackageSack
from yum.packageSack import PackageSack
from yum.packages import YumLocalPackage, YumHeaderPackage, YumAvailablePackage
from yum.repoMDObject import RepoMD
import rpmUtils, yum.Errors
class CPacManPackage(YumLocalPackage):
def __init__(self,repo,hdr,localpath):
self.localpath=localpath
# YumHeaderPackage.__init__(self,repo,hdr)
YumLocalPackage.__init__(self,filename=self.localpath)
self.pkgid = self.hdr[rpm.RPMTAG_SHA1HEADER]
if not self.pkgid:
self.pkgid = "%s.%s" %(self.hdr['name'], self.hdr['buildtime'])
self.id=self.pkgid
self.pkgKey = self.__hash__()
def __getattr__(self, thing):
#FIXME - if an error - return AttributeError, not KeyError
# ONLY FIX THIS AFTER THE API BREAK
if thing.startswith('__') and thing.endswith('__'):
# If these existed, then we wouldn't get here ...
# So these are missing.
raise AttributeError, "%s has no attribute %s" % (self, thing)
try:
return self.hdr[thing]
except KeyError:
# Note above, API break to fix this ... this at least is a nicer
# msg. so we know what we accessed that is bad.
# print "%s has no attribute %s" % (self, thing)
# return None
raise KeyError, "%s (%s) has no attribute %s" % (self, self.repoid, thing)
except ValueError:
# Note above, API break to fix this ... this at least is a nicer
# msg. so we know what we accessed that is bad.
raise ValueError, "%s (%s) has no attribute %s" % (self, self.repoid, thing)
def returnChecksums(self):
return YumAvailablePackage.returnChecksums(self)
class CPacManSack(PackageSack):
def __init__(self):
PackageSack.__init__(self)
# looks like this is required property in some cases...
self._pkgtup2pkgs=[]
self.added={}
def populate(self, repo, mdtype='metadata', callback=None, cacheonly=0):
# print "In CPacMan Populate"
self.added[repo]=[]
def get_added(self):
return []
# added=property(get_added)
def searchPrimaryFieldsMultipleStrings(self,fields,searchstrings):
##FIXME
print "-=search=--=search=-"
return []
class CPacManYumRepo(YumRepository):
"""CPacMan repo that translates from collects all of the available packages into
a neat Yum-like Repo object"""
def __init__(self,server_name,repo_path,repoid):
YumRepository.__init__(self,repoid)
self.cpacman_sack=CPacManSack()
self.repo_filter=[repo_path,]
self.pkgdir=repo_path
self.cost=1500
self.__init_sack()
##FIXME Hacking around EL5 deficiencies
try:
self.repoXML=RepoMD(self.id)
except TypeError:
# looks like older version of Yum... we should be OK
pass
# raise "Unfinished!"
def setPathFilter(self,path_list):
self.repo_filter=path_list
def __init_sack(self):
## Sample code to populate sacks:
## updatepo = FakePackage('zip', '2', '1', '0', 'i386')
## self.xsack.addPackage(updatepo)
ts=rpm.TransactionSet()
# hack to disable DIGEST and SIGNATURE checks
ts.setVSFlags((rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS))
for fn in os.listdir(self.pkgdir):
if fn[-4:] == '.rpm':
localpath=os.path.join(self.pkgdir,fn)
hdr=rpmUtils.miscutils.hdrFromPackage(ts,localpath)
try:
hdr = rpmUtils.miscutils.hdrFromPackage(ts, localpath)
except rpmUtils.RpmUtilsError:
raise yum.Errors.MiscError, \
'Could not open local rpm file: %s' % localpath
# ypkg=YumHeaderPackage(self, hdr)
ypkg=CPacManPackage(self, hdr, localpath)
ypkg._populatePrco()
ypkg.basepath='file://'+localpath
ypkg.relativepath=fn
# print ypkg
self.cpacman_sack.addPackage(ypkg)
def getPackageSack(self):
return self.cpacman_sack
yb = yum.YumBase()
basecachedir = '/tmp/newrepo'
try:
if not os.path.exists(yb.conf.basecachedir):
os.makedirs(yb.conf.basecachedir)
except AttributeError:
print "can't set YumBase.conf.basecachedir"
yb.conf.cache = 0
url = sys.argv[1]
if len(sys.argv)>2:
pkg_name=sys.argv[2]
else:
pkg_name=u'dummy'
# yb.repos.disableRepo('*')
###HACK newrepo = yum.yumRepo.YumRepository('myrepo')
newrepo = CPacManYumRepo(u'someserver.com',url,u'myrepo')
newrepo.name = u'myrepo - %s' % url
newrepo.baseurl = [url]
try:
newrepo.basecachedir = basecachedir
except AttributeError:
print "can't set basecachedir"
newrepo.enablegroups = True
yb.repos.add(newrepo)
yb.repos.enableRepo(newrepo.id)
yb.doRepoSetup(thisrepo=newrepo.id)
print "Enabled repos:"
for repo in yb.repos.listEnabled():
print "\t",repo
yb.install(name=u'dummy')
## yb.remove(name='someotherpackage')
yb.resolveDeps()
yb.processTransaction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment