Changes between Version 1 and Version 2 of 5MinuteExamples

Show
Ignore:
Author:
skvidal (IP: 24.211.246.61)
Timestamp:
11/14/08 18:06:30 (9 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • 5MinuteExamples

    v1 v2  
    33There are a lot of lines of code in yum, and it can be somewhat intimidating at first glace. However a significant amount of effort has been made to make simple things easy, and the hard things not so hard. The start of any code using yum, will almost certainly have these four lines (and always the first and last one :). 
    44{{{ 
    5     1 #! /usr/bin/python -tt 
    6     2  
    7     3 import os 
    8     4 import sys 
    9     5 import yum 
     5    #! /usr/bin/python -tt 
     6     
     7    import os 
     8    import sys 
     9    import yum 
    1010}}} 
    1111 
    1313 
    1414{{{ 
    15     7 yb = yum.YumBase() 
     15    yb = yum.YumBase() 
    1616}}} 
    1717 
    1818This creates a yum instance, that you can work with. Then one more piece, that is very useful: 
    1919{{{ 
    20     9 yb.conf.cache = os.geteuid() != 0 
     20    yb.conf.cache = os.geteuid() != 0 
    2121}}} 
    2222 
    2828Simple version of "yum list" command 
    2929{{{ 
    30    11 # Get the repository package objects matching the passed arguments 
    31    12 pkgs = yb.pkgSack.returnNewestByNameArch(patterns=sys.argv[1:]) 
    32    13  
    33    14 for pkg in pkgs: 
    34    15     print "%s: %s" % (pkg, pkg.summary) 
     30   # Get the repository package objects matching the passed arguments 
     31   pkgs = yb.pkgSack.returnNewestByNameArch(patterns=sys.argv[1:]) 
     32    
     33   for pkg in pkgs: 
     34       print "%s: %s" % (pkg, pkg.summary) 
    3535}}} 
    3636Simple stats. gathering from installed packages 
    3737{{{ 
    38    17 # Find the ten biggest installed packages 
    39    18 pkgs = yb.rpmdb.returnPackages() 
    40    19 pkgs.sort(key=lambda x: x.size, reverse=True) 
    41    20 print "Top ten installed packages:" 
    42    21 done = set() 
    43    22 for pkg in pkgs: 
    44    23     if pkg.name in done: 
    45    24         continue 
    46    25     done.add(pkg.name) 
    47    26     print "%s: %sMB" % (pkg, pkg.size / (1024 * 1024)) 
    48    27     if len(done) >= 10: 
    49    28         break 
     38   # Find the ten biggest installed packages 
     39   pkgs = yb.rpmdb.returnPackages() 
     40   pkgs.sort(key=lambda x: x.size, reverse=True) 
     41   print "Top ten installed packages:" 
     42   done = set() 
     43   for pkg in pkgs: 
     44       if pkg.name in done: 
     45           continue 
     46       done.add(pkg.name) 
     47       print "%s: %sMB" % (pkg, pkg.size / (1024 * 1024)) 
     48       if len(done) >= 10: 
     49           break 
    5050 
    5151}}} 
    5656 
    5757{{{ 
    58    30 # Pacakge tuple: 
    59    31 (pkg.name, pkg.arch, pkg.epoch, pkg.version, pkg.release) 
    60    32 # Dependency (or the Provides/Requires/Conflicts/Obsoletes (PRCO)) tuple: 
    61    33 (pkg.name, 'EQ', (pkg.epoch, pkg.version, pkg.release)) 
     58   # Pacakge tuple: 
     59   (pkg.name, pkg.arch, pkg.epoch, pkg.version, pkg.release) 
     60   # Dependency (or the Provides/Requires/Conflicts/Obsoletes (PRCO)) tuple: 
     61   (pkg.name, 'EQ', (pkg.epoch, pkg.version, pkg.release)) 
    6262}}} 
    6363 
    6565"Clever" way to manually update almost all the metadata for any usable repos. installed 
    6666{{{ 
    67     1 #! /usr/bin/python -tt 
    68     2  
    69     3 import os 
    70     4 import sys 
    71     5 import yum 
    72     6  
    73     7 yb = yum.YumBase() 
    74     8  
    75     9 yb.conf.cache = os.geteuid() != 0 
    76    10  
    77    11 from urlgrabber.progress import TextMeter 
    78    12  
    79    13 # Use the "internal" output mode of yum's cli 
    80    14 sys.path.insert(0, '/usr/share/yum-cli') 
    81    15 import output 
    82    16  
    83    17 # Try not to be annoying if run from cron etc. 
    84    18 if sys.stdout.isatty(): 
    85    19     yb.repos.setProgressBar(TextMeter(fo=sys.stdout)) 
    86    20     yb.repos.callback = output.CacheProgressCallback() 
    87    21     yumout = output.YumOutput() 
    88    22     freport = ( yumout.failureReport, (), {} ) 
    89    23     yb.repos.setFailureCallback( freport ) 
    90    24  
    91    25 # Enable all the repos. a user might want to use and sync. the metadata. 
    92    26 # Note this needs to be done before the repositories are used. 
    93    27 for name in ('updates-testing', 'rawhide', 'livna', 'adobe-linux-i386', 
    94    28              'brew', 'rhts', 'koji-static'): 
    95    29     yb.repos.enableRepo(name + ',') 
    96    30 for repo in yb.repos.listEnabled(): 
    97    31     yb.repos.enableRepo(repo.id + '-source'    + ',') 
    98    32     yb.repos.enableRepo(repo.id + '-debuginfo' + ',') 
    99    33 yb.repos.doSetup() 
    100    34 for repo in yb.repos.listEnabled(): 
    101    35     repo.mdpolicy        = 'group:main' 
    102    36     repo.metadata_expire = 0 
    103    37     repo.repoXML 
    104    38 # This is somehwat "magic", it unpacks the metadata making it usable. 
    105    39 yb.repos.populateSack(mdtype='metadata', cacheonly=1) 
    106    40 yb.repos.populateSack(mdtype='filelists', cacheonly=1) 
     67   #! /usr/bin/python -tt 
     68    
     69   import os 
     70   import sys 
     71   import yum 
     72    
     73   yb = yum.YumBase() 
     74    
     75   yb.conf.cache = os.geteuid() != 0 
     76    
     77   from urlgrabber.progress import TextMeter 
     78    
     79   # Use the "internal" output mode of yum's cli 
     80   sys.path.insert(0, '/usr/share/yum-cli') 
     81 
     82   import output 
     83    
     84   # Try not to be annoying if run from cron etc. 
     85   if sys.stdout.isatty(): 
     86       yb.repos.setProgressBar(TextMeter(fo=sys.stdout)) 
     87       yb.repos.callback = output.CacheProgressCallback() 
     88       yumout = output.YumOutput() 
     89       freport = ( yumout.failureReport, (), {} ) 
     90       yb.repos.setFailureCallback( freport ) 
     91     
     92   # Enable all the repos. a user might want to use and sync. the metadata. 
     93   # Note this needs to be done before the repositories are used. 
     94   for name in ('updates-testing', 'rawhide', 'livna', 'adobe-linux-i386', 
     95                'brew', 'rhts', 'koji-static'): 
     96       yb.repos.enableRepo(name + ',') 
     97    
     98   for repo in yb.repos.listEnabled(): 
     99       yb.repos.enableRepo(repo.id + '-source'    + ',') 
     100       yb.repos.enableRepo(repo.id + '-debuginfo' + ',') 
     101   yb.repos.doSetup() 
     102    
     103   for repo in yb.repos.listEnabled(): 
     104       repo.mdpolicy        = 'group:main' 
     105       repo.metadata_expire = 0 
     106       repo.repoXML 
     107   # This is somehwat "magic", it unpacks the metadata making it usable. 
     108   yb.repos.populateSack(mdtype='metadata', cacheonly=1) 
     109   yb.repos.populateSack(mdtype='filelists', cacheonly=1) 
    107110}}} 
    108111