Changes between Version 5 and Version 6 of YumCodeSnippets

Show
Ignore:
Author:
james (IP: 65.172.155.230)
Timestamp:
02/02/09 15:43:51 (9 years ago)
Comment:

move snippets from code snippets page to their own pages

Legend:

Unmodified
Added
Removed
Modified
  • YumCodeSnippets

    v5 v6  
    33This is just snippets of code using the yum modules for common/simple tasks. Add/edit/comment as you'd like. All of these snippets assume that you have done "import yum, os, sys". 
    44 
    5 Open a local package as a yum package object w/o all the rest of the yum stuff: 
    6  
    7 {{{ 
    8 #!python 
    9 import yum.packages 
    10 import rpmUtils.transaction 
    11 ts = rpmUtils.transaction.initReadOnlyTransaction() 
    12 lp = yum.packages.YumLocalPackage(ts, '/home/skvidal/yum-3.2.8-1.noarch.rpm') 
    13 }}} 
    14  
    15 replace '/home/skvidal/yum-3.2.8-1.noarch.rpm' with the full path to the package 
    16  
    17  
    18 Return the gpg signature info from a package: 
    19 {{{ 
    20 #!python 
    21 import yum.packages 
    22 import rpmUtils.transaction 
    23 from rpmUtils.miscutils import getSigInfo 
    24 ts = rpmUtils.transaction.initReadOnlyTransaction() 
    25 lp = yum.packages.YumLocalPackage(ts, '/home/skvidal/yum-3.2.8-1.noarch.rpm') 
    26  
    27 print getSigInfo(lp.hdr) 
    28  
    29 }}} 
    30  
    31 Given a filename of an rpm parse out the major information that's available: 
    32  
    33 {{{ 
    34 #!python 
    35 from rpmUtils.miscutils import splitFilename 
    36  
    37 myfn = 'a2ps-4.13b-57.1.el5.src.rpm' 
    38  
    39 (name, ver, rel, epoch, arch) = splitFilename(myfn) 
    40  
    41 }}} 
    42  
    43  
    44 Simplest transaction you can have (yum 3.2.7 and above): 
    45  
    46 {{{ 
    47 #!python 
    48 my = yum.YumBase() 
    49 my.install(name='somepackage') 
    50 my.remove(name='someotherpackage') 
    51 my.resolveDeps() 
    52 my.processTransaction() 
    53 }}} 
    54  
    55 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: 
    56  
    57 {{{ ipython ./thisfile.py url://to/my/repo }}} 
    58  
    59 {{{ 
    60 #!python 
    61 my = yum.YumBase() 
    62 my.conf.basecachedir = '/tmp/whereever' 
    63 if not os.path.exists(my.conf.basecachedir): 
    64 os.makedirs(my.conf.basecachedir) 
    65 my.conf.cache = 0 
    66 url = sys.argv[1]  
    67  
    68 my.repos.disableRepo('*') 
    69  
    70 newrepo = yum.yumRepo.YumRepository('myrepo') 
    71 newrepo.name = 'myrepo - %s' % url 
    72 newrepo.baseurl = [url]  
    73 newrepo.basecachedir = my.conf.basecachedir 
    74 newrepo.enablegroups = True 
    75 my.repos.add(newrepo) 
    76 my.repos.enableRepo(newrepo.id) 
    77 my.doRepoSetup(thisrepo=newrepo.id) 
    78  
    79 }}} 
    80  
    81  
    82 Search for which package(s) own a file on the system 
    83 {{{ 
    84 #!python 
    85 my = yum.YumBase() 
    86 pkgs = my.rpmdb.whatProvides('/boot/vmlinuz-2.6.23.9-85.fc8', None, (None, None,None)) 
    87 for pkg in pkgs: 
    88     print pkg 
    89 }}} 
    90  
    91 Report back the list of issues with an installed package (like rpm -V pkgname): 
    92  
    93 {{{ 
    94 #!python 
    95 my = yum.YumBase() 
    96 pkgs = my.rpmdb.searchNevra(name='yum') 
    97 for pkg in pkgs: 
    98     errors = pkg.verify() 
    99     for fn in errors.keys(): 
    100         for prob in errors[fn]: 
    101             print '%s - %s' % (fn, prob.message) 
    102  
    103 }}} 
    104  
    105 A simple version of the "yum list" command: 
    106  
    107 {{{ 
    108 #!python 
    109 yb = yum.YumBase() 
    110 yb.conf.cache = os.geteuid() != 1 
    111 pl = yb.doPackageLists(patterns=sys.argv[1:]) 
    112 if pl.installed: 
    113     print "Installed Packages" 
    114     for pkg in sorted(pl.installed): 
    115         print pkg 
    116 if pl.available: 
    117     print "Available Packages" 
    118     for pkg in sorted(pl.available): 
    119         print pkg, pkg.repo 
    120 if pl.reinstall_available: 
    121     print "Re-install Available Packages" 
    122     for pkg in sorted(pl.reinstall_available): 
    123         print pkg, pkg.repo 
    124 }}} 
    125  
     5 1. [YumCodeSnippet/OpenLocalPkg Open a local package] 
     6 1. [YumCodeSnippet/ReturnGpgInfo Return the gpg signature info from a package] 
     7 1. [YumCodeSnippet/ParseRpmFilename Get the naming data from an rpm filename] 
     8 1. [YumCodeSnippet/FindFileOwner Find owner of a file] 
     9 1. [YumCodeSnippet/SimplestTransaction Simplest transaction you can have] 
     10 1. [YumCodeSnippet/SetupArbitraryRepo Setup a repo at an arbitrary location] 
     11 1. [YumCodeSnippet/VerifyPkg Verify an installed package] 
     12 1. [YumCodeSnippet/YumList A simple version of the "yum list" command] 
     13 1. [YumCodeSnippet/YumSearch A simple version of the "yum search" command] 
    12614 
    12715more?