From Fedora Project Wiki
(Replacing page with '#REDIRECT [http://yum.baseurl.org/wiki/YumCodeSnippets]')
Line 1: Line 1:
#REDIRECT [[http://yum.baseurl.org/wiki/YumCodeSnippets]]
#REDIRECT [http://yum.baseurl.org/wiki/YumCodeSnippets]
 
== Yum Code Snippets ==
 
This is just snippets of code using the yum modules for common/simple tasks. Add/edit/comment as you'd like.
 
Open a local package as a yum package object w/o all the rest of the yum stuff:
 
<pre>
import yum.packages
import rpmUtils.transaction
ts = rpmUtils.transaction.initReadOnlyTransaction()
lp = yum.packages.YumLocalPackage(ts, '/home/skvidal/yum-3.2.8-1.noarch.rpm')
</pre>
 
replace '/home/skvidal/yum-3.2.8-1.noarch.rpm' with the full path to the package
 
 
Return the gpg signature info from a package:
<pre>
import yum.packages
import rpmUtils.transaction
from rpmUtils.miscutils import getSigInfo
ts = rpmUtils.transaction.initReadOnlyTransaction()
lp = yum.packages.YumLocalPackage(ts, '/home/skvidal/yum-3.2.8-1.noarch.rpm')
 
print getSigInfo(lp.hdr)
 
</pre>
 
Given a filename of an rpm parse out the major information that's available:
<pre>
from rpmUtils.miscutils import splitFilename
 
myfn = 'a2ps-4.13b-57.1.el5.src.rpm'
 
(name, ver, rel, epoch, arch) = splitFilename(myfn)
 
</pre>
 
 
Simplest transaction you can have (yum 3.2.7 and above):
 
<pre>
 
import yum
my = yum.YumBase()
my.install(name='somepackage')
my.remove(name='someotherpackage')
my.resolveDeps()
my.processTransaction()
</pre>
 
This helps you setup a repo at an arbitrary location, as a user and play with the object. Paste this into a file then run it with ipython for maximum usefulness like:
 
<code> ipython ./thisfile.py url://to/my/repo </code>
 
<pre>
 
import yum, os, sys
my = yum.YumBase()
my.conf.basecachedir = '/tmp/whereever'
if not os.path.exists(my.conf.basecachedir):
os.makedirs(my.conf.basecachedir)
my.conf.cache = 0
url = sys.argv[1]
 
my.repos.disableRepo('*')
 
newrepo = yum.yumRepo.YumRepository('myrepo')
newrepo.name = 'myrepo - %s' % url
newrepo.baseurl = [url]
newrepo.basecachedir = my.conf.basecachedir
newrepo.enablegroups = True
my.repos.add(newrepo)
my.repos.enableRepo(newrepo.id)
my.doRepoSetup(thisrepo=newrepo.id)
 
 
</pre>
 
 
Search for which package(s) own a file on the system
<pre>
import yum
my = yum.YumBase()
pkgs = my.rpmdb.whatProvides('/boot/vmlinuz-2.6.23.9-85.fc8', None, (None, None,None))
for pkg in pkgs:
print pkg
</pre>
 
more?

Revision as of 20:56, 21 October 2008

  1. REDIRECT [1]